import groovy.xml.StreamingMarkupBuilder import java.text.SimpleDateFormat class KmlQueryController { def grailsApplication def rasterEntrySearchService def sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") def getkml = { // Google sends the BBOX with the request if ( params.BBOX ) { def bounds = params.BBOX?.split(',') params.aoiMinLon = bounds[0] params.aoiMinLat = bounds[1] params.aoiMaxLon = bounds[2] params.aoiMaxLat = bounds[3] } if ( params.max == null || Integer.parseInt(params.max) > 100 ) { params.max = 10 } def queryParams = new RasterEntryQuery() bindData(queryParams, params) if ( !params.containsKey("dateSort") || params?.dateSort == "true" ) { params.order = 'desc' params.sort = 'acquisitionDate' queryParams.endDate = new Date() } //println params log.info(queryParams.toMap()) //println "kml queryParams: ${queryParams.toMap()}" def rasterEntries = rasterEntrySearchService.runQuery(queryParams, params) def kmlbuilder = new StreamingMarkupBuilder() kmlbuilder.encoding = "UTF-8" def kmlnode = { mkp.xmlDeclaration() kml("xmlns": "http://earth.google.com/kml/2.1") { Folder() { name("OMAR_WMS") rasterEntries?.each {rasterEntry -> def acquisition = (rasterEntry?.acquisitionDate) ? sdf.format(rasterEntry?.acquisitionDate) : null GroundOverlay() { name(rasterEntry.mainFile.name) open("1") visibility("1") Icon() { def wmsURL = createLink(absolute: true, controller: "ogc", action: "wms", params: [ version: "1.1.1", REQUEST: "GetMap", layers: rasterEntry?.id, SRS: "EPSG:4326", WIDTH: 512, HEIGHT: 512, TRANSPARENT: "TRUE", FORMAT: "image/png" ]) //println wmsURL href(wmsURL) viewRefreshMode("onStop") viewRefreshTime("2") viewBoundScale("0.85") } LatLonBox() { north(rasterEntry?.groundGeom?.bounds?.maxLat) south(rasterEntry?.groundGeom?.bounds?.minLon) east(rasterEntry?.groundGeom?.bounds?.maxLat) west(rasterEntry?.groundGeom?.bounds?.minLat) } if ( acquisition ) { TimeStamp() { when(acquisition) } } } } } } } def kmlwriter = new StringWriter() kmlwriter << kmlbuilder.bind(kmlnode) render(contentType: "application/vnd.google-earth.kml+xml", text: kmlwriter.buffer, encoding: "UTF-8") } }