-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboxes.py
104 lines (73 loc) · 2.3 KB
/
boxes.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
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
95
96
97
98
99
100
101
102
103
104
import json
import requests
import xml.etree.ElementTree as ET
import time
from pprint import pprint
def bbox_from_polygon(polygon):
minLat, maxLat, minLon, maxLon = 90, 0, 180, 0
# this margin is used to enlarge the bounding box in order to actually
# catch the streets around the area in question
margin = 0.001
for point in polygon[0]:
if point[0] < minLat:
minLat = point[0] - margin
if point[0] > maxLat:
maxLat = point[0] + margin
if point[1] < minLon:
minLon = point[1] - margin
if point[1] > maxLon:
maxLon = point[1] + margin
bbox = { "box": [minLat, minLon, maxLat, maxLon], "center": [(minLat + maxLat) / 2, (minLon + maxLon) / 2] }
return bbox
def extract_ways(box):
url = "http://api.openstreetmap.org/api/0.6/map?bbox=%f,%f,%f,%f" % tuple(box["box"])
r = requests.get(url)
ways = []
data = ET.fromstring(r.text)
it = data.getiterator("way")
for el in data.getiterator("way"):
highwayCheck = el.find(".//tag[@k='highway']")
if highwayCheck is None:
continue
name = el.find(".//tag[@k='name']")
if name is None:
continue
else:
name = name.get("v")
level = highwayCheck.get("v")
wayInfo = {"name": name, "level": level }
if name not in [x["name"] for x in ways]:
ways.append(wayInfo)
return ways
def process_feature(feature):
geometry = feature["geometry"]
geoinfo = {}
geoinfo["id"] = feature["properties"]["spatial_alias"]
bboxes = []
if geometry["type"] == "MultiPolygon":
for polygon in geometry["coordinates"]:
bbox = bbox_from_polygon(polygon)
bboxes.append(bbox)
else:
bbox = bbox_from_polygon(geometry["coordinates"])
bboxes.append(bbox)
geoinfo["ways"] = []
for box in bboxes:
geoinfo["ways"] = extract_ways(box)
print("processed \"%s\" - (%d of %d)" % (geoinfo["id"], len(geoinfos), len(json_data["features"])))
return geoinfo
# Process all features in 'plan.geojson' for now
fd = open('plan.geojson')
json_data = json.load(fd)
fd.close()
geoinfos = []
i = 3
for feature in json_data["features"]:
geoinfos.append(process_feature(feature))
#time.sleep(4)
i -= 1
if i == 0:
break
fd = open('geoinfos.json', 'w')
json.dump(geoinfos, fd, ensure_ascii=False, indent=4, sort_keys=True, separators=(',', ': '))
fd.close()