From d9879cc153beaa9555dd3f20f27a243b4f4a4ae4 Mon Sep 17 00:00:00 2001 From: Ted Date: Thu, 5 Nov 2026 03:26:41 -0500 Subject: [PATCH 1/2] fix(api): treat RV Trip Wizard placeholder coordinates as no location RV Trip Wizard writes -1 into latitude/longitude cells it could not resolve. -1 is in range, so the sentinel reached PostGIS as a POINT in the Gulf of Guinea and every stop-page map for the affected trip centered on the ocean instead of the campground. The parser drops a coordinate pair when either value is the -1 placeholder and warns during preview; public stop-detail and trip-segments reads report placeholder pairs as missing so the map hides instead of misleading. --- api/app/imports/rv_trip_wizard.py | 20 +++++- api/app/routers/journey.py | 3 +- api/app/routers/stops.py | 3 +- api/app/services/coordinates.py | 23 ++++++ api/tests/test_coordinate_sentinels.py | 98 ++++++++++++++++++++++++++ 5 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 api/app/services/coordinates.py create mode 100644 api/tests/test_coordinate_sentinels.py diff --git a/api/app/imports/rv_trip_wizard.py b/api/app/imports/rv_trip_wizard.py index a069a33..f098c89 100644 --- a/api/app/imports/rv_trip_wizard.py +++ b/api/app/imports/rv_trip_wizard.py @@ -15,6 +15,8 @@ import openpyxl +from app.services.coordinates import MISSING_COORDINATE + @dataclass class ParsedStop: @@ -140,12 +142,16 @@ def parse_float(val) -> float | None: def parse_latitude(val) -> float | None: f = parse_float(val) - return f if f is not None and -90 <= f <= 90 else None + if f is None or f == MISSING_COORDINATE: + return None + return f if -90 <= f <= 90 else None def parse_longitude(val) -> float | None: f = parse_float(val) - return f if f is not None and -180 <= f <= 180 else None + if f is None or f == MISSING_COORDINATE: + return None + return f if -180 <= f <= 180 else None def parse_int(val) -> int | None: @@ -289,6 +295,16 @@ def parse_excel(file_path: str) -> ParsedTrip: ) stop.features = parse_features(stop.features_raw) stop.fingerprint = compute_fingerprint(stop) + if stop.latitude is None or stop.longitude is None: + # A coordinate is useful only as a pair; a lone half is a + # placeholder artifact, not a location. + stop.latitude = None + stop.longitude = None + result.warnings.append( + f"Stop '{stop.name}' (row {row_num}) has no usable coordinates " + "(blank, unparseable, or RV Trip Wizard's -1 placeholder); " + "applying it will be rejected until the spreadsheet provides them." + ) result.stops.append(stop) return result diff --git a/api/app/routers/journey.py b/api/app/routers/journey.py index e1a83a9..4d672b7 100644 --- a/api/app/routers/journey.py +++ b/api/app/routers/journey.py @@ -23,6 +23,7 @@ RecentUpdate, TimelineOut, ) +from app.services.coordinates import usable_coordinates from app.services.current_stop import select_home_stop from app.services.trip_order import order_trips_newest_first from app.services.visibility import visible_ready_cover_media, visible_ready_media @@ -131,7 +132,7 @@ async def _coordinates_for_stops(session: AsyncSession, stops) -> dict[uuid.UUID func.ST_X(point).label("longitude"), ).where(Stop.id.in_(stop_ids)) ) - return {row.id: (row.latitude, row.longitude) for row in result.all()} + return {row.id: usable_coordinates(row.latitude, row.longitude) for row in result.all()} def _stop_out(stop: Stop | None, coords: dict[uuid.UUID, tuple[float, float]], user) -> PublicStopSummary | None: diff --git a/api/app/routers/stops.py b/api/app/routers/stops.py index 63fa5f7..de5caeb 100644 --- a/api/app/routers/stops.py +++ b/api/app/routers/stops.py @@ -20,6 +20,7 @@ PublicStopDetail, PublicStopSibling, ) +from app.services.coordinates import usable_coordinates from app.services.current_stop import select_live_stop from app.services.visibility import visible_ready_cover_media, visible_ready_media @@ -91,7 +92,7 @@ async def get_stop( select(func.ST_Y(point), func.ST_X(point)).where(Stop.id == stop.id) ) coord = coord_row.first() - latitude, longitude = (coord[0], coord[1]) if coord else (None, None) + latitude, longitude = usable_coordinates(coord[0], coord[1]) if coord else (None, None) # Own media (assets directly attached to this stop). Post media also carries # stop_id for scoping, but it should render only through its published post. diff --git a/api/app/services/coordinates.py b/api/app/services/coordinates.py new file mode 100644 index 0000000..a7c9272 --- /dev/null +++ b/api/app/services/coordinates.py @@ -0,0 +1,23 @@ +"""Guards for stop coordinates read out of the PostGIS geometry columns.""" + +import math + +# RV Trip Wizard writes -1 into coordinate cells it could not resolve, and +# -1 is an in-range degree value, so range checks alone let the placeholder +# reach the database as a POINT in the Gulf of Guinea. A stop whose latitude +# or longitude is exactly -1 has no usable location; reads report it as +# missing so map surfaces hide instead of centering on the ocean. +MISSING_COORDINATE = -1.0 + + +def usable_coordinates( + lat: float | None, lon: float | None +) -> tuple[float | None, float | None]: + """Return (lat, lon), or (None, None) when the point is absent or a placeholder.""" + if lat is None or lon is None: + return (None, None) + if not (math.isfinite(lat) and math.isfinite(lon)): + return (None, None) + if lat == MISSING_COORDINATE or lon == MISSING_COORDINATE: + return (None, None) + return (lat, lon) diff --git a/api/tests/test_coordinate_sentinels.py b/api/tests/test_coordinate_sentinels.py new file mode 100644 index 0000000..1e30357 --- /dev/null +++ b/api/tests/test_coordinate_sentinels.py @@ -0,0 +1,98 @@ +"""The stop-page map centers on the stop's coordinates, so a placeholder +coordinate became a map of the Gulf of Guinea. + +RV Trip Wizard writes -1 into latitude/longitude cells it could not resolve, +and -1 passes every range check; the Foretravel Rally import shipped with +stops parked at (-1, -1) and (0, -1). The parser drops placeholder pairs as +missing, and public coordinate reads report them as no location so map +surfaces hide instead of centering on the ocean. +""" + +import openpyxl + +from app.imports.rv_trip_wizard import parse_excel +from app.services.coordinates import usable_coordinates + + +def _workbook(tmp_path, rows): + wb = openpyxl.Workbook() + ws = wb.active + ws.title = "Trip Summary" + ws.cell(row=1, column=1, value="Foretravel Rally - 2026") + ws.cell(row=2, column=1, value="10/01/26") + for col, name in enumerate(("Stop Name", "Latitude", "Longitude"), start=1): + ws.cell(row=4, column=col, value=name) + for offset, (name, lat, lon) in enumerate(rows): + ws.cell(row=5 + offset, column=1, value=name) + if lat is not None: + ws.cell(row=5 + offset, column=2, value=lat) + if lon is not None: + ws.cell(row=5 + offset, column=3, value=lon) + path = tmp_path / "trip.xlsx" + wb.save(path) + return str(path) + + +def test_wizard_placeholder_pairs_parse_as_no_location(tmp_path): + path = _workbook( + tmp_path, + [ + ("The Ridge Outdoor Resort", -1, -1), + ("Brushy Mountain State Penitentiary", 0, -1), + ("Real Campground", 35.8412592, -83.5405319), + ], + ) + + parsed = parse_excel(path) + by_name = {stop.name: stop for stop in parsed.stops} + + ridge = by_name["The Ridge Outdoor Resort"] + assert (ridge.latitude, ridge.longitude) == (None, None) + + # A lone -1 longitude must not leave a half pair behind. + brushy = by_name["Brushy Mountain State Penitentiary"] + assert (brushy.latitude, brushy.longitude) == (None, None) + + real = by_name["Real Campground"] + assert (real.latitude, real.longitude) == (35.8412592, -83.5405319) + + +def test_stops_without_coordinates_are_warned(tmp_path): + path = _workbook( + tmp_path, + [ + ("The Ridge Outdoor Resort", -1, -1), + ("Real Campground", 35.84, -83.54), + ], + ) + + parsed = parse_excel(path) + + assert any( + "The Ridge Outdoor Resort" in w and "coordinates" in w + for w in parsed.warnings + ) + assert not any("Real Campground" in w for w in parsed.warnings) + + +def test_blank_coordinate_cells_parse_as_no_location(tmp_path): + path = _workbook(tmp_path, [("Blank Campground", None, None)]) + + parsed = parse_excel(path) + + stop = parsed.stops[0] + assert (stop.latitude, stop.longitude) == (None, None) + + +def test_placeholder_pairs_report_no_location_on_read(): + assert usable_coordinates(-1.0, -1.0) == (None, None) + assert usable_coordinates(0.0, -1.0) == (None, None) + assert usable_coordinates(-1.0, 35.84) == (None, None) + assert usable_coordinates(35.8412592, -83.5405319) == (35.8412592, -83.5405319) + + +def test_absent_and_non_finite_coordinates_report_no_location(): + assert usable_coordinates(None, -83.5) == (None, None) + assert usable_coordinates(35.8, None) == (None, None) + assert usable_coordinates(float("nan"), -83.5) == (None, None) + assert usable_coordinates(35.8, float("inf")) == (None, None) From 7831663ec33280bf49b6af183a9c98b065a5a09b Mon Sep 17 00:00:00 2001 From: Ted Date: Thu, 5 Nov 2026 03:35:47 -0500 Subject: [PATCH 2/2] fix(api): reflect nullable coordinates in _coordinates_for_stops annotation --- api/app/routers/journey.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/api/app/routers/journey.py b/api/app/routers/journey.py index 4d672b7..0687082 100644 --- a/api/app/routers/journey.py +++ b/api/app/routers/journey.py @@ -119,7 +119,9 @@ def _is_visible_stop_status(stop: Stop) -> bool: return stop.status == StopStatus.PUBLISHED -async def _coordinates_for_stops(session: AsyncSession, stops) -> dict[uuid.UUID, tuple[float, float]]: +async def _coordinates_for_stops( + session: AsyncSession, stops +) -> dict[uuid.UUID, tuple[float | None, float | None]]: stop_ids = [stop.id for stop in stops] if not stop_ids: return {}