-
Notifications
You must be signed in to change notification settings - Fork 0
/
osm-stats.py
executable file
·68 lines (51 loc) · 1.6 KB
/
osm-stats.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
#!/usr/bin/env python
import sys
import urllib2
import xmltodict
def main(user, t1=None, t2=None):
xml_file = "http://www.openstreetmap.org/api/0.6/changesets?display_name={}".format(user)
if t1:
xml_file += "&time={}".format(t1)
if t2:
xml_file += ",{}".format(t2)
changesets = parse_changesets(xml_file)
print('Number of changesets: {}'.format(len(changesets)))
nedits = 0
for changeset in changesets:
nedits += parse_changeset(changeset)
print('Number of changes: {}'.format(nedits))
return 0
def parse_changesets(xml_file):
changesets = []
data = parse_xml(xml_file)
for item in data['osm']['changeset']:
changesets.append(item['@id'])
return changesets
def parse_changeset(changeset):
xml_file = "http://www.openstreetmap.org/api/0.6/changeset/{}/download".format(changeset)
data = parse_xml(xml_file)
nedits = 0
for item in ('create', 'modify', 'delete'):
if item not in data['osmChange'].keys():
continue
for subitem in data['osmChange'][item]:
nedits += 1
return nedits
def parse_xml(xml_file):
try:
fd = urllib2.urlopen(xml_file)
except urllib2.URLError as e:
sys.exit(e)
obj = xmltodict.parse(fd.read())
fd.close()
return obj
if __name__ == "__main__":
if len(sys.argv) == 1:
sys.exit("User not defined")
user = sys.argv[1]
t1 = t2 = None
if len(sys.argv) > 2:
t1 = sys.argv[2]
if len(sys.argv) > 3:
t2 = sys.argv[3]
sys.exit(main(user, t1, t2))