Repository navigation
fix(src-osm): rebuild way geometry in read_pbf_roads (supersedes #483) - #499
Merged
Merged
Conversation
…ing it DuckDB spatial's ST_ReadOSM is a raw reader (kind, id, tags, refs, lat, lon, ...) with no way geometry, so the old `ST_AsWKB(geom)` query failed on every real call. #483 proposes deleting the function, but MILOU (dev_simon) still calls it from ingest_osm_surface, ingest_osm_road_network and bench_osm_corridor. Rebuild LineStrings from node coordinates instead: - temp table of highway ways with the requested tags only; - temp table of the referenced nodes only, each projected once with ST_Transform(..., always_xy := true) (EPSG:4326 is otherwise read as lat/lon and the layer lands near (6.5e6, -3.4e6)); - (way_id, x, y) rows sorted by way and node position (spillable sort), assembled with shapely.linestrings(indices=...). Ways with < 2 resolved nodes or zero length are dropped and counted in gdf.attrs["diagnostics"] with a warning; partial ways are listed in gdf.attrs["partially_resolved_ids"]. Duplicate ids, OOM, missing spatial extension and invalid CRS get dedicated error codes. Output columns are unchanged: id, highway, requested tags (string dtype), LineString geometry. DuckDB's build_side_probe_side optimizer is disabled for the node semi-join only (it hashed every node of the file: 3.2 GB -> 0.8 GB on Flanders) and restored afterwards. Belgium (686 MB, 1.48 M ways): ~10 s, 2.0 GB peak RSS, also completes with GISPULSE_DUCKDB_MEMORY_LIMIT=1GB. Geometry matches GDAL's OSM driver on Luxembourg, Brussels and Flanders. Tests run real ST_ReadOSM on a hand-encoded synthetic PBF (tests/unit/_synthetic_pbf.py); no real OSM extract is committed. Supersedes #483. Fixes #482. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: sducournau <simon@imago.dev>
…f_roads Catch duckdb.Error instead of Exception around the disabled_optimizers toggle and the spatial-availability test fixture, index LineString coords directly, and replace the noqa'd lambda in the synthetic PBF writer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: sducournau <simon@imago.dev>
Merged
imagodata
pushed a commit
that referenced
this pull request
Oct 1, 2026
Bump gispulse to 2.5.0 (pyproject, QGIS plugin metadata, uv.lock, which had stayed at 2.3.0). Promote the changelog for #496 (PICC/UrbIS/GRB road layers, counted pagination core, GRB face classification), #501 (GRB road-crossings level evidence) and #499 (read_pbf_roads), mirrored into docs-site FR and EN. The PICC, UrbIS and GRB plugins import the counted pagination core that first ships in 2.5.0, so their dependency floor is raised to gispulse>=2.5.0 and their READMEs no longer point at integration/v2.4. Capability count unchanged (144): the new GRB functions, including build_grb_road_crossings, are not registered capabilities. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: sducournau <simon@imago.dev>
imagodata
added a commit
that referenced
this pull request
Oct 1, 2026
Bump gispulse to 2.5.0 (pyproject, QGIS plugin metadata, uv.lock, which had stayed at 2.3.0). Promote the changelog for #496 (PICC/UrbIS/GRB road layers, counted pagination core, GRB face classification), #501 (GRB road-crossings level evidence) and #499 (read_pbf_roads), mirrored into docs-site FR and EN. The PICC, UrbIS and GRB plugins import the counted pagination core that first ships in 2.5.0, so their dependency floor is raised to gispulse>=2.5.0 and their READMEs no longer point at integration/v2.4. Capability count unchanged (144): the new GRB functions, including build_grb_road_crossings, are not registered capabilities. Signed-off-by: sducournau <simon@imago.dev> Co-authored-by: sducournau <simon@imago.dev> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Supersedes #483 (fixes #482).
Why fix instead of remove
#483 deletes
read_pbf_roadson the premise that no consumer is left ("MILOU migrated to GDAL in imagodata/milou#1136"). That migration was merged into MILOU'sbeta-erwanbranch only. MILOU's active branch (dev_simon) still calls the function in three places:src/milou/io/ingest_osm_surface.pyread_pbf_roads(pbf, tags=("surface",), target_crs=None)→ expectsid,surfacescripts/ingest_osm_road_network.pyread_pbf_roads(pbf, target_crs="EPSG:31370", ...)→ LineStrings for corridor edges and complex crossingsscripts/bench_osm_corridor.pyread_pbf_roads(pbf, target_crs="EPSG:31370")Deleting the function turns those call sites into an
ImportError. MILOU then cannot rebuild its OSM surface artefact, which blocks the klinker surface-mapping fix and the motorway centerlines used by the Cat.2 drilling work. The function was broken, not unused, so this PR fixes it.The defect
DuckDB spatial's
ST_ReadOSMis a raw reader. Its columns arekind, id, tags, refs, lat, lon, ref_roles, ref_types(checked on duckdb 1.5.2 / spatialdc1996b), and it has no way geometry. The old query selectedST_AsWKB(geom), so every real call raisedOSM_PBF_READ_FAILED. The only test used a fake connection that echoed the broken SQL back.What changed (
plugins/gispulse-src-osm/gispulse_src_osm/pbf.py)Way geometry is rebuilt from node coordinates in three steps:
kind='way'rows that carryhighway, keeping only the requested tags (stored internally ast0..tn).unnest(refs). Each node is projected once withST_Transform(ST_Point(lon, lat), 'EPSG:4326', <target>, always_xy := true).(way_id, x, y)row per resolved ref, sortedORDER BY way_id ASC, ord ASC, then assembled into LineStrings with a vectorisedshapely.linestrings(coords, indices=...).Behaviour:
id(int64),highway, onestring-dtype column per requested tag (NA when the tag is absent, same dtype when the result is empty), and LineStringgeometry, sorted byid.target_crs=Nonekeeps EPSG:4326 in lon/lat order. The default target is stillEPSG:31370. A non-string CRS (int, pyproj object) is also accepted.always_xy. Without it, EPSG:4326 is read as (lat, lon) and the whole layer lands near (6.5e6, −3.4e6). A test catches this: it fails whenalways_xyis removed.gdf.attrs["diagnostics"]holdsways_total,ways_kept,ways_dropped_incomplete(< 2 resolved nodes),ways_dropped_degenerate(zero length) andways_partially_resolved. The counts add up toways_total.gdf.attrs["partially_resolved_ids"]lists the partial ways. In those, a straight segment bridges the missing nodes.OSM_PBF_DUPLICATE_IDSfor non-deduplicated extracts, which would otherwise mix node positions.OSM_PBF_OUT_OF_MEMORY(DuckDB OOM orMemoryError), whose recovery hint points to the DuckDB limit env vars.OSM_PBF_SPATIAL_UNAVAILABLEwhenINSTALL/LOAD spatialor the DuckDB limits fail.OSM_PBF_CRS_INVALIDfor an unknown target CRS.temp.main.*, so a caller's table with the same name is never dropped.ORDER BY/ sort is explicitASC, so a caller connection'sdefault_orderhas no effect.disabled_optimizers, which is best-effort and logs a warning if it fails.INSTALL/LOAD spatialfails.idorgeometryare rejected.connection_factory(the test hook) must now return a real DuckDB connection withspatialloaded. MILOU's tests monkeypatchread_pbf_roadsas a whole, so they do not depend on the old fake-connection protocol.Memory
Measured with
/usr/bin/time -v(peak RSS of the whole Python process, ~200 MB of which is imports), 24 threads:GISPULSE_DUCKDB_MEMORY_LIMIT=1GBTwo traps found along the way are fixed and documented in the code:
build_side_probe_sideoptimizer flips the node semi-join, so the hash table is built over every node of the file instead of over the referenced ids. That cost 3.2 GB instead of 0.8 GB on Flanders. The optimizer is disabled for that one statement, and the previousdisabled_optimizersvalue is restored afterwards. DuckDB versions without this optimizer simply skip it.list(... ORDER BY ord)orlist(ST_Point(...))inside aGROUP BYcannot spill to disk, and ran out of memory under a 400 MB limit on Luxembourg. A sort, which can spill, followed by numpy/shapely assembly avoids both that and the deprecated->lambda syntax.Verification
Cross-check against GDAL's OSM driver on Luxembourg. All 140 144 highway lines GDAL produces are matched with identical geometry, both in EPSG:4326 (1e-6°) and EPSG:31370 (1 cm). The 489 extra ways are closed highway ways, which GDAL puts in its
multipolygonslayer.Tests.
tests/unit/test_src_osm.pynow runs against real DuckDBST_ReadOSMon a synthetic PBF.tests/unit/_synthetic_pbf.pyhand-encodes OSMHeader + DenseNodes + Ways, with no osmium and no real OSM extract committed. The tests cover:ordsort is removed;target_crs=None;Mutation-checked: removing
always_xy, theordsort, the waysORDER BY id, or the cleanup on error each makes a test fail. 29 tests pass.Two adversarial reviews. All findings fixed or answered. The second review also:
load_pbf_surface_tagsagainst this branch: columnsid, highway, surface, geometry,osm_idlike"4015956", 90 588surfacetags on Belgium;ruff check/ruff format --checkare clean.Full local suite: 7 272 passed, 8 failed. The failures are in
test_run_control,test_run_eventsandtest_step_kinds, and are unrelated to this diff, which touches neithersrc/nor those tests. In isolation, 5 still fail withsteps.0.type: 'external' is not one of [...](theexternalstep kind is not registered in this local venv); the other 3 only failed under load.Known limits
[a, …missing…, a]), it counts asways_dropped_degenerate. GDAL keeps such a way as a zero-length line.geometry=Falsemode could be a follow-up.GISPULSE_DUCKDB_MEMORY_LIMITbounds DuckDB's own buffer pool.ST_ReadOSMread buffers and the numpy/shapely assembly add on top: Belgium peaks at about 2 GB RSS whatever the limit.CI on
mainwas already red before this PR (release: 2.4.0 (#498)and the previous runs); this PR does not touch that.Not closing #483 — leaving that to its author / maintainers.
🤖 Generated with Claude Code