Skip to content

Instantly share code, notes, and snippets.

@TJC
Created February 6, 2014 04:47
Show Gist options
  • Save TJC/8838525 to your computer and use it in GitHub Desktop.
Save TJC/8838525 to your computer and use it in GitHub Desktop.
Dedupe placemarks in KML file
// Run with "scala convert.scala"
import scala.xml.XML
import scala.xml.Node
// Given a node, return a trimmed string containing its coordinates
def toCoords(node: Node): String = node match {
case node if ! (node \ "Point").isEmpty => (node \ "Point" \ "coordinates").text.trim
case node if ! (node \ "LineString").isEmpty => (node \ "LineString" \ "coordinates").text.trim
case node if ! (node \ "Polygon").isEmpty => (node \ "Polygon" \ "outerBoundaryIs" \ "LinearRing" \ "coordinates").text.trim
case _ => println(f"node unknown coords: %s", node \ "name"); "unknown"
}
// Given two nodes, try to pick the best one.
// TODO: Actually merge the descriptions rather than picking biggest
def mergeNodes(a: Node, b: Node): Node = {
val firstDesc = (a \ "Description").text.trim
val secondDesc = (b \ "Description").text.trim
if (firstDesc.length > secondDesc.length) a
else b
}
val doc = XML.loadFile("doc.kml")
// val placenodes = doc \ "Folder" \ "Document" \ "Folder" \ "Folder" \ "Folder" \ "Placemark"
val placenodes = doc \\ "Placemark"
var places = placenodes.foldLeft(Map[String,Node]()) {
(acc, n) =>
val coord = toCoords(n)
if (acc.contains(coord)) acc + (coord -> mergeNodes(acc(coord), n))
else acc + (coord -> n)
}
places.values.foreach(println)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment