-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff_parser.py
125 lines (105 loc) · 4.19 KB
/
diff_parser.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
from osmdiff import OSMChange
from osmdiff import osm
from bbox import BoundingBox
bboxes = []
mergeDistance = 0
mergePercentage = 0
polygonMinSize = 0
def parse_object(osm_object):
object_type = 0
if type(osm_object) == osm.osm.Node:
inserted = False
for bbox in bboxes:
if bbox.__contains__(osm_object):
inserted = True
else:
if bbox.distance_to_osm_object(osm_object) < bbox.get_merge_distance():
bbox.insert_object(osm_object)
inserted = True
if not inserted:
# Some tools, like mapproxy-seed, doesnt want to work with polygons that have all points at the same spot
bbox = BoundingBox([float(osm_object.attribs['lon']) + polygonMinSize,
float(osm_object.attribs['lat']) - polygonMinSize],
[float(osm_object.attribs['lon']),
float(osm_object.attribs['lat'])], mergeDistance, mergePercentage)
bboxes.append(bbox)
object_type = 1
if type(osm_object) == osm.osm.Way:
object_type = 2
if type(osm_object) == osm.osm.Relation:
object_type = 3
return object_type
def parse_diff(file, configMergeDistance, configPercentageToMerge, polygonMinimumSize, verbose):
nodes_count = 0
ways_count = 0
relations_count = 0
global mergeDistance
mergeDistance = configMergeDistance
global mergePercentage
mergePercentage = configPercentageToMerge
global polygonMinSize
polygonMinSize = polygonMinimumSize
d = OSMChange(file=file)
for osmObject in d.create:
type_of = parse_object(osmObject)
if type_of == 1:
nodes_count += 1
elif type_of == 2:
ways_count += 1
elif type_of == 3:
relations_count += 1
else:
print('This part shouldn\'t be reached, check algorithm')
if verbose is True:
print('Created: {nodes} nodes, {ways} ways and {relations} relations'.format(nodes=nodes_count, ways=ways_count,
relations=relations_count))
nodes_count = 0
ways_count = 0
relations_count = 0
for osmObject in d.modify:
type_of = parse_object(osmObject)
if type_of == 1:
nodes_count += 1
elif type_of == 2:
ways_count += 1
elif type_of == 3:
relations_count += 1
else:
print('This part shouldn\'t be reached, check algorithm')
if verbose is True:
print('Modified: {nodes} nodes, {ways} ways and {relations} relations'.format(nodes=nodes_count, ways=ways_count,
relations=relations_count))
nodes_count = 0
ways_count = 0
relations_count = 0
for osmObject in d.delete:
type_of = parse_object(osmObject)
if type_of == 1:
nodes_count += 1
elif type_of == 2:
ways_count += 1
elif type_of == 3:
relations_count += 1
else:
print('This part shouldn\'t be reached, check algorithm')
if verbose is True:
print('Deleted: {nodes} nodes, {ways} ways and {relations} relations'.format(nodes=nodes_count, ways=ways_count,
relations=relations_count))
iterator = 0
# Merge polygons until its possible
while True:
merged = False
for bbox1 in bboxes:
if bbox1.merged is False:
for bbox2 in bboxes:
if bbox1 != bbox2 and bbox2.merged is False:
area = bbox1.get_poly().intersection(bbox2.get_poly()).area
if (bbox1.is_suitable_for_merge(area)) or bbox2.is_suitable_for_merge(area):
bbox1.merge_into(bbox2)
merged = True
if not merged:
break
iterator += 1
if verbose is True:
print('Merged in {} iteration(s)'.format(iterator))
return filter(lambda x: x.merged is False, bboxes)