Created
February 26, 2012 11:23
-
-
Save kimenye/1916174 to your computer and use it in GitHub Desktop.
Opensource GEO Utility to convert csv file to KML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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