<commit_before>package org.ossim.omar

import groovy.xml.StreamingMarkupBuilder
import java.text.SimpleDateFormat
import org.springframework.context.ApplicationContextAware
import org.springframework.context.ApplicationContext
import org.springframework.beans.factory.InitializingBean

import org.ossim.omar.RasterEntry
import org.ossim.omar.VideoDataSet

class KmlService implements ApplicationContextAware, InitializingBean {

  boolean transactional = false
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
  ApplicationContext applicationContext
  def grailsApplication
  def tagLibBean
  def flashDirRoot
  def flashUrlRoot

/*
  String createKml(List<RasterEntry> rasterEntries)
  {
    def kmlbuilder = new StreamingMarkupBuilder()
    def width = 1024;
    def height = 1024;

    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?.metadata?.acquisitionDate) ? sdf.format(rasterEntry?.metadata?.acquisitionDate) : null

            def bounds = rasterEntry?.metadata?.groundGeom?.bounds

            def groundCenterLon = (bounds?.minLon + bounds?.maxLon) * 0.5;
            def groundCenterLat = (bounds?.minLat + bounds?.maxLat) * 0.5;

            Folder() {
              name((rasterEntry.mainFile.name as File).name)
              Placemark() {
                name((rasterEntry.mainFile.name as File).name)

                Point() {
                  coordinates("${groundCenterLon},${groundCenterLat},0")
                }
              }
              GroundOverlay() {
                open("1")
                visibility("1")
                Icon() {
                  def wmsURL = tagLibBean.createLink(absolute: true, controller: "ogc", action: "wms", params: [
                      version: "1.1.1",
                      REQUEST: "GetMap",
                      layers: rasterEntry?.id,
                      SRS: "EPSG:4326",
                      WIDTH: "${width}",
                      HEIGHT: "${height}",
                      TRANSPARENT: "TRUE",
                      FORMAT: "image/png"
                  ])
                  href(wmsURL)
                  viewRefreshMode("onStop")
                  viewRefreshTime("2")
                  viewBoundScale("0.85")
                }
                LatLonBox() {
                  north(bounds?.maxLat)
                  south(bounds?.minLon)
                  east(bounds?.maxLat)
                  west(bounds?.minLat)
                }
                if ( acquisition )
                {
                  TimeStamp() {
                    when(acquisition)
                  }
                }
              }
            }
          }
        }
      }
    }
    def kmlwriter = new StringWriter()

    kmlwriter << kmlbuilder.bind(kmlnode)

    String kmlText = kmlwriter.buffer
    return kmlText
  }
*/

  String createKml(List<RasterEntry> rasterEntries) {
    def kmlbuilder = new StreamingMarkupBuilder()
    def width = 1024;
    def height = 1024;

    def rasterIdx = 0
    def descriptionMap = [:]
    rasterEntries?.each {rasterEntry ->
      def mpp = rasterEntry.getMetersPerPixel()
      def fieldMap = [File: (rasterEntry.mainFile.name as File).name,
              Entry_Id: rasterEntry.entryId,
              Width: rasterEntry.width,
              Height: rasterEntry.height,
              Bands: rasterEntry.numberOfBands,
              Acquistion_Date: rasterEntry.acquisitionDate,
              Meters_per_pixel: mpp]
      def imageUrl = tagLibBean.createLink(absolute: true, controller: "mapView", params: [rasterEntryIds: rasterEntry.id])

      def descriptionBuilder = new StreamingMarkupBuilder().bind {
        body() {
          table() {
            tr() {
              td("Image link:")
              td() {
                p() {
                  font(size: 5)
                  a(href: imageUrl, "Browse image")
                }
              }
            }
            fieldMap.each {k, v ->
              tr() {
                td("${k}:")
                td(v)
              }
            }
          }
        }
      }
      descriptionMap.put(rasterIdx, descriptionBuilder.toString())
    }

    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

            def groundCenterLon = (rasterEntry?.groundGeom?.bounds?.minLon + rasterEntry?.groundGeom?.bounds?.maxLon) * 0.5;
            def groundCenterLat = (rasterEntry?.groundGeom?.bounds?.minLat + rasterEntry?.groundGeom?.bounds?.maxLat) * 0.5;

            def renderedHtml = "${descriptionMap.get(rasterIdx)}"
            rasterIdx++

            GroundOverlay() {
              name((rasterEntry.mainFile.name as File).name)
              Snippet(maxLines: "0", "")
              description(renderedHtml)
              LookAt() {
                longitude(groundCenterLon)
                latitude(groundCenterLat)
                altitude(0.0)
                heading(0.0)
                tilt(0.0)
                range(15000)
                altitudeMode("clampToGround")
              }
              open("1")
              visibility("1")
              Icon() {
                def wmsURL = tagLibBean.createLink(absolute: true, controller: "ogc", action: "wms", params: [
                        version: "1.1.1",
                        REQUEST: "GetMap",
                        layers: rasterEntry?.id,
                        SRS: "EPSG:4326",
                        WIDTH: "${width}",
                        HEIGHT: "${height}",
                        TRANSPARENT: "TRUE",
                        FORMAT: "image/png"
                ])
//                   println wmsURL
                href(wmsURL)
                viewRefreshMode("onStop")
                viewRefreshTime("1")
                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)

    String kmlText = kmlwriter.buffer
    return kmlText
  }
/*
  String createImagesKml(List<RasterEntry> rasterEntries, Map wmsParams, Map params)
  {
    def kmlbuilder = new StreamingMarkupBuilder()
    def width = 1024;
    def height = 1024;

    kmlbuilder.encoding = "UTF-8"

    wmsParams?.request = "GetMap"
    if ( !params?.containsKey("version") )
    {
      wmsParams.version = "1.1.1"
    }
    if ( !params?.containsKey("width") )
    {
      wmsParams.width = "1024"
    }
    if ( !params?.containsKey("height") )
    {
      wmsParams.height = "1024"
    }
    if ( !params?.containsKey("format") )
    {
      wmsParams.format = "image/png"
    }
    if ( !params?.containsKey("transparent") )
    {
      wmsParams.transparent = "TRUE"
    }
    def bbox = wmsParams?.bbox?.split(',')
    wmsParams?.srs = "EPSG:4326"
    wmsParams?.remove("bbox");
    wmsParams.remove("action")
    wmsParams.remove("controller")
    def rasterIdx = 0
    def descriptionMap = [:]
    rasterEntries?.each {rasterEntry ->
      def mpp = rasterEntry.getMetersPerPixel()
      def fieldMap = [File: (rasterEntry.mainFile.name as File).name,
          Entry_Id: rasterEntry.entryId,
          Width: rasterEntry.width,
          Height: rasterEntry.height,
          Bands: rasterEntry.numberOfBands,
          Acquistion_Date: rasterEntry?.metadata?.acquisitionDate,
          Meters_per_pixel: mpp]
      def imageUrl = tagLibBean.createLink(absolute: true, controller: "mapView", params: [rasterEntryIds: rasterEntry.id])

      def descriptionBuilder = new StreamingMarkupBuilder().bind {
        body() {
          table() {
            tr() {
              td("Image link:")
              td() {
                p() {
                  font(size: 5)
                  a(href: imageUrl, "Browse image")
                }
              }
            }
            fieldMap.each {k, v ->
              tr() {
                td("${k}:")
                td(v)
              }
            }
          }
        }
      }
      descriptionMap.put(rasterIdx, descriptionBuilder.toString())
      rasterIdx++;
    }
    def kmlnode = {
      mkp.xmlDeclaration()
      kml("xmlns": "http://earth.google.com/kml/2.1") {
        Document() {
          rasterIdx = 0
          rasterEntries?.each {rasterEntry ->
            def acquisition = (rasterEntry?.metadata?.acquisitionDate) ? sdf.format(rasterEntry?.metadata?.acquisitionDate) : null

            def bounds = rasterEntry?.metadata?.groundGeom?.bounds

            def groundCenterLon = (bounds?.minLon + bounds?.maxLon) * 0.5;
            def groundCenterLat = (bounds?.minLat + bounds?.maxLat) * 0.5;
            wmsParams?.layers = rasterEntry?.id

            def renderedHtml = "${descriptionMap.get(rasterIdx)}"
            rasterIdx++

            GroundOverlay() {
              name((rasterEntry.mainFile.name as File).name)
              Snippet(maxLines: "0", "")
              description(renderedHtml)

              LookAt() {
                longitude(groundCenterLon)
                latitude(groundCenterLat)
                altitude(0.0)
                heading(0.0)
                tilt(0.0)
                range(15000)
                altitudeMode("clampToGround")
              }

              open("1")
              visibility("1")
              Icon() {
                def wmsURL = tagLibBean.createLink(absolute: true, controller: "ogc", action: "wms", params: wmsParams)

                href(wmsURL)
                viewRefreshMode("onStop")
                viewRefreshTime("2")
                viewBoundScale("0.85")
              }

              LatLonBox() {
                north(bbox[2])
                south(bbox[1])
                east(bbox[0])
                west(bbox[3])
              }
              if ( acquisition )
              {
                TimeStamp() {
                  when(acquisition)
                }
              }
            }
          }
        }
      }
    }
    def kmlwriter = new StringWriter()

    kmlwriter << kmlbuilder.bind(kmlnode)

    String kmlText = kmlwriter.buffer

    return kmlText
  }
*/

  String createImagesKml(List<RasterEntry> rasterEntries, Map wmsParams, Map params) {
    def kmlbuilder = new StreamingMarkupBuilder()
//    def width = 1024;
//    def height = 1024;

    kmlbuilder.encoding = "UTF-8"

    wmsParams?.request = "GetMap"
    if (!params?.containsKey("version")) {
      wmsParams.version = "1.1.1"
    }
    if (!params?.containsKey("width")) {
      wmsParams.width = "1024"<commit_msg>The createKml can now take a wms Params map so you can pass along params such as stretch and sharpen<commit_after>package org.ossim.omar

import groovy.xml.StreamingMarkupBuilder
import java.text.SimpleDateFormat
import org.springframework.context.ApplicationContextAware
import org.springframework.context.ApplicationContext
import org.springframework.beans.factory.InitializingBean

import org.ossim.omar.RasterEntry
import org.ossim.omar.VideoDataSet

class KmlService implements ApplicationContextAware, InitializingBean {

  boolean transactional = false
  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
  ApplicationContext applicationContext
  def grailsApplication
  def tagLibBean
  def flashDirRoot
  def flashUrlRoot

  String createKml(List<RasterEntry> rasterEntries, Map wmsParams) {
    def kmlbuilder = new StreamingMarkupBuilder()
    def rasterIdx = 0
    def descriptionMap = [:]
    rasterEntries?.each {rasterEntry ->
      def mpp = rasterEntry.getMetersPerPixel()
      def fieldMap = [File: (rasterEntry.mainFile.name as File).name,
              Entry_Id: rasterEntry.entryId,
              Width: rasterEntry.width,
              Height: rasterEntry.height,
              Bands: rasterEntry.numberOfBands,
              Acquistion_Date: rasterEntry.acquisitionDate,
              Meters_per_pixel: mpp]
      def imageUrl = tagLibBean.createLink(absolute: true, controller: "mapView", params: [rasterEntryIds: rasterEntry.id])

      def descriptionBuilder = new StreamingMarkupBuilder().bind {
        body() {
          table() {
            tr() {
              td("Image link:")
              td() {
                p() {
                  font(size: 5)
                  a(href: imageUrl, "Browse image")
                }
              }
            }
            fieldMap.each {k, v ->
              tr() {
                td("${k}:")
                td(v)
              }
            }
          }
        }
      }
      descriptionMap.put(rasterIdx, descriptionBuilder.toString())
    }
    if(!wmsParams?.width)
    {
      wmsParams?.width = "1024"
    }
    if(!wmsParams?.height)
    {
      wmsParams?.height = "1024"
    }
    wmsParams.remove("action")
    wmsParams.remove("controller")
    wmsParams?.version= "1.1.1"
    wmsParams?.request= "GetMap"
    wmsParams?.srs= "EPSG:4326"
    wmsParams?.transparent= "TRUE"
    wmsParams?.format= "image/png"
    kmlbuilder.encoding= "UTF-8"
    def bbox = wmsParams?.bbox;
    wmsParams?.remove("bbox");
    def kmlnode = {
      mkp.xmlDeclaration()
      kml("xmlns": "http://earth.google.com/kml/2.1") {
        Folder() {
          name("OMAR_WMS")
          rasterEntries?.each {rasterEntry ->
            wmsParams?.layers="${rasterEntry?.id}"
            def acquisition = (rasterEntry?.acquisitionDate) ? sdf.format(rasterEntry?.acquisitionDate) : null

            def groundCenterLon = (rasterEntry?.groundGeom?.bounds?.minLon + rasterEntry?.groundGeom?.bounds?.maxLon) * 0.5;
            def groundCenterLat = (rasterEntry?.groundGeom?.bounds?.minLat + rasterEntry?.groundGeom?.bounds?.maxLat) * 0.5;

            def renderedHtml = "${descriptionMap.get(rasterIdx)}"
            rasterIdx++

            GroundOverlay() {
              name((rasterEntry.mainFile.name as File).name)
              Snippet(maxLines: "0", "")
              description(renderedHtml)
              LookAt() {
                longitude(groundCenterLon)
                latitude(groundCenterLat)
                altitude(0.0)
                heading(0.0)
                tilt(0.0)
                range(15000)
                altitudeMode("clampToGround")
              }
              open("1")
              visibility("1")
              Icon() {
                def wmsURL = tagLibBean.createLink(absolute: true, controller: "ogc", action: "wms", params: wmsParams)
//                   println wmsURL
                href(wmsURL)
                viewRefreshMode("onStop")
                viewRefreshTime("1")
                viewBoundScale("0.85")
              }
              LatLonBox() {

                 if(bbox)
                 {
                   north(bbox[2])
                   south(bbox[1])
                   east(bbox[0])
                   west(bbox[3])
                 }
                 else
                 {
                   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)

    String kmlText = kmlwriter.buffer
    return kmlText
  }

  String createImagesKml(List<RasterEntry> rasterEntries, Map wmsParams, Map params) {
    def kmlbuilder = new StreamingMarkupBuilder()
//    def width = 1024;
//    def height = 1024;

    kmlbuilder.encoding = "UTF-8"

    wmsParams?.request = "GetMap"
    if (!params?.containsKey("version")) {
      wmsParams.version = "1.1.1"
    }
    if (!params?.containsKey("width")) {
      wmsParams.width = "1024"
{'fraction_non_alphanumeric': 0.11167421893632244, 'fraction_numerical': 0.012163129024564751, 'mean_word_length': 1.6126168224299064, 'pattern_counts': {'":': 4, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '14270', 'n_tokens_mistral': 5028, 'n_tokens_neox': 4717, 'n_words': 813}