[boundaries] lines sharing a point are added to the polygon head-to-tail, reversing the node order as needed, produces accurate OSM polygons for reverse geocoding lookups
This commit is contained in:
@@ -65,8 +65,8 @@ class OSMPolygonReader(object):
|
|||||||
def node_coordinates(self, coords, indptr, idx):
|
def node_coordinates(self, coords, indptr, idx):
|
||||||
start_index = indptr[idx] * 2
|
start_index = indptr[idx] * 2
|
||||||
end_index = indptr[idx + 1] * 2
|
end_index = indptr[idx + 1] * 2
|
||||||
return zip(coords[start_index:end_index - 2:2],
|
node_coords = coords[start_index:end_index]
|
||||||
coords[start_index + 1:end_index - 1:2])
|
return zip(node_coords[::2], node_coords[1::2])
|
||||||
|
|
||||||
def sparse_deps(self, data, indptr, idx):
|
def sparse_deps(self, data, indptr, idx):
|
||||||
return [data[i] for i in xrange(indptr[idx], indptr[idx + 1])]
|
return [data[i] for i in xrange(indptr[idx], indptr[idx + 1])]
|
||||||
@@ -136,13 +136,32 @@ class OSMPolygonReader(object):
|
|||||||
|
|
||||||
seen = set()
|
seen = set()
|
||||||
|
|
||||||
q = component[:1]
|
if not component:
|
||||||
|
continue
|
||||||
|
|
||||||
|
q = [(c, False) for c in component[:1]]
|
||||||
while q:
|
while q:
|
||||||
way_id = q.pop()
|
way_id, reverse = q.pop()
|
||||||
way_index = way_indices[way_id]
|
way_index = way_indices[way_id]
|
||||||
poly_nodes.extend(self.node_coordinates(self.way_coords, self.way_indptr, way_index)[:-1])
|
|
||||||
neighbors = way_graph[way_id]
|
node_coords = self.node_coordinates(self.way_coords, self.way_indptr, way_index)
|
||||||
q.extend([w for w in neighbors if w not in seen])
|
|
||||||
|
head, tail = start_end_nodes[way_id]
|
||||||
|
|
||||||
|
if reverse:
|
||||||
|
node_coords = node_coords[::-1]
|
||||||
|
head, tail = tail, head
|
||||||
|
|
||||||
|
for neighbor in way_graph[way_id]:
|
||||||
|
if neighbor in seen:
|
||||||
|
continue
|
||||||
|
neighbor_head, neighbor_tail = start_end_nodes[neighbor]
|
||||||
|
neighbor_reverse = neighbor_head == head or neighbor_tail == tail
|
||||||
|
q.append((neighbor, neighbor_reverse))
|
||||||
|
|
||||||
|
way_start = 0 if q else 1
|
||||||
|
poly_nodes.extend(node_coords[way_start:-1])
|
||||||
|
|
||||||
seen.add(way_id)
|
seen.add(way_id)
|
||||||
|
|
||||||
polys.append(poly_nodes)
|
polys.append(poly_nodes)
|
||||||
|
|||||||
Reference in New Issue
Block a user