forked from simwrapper/simwrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-json-network.py
More file actions
executable file
·57 lines (45 loc) · 1.56 KB
/
create-json-network.py
File metadata and controls
executable file
·57 lines (45 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# matsim network converter
try:
import sys, json, gzip
import matsim
from dfply import *
from pyproj import Transformer
except:
print("OOPS! Error importing required libraries.")
print('try "pip3 install matsim-tools dfply pyproj"')
if len(sys.argv) != 3:
print(
"USAGE: python create-json-network.py [network] [coord-system]"
)
sys.exit(1)
p_network = sys.argv[1]
p_coords = sys.argv[2]
out_file = p_network.replace(".xml.", ".json.")
coord_transformer = Transformer.from_crs(p_coords, "EPSG:4326")
print("reading network:", p_network)
network = matsim.read_network(p_network)
# Build link x/y lookup
nodes = network.nodes >> mutate(to_node=X.node_id, from_node=X.node_id)
links = (
network.links
>> inner_join(nodes, by="from_node")
>> select(X.link_id, X.from_node, X.to_node_x, X.x, X.y)
>> mutate(x0=X.x, y0=X.y, to_node=X.to_node_x)
>> inner_join(nodes, by="to_node")
>> select(X.link_id, X.x0, X.y0, X.x_y, X.y_y)
)
# convert coords to lat/lon
converted_links = {}
for link in links.values:
coords = []
fromY, fromX = coord_transformer.transform(link[1],link[2])
coords.extend([round(fromX, 5), round(fromY, 5)])
toY, toX = coord_transformer.transform(link[3], link[4])
coords.extend([round(toX, 5), round(toY, 5)])
converted_links[link[0]] = coords
# write it out
print("Writing:", out_file)
links_bytes = json.dumps(converted_links, separators=(',',":")).encode("utf-8")
with gzip.open(out_file, "wb") as f:
f.write(links_bytes)
print(len(converted_links), "links written.")