|
| 1 | +package scripts |
| 2 | + |
| 3 | +import java.io.File |
| 4 | +import scala.collection.mutable |
| 5 | +import scala.xml.transform.{RewriteRule, RuleTransformer} |
| 6 | +import scala.xml.{Elem, Node, PrettyPrinter, XML} |
| 7 | + |
| 8 | +case class LatLon(lat: Double, lon: Double) |
| 9 | + |
| 10 | +// Usage: |
| 11 | +// ./gradlew :execute \ |
| 12 | +// -PmaxRAM=10 \ |
| 13 | +// -PmainClass=scripts.ConsolidateOSMNodes \ |
| 14 | +// -PappArgs="['links0.osm','links_consolidated.osm']" |
| 15 | +object ConsolidateOSMNodes { |
| 16 | + |
| 17 | + private val locationToIds: mutable.Map[LatLon, mutable.Seq[Long]] = |
| 18 | + mutable.Map.empty.withDefaultValue(mutable.Seq.empty) |
| 19 | + private val idToLocation: mutable.Map[Long, LatLon] = mutable.Map.empty |
| 20 | + |
| 21 | + private val replaceRedundantId = new RewriteRule { |
| 22 | + |
| 23 | + override def transform(node: Node): Seq[Node] = { |
| 24 | + node match { |
| 25 | + case nd: Elem if nd.label == "nd" => |
| 26 | + val id = (nd \ "@ref").text.toInt |
| 27 | + val latLon = idToLocation(id) |
| 28 | + val head :: tail = locationToIds(latLon).toList |
| 29 | + if (tail.contains(id)) { |
| 30 | + val metaData = |
| 31 | + scala.xml.Attribute(key = "ref", value = scala.xml.Text(head.toString), next = scala.xml.Null) |
| 32 | + nd % metaData |
| 33 | + } else nd |
| 34 | + case n => n |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private val removeNode = new RewriteRule { |
| 40 | + |
| 41 | + override def transform(node: Node): Seq[Node] = { |
| 42 | + node match { |
| 43 | + case node: Elem if node.label == "node" => |
| 44 | + val id = (node \ "@id").text.toInt |
| 45 | + val latLon = LatLon( |
| 46 | + (node \ "@lat").text.toDouble, |
| 47 | + (node \ "@lon").text.toDouble |
| 48 | + ) |
| 49 | + val _ :: tail = locationToIds(latLon).toList |
| 50 | + if (tail.contains(id)) Seq.empty |
| 51 | + else node |
| 52 | + case n => n |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private def populateState(xml: Node): Unit = { |
| 58 | + for { |
| 59 | + osm <- xml \\ "osm" |
| 60 | + node <- osm \\ "node" |
| 61 | + } { |
| 62 | + val id = (node \ "@id").text.toLong |
| 63 | + val latLon = LatLon( |
| 64 | + (node \ "@lat").text.toDouble, |
| 65 | + (node \ "@lon").text.toDouble |
| 66 | + ) |
| 67 | + idToLocation.update(id, latLon) |
| 68 | + val seq = locationToIds(latLon) |
| 69 | + locationToIds.update(latLon, seq :+ id) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + def main(args: Array[String]): Unit = { |
| 74 | + if (args.length != 2) { |
| 75 | + println(""" |
| 76 | + |Usage: |
| 77 | + |./gradlew :execute \ |
| 78 | + | -PmaxRAM=10 \ |
| 79 | + | -PmainClass=scripts.ConsolidateOSMNodes \ |
| 80 | + | -PappArgs="['links0.osm','links_consolidated.osm']" |
| 81 | + |""".stripMargin) |
| 82 | + System.exit(1) |
| 83 | + } |
| 84 | + |
| 85 | + val osmFile = new File(args(0)) |
| 86 | + println("Loading xml..") |
| 87 | + val xml = XML.loadFile(osmFile) |
| 88 | + populateState(xml) |
| 89 | + |
| 90 | + val transformer = new RuleTransformer( |
| 91 | + replaceRedundantId, |
| 92 | + removeNode |
| 93 | + ) |
| 94 | + |
| 95 | + println("Consolidating network nodes..") |
| 96 | + val output = { |
| 97 | + val root = transformer.transform(xml) |
| 98 | + val printer = new PrettyPrinter(120, 2, true) |
| 99 | + XML.loadString(printer.format(root.head)) |
| 100 | + } |
| 101 | + |
| 102 | + println("Writing xml..") |
| 103 | + XML.save(args(1), output, "UTF-8", xmlDecl = true, null) |
| 104 | + } |
| 105 | +} |
0 commit comments