forked from simwrapper/simwrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-geojson-network.py
More file actions
executable file
·80 lines (66 loc) · 2.12 KB
/
create-geojson-network.py
File metadata and controls
executable file
·80 lines (66 loc) · 2.12 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
# 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-geojson-network.py [network] [coord-system]"
)
sys.exit(1)
p_network = sys.argv[1]
p_coords = sys.argv[2]
out_file = p_network + ".geojson.gz"
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
print("reprojecting coordinates")
converted_links = {}
for link in links.values:
coords = []
fromY, fromX = coord_transformer.transform(link[1],link[2])
coords.extend([round(fromX, 6), round(fromY, 6)])
toY, toX = coord_transformer.transform(link[3], link[4])
coords.extend([round(toX, 6), round(toY, 6)])
converted_links[link[0]] = coords
# convert to geojson
print("creating geojson")
geojson = {
"type": "FeatureCollection",
"features": []
}
for link_id in converted_links:
points = converted_links[link_id]
geojson["features"].append({
"type": "Feature",
"id": link_id,
"geometry": {
"type": "LineString",
"coordinates": [ [points[0],points[1]], [points[2],points[3]]]
}
# -- for now, no properties. Saves space
# "properties": {
# }
})
# write it out
print("writing:", out_file)
#links_bytes = json.dumps(geojson, separators=(',',":")).encode("utf-8")
links_bytes = json.dumps(geojson).encode("utf-8")
with gzip.open(out_file, "wb") as f:
f.write(links_bytes)
print(len(converted_links), "links written.")