-
Notifications
You must be signed in to change notification settings - Fork 4
/
convert_cities_json_to_geojson.py
45 lines (39 loc) · 1.17 KB
/
convert_cities_json_to_geojson.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
import json
import requests
# Converts the Mapzen metro extracts JSON array to a GeoJSON feature collection.
# Used once to import the original cities into this repository.
response = requests.get("https://raw.githubusercontent.com/mapzen/metro-extracts/master/cities.json")
response.raise_for_status()
cities = response.json()
def translate(obj):
west, south, east, north = map(float, [
obj['bbox']['left'],
obj['bbox']['bottom'],
obj['bbox']['right'],
obj['bbox']['top']
])
return {
"type": "Feature",
"id": obj["id"],
"geometry": {
"type": "Polygon",
"coordinates":[[
[west, south],
[west, north],
[east, north],
[east, south],
[west, south]
]]
},
"properties": {
"name": obj['name'],
"region": obj['region'],
"country": obj['country']
}
}
feature_collection = {
"type": "FeatureCollection",
"features": [translate(o) for o in cities],
}
with open('cities.geojson', 'w') as out_f:
json.dump(feature_collection, out_f, indent=4)