-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretrieve.py
executable file
·142 lines (118 loc) · 4.4 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
#!/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
def findIntersection(street1, street2, street3=False):
a = '%3Cosm-script%20output%3D%22json%22%20timeout%3D%2225%22%3E%0A%20%20%20%20%3Cid-query%20type%3D%22area%22%20ref%3D%223600062594%22%20into%3D%22area%22%2F%3E%0A%20%20%20%20%3Cunion%3E%0A%20%20%20%20%20%20%20%20%3Cquery%20type%3D%22way%22%3E%0A%20%20%20%20%20%20%3Chas-kv%20k%3D%22name%22%20v%3D%22'
b = '%22%2F%3E%0A%20%20%20%20%20%20%3Carea-query%20from%3D%22area%22%2F%3E%0A%20%20%20%20%3C%2Fquery%3E%0A%20%20%3C%2Funion%3E%0A%20%20%3C%21--%20print%20results%20--%3E%0A%20%20%3Cprint%20mode%3D%22body%22%2F%3E%0A%20%20%3Crecurse%20type%3D%22down%22%2F%3E%0A%20%20%3Cprint%20mode%3D%22skeleton%22%20order%3D%22quadtile%22%2F%3E%0A%3C%2Fosm-script%3E'
data = '<osm-script>' + \
'<osm-script output="json" timeout="25">' + \
'<id-query into="area" {{nominatimArea:Chemnitz}} type="area"/>' + \
'<union>' + \
'<query type="way">' + \
'<has-kv k="name" v="PLACEHOLDER"/>' + \
'<area-query from="area"/>' + \
'</query>' + \
'</union>' + \
'<print mode="body"/>' + \
'<recurse type="down"/>' + \
'<print mode="skeleton" order="quadtile"/>' + \
'</osm-script>' + \
'</osm-script>'
response = urllib.request.urlopen("http://overpass-api.de/api/interpreter?data=" + a + urllib.parse.quote(street1) + b )
content = response.read()
data1 = json.loads(content.decode('utf8'))
response = urllib.request.urlopen("http://overpass-api.de/api/interpreter?data=" + a + urllib.parse.quote(street2) + b )
content = response.read()
data2 = json.loads(content.decode('utf8'))
if street3:
response = urllib.request.urlopen("http://overpass-api.de/api/interpreter?data=" + a + urllib.parse.quote(street3) + b )
content = response.read()
data3 = json.loads(content.decode('utf8'))
if len(data3['elements']) == 0:
return []
if len(data1['elements']) == 0 or len(data2['elements']) == 0:
return []
nodes1 = []
for d in data1['elements']:
if d['type'] == 'way':
nodes1 = nodes1 + d['nodes']
nodes2 = []
for d in data2['elements']:
if d['type'] == 'way':
nodes2 = nodes2 + d['nodes']
if street3:
nodes3 = []
for d in data3['elements']:
if d['type'] == 'way':
nodes3 = nodes3 + d['nodes']
nodes3 = set(nodes3)
nodes1 = set(nodes1)
nodes2 = set(nodes2)
sameNodes = list(nodes2.intersection(nodes1))
tmp = data1['elements'] + data2['elements']
if street3:
tmp = tmp + data3['elements']
allNodes = {}
for node in tmp:
if node['type'] == 'node':
allNodes[node['id']] = {'lat': node['lat'], 'lng': node['lon']}
result = []
if street3:
# specifc way
mergedWays = listConcat()
for d in data1['elements']:
if d['type'] == 'way':
mergedWays.add(d['nodes'])
ways = mergedWays.get()
sameNodes2 = list(nodes3.intersection(nodes1))
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:
# specific point
for node in data2['elements']:
if node['type'] == 'node' and node['id'] in sameNodes:
result.append({'lat': node['lat'], 'lng': node['lon']})
return result
def extract():
f = open('data.json')
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 %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 %s %s %s'%(i, len(data), street1, street2, street3))
geodata = findIntersection(street1, street2, street3)
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.json', 'w')
json.dump(found, f, cls=DateTimeEncoder)
f.close()
f = open('data-parsed-notfound.json', 'w')
json.dump(notfound, f, cls=DateTimeEncoder)
f.close()
if __name__ == "__main__":
extract()