Skip to content

Commit d66d6d3

Browse files
committed
scripts: seattle network helpers
1 parent 4973140 commit d66d6d3

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import sys
2+
import shapefile
3+
4+
if len(sys.argv) != 3:
5+
print("python remove_duplicate_links.py <input shapefile> <output location>")
6+
exit()
7+
8+
reader = shapefile.Reader(sys.argv[1])
9+
10+
writer = shapefile.Writer(sys.argv[2])
11+
12+
writer.field('ID', 'C', 20, 0)
13+
writer.field('MODES', 'C', 64, 0)
14+
writer.field('LANES', 'N', 35, 7)
15+
writer.field('DATA1', 'N', 35, 7) # hourly capacity per lane
16+
writer.field('DATA2', 'C', 35, 7) # add mph
17+
18+
s = set()
19+
for n in reader.iterShapeRecords():
20+
record = n.record
21+
shape = n.shape
22+
if record['ID'] not in s:
23+
s.add(f"{record['JNODE']}-{record['INODE']}")
24+
writer.record(
25+
record['ID'],
26+
record['MODES'],
27+
record['LANES'],
28+
record['DATA1'],
29+
str(record['DATA2']) + " mph",
30+
)
31+
writer.shape(shape)
32+
33+
writer.close()
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)