Skip to content

Instantly share code, notes, and snippets.

@kimenye
Created February 26, 2012 11:23
Show Gist options
  • Save kimenye/1916174 to your computer and use it in GitHub Desktop.
Save kimenye/1916174 to your computer and use it in GitHub Desktop.
Opensource GEO Utility to convert csv file to KML
def file = new File(args[0])
def mb = new groovy.xml.StreamingMarkupBuilder()
new OutputStreamWriter(new FileOutputStream(args[1]),'utf-16') << mb.bind {
mkp.xmlDeclaration()
mkp.declareNamespace( kml: "http://earth.google.com/kml/2.0" )
kml {
Document {
Folder {
name("AfTerFibre Network")
Schema(name: "Fibre", id: "Fibre") {
SimpleField(name: "name", type: "String")
SimpleField(name: "description", type: "String")
SimpleField(name:"OPERATOR", type:"String")
SimpleField(name:"ROUTE", type:"String")
SimpleField(name:"STATUS", type:"String")
}
file.eachLine() { line ->
def fields = line.tokenize(",")
if (fields[0] == "Kenya")
{
Placemark {
ExtendedData {
SchemaData(schemaUrl:"#Fibre") {
name(fields[0] + " " + fields[1])
description(fields[2] + " Status: " + fields[3])
SimpleData(name: "OPERATOR", fields[1])
SimpleData(name: "ROUTE", fields[2])
SimpleData(name: "STATUS", fields[3])
}
}
LineString{
coordinates(buildCoordinates(line))
}
}
}
}
}
}
}
}
def buildCoordinates(str) {
def fields = str.tokenize(",")
def coordinateString = ""
def startCoordinatesIdx = 5
fields.eachWithIndex { field, idx ->
if (idx >= startCoordinatesIdx) {
def latLongs = field.tokenize(" ")
coordinateString += latLongs[0] + "," + latLongs[1] + " "
}
}
coordinateString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment