Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions api/app/imports/rv_trip_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import openpyxl

from app.services.coordinates import MISSING_COORDINATE


@dataclass
class ParsedStop:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
7 changes: 5 additions & 2 deletions api/app/routers/journey.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -118,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 {}
Expand All @@ -131,7 +134,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:
Expand Down
3 changes: 2 additions & 1 deletion api/app/routers/stops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions api/app/services/coordinates.py
Original file line number Diff line number Diff line change
@@ -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)
98 changes: 98 additions & 0 deletions api/tests/test_coordinate_sentinels.py
Original file line number Diff line number Diff line change
@@ -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)