forked from simwrapper/simwrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-csv-network.py
More file actions
executable file
·46 lines (37 loc) · 1.19 KB
/
create-csv-network.py
File metadata and controls
executable file
·46 lines (37 loc) · 1.19 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
# VSP matsim network reader - creates a CSV from an output.xml.gz file
# Node attributes are ignored.
try:
import sys, json, gzip
import matsim
from dfply import *
except:
print("OOPS! Error importing required libraries.")
print('try "pip3 install matsim-tools dfply"')
if len(sys.argv) != 2:
print(
"USAGE: python3 create-csv-network.py [network]"
)
sys.exit(1)
p_network = sys.argv[1]
out_file = p_network.replace(".xml.gz", ".csv")
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)
link_coords = (
network.links
>> inner_join(nodes, by="from_node")
>> 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
wkt = []
for link in link_coords.values:
wkt.append(f"LINESTRING ({link[1]} {link[2]}, {link[3]} {link[4]})")
# append wkt to the dataframe
links = network.links >> mutate(wkt = wkt)
# write it out
print("Writing:", out_file)
links.to_csv(out_file, index=False )
print(len(links.values), "links written.")