Fix OSM multipolygon buildings
This commit is contained in:
@@ -1,17 +1,130 @@
|
||||
"""Building feature assembly (`building=*`)."""
|
||||
|
||||
import bpy
|
||||
from mathutils import Vector
|
||||
from mathutils.geometry import tessellate_polygon
|
||||
|
||||
from osmassets.mesh import MeshBatch, add_roof, add_wall_panel, make_prism
|
||||
from osmassets.osm import parse_height
|
||||
|
||||
|
||||
def add_details(name, ring, height, industrial, materials, collection):
|
||||
footprint = ring[:-1] if len(ring) > 1 and ring[0] == ring[-1] else ring
|
||||
if len(footprint) < 3:
|
||||
def _open_ring(ring):
|
||||
if len(ring) > 1 and ring[0] == ring[-1]:
|
||||
return ring[:-1]
|
||||
return ring
|
||||
|
||||
|
||||
def _valid_rings(outer, inner_rings):
|
||||
rings = [_open_ring(outer)]
|
||||
rings.extend(_open_ring(ring) for ring in (inner_rings or []))
|
||||
return [ring for ring in rings if len(ring) >= 3]
|
||||
|
||||
|
||||
def _flat_vertices(rings, z):
|
||||
vertices = []
|
||||
for ring in rings:
|
||||
vertices.extend((x, y, z) for x, y in ring)
|
||||
return vertices
|
||||
|
||||
|
||||
def _ring_offsets(rings):
|
||||
offsets = []
|
||||
offset = 0
|
||||
for ring in rings:
|
||||
offsets.append(offset)
|
||||
offset += len(ring)
|
||||
return offsets
|
||||
|
||||
|
||||
def _tessellated_faces(rings, offset=0, reverse=False):
|
||||
polygons = [[Vector((x, y, 0.0)) for x, y in ring] for ring in rings]
|
||||
faces = []
|
||||
for triangle in tessellate_polygon(polygons):
|
||||
face = tuple(offset + index for index in triangle)
|
||||
faces.append(tuple(reversed(face)) if reverse else face)
|
||||
return faces
|
||||
|
||||
|
||||
def _side_faces(rings, offsets, top_offset):
|
||||
faces = []
|
||||
for ring_index, ring in enumerate(rings):
|
||||
offset = offsets[ring_index]
|
||||
for i in range(len(ring)):
|
||||
j = (i + 1) % len(ring)
|
||||
if ring_index == 0:
|
||||
faces.append((offset + i, offset + j,
|
||||
top_offset + offset + j, top_offset + offset + i))
|
||||
else:
|
||||
# Inner-ring walls face the courtyard / void, opposite to the
|
||||
# outer shell.
|
||||
faces.append((offset + i, top_offset + offset + i,
|
||||
top_offset + offset + j, offset + j))
|
||||
return faces
|
||||
|
||||
|
||||
def make_prism_with_holes(name, outer, inner_rings, base, height, material, collection):
|
||||
rings = _valid_rings(outer, inner_rings)
|
||||
if not rings:
|
||||
return None
|
||||
if len(rings) == 1:
|
||||
return make_prism(name, rings[0], base, height, material, collection)
|
||||
|
||||
base_vertices = _flat_vertices(rings, base)
|
||||
top_vertices = _flat_vertices(rings, base + height)
|
||||
top_offset = len(base_vertices)
|
||||
offsets = _ring_offsets(rings)
|
||||
faces = []
|
||||
faces.extend(_tessellated_faces(rings, reverse=True))
|
||||
faces.extend(_tessellated_faces(rings, offset=top_offset))
|
||||
faces.extend(_side_faces(rings, offsets, top_offset))
|
||||
mesh = bpy.data.meshes.new(name + "Mesh")
|
||||
mesh.from_pydata(base_vertices + top_vertices, [], faces)
|
||||
mesh.materials.append(material)
|
||||
mesh.update()
|
||||
obj = bpy.data.objects.new(name, mesh)
|
||||
collection.objects.link(obj)
|
||||
return obj
|
||||
|
||||
|
||||
def add_roof_with_holes(name, outer, inner_rings, z, material, collection):
|
||||
rings = _valid_rings(outer, inner_rings)
|
||||
if not rings:
|
||||
return None
|
||||
if len(rings) == 1:
|
||||
return add_roof(name, rings[0], z, material, collection)
|
||||
|
||||
mesh = bpy.data.meshes.new(name + "_RoofMesh")
|
||||
mesh.from_pydata(_flat_vertices(rings, z), [], _tessellated_faces(rings))
|
||||
mesh.materials.append(material)
|
||||
mesh.update()
|
||||
obj = bpy.data.objects.new(name + "_Roof", mesh)
|
||||
collection.objects.link(obj)
|
||||
return obj
|
||||
|
||||
|
||||
def explicit_height(tag):
|
||||
if "height" not in tag:
|
||||
return None
|
||||
return parse_height(tag, None)
|
||||
|
||||
|
||||
def render_height(tag, industrial):
|
||||
explicit = explicit_height(tag)
|
||||
source_height = max(3.0, explicit if explicit is not None else parse_height(tag, 12.0))
|
||||
height = source_height if industrial or explicit is not None or source_height >= 30.0 else 11.4
|
||||
return source_height, height
|
||||
|
||||
|
||||
def add_details(name, ring, height, industrial, materials, collection, inner_rings=None):
|
||||
footprints = _valid_rings(ring, inner_rings)
|
||||
if not footprints:
|
||||
return
|
||||
glass_mat = materials["factory_glass"] if industrial else materials["glass"]
|
||||
|
||||
glass_batch = MeshBatch(name + "_Windows", collection, glass_mat)
|
||||
edges = list(zip(footprint, footprint[1:] + footprint[:1]))
|
||||
edges = []
|
||||
for footprint in footprints:
|
||||
edges.extend(zip(footprint, footprint[1:] + footprint[:1]))
|
||||
if industrial:
|
||||
band_height = min(1.8, max(0.75, height * 0.16))
|
||||
band_base = max(0.9, height * 0.52)
|
||||
@@ -31,15 +144,15 @@ def add_details(name, ring, height, industrial, materials, collection):
|
||||
glass_batch.finish()
|
||||
|
||||
|
||||
def assemble(ring, way_id, tag, office_overrides, collection, materials):
|
||||
def assemble(ring, way_id, tag, office_overrides, collection, materials,
|
||||
inner_rings=None):
|
||||
industrial = (tag.get("building") == "industrial" and
|
||||
way_id not in office_overrides)
|
||||
source_height = max(3.0, parse_height(tag, 12.0))
|
||||
height = source_height if industrial or source_height >= 30.0 else 11.4
|
||||
source_height, height = render_height(tag, industrial)
|
||||
material = materials["industrial"] if industrial else materials["default"]
|
||||
building_name = "Building_" + way_id
|
||||
building_obj = make_prism(building_name, ring, 0.08, height,
|
||||
material, collection)
|
||||
building_obj = make_prism_with_holes(
|
||||
building_name, ring, inner_rings, 0.08, height, material, collection)
|
||||
if building_obj:
|
||||
building_obj["osm_height"] = source_height
|
||||
building_obj["render_height"] = height
|
||||
@@ -51,6 +164,8 @@ def assemble(ring, way_id, tag, office_overrides, collection, materials):
|
||||
bevel.segments = 2
|
||||
roof_mat = (materials["industrial_roof"] if industrial
|
||||
else materials["office_roof"])
|
||||
add_roof(building_name, ring, height + 0.095, roof_mat, collection)
|
||||
add_details(building_name, ring, height, industrial, materials, collection)
|
||||
add_roof_with_holes(building_name, ring, inner_rings, height + 0.095,
|
||||
roof_mat, collection)
|
||||
add_details(building_name, ring, height, industrial, materials, collection,
|
||||
inner_rings=inner_rings)
|
||||
return 1, int(industrial), ring
|
||||
|
||||
@@ -7,11 +7,62 @@ import math
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
|
||||
def _element_id(element):
|
||||
return element.attrib.get("id", "")
|
||||
|
||||
|
||||
def tags(element):
|
||||
return {t.attrib.get("k", ""): t.attrib.get("v", "")
|
||||
for t in element.findall("tag")}
|
||||
|
||||
|
||||
def _coords_from_refs(refs, nodes):
|
||||
return [nodes[r] for r in refs if r in nodes]
|
||||
|
||||
|
||||
def _open_ring(coords):
|
||||
if len(coords) > 1 and coords[0] == coords[-1]:
|
||||
return coords[:-1]
|
||||
return coords
|
||||
|
||||
|
||||
def _is_closed_ring(coords):
|
||||
return len(coords) >= 4 and coords[0] == coords[-1] and len(_open_ring(coords)) >= 3
|
||||
|
||||
|
||||
def _join_member_rings(members):
|
||||
"""Build closed rings from relation member coordinate runs.
|
||||
|
||||
Handles the common OSM multipolygon cases: members are either already
|
||||
closed ways, or open way fragments whose endpoints can be stitched together.
|
||||
Malformed leftovers are dropped rather than aborting the whole import.
|
||||
"""
|
||||
rings = []
|
||||
pending = [list(member) for member in members if len(member) >= 2]
|
||||
while pending:
|
||||
ring = pending.pop(0)
|
||||
changed = True
|
||||
while not _is_closed_ring(ring) and changed:
|
||||
changed = False
|
||||
for index, candidate in enumerate(pending):
|
||||
if ring[-1] == candidate[0]:
|
||||
ring.extend(candidate[1:])
|
||||
elif ring[-1] == candidate[-1]:
|
||||
ring.extend(reversed(candidate[:-1]))
|
||||
elif ring[0] == candidate[-1]:
|
||||
ring = candidate[:-1] + ring
|
||||
elif ring[0] == candidate[0]:
|
||||
ring = list(reversed(candidate[1:])) + ring
|
||||
else:
|
||||
continue
|
||||
pending.pop(index)
|
||||
changed = True
|
||||
break
|
||||
if _is_closed_ring(ring):
|
||||
rings.append(ring)
|
||||
return rings
|
||||
|
||||
|
||||
def parse_osm(path):
|
||||
root = ET.parse(path).getroot()
|
||||
bounds_node = root.find("bounds")
|
||||
@@ -37,6 +88,7 @@ def parse_osm(path):
|
||||
continue
|
||||
|
||||
ways = []
|
||||
way_coords_by_id = {}
|
||||
for way in root.findall("way"):
|
||||
if way.attrib.get("action") == "delete":
|
||||
continue
|
||||
@@ -46,17 +98,53 @@ def parse_osm(path):
|
||||
refs.append(int(ref.attrib["ref"]))
|
||||
except (KeyError, ValueError):
|
||||
pass
|
||||
coords = [nodes[r] for r in refs if r in nodes]
|
||||
coords = _coords_from_refs(refs, nodes)
|
||||
way_id = _element_id(way)
|
||||
if len(coords) >= 2:
|
||||
ways.append({"id": way.attrib.get("id", ""),
|
||||
"coords": coords, "tags": tags(way)})
|
||||
way_coords_by_id[way_id] = coords
|
||||
way_tags = tags(way)
|
||||
if len(coords) >= 2:
|
||||
ways.append({"id": way_id, "coords": coords, "tags": way_tags})
|
||||
for relation in root.findall("relation"):
|
||||
if relation.attrib.get("action") == "delete":
|
||||
continue
|
||||
relation_tags = tags(relation)
|
||||
if relation_tags.get("type") != "multipolygon" or "building" not in relation_tags:
|
||||
continue
|
||||
outer_members = []
|
||||
inner_members = []
|
||||
for member in relation.findall("member"):
|
||||
if member.attrib.get("type") != "way":
|
||||
continue
|
||||
coords = way_coords_by_id.get(member.attrib.get("ref", ""))
|
||||
if not coords:
|
||||
continue
|
||||
role = member.attrib.get("role", "")
|
||||
if role == "inner":
|
||||
inner_members.append(coords)
|
||||
elif role in ("", "outer"):
|
||||
outer_members.append(coords)
|
||||
outer_rings = _join_member_rings(outer_members)
|
||||
if not outer_rings:
|
||||
continue
|
||||
inner_rings = _join_member_rings(inner_members)
|
||||
relation_id = _element_id(relation)
|
||||
for index, outer in enumerate(outer_rings):
|
||||
synthetic_id = relation_id if len(outer_rings) == 1 else f"{relation_id}:{index + 1}"
|
||||
ways.append({
|
||||
"id": synthetic_id,
|
||||
"coords": outer,
|
||||
"inner_coords": inner_rings,
|
||||
"tags": relation_tags,
|
||||
"source": "relation",
|
||||
})
|
||||
return bounds, ways, point_features
|
||||
|
||||
|
||||
def parse_height(feature_tags, default):
|
||||
try:
|
||||
return max(0.5, float(feature_tags.get("height", default)))
|
||||
except ValueError:
|
||||
except (TypeError, ValueError):
|
||||
return default
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user