-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert-matsim-to-gmns-network.py
More file actions
executable file
·95 lines (72 loc) · 2.69 KB
/
convert-matsim-to-gmns-network.py
File metadata and controls
executable file
·95 lines (72 loc) · 2.69 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
# matsim network converter -> GMNS
# this does NOT convert coordinates: They are kept in their original coordinate system
# The GMNS npm library will convert this to proper 4326 (lng/lat) coordinates
try:
import sys, zipfile, csv, os
import matsim
import pandas
from dfply import *
except:
print("OOPS! Error importing required libraries.")
print('try "pip3 install pandas dfply matsim-tools"')
if len(sys.argv) < 3:
print(
"USAGE: python convert-matsim-to-gmns-network.py [network] [coord-system] (optional linkGeometries)"
)
sys.exit(1)
p_network = sys.argv[1]
p_coords = sys.argv[2]
p_geom = len(sys.argv) > 3 and sys.argv[3] or None
out_file = p_network + ".gmns.zip"
if not p_geom: print("--- No link geometries specified. Stick network will be generated.")
print("reading network:", p_network)
network = matsim.read_network(p_network)
# ----- config
# TODO: get real config from file instead of hardcoded everything
config = """dataset_name,short_length,long_length,speed,crs,geometry_field_format,currency,version_number
network,m,km,kph,"""+p_coords+",wkt,Euro cents,0.94"
with open("config.csv", "w") as f:
f.write(config)
# fix header line - pandas puts double quotes around column names, which Papaparse borks
trimcmd = sys.platform == 'darwin' and """sed -i '' '1s/"//g' """ or """sed -i '1s/"//g' """
# ----- nodes
network.nodes = network.nodes.rename(columns={
"x": "x_coord",
"y": "y_coord",
})
network.nodes.to_csv("node.csv", index=False, quoting=csv.QUOTE_NONNUMERIC)
os.system(trimcmd + "node.csv")
# ----- links
network.links = network.links.rename(columns={
"from_node": "from_node_id",
"to_node": "to_node_id",
})
network.links['geometry_id'] = network.links['link_id']
# drop everything (for debug)
# network.links = network.links[['link_id','from_node_id','to_node_id']]
# write links
network.links.to_csv("link.csv", index=False, quoting=csv.QUOTE_NONNUMERIC)
os.system(trimcmd + "link.csv")
# ----- geometry
if p_geom:
print("Reading geometry")
geom = pandas.read_csv(p_geom)
geom['wkt'] = geom['Geometry'].apply(lambda g: (
'LINESTRING ' + ",".join([item.replace(',',' ') for item in g.split("),(")])
))
del geom['Geometry']
geom = geom.rename(columns={
"LinkId": "geometry_id",
"wkt": "geometry",
})
# write geom
geom.to_csv("geometry.csv", index=False, quoting=csv.QUOTE_NONNUMERIC)
os.system(trimcmd + "geometry.csv")
# write it out
print("create zip:", out_file)
with zipfile.ZipFile(out_file, "w", compression=zipfile.ZIP_DEFLATED) as f:
f.write("config.csv")
f.write("node.csv")
f.write("link.csv")
if p_geom: f.write("geometry.csv")