navi/backend/services/navi_offroute/tests/test_mvum_parking.py
malice f9f2eb9b8f
MVUM Layer 3b: OSM parking as multi-modal Auto transition candidates (#31)
* MVUM Layer 3b: OSM parking as multi-modal Auto transition candidates

Adds OSM parking lots as a third multi-modal-Auto transition source alongside
MVUM trailheads (3a) and surface-change points (3c), so Auto can suggest
"drive to a parking lot, switch to foot/2w/4w" trips where no MVUM trailhead
exists -- BLM/state land, urban edges, anywhere OSM has parking but the USFS
trailhead layer does not. Backend-only; consumes the already-ingested
/mnt/nav/osm-parking.db read-only (no data-pipeline change).

- mvum_parking.py: OSMParkingIndex (process-wide singleton via load_parking_index)
  over a shapely STRtree of parking points, mirroring MVUMSpatialIndex /
  TrailheadIndex. Read-only SQLite. Drops access in (private,no,permit) at load.
  query_parking_near_line(coords, buffer_m=2000) with the same coarse-bbox +
  precise-distance filter as TrailheadIndex. Records carry
  {lat, lon, name, road_class="parking", parking_type, access}.
  Perf note: the ingest already stored representative_point() in lat/lon, so the
  STRtree is built straight from those columns -- parsing the 1.6M WKB blobs at
  boot would add minutes for an identical point.
- router.py: _try_hybrid_auto generalized to gather candidates from each AVAILABLE
  source (trailhead index if present + surface-change always + parking index if
  present) instead of hard-returning when trailhead_index is None, so parking-only
  candidates still work. Combined list keeps the existing closest-first sort +
  HYBRID_MAX_TRAILHEADS cap. Signature unchanged; record shape already compatible.
- app.py / offroute_route.py: load + inject the OSM parking singleton, mirroring
  MVUM_SPATIAL_INDEX / MVUM_TRAILHEAD_INDEX. Failure logs a warning, degrades None.
- admin.py: GET /api/admin/osm-parking/info -> {count, build_time_seconds,
  memory_estimate_mb}, mirroring /api/admin/mvum-spatial/info.
- backend/scripts/ingest_parking.py + README-osm-parking-ingest.md: the
  data-pipeline ingest lifted to the repo with argparse (--geojsonseq/--db, no
  /tmp) + the download/filter/export/ingest/restart refresh recipe.

Tests: test_mvum_parking.py (loads, near-line close-only, private/no/permit
filtered, null-access kept) + test_offroute.py::test_hybrid_consumes_parking_
candidates (parking-only source probed as a leg-1 destination). Full offroute
suite: 82 passed.

Real-DB sanity (not deployed): index loads 1,489,054 usable parking objects
(182,945 access-blocked dropped) in ~12 s using ~950 MB RSS per worker; a Redfish
Lake/Sawtooth corridor query returns 8 lots. The ~950 MB/worker memory cost is
notable -- flagging for review.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Numpy-pack OSMParkingIndex coords + lazy records to cut RSS (~950->~570 MB/worker)

Store parking coords as packed float64 numpy arrays (_lats/_lons) and the
attribute columns as interned lists (_names/_parking_types/_accesses), and build
candidate record dicts lazily in query_parking_near_line instead of materializing
1.5M dicts + 1.5M shapely Point objects up front. road_class is the constant
"parking" so it is not stored per row.

Measured on the real /mnt/nav/osm-parking.db (1,489,054 usable rows):
RSS/worker ~950 MB -> ~570 MB (~40%), build ~11 s. Across 2 gunicorn workers that
is ~1.9 GB -> ~1.14 GB.

NOTE: this does NOT reach the ~250 MB originally targeted. The remaining cost is
the shapely STRtree itself: it permanently retains the input geometries
(tree.geometries len == row count), so the transient `del points` does not free
them. Attribution on the real DB: columns-only 137 MB, retained Point objects
+230 MB, STRtree index +110 MB. Reaching ~250 MB would require dropping the
shapely STRtree for a coordinate-only structure (e.g. scipy cKDTree over the
lon/lat arrays), which changes the line-buffer query into a per-vertex radius
query -- a behavior change beyond this fix-up's scope. Flagged for a follow-up.

Tests unchanged except one assertion (`len(idx.records) == idx.count`); full
offroute suite 82 passed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Matt <mj@k7zvx.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 14:48:35 -06:00

75 lines
2.8 KiB
Python

"""MVUM Layer 3b tests: OSMParkingIndex over a synthetic osm-parking.db."""
import sqlite3
import pytest
from services.navi_offroute.mvum_parking import OSMParkingIndex
def _parking_db(tmp_path, rows):
"""rows: list of (osm_id, osm_type, name, capacity, access, parking_type, lat, lon)."""
db = tmp_path / "osm-parking.db"
conn = sqlite3.connect(db)
conn.execute(
"CREATE TABLE parking (id INTEGER PRIMARY KEY, osm_id TEXT, osm_type TEXT, "
"name TEXT, capacity INTEGER, access TEXT, parking_type TEXT, "
"lat REAL, lon REAL, shape BLOB)")
conn.executemany(
"INSERT INTO parking (osm_id,osm_type,name,capacity,access,parking_type,lat,lon) "
"VALUES (?,?,?,?,?,?,?,?)", rows)
conn.commit()
conn.close()
return db
def test_parking_index_loads(tmp_path):
db = _parking_db(tmp_path, [
("1", "node", "Lot A", 20, None, "surface", 44.00, -114.00),
("2", "way", "Lot B", None, "yes", "surface", 44.01, -114.02),
("3", "way", "", None, "customers", None, 44.02, -114.03),
])
idx = OSMParkingIndex(db_path=db)
assert idx.count == 3
assert len(idx.records) == idx.count == 3
rec = idx.records[0]
assert rec["name"] == "Lot A"
assert rec["road_class"] == "parking"
assert rec["parking_type"] == "surface"
assert rec["lat"] == 44.00 and rec["lon"] == -114.00
def test_query_parking_near_line_returns_close_only(tmp_path):
db = _parking_db(tmp_path, [
("1", "node", "On Line", None, None, "surface", 44.000, -114.000),
("2", "node", "Far Away", None, None, "surface", 44.300, -114.000), # ~33 km N
])
idx = OSMParkingIndex(db_path=db)
line = [(44.000, -114.010), (44.000, -113.990)] # ~1.6 km segment through the close pt
near = idx.query_parking_near_line(line, buffer_m=2000)
names = {r["name"] for r in near}
assert "On Line" in names
assert "Far Away" not in names
def test_private_parking_filtered_out(tmp_path):
db = _parking_db(tmp_path, [
("1", "way", "Public", None, "yes", "surface", 44.00, -114.00),
("2", "way", "Private", None, "private", "surface", 44.01, -114.01),
("3", "way", "NoAccess", None, "no", "surface", 44.02, -114.02),
("4", "way", "PermitOnly", None, "permit", "surface", 44.03, -114.03),
])
idx = OSMParkingIndex(db_path=db)
names = {r["name"] for r in idx.records}
assert names == {"Public"}
assert idx.count == 1
assert idx.skipped_access == 3
def test_no_access_field_kept(tmp_path):
# Most OSM parking rows have NULL access -> must be kept (not treated as blocked).
db = _parking_db(tmp_path, [
("1", "way", "Unspecified", None, None, "surface", 44.00, -114.00),
])
idx = OSMParkingIndex(db_path=db)
assert idx.count == 1
assert idx.records[0]["access"] is None