forked from balrog-kun/osm-addr-tools
-
Notifications
You must be signed in to change notification settings - Fork 3
/
find_broken_buildings.py
81 lines (67 loc) · 1.87 KB
/
find_broken_buildings.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
import overpass
import json
from urllib.request import urlopen
from bs4 import BeautifulSoup
import argparse
def get_data_From_area(terc, user, date):
query = """
[out:json]
[timeout:1200]
;
area
["boundary"="administrative"]
["admin_level"="9"]
["teryt:terc"="%s"]
["type"="boundary"]
->.boundryarea;
(
way
(changed:"%s")
(user:"%s")
(area.boundryarea)
["building"];
);
out meta bb qt;
""" % (
terc,
date,
user,
)
return json.loads(overpass.query(query))
def xml_to_json(data):
bs = BeautifulSoup(data)
way = bs.find("way")
element = dict(way.attrs)
element["nodes"] = list(map(lambda x: int(x.get("ref")), way.find_all("nd")))
element["tags"] = dict((x.get("k"), x.get("v")) for x in way.find_all("tag"))
return element
def osm_api_get(url):
url = "https://api.openstreetmap.org/%s" % (url,)
return xml_to_json(urlopen(url))
def get_history(obj):
ret = {}
for i in obj["elements"]:
ret[i["id"]] = osm_api_get(
"/api/0.6/%s/%s/%s" % (i["type"], i["id"], int(i["version"]) - 1)
)
return ret
def main():
parser = argparse.ArgumentParser(description="finds buildings with removed nodes")
parser.add_argument("--terc", help="TERYT terc code for area", required=True)
parser.add_argument("--user", help="User name doing changes", required=True)
parser.add_argument(
"--date", help="Date in ISO format, ex. 2015-10-01T00:00:00Z", required=True
)
args = parser.parse_args()
ways = get_data_From_area(args.terc, args.user, args.date)
history = get_history(ways)
ret = []
for i in ways["elements"]:
if (
len(set(i["nodes"]).symmetric_difference(set(history[i["id"]]["nodes"])))
> 0
):
ret.append(i["id"])
print(",".join(map(str, ret)))
if __name__ == "__main__":
main()