This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathretrieve.py
executable file
·188 lines (149 loc) · 5.34 KB
/
retrieve.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib.request, json, urllib.parse
from collections import deque
from helper.listConcat import listConcat
from scrape import DateTimeEncoder
import glob
streetCache = {}
def searchStreet(street):
if street in streetCache:
print('cache hit: %s ' % street)
return streetCache[street]
data = '<osm-script output="json" timeout="25">' + \
'<id-query into="area" ref="3600062594" type="area"/>' + \
'<difference>' + \
'<query type="way">' + \
'<has-kv k="name" v="PLACEHOLDER"/>' + \
'<has-kv k="highway" />' + \
'<area-query from="area"/>' + \
'</query>' + \
'<union>' + \
'<query type="way">' + \
'<has-kv k="highway" v="service"/>' + \
'<area-query from="area"/>' + \
'</query>' + \
'<query type="way">' + \
'<has-kv k="highway" v="track"/>' + \
'<area-query from="area"/>' + \
'</query>' + \
'</union>' + \
'</difference>' + \
'<print mode="body"/>' + \
'<recurse type="down"/>' + \
'<print mode="skeleton" order="quadtile"/>' + \
'</osm-script>'
data = data.replace('PLACEHOLDER', street)
response = urllib.request.urlopen("http://overpass-api.de/api/interpreter?data=" + urllib.parse.quote_plus(data))
content = response.read()
data = json.loads(content.decode('utf8'))
if len(data['elements']) == 0:
print('street %s couldn\'t be found' % street)
raise Exception
nodes = []
ways = []
detailedNodes = {}
for d in data['elements']:
if d['type'] == 'way':
nodes = nodes + d['nodes']
ways.append(d['nodes'])
if d['type'] == 'node':
detailedNodes[d['id']] = {'lng': d['lon'], 'lat': d['lat']}
if len(nodes) == 0:
print('no nodes for street %s' % street)
raise Exception
result = {'nodes': set(nodes), 'detailed': detailedNodes, 'ways': ways}
streetCache[street] = result
return result
def findIntersection(street1, street2, street3=False):
try:
data1 = searchStreet(street1)
data2 = searchStreet(street2)
except Exception:
return []
sameNodes = list(data2['nodes'].intersection(data1['nodes']))
if len(sameNodes) == 0:
print('sameNodes empty')
allNodes = {}
allNodes.update(data1['detailed'])
allNodes.update(data2['detailed'])
result = []
if street3:
# specifc way
try:
data3 = searchStreet(street3)
except Exception:
return []
allNodes.update(data3['detailed'])
mergedWays = listConcat()
for way in data1['ways']:
mergedWays.add(way)
ways = mergedWays.get()
sameNodes2 = list(data3['nodes'].intersection(data3['nodes']))
if len(sameNodes2) == 0:
print('sameNodes2 empty')
for node1 in sameNodes:
for node2 in sameNodes2:
for way in ways:
if node1 in way and node2 in way:
result.append([allNodes[n] for n in way])
else:
for node in sameNodes:
result.append(allNodes[node])
return result
def findStreet(street):
data = searchStreet(street)
result = []
for way in data['ways']:
result.append([data['detailed'][n] for n in way])
return result
def extract():
files = glob.glob('data-2*.json')
files.sort()
files.reverse()
if len(files) == 0:
raise SystemExit
# extract date
filename = files[0]
date = filename[5:-5]
print('file used: %s' % filename)
f = open(filename)
data = json.load(f)
found = []
notfound = []
i = 0
for entry in data:
i += 1
if 'location' in entry['parsed']:
street1 = entry['street']
street2 = entry['parsed']['location']['streets'][0]
# remove "
street2 = street2.replace('"', '')
if entry['parsed']['location']['relation'] == 'intersection':
print('%2i/%2i process - intersection - %s %s' % (i, len(data), street1, street2))
geodata = findIntersection(street1, street2)
elif entry['parsed']['location']['relation'] == 'between':
street3 = entry['parsed']['location']['streets'][1]
print('%2i/%2i process - between - %s %s %s' % (i, len(data), street1, street2, street3))
geodata = findIntersection(street1, street2, street3)
else:
print('%2i/%2i skip - %s' % (i, len(data), entry['parsed']['location']['relation']))
continue
if len(geodata) > 0:
print('\tfound')
entry['geodata'] = geodata
found.append(entry)
else:
print('\tnot found')
notfound.append(entry)
else:
print('%2i/%2i' % (i, len(data)))
f = open('data-parsed-' + date + '.json', 'w')
json.dump(found, f, cls=DateTimeEncoder)
f.close()
f = open('data-parsed-' + date + '-notfound.json', 'w')
json.dump(notfound, f, cls=DateTimeEncoder)
f.close()
print('stats: found: %2i/%2i' % (len(found), len(data)))
if __name__ == "__main__":
extract()