import java.awt.image.RenderedImage import javax.imageio.ImageIO import java.awt.image.BufferedImage import java.awt.image.ColorModel import java.awt.image.WritableRaster import java.awt.image.DataBuffer import java.awt.image.DataBufferByte import java.awt.Point import java.awt.image.ComponentColorModel import java.awt.color.ColorSpace import java.awt.Transparency import org.ossim.oms.image.omsImageSource class WebMappingService { boolean transactional = true static def synchVar = 0 static { synchronized ( WebMappingService.synchVar ) { joms.oms.Init.instance().initialize() } } def getMap(def wmsRequest, def response) { RenderedImage image = null def enableOMS = true def layersSplit = wmsRequest?.layers?.split(',') org.ossim.oms.image.WMSMap wmsMap = null if ( enableOMS ) { wmsMap = new org.ossim.oms.image.WMSMap() layersSplit.each { def rasterEntry = RasterEntry.get(it) if ( rasterEntry ) { def filename = rasterEntry.mainFile?.name def entryId = rasterEntry.entryId?.toInteger() wmsMap.addFile(filename, entryId) } } } // need to synchronize file creation for people hitting the server same time for the same request // def bounds = wmsRequest?.bbox?.split(',') def mode = "five" switch ( mode ) { case "one": File f = File.createTempFile("ogcoms", ".jpg"); if ( wmsMap.writeMap(f as String, wmsRequest?.format?.toLowerCase(), wmsRequest.srs, bounds[0] as Double, bounds[1] as Double, bounds[2] as Double, bounds[3] as Double, Integer.parseInt(wmsRequest.width), Integer.parseInt(wmsRequest.height)) ) { image = ImageIO.read(f) } f.delete(); break case "two": image = wmsMap.getMap(wmsRequest.srs, bounds[0] as Double, bounds[1] as Double, bounds[2] as Double, bounds[3] as Double, Integer.parseInt(wmsRequest.width), Integer.parseInt(wmsRequest.height)) break case "three": def inputFilenames = [] wmsRequest?.layers.split(',').each { inputFilenames << RasterEntry.get(it)?.mainFile.name } def imageFile = File.createTempFile("ogcoms", ".jpg"); def cmd = "orthoigen --geo --cut-bbox-ll ${bounds[1]} ${bounds[0]} ${bounds[3]} ${bounds[2]} -t ${wmsRequest?.width} --resample-type bilinear --scale-to-8-bit ${inputFilenames.join(' ')} ${imageFile}" println cmd def process = cmd.execute() process.waitFor(); image = ImageIO.read(imageFile) break case "four": image = new BufferedImage( wmsRequest?.width?.toInteger(), wmsRequest?.height?.toInteger(), BufferedImage.TYPE_INT_RGB ) break case "five": /* * BIL: pixelStride = 1, lineStride = 3*width, bandOffsets = {0, width, 2*width} * BSQ: pixelStride = 1, lineStride = width, bandOffsets = {0, width*height, 2*width*height} * BIP: pixelStride = 3, lineStride = 3*width, bandOffsets = {0, 1, 2} */ int width = wmsRequest?.width?.toInteger() int height = wmsRequest?.height?.toInteger() // int pixelStride = 1 int pixelStride = 3 // int lineStride = width int lineStride = 3 * width // int[] bandOffsets = [0, width * height, 2 * width * height] as int[] int[] bandOffsets = [0, 1, 2] as int[] Point location = null byte[] data = new byte[width * height * 3] if ( enableOMS ) { wmsMap.getMap(wmsRequest.srs, bounds[0] as Double, bounds[1] as Double, bounds[2] as Double, bounds[3] as Double, Integer.parseInt(wmsRequest.width), Integer.parseInt(wmsRequest.height), data ) } else { new java.util.Random().nextBytes(data) } DataBuffer dataBuffer = new DataBufferByte(data, data.size()) WritableRaster raster = WritableRaster.createInterleavedRaster( dataBuffer, width, height, lineStride, pixelStride, bandOffsets, location) /* WritableRaster raster = WritableRaster.createBandedRaster( dataBuffer, width, height, lineStride, [0, 0, 0] as int[], bandOffsets, location ) */ ColorModel colorModel = omsImageSource.createColorModel(raster.sampleModel) boolean isRasterPremultiplied = true Hashtable properties = null image = new BufferedImage( colorModel, raster, isRasterPremultiplied, properties ) break } switch ( wmsRequest?.format?.toLowerCase() ) { case "image/jpeg": case "image/jpg": response.contentType = "image/jpeg" ImageIO.write(image, "jpeg", response.outputStream) break case "image/png": response.contentType = wmsRequest.format ImageIO.write(image, "png", response.outputStream) break } //System.out.println("DONE!"); wmsMap.cleanUp(); wmsMap = null; // wmsMap?.cleanUp() //System.gc(); return null; } def getCapabilities(def wmsRequest) { def wmsCapabilites = new WMSCapabilities(wmsRequest?.layers?.split(','), "http://${InetAddress.localHost.address}:8080/ServiceTest/ogc/wms?") return wmsCapabilites.getCapabilities() } }