import groovy.xml.StreamingMarkupBuilder import org.springframework.beans.factory.InitializingBean class VideoStreamingController implements InitializingBean { def grailsApplication def flashDirRoot def flashUrlRoot def show = { def flvUrl def title def videoDataSet = VideoDataSet.get(params.id) if ( videoDataSet ) { File mpegFile = videoDataSet.mainFile.name as File File flvFile = "${flashDirRoot}/${mpegFile.name}.flv" as File flvUrl = new URL("${flashUrlRoot}/${flvFile.name}") title = mpegFile.name if ( !flvFile.exists() ) { def cmd = "ffmpeg -i ${mpegFile.absolutePath} -an -vb 2048k -r 15 ${flvFile.absolutePath}" println cmd def process = cmd.execute() process.waitFor() } } else { flash.message = "VideoDataSet not found with id ${params.id}" redirect(controller: "videoDataSet", action: "list", params: [flash: flash]) } [flvUrl: flvUrl, videoDataSet: videoDataSet, title: title] } def getKML = { def videoDataSet = VideoDataSet.get(params.id) if ( videoDataSet ) { def groundGeom = videoDataSet.groundGeom.toString() def list = [] def point def polygons = [] def kmlPolygon File mpegFile = videoDataSet.mainFile.name as File File flvFile = "${flashDirRoot}/${mpegFile.name}.flv" as File URL flvUrl = new URL("${flashUrlRoot}/${flvFile.name}") def flashPlayerUrl = createLinkTo(dir: "js", file: "player.swf", absolute: true) def descriptionText = "]]>" // The code below translate the asText(ground_geom) from PostGIS to a KML polygon. We will later // iterate over the polygon list to create the KML MultiGeometry def polygonMatch = /\({2,3}(.*?)\){2}/ groundGeom.eachMatch(polygonMatch) {matcher -> matcher[1]?.toString().split(",").each { list << "${it.replaceFirst(' ', ",")},0" } kmlPolygon = list.join(" ") polygons << kmlPolygon point = list[0] } def kmlBuilder = new StreamingMarkupBuilder() kmlBuilder.encoding = "UTF-8" def kmlNode = { mkp.xmlDeclaration() kml("xmlns": "http://earth.google.com/kml/2.1") { Document() { Style("id": "sh_red") { LineStyle() { color("ff0000ff") } PolyStyle { color("7f00005f") } IconStyle() { color("ff00007f") } Icon() { href("http://maps.google.com/mapfiles/kml/pushpin/red-pushpin.png") hotspot("x": 20, "y": 2, "xunits": "pixels", "yunits": "pixels") } } StyleMap("id": "red") { Pair() { key("normal") styleUrl("#sh_red") } Pair() { key("highlight") styleUrl("#sh_red") } } Placemark() { styleUrl("#red") name(mpegFile.name) description { mkp.yieldUnescaped(descriptionText) } MultiGeometry() { polygons.each {polygon -> Polygon() { tessellate("1") outerBoundryIs() { LinearRing() { coordinates("${polygon}") } } } } } Point() { coordinates("${point}") } } } } } def kmlWriter = new StringWriter() kmlWriter << kmlBuilder.bind(kmlNode) response.setHeader("Content-disposition", "attachment; filename=foo.kml") render(contentType: "application/vnd.google-earth.kml+xml", text: kmlWriter.buffer, encoding: "UTF-8") } else { response.setHeader("Content-disposition", "attachment; filename=foo.kml") render(contentType: "application/vnd.google-earth.kml+xml", text: "", encoding: "UTF-8") } } public void afterPropertiesSet() { flashDirRoot = grailsApplication.config.videoStreaming.flashDirRoot flashUrlRoot = grailsApplication.config.videoStreaming.flashUrlRoot } }