forked from balrog-kun/osm-addr-tools
-
Notifications
You must be signed in to change notification settings - Fork 3
/
deduplicate-addresses.py
71 lines (55 loc) · 1.56 KB
/
deduplicate-addresses.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
#!/usr/bin/env python3.4
from bs4 import BeautifulSoup
import sys
def getTag(node, tag):
n = node.find("tag", k=tag)
if n:
return n.get("v")
else:
return None
def makeEntry(soup):
return {
"soup": soup,
"addr:city": getTag(soup, "addr:city"),
"addr:place": getTag(soup, "addr:place"),
"addr:street": getTag(soup, "addr:street"),
"addr:housenumber": getTag(soup, "addr:housenumber"),
}
def makeKey(dct):
return (
dct.get("addr:city"),
dct.get("addr:place"),
dct.get("addr:street"),
dct.get("addr:housenumber"),
)
def markDuplicate(soup, lst):
print("marking dups")
value = "Duplicate (first)"
while lst:
dct = lst.pop()
node = dct["soup"]
nt = soup.new_tag("tag", k="fixme", v=value)
node.append(nt)
value = "Duplicate"
def main():
with open(sys.argv[1]) as f:
soup = BeautifulSoup(f)
ret = {}
for i in soup.osm.find_all(
lambda x: int(x.get("id", 1)) < 0 and x.find("tag", k="addr:housenumber"),
recursive=False,
):
entry = makeEntry(i)
key = makeKey(entry)
try:
lst = ret[key]
except KeyError:
lst = []
ret[key] = lst
lst.append(entry)
for i in filter(lambda x: len(x) > 1, ret.values()):
markDuplicate(soup, i)
with open("output.osm", "w+") as f:
f.write(soup.prettify())
if __name__ == "__main__":
main()