-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_merge-building-addrs.py
executable file
·190 lines (165 loc) · 5.63 KB
/
_merge-building-addrs.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
#!/usr/bin/env python3
# vim: fileencoding=utf-8 encoding=utf-8 et sw=4
import sys
import os
import xml.etree.ElementTree as ElementTree
import string
import argparse
def print_usage():
print("Usage: python3 script_name.py <building_file.osm> <address_file.osm> [options]")
print("\nThis script processes OpenStreetMap (OSM) data to combine building outlines with address information.")
print("\nArguments:")
print(" building_file.osm OSM file containing building data")
print(" address_file.osm OSM file containing address data")
print("\nOptions:")
print(" -h, --help Show this help message and exit")
print(" -o OUTPUT, --output OUTPUT")
print(" Specify the output file name (default: output.osm)")
# Parse command line arguments
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('-h', '--help', action='store_true', help='Show help message and exit')
parser.add_argument('files', nargs='*', help='Input OSM files')
parser.add_argument('-o', '--output', default='output.osm', help='Specify the output file name')
args = parser.parse_args()
if args.help or len(args.files) != 2:
print_usage()
sys.exit(0)
building_file, address_file = args.files
output_file = args.output
outroot = ElementTree.Element("osm", {"version": "0.6"})
bldgroot = ElementTree.parse(building_file).getroot()
addrroot = ElementTree.parse(address_file).getroot()
waynodes = {}
bldgs = []
addrs = []
# Read the building outlines
for elem in bldgroot:
if 'id' not in elem.attrib:
continue
id = int(elem.attrib['id'])
if elem.tag == 'node':
lat = float(elem.attrib['lat'])
lon = float(elem.attrib['lon'])
waynodes[id] = (lat, lon)
outroot.append(elem)
if elem.tag != 'way':
outroot.append(elem)
continue
tags = {}
for sub in elem:
if sub.tag != 'tag':
continue
v = sub.attrib['v'].strip()
if v:
tags[sub.attrib['k']] = v
# Tag transformations can happen here
# Parse the geometry, store in a convenient format,
# also find some point in the middle of the outline to be used to
# speed up distance calculation
way = []
refs = []
j = 0
lat = 0.0
lon = 0.0
for sub in elem:
if sub.tag != 'nd':
continue
ref = int(sub.attrib['ref'])
if ref not in waynodes:
print(f"Warning: Node {ref} referenced in way {id} not found", file=sys.stderr)
continue
way.append(waynodes[ref])
refs.append(ref)
j += 1
lat += waynodes[ref][0]
lon += waynodes[ref][1]
if j == 0:
print(f"Warning: Way {id} has no valid nodes", file=sys.stderr)
outroot.append(elem)
continue
lat /= j
lon /= j
if refs[0] != refs[-1]:
print(f"Warning: Way {id} is not closed", file=sys.stderr)
outroot.append(elem)
continue
if 'version' in elem.attrib:
v = int(elem.attrib['version'])
else:
v = 1
bldgs.append((lat, lon, way, refs, tags, id, v, []))
bldgroot = None # Make python release the XML structure
def contains(poly, pos):
cont = False
prev = poly[0]
for node in poly[1:]:
if (node[1] > pos[1]) != (prev[1] > pos[1]) and pos[0] < node[0] + \
(prev[0] - node[0]) * (pos[1] - node[1]) / (prev[1] - node[1]):
cont = not cont
prev = node
return cont
# Read the address nodes data
for elem in addrroot:
if 'id' not in elem.attrib:
continue
tags = {}
for sub in elem:
if sub.tag != 'tag':
continue
v = sub.attrib['v'].strip()
if v:
tags[sub.attrib['k']] = v
if elem.tag != 'node':
continue
lat = float(elem.attrib['lat'])
lon = float(elem.attrib['lon'])
id = int(elem.attrib['id'])
if 'version' in elem.attrib:
v = int(elem.attrib['version'])
else:
v = 1
addr = (lat, lon, tags, id, v, [])
addrs.append(addr)
# Find what if any building shapes contain this lat/lon
for elat, elon, way, refs, btags, id, v, newaddrs in bldgs:
if 'addr:housenumber' in btags:
continue
if abs(elat - lat) + abs(elon - lon) > 0.006:
continue
if not contains(way, (lat, lon)):
continue
newaddrs.append(addr)
break
addrroot = None
for lat, lon, way, refs, tags, id, v, newaddrs in bldgs:
attrs = {"version": str(v), "id": str(id)}
# If this building contains only a single address node, copy its tags
# to the building way and mark the node as no longer needed.
if len(newaddrs) == 1:
newaddrs[0][5].append(1)
if 'source' in newaddrs[0][2]:
newaddrs[0][2]['source:addr'] = newaddrs[0][2]['source']
del newaddrs[0][2]['source']
tags.update(newaddrs[0][2])
attrs['action'] = 'modify'
elem = ElementTree.SubElement(outroot, "way", attrs)
for k in tags:
ElementTree.SubElement(elem, 'tag', {'k': k, 'v': tags[k]})
for ref in refs:
ElementTree.SubElement(elem, 'nd', {'ref': str(ref)})
# Add remaining addresses as nodes
for lat, lon, tags, id, v, bbs in addrs:
if bbs:
continue
i = id
if i < 0:
i -= 2000000
elem = ElementTree.SubElement(outroot, "node", {
'lat': str(lat),
'lon': str(lon),
"version": str(v),
"id": str(i)})
for k in tags:
ElementTree.SubElement(elem, 'tag', {'k': k, 'v': tags[k]})
print(f"Writing to {output_file}")
ElementTree.ElementTree(outroot).write(output_file, encoding="utf-8", xml_declaration=True)