-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenStreetMapProject.py
465 lines (321 loc) · 12.7 KB
/
openStreetMapProject.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# coding: utf-8
# Loading data
# In[2]:
import xml.etree.cElementTree as ET
import pprint
import re
import codecs
import json
from collections import defaultdict
from pymongo import MongoClient
# In[3]:
k = 100 # Parameter: take every k-th top level element
SAMPLE_FILE = "mapsimple.xml"
xmlFile = "map.xml"
def get_element(xml_file, tags=('node', 'way', 'relation')):
"""Yield element if it is the right type of tag
Reference:
http://stackoverflow.com/questions/3095434/inserting-newlines-in-xml-file-generated-via-xml-etree-elementtree-in-python
"""
context = iter(ET.iterparse(xml_file, events=('start', 'end')))
_, root = next(context)
for event, elem in context:
if event == 'end' and elem.tag in tags:
yield elem
root.clear()
with open(SAMPLE_FILE, 'wb') as output:
output.write('<?xml version="1.0" encoding="UTF-8"?>\n')
output.write('<osm>\n ')
# Write every kth top level element
for i, element in enumerate(get_element(xmlFile)):
if i % k == 0:
output.write(ET.tostring(element, encoding='utf-8'))
output.write('</osm>')
# In[4]:
CREATED = [ "version", "changeset", "timestamp", "user", "uid"]
problemchars = re.compile(r'[=\+/&<>;\'"\?%#$@\,\. \t\r\n]')
lower = re.compile(r'^([a-z]|_)*$')
lower_colon = re.compile(r'^([a-z]|_)*:([a-z]|_)*$')
#Changing this regex to match brasilian names of streets
street_type_re = re.compile(r'^\b\S+\.?', re.IGNORECASE)
street_types = defaultdict(set)
expected = [u"Rua", u"Avenida", u"Travessa", u"Beco", u"Rodovia", u"Estrada", u"Logradouro", u"Viela", u"Alameda", u"Via", u"Praça"]
# UPDATE THIS VARIABLE
mapping = { "R": "Rua",
"R.": "Rua",
"r." : "Rua",
"rua": "Rua",
"RUA": "Rua",
"Rua-": "Rua ",
"Ruas": "Rua",
"Av.": "Avenida",
"AV.": "Avenida",
"AVENIDA":"Avenida",
"Alamenda": "Alameda",
"Rodovoa": "Rodovia"
}
def audit_street_type(street_types, street_name):
m = street_type_re.search(street_name)
if m:
street_type = m.group()
if street_type not in expected:
street_types[street_type].add(street_name)
def shape_element(element):
node = {}
if element.tag == "node" or element.tag == "way" :
node["type"]=element.tag
for at in element.attrib:
if at in CREATED :
if node.get("created") == None:
node[u"created"] = {}
node[u"created"].update({at : element.get(at)})
elif at in "lon":
if node.get("pos") ==None:
node[u"pos"] = [0,1]
node[u"pos"][1] = float(element.get(at))
elif at in "lat":
if node.get("pos") ==None:
node[u"pos"] = [0,1]
node[u"pos"][0]= float(element.get(at))
else:
node[at] = element.get(at)
for child in element:
if child.get("k"):
if problemchars.search(child.get("k")):
continue
elif re.search(':',child.get("k")) != None:
name = child.get("k").split(":")
if node.get(name[0]) == None:
node[name[0]] = {}
if name[1]==u"street":
audit_street_type(street_types, child.get("v"))
node[name[0]].update({name[1] : child.get("v")})
else:
if node.get("caract")==None:
node[u"caract"]={}
node[u"caract"].update({child.get("k"):child.get("v")})
if element.tag == "way":
if child.tag == "nd":
if node.get("node_refs") == None:
node[u"node_refs"] = []
node[u"node_refs"].append(child.get("ref"))
return node
else:
return None
def key_type(element, keys):
if element.tag == "way":
for tag in element.iter("tag"):
k = tag.get("k")
if lower.search(k):
keys['lower'] +=1
elif lower_colon.search(k):
keys['lower_colon'] +=1
elif problemchars.search(k):
keys['problemchars'] +=1
else:
keys['other'] +=1
return keys
def process_keys(file_in):
keys = {"lower": 0, "lower_colon": 0, "problemchars": 0, "other": 0}
for event, element in ET.iterparse(file_in, events=("start",)):
keys = key_type(element, keys)
return keys
def process_map(file_in, pretty = False):
#street_types = defaultdict(set)
file_out = "{0}.json".format(file_in)
data = []
with codecs.open(file_out, "w") as fo:
for _, element in ET.iterparse(file_in):
el = shape_element(element)
if el:
data.append(el)
if pretty:
fo.write(json.dumps(el, indent=2)+"\n")
else:
fo.write(json.dumps(el) + "\n")
return data
def update_name(name):
m = street_type_re.search(name)
if m:
if m in mapping:
name = name.replace(m.group(), mapping[m.group()])
return name
def audit_street_name(data):
for tupla in data:
if tupla.get('addr'):
if tupla.get('addr').get('street'):
name = tupla.get('addr').get('street')
tupla.get('addr')['street'] = update_name(name)
# In[5]:
data = process_map(xmlFile, True)
#auditing street names
audit_street_name(data)
# In[14]:
client = MongoClient("mongodb://localhost:27017")
db = client['goiania']
#drop the documents if exists to avoid append the same data twice
db.goiania.drop();
# In[15]:
print db
# In[16]:
db.goiania.insert_many(data)
print db.goiania.find_one()
# In[17]:
db.goiania.find().count()
# In[32]:
user = db.goiania.distinct("created.user")
print len(user)
# In[33]:
#quantity contribution by user
query = []
query.append({"$group" : {"_id" : "$created.user", "count":{"$sum":1}}})
query.append({"$sort" : {"count": -1}})
query.append({"$limit" : 10})
result = db.goiania.aggregate(query)
for doc in result:
pprint.pprint(doc)
# In[20]:
#user that have the most number of contributions
query = []
query.append({"$group" : {"_id" : "$created.user", "count":{"$sum":1}}})
query.append({"$sort" : {"count": -1}})
query.append({"$limit" : 1})
result = db.goiania.aggregate(query)
for doc in result:
pprint.pprint(doc)
# In[58]:
#unique contributors
query = []
query.append({"$group" : {"_id" : "$created.user", "count":{"$sum":1}}})
query.append({"$match" : {"count" : 1}})
result = db.goiania.aggregate(query)
print len(list(db.goiania.aggregate(query)))
for doc in result:
pprint.pprint(doc)
# In[21]:
#quantity by type (node\way)
query = []
query.append({"$group" : {"_id" : "$type", "count":{"$sum":1}}})
query.append({"$sort" : {"count": -1}})
result = db.goiania.aggregate(query)
for doc in result:
pprint.pprint(doc)
# In[22]:
#amenities
db.goiania.distinct("caract.amenity")
# In[34]:
#quantity amenities
query = []
query.append({"$group" : {"_id" : "$caract.amenity", "count":{"$sum":1}}})
query.append({"$sort" : {"count": -1}})
query.append({"$limit" : 10})
result = db.goiania.aggregate(query)
for doc in result:
pprint.pprint(doc)
# In[24]:
#streets
db.goiania.distinct("addr.street")
# In[45]:
#streets without street info
query = []
query.append({"$match":{"addr.street": {"$eq": "Qd4 Lt10"}}})
result = db.goiania.aggregate(query)
for doc in result:
pprint.pprint(doc)
# In[25]:
#all the documents that street starts with Qd
result = db.goiania.find({"addr.street": {'$regex': '^Qd', '$options': 'i'}})
print db.goiania.find({"addr.street": {'$regex': '^Qd', '$options': 'i'}}).count()
print db.goiania.find({"addr.street": {'$regex': '^Qd', '$options': 'i'}}).distinct("created.user")
for doc in result:
print pprint.pprint(doc)
# In[28]:
#finding the regex
string = "74.110-100"
print re.match(r"^74\.?\d{3}\-?\d{3}$", string)
# In[29]:
#count quantity of addr tags
print db.goiania.find({"addr": {'$exists': True}}).count()
#count quantity of addr tags with street information
print db.goiania.find({"addr.street": {'$exists': True}}).count()
#postcodes are like the regex?
print db.goiania.find({"addr.postcode":{'$exists':True}}).count()
print db.goiania.find({"addr.postcode":{'$exists':True, "$not": re.compile(r"^74\.?\d{3}\-?\d{3}$")}}).count()
postcodes = db.goiania.find({"addr.postcode":{'$exists':True, "$not": re.compile(r"^74\.?\d{3}\-?\d{3}$}")}})
for doc in postcodes:
print pprint.pprint(doc)
#checking out the cities of wrong postcodes
print db.goiania.find({"addr.postcode":{'$exists':True, "$not": re.compile(r"^74\.?\d{3}\-?\d{3}$")}}).distinct("addr.city")
# In[30]:
#amenity: restaurant cousine
print db.goiania.find({"caract.amenity":{"$exists": True, "$eq": u'restaurant'}}).distinct("caract.cuisine")
#most frequent cuisine
query = []
query.append({"$match":{"caract.amenity":{"$exists": True, "$eq": u'restaurant'}}})
query.append({"$group" : {"_id" : "$caract.cuisine", "count":{"$sum":1}}})
query.append({"$sort" : {"count": -1}})
cuisine = db.goiania.aggregate(query)
for doc in cuisine:
print pprint.pprint(doc)
#amenity: restaurant cousine
print db.goiania.find({"caract.amenity":{"$exists": True, "$eq": u'fast_food'}}).distinct("caract.cuisine")
#most frequent cuisine
query = []
query.append({"$match":{"caract.amenity":{"$exists": True, "$eq": u'fast_food'}}})
query.append({"$group" : {"_id" : "$caract.cuisine", "count":{"$sum":1}}})
query.append({"$sort" : {"count": -1}})
cuisine = db.goiania.aggregate(query)
for doc in cuisine:
print pprint.pprint(doc)
#amenity: postal_office user geocorreios
print db.goiania.find({"caract.amenity":{"$exists": True, "$eq": u'post_office'}}).distinct("created.user")
#addr suburb (bairro)
print db.goiania.find({"addr.suburb":{"$exists": True}}).count()
query = []
query.append({"$match":{"addr.suburb":{"$exists": True}}})
query.append({"$group" : {"_id" : "$addr.suburb", "count":{"$sum":1}}})
query.append({"$sort" : {"count": -1}})
#bairros = db.goiania.aggregate(query)
#for doc in bairros:
# print pprint.pprint(doc)
# In[36]:
#amenity: place_of_worship
print db.goiania.find({"caract.amenity":{"$exists": True, "$eq": u'place_of_worship'}}).distinct("caract.religion")
#most frequent religion
query = []
query.append({"$match":{"caract.amenity":{"$exists": True, "$eq": u'place_of_worship'}}})
query.append({"$group" : {"_id" : "$caract.religion", "count":{"$sum":1}}})
query.append({"$sort" : {"count": -1}})
religion = db.goiania.aggregate(query)
for doc in religion:
print pprint.pprint(doc)
# In[61]:
#information about the most common amenity parking
parking = db.goiania.find({"caract.amenity":{"$exists": True, "$eq": u'parking'}})
print db.goiania.find({"caract.amenity":{"$exists": True, "$eq": u'parking'}}).count()
print db.goiania.find({"$and" : [{"caract.amenity":{"$exists": True, "$eq": u'parking'}}, {"addr":{"$exists": True}}]}).count()
#for doc in parking:
# print pprint.pprint(doc)
# In[60]:
#information about quantity of addr in the amenities restaurant
print db.goiania.find({"caract.amenity":{"$exists": True, "$eq": u'restaurant'}}).count()
print db.goiania.find({"$and" : [{"caract.amenity":{"$exists": True, "$eq": u'restaurant'}}, {"addr":{"$exists": True}}]}).count()
# In[62]:
#information about quantity of addr in the amenities fuel
print db.goiania.find({"caract.amenity":{"$exists": True, "$eq": u'fuel'}}).count()
print db.goiania.find({"$and" : [{"caract.amenity":{"$exists": True, "$eq": u'fuel'}}, {"addr":{"$exists": True}}]}).count()
# In[63]:
#information about quantity of addr in the amenities bank
print db.goiania.find({"caract.amenity":{"$exists": True, "$eq": u'bank'}}).count()
print db.goiania.find({"$and" : [{"caract.amenity":{"$exists": True, "$eq": u'bank'}}, {"addr":{"$exists": True}}]}).count()
# In[40]:
#information about the second most common amenity fuel
fuel = db.goiania.find({"caract.amenity":{"$exists": True, "$eq": u'fuel'}})
print db.goiania.find({"caract.amenity":{"$exists": True, "$eq": u'fuel'}}).distinct("caract.brand")
#for doc in parking:
# print pprint.pprint(doc)
# In[41]:
#information about school
school = db.goiania.find({"caract.amenity":{"$exists": True, "$eq": u'school'}})
#for doc in school:
# print pprint.pprint(doc)