Refactor road assembly module

This commit is contained in:
2026-08-03 13:39:05 +08:00
parent 64517993e9
commit 0f3c1ee635
12 changed files with 315 additions and 39 deletions

View File

@@ -35,10 +35,7 @@ if _HERE not in sys.path:
from osmassets import catalog # noqa: E402
from osmassets.geom import ( # noqa: E402 (needs the sys.path line above)
clip_polygon,
distance_to_ring,
feature_in_bounds,
geometry_rings,
point_in_polygon,
polygon_area,
sample_polygon_interior,
@@ -51,7 +48,6 @@ from osmassets.materials import ( # noqa: E402
)
from osmassets.mesh import ( # noqa: E402
MeshBatch,
add_polyline,
new_collection,
)
from osmassets.osm import Projector, parse_height, parse_osm # noqa: E402
@@ -59,6 +55,7 @@ from osmassets import building as _building # noqa: E402
from osmassets import fountain as _fountain # noqa: E402
from osmassets import water as _water # noqa: E402
from osmassets import grass as _grass # noqa: E402
from osmassets import roads as _roads # noqa: E402
from osmassets import scrub as _scrub # noqa: E402
from osmassets import tree as _tree # noqa: E402
@@ -145,29 +142,6 @@ def cli_args():
return values
def add_geojson_layer(path, layer, projector, collection, material, z):
if not os.path.exists(path):
return 0
with open(path, "r", encoding="utf-8") as handle:
data = json.load(handle)
batch = MeshBatch("Road_" + layer, collection, material)
b = projector.bounds
xmin, ymin = projector.xy((b["min_lon"], b["min_lat"]))
xmax, ymax = projector.xy((b["max_lon"], b["max_lat"]))
count = 0
for feature in data.get("features", []):
if not feature_in_bounds(feature, projector):
continue
for ring in geometry_rings(feature.get("geometry")):
points = [projector.xy(pair) for pair in ring]
points = clip_polygon(points, xmin, xmax, ymin, ymax)
if len(points) >= 3:
batch.add_polygon(points, z)
count += 1
batch.finish()
return count
def add_tree_batch(positions, collection, trunk_material, leaf_material):
trunk = MeshBatch("Tree_Trunks", collection, trunk_material)
leaves = MeshBatch("Tree_Crowns", collection, leaf_material)
@@ -743,17 +717,13 @@ def build(args):
print("Layer catalog warning:", problem)
for layer in catalog.ROAD_LAYERS:
layer_id = layer["id"]
road_counts[layer_id] = add_geojson_layer(
road_counts[layer_id] = _roads.assemble_geojson_layer(
os.path.join(geojson_dir, layer_id + ".geojson"), layer_id,
projector, roads_c, road_mats[layer_id], layer["z"])
if road_counts.get("road_surface", 0) == 0:
for way in ways:
highway = way["tags"].get("highway")
if highway and len(way["coords"]) >= 2:
width = {"secondary": 7.0, "residential": 5.5, "service": 3.5}.get(highway, 4.0)
add_polyline("OSM_Road_" + str(way["id"]), way["coords"], projector,
roads_c, road_mats["road_surface"], width, 0.03)
_roads.assemble_osm_fallback(
ways, projector, roads_c, road_mats["road_surface"])
trees = []
individual_tree_count = 0