-
Notifications
You must be signed in to change notification settings - Fork 4
/
merge.py
65 lines (53 loc) · 1.9 KB
/
merge.py
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
import json
import os
from shapely.geometry import MultiPoint, mapping
CITY_DATA = [{
"name": "Seattle",
"key": "wa.seattle",
"center": [-122.339, 47.604, 15],
"transportation_data": "./cities/seattle/output/transportation.geojson",
}, {
"name": "Mt. Vernon",
"key": "wa.mtvernon",
"center": [-122.336, 48.419, 14],
"transportation_data": "./cities/mtvernon/output/transportation.geojson",
}, {
"name": "Bellingham",
"key": "wa.bellingham",
"center": [-122.478, 48.751, 13.5],
"transportation_data": "./cities/bellingham/output/transportation.geojson"
}]
def merge_geojson(datasets):
merged = {"type": "FeatureCollection", "features": []}
datasets_metadata = {"type": "FeatureCollection", "features": []}
for dataset in datasets:
with open(dataset["transportation_data"]) as f:
fc = json.load(f)
points = []
for feature in fc["features"]:
merged["features"].append(feature)
points += feature["geometry"]["coordinates"]
points = MultiPoint(points)
hull = points.convex_hull
bounds = list(hull.bounds);
datasets_metadata["features"].append({
"type": "Feature",
"geometry": mapping(hull),
"properties": {
"bounds": bounds,
"key": dataset["key"],
"name": dataset["name"],
"lon": dataset["center"][0],
"lat": dataset["center"][1],
"zoom": dataset["center"][2],
}
})
return merged, datasets_metadata
if __name__ == "__main__":
merged, datasets_metadata = merge_geojson(CITY_DATA)
if not os.path.exists("./merged"):
os.mkdir("./merged")
with open("./merged/transportation.geojson", "w") as f:
json.dump(merged, f)
with open("./merged/regions.geojson", "w") as f:
json.dump(datasets_metadata, f)