latlonfix is geocode QA for a field-asset registry (poles, meters, cabinets, hydrants). It flags
null-island rows, invalid ranges, assets outside their stated service territory, low-precision
coordinates, centroid geocodes and coordinates shared across asset types. For assets that fail the
territory or range test it tries nine invertible repair hypotheses (swap, sign drops, decimal
shifts) and proposes a repair only when exactly one distinct point puts it back inside the
stated territory; otherwise the result is ambiguous or unresolved. It is pure Python with no
runtime dependencies and no GIS stack. All data in this repository is synthetic.
Utilities and telecoms keep asset registries whose coordinates come from decades of imports. Latitude and longitude get swapped, minus signs get dropped, decimals shift, postcode centroids stand in for real positions, and (0, 0) appears when a geocode fails. Crews get dispatched to the wrong place. A range check only catches values that are impossible, and a swapped pair such as (-88.1, 35.2) is a perfectly valid position. Automatic fixes that guess are worse than none, because a wrong fix looks valid afterwards. latlonfix trades coverage for a target of zero wrong repairs on the corruptions it models (the nine hypotheses), measured against planted corruptions.
flowchart TD
A[Asset row<br/>id, type, territory, lat, lon text] --> B{Range, null island,<br/>precision, centroid checks}
B --> C{Inside stated<br/>territory and in range?}
C -- yes --> D[No repair attempted]
C -- no --> E[Apply all 9 invertible<br/>hypotheses]
E --> F{Candidates inside the<br/>stated territory}
F -- exactly one --> G[proposed repair]
F -- two or more --> H[ambiguous<br/>candidates in name order]
F -- none --> I[unresolved]
geometry.py: ray-casting point-in-polygon where points on an edge or vertex are inside, bounding boxes, haversine distance, area-weighted polygon centroid.territories.py: territory polygons from a bundled GeoJSON, a by-idcontains(used by checks and repair) and a grid-bucketlocatethat finds which territories hold a point (library API, not used by the CLI).checks.py: the per-asset checks. Decimal places are counted from the CSV text, so35.120000has 6 places and35.12has 2.hypotheses.py:lat_div10,lat_x10,lon_div10,lon_x10,negate_both,negate_lat,negate_lon,swap,swap_negate_lon; each has an exact inverse.repair.py: the uniqueness gate. Candidates are listed in hypothesis-name order.generate.pyandevaluate.py: the seeded synthetic registry with ground truth, and the scorer.
python -m venv .venv && . .venv/bin/activate
pip install -e ".[dev]"
latlonfix generate --seed 17 --out data
latlonfix check data/assets.csv --territories data/territories.geojson
latlonfix repair data/assets.csv --territories data/territories.geojson --apply --out data/fixed.csv
latlonfix summary data/assets.csv --truth data/ground_truth.csv
python scripts/demo.py
pytest -q--territories defaults to the bundled GeoJSON, which is the territory set of seed 17.
repair without --apply only reports; add --list to print every decision.
Output of python scripts/demo.py (latlonfix summary on the seed-17 registry):
latlonfix summary: 20000 assets, 24 territories
Checks (assets flagged)
null_island 60
out_of_range 0
outside_territory 636
low_precision 160
centroid_geocode 120
shared_coordinates 120
Repairs (assets outside their territory or out of range: 636)
proposed 576
swap 276
negate_lon 200
lat_x10 60
lon_x10 40
ambiguous 0
unresolved 60
Evaluation against ground truth
wrong repairs 0 of 576 proposed
repair recall: swap_west 1.000, swap_east 1.000, dropped_lon_sign 1.000, decimal_shift 1.000
swap_west planted 203 undetectable 0 repaired 203 ambiguous 0 unresolved 0
swap_east planted 97 undetectable 24 repaired 73 ambiguous 0 unresolved 0
dropped_lon_sign planted 200 undetectable 0 repaired 200 ambiguous 0 unresolved 0
decimal_shift planted 100 undetectable 0 repaired 100 ambiguous 0 unresolved 0
undetectable corruptions 24 (corrupted point still inside its stated territory; never repaired)
null_island precision 1.000 recall 1.000
low_precision precision 1.000 recall 1.000
centroid_geocode precision 1.000 recall 1.000
Example repairs
A000054 swap: (-83.364334, 33.117004) -> (33.117004, -83.364334) in T07, moved 13995 km
A000027 negate_lon: (38.680743, 85.638606) -> (38.680743, -85.638606) in T14, moved 11367 km
A000066 lat_x10: (4.155789, 41.233555) -> (41.557888, 41.233555) in T17, moved 4159 km
The 60 unresolved assets are the planted (0, 0) rows: no transform moves (0, 0) anywhere. The 24 undetectable swaps are east-block assets whose swapped point still falls inside their own territory; no check can see them and nothing repairs them. The shared-coordinates findings are the centroid-cluster assets again, which sit at one point with mixed asset types.
- The stated territory is the only judge. A candidate counts only if it lies inside the
territory the registry names for that asset. No distance heuristics, no scoring. The cost is
coverage: when two hypotheses fit and give different points, the row stays
ambiguousand is listed with its candidates for a person to decide. Hypotheses that land on the same point (for examplenegate_latandswap_negate_lonon (-30, 30) in a symmetric territory) are one repair, not a conflict, and the first in name order is reported. - Zero wrong repairs is scoped to modelled corruptions. The generator plants corruptions with
the
invertof the same hypotheses thatrepairapplies, so the seed-17 score shows the uniqueness gate and the transforms agree, not that real-world displacements are safe. No unmodelled displacement (a point moved a few hundred metres, say) is planted, so a false repair of one by a modelled hypothesis that happens to land inside the territory is untested. On seed 17 no row is ambiguous, so the ambiguous path is covered only by the hand fixtures. - Hypotheses are invertible transforms. Each has an
apply(the repair) and aninvert(the corruption). The generator plants corruptions withinvert, and the tests check that the two round-trip exactly for swaps and negations and within 1e-12 for the x10 and /10 shifts. - Boundary points are inside. A point on an edge or vertex of a territory is inside, so a surveyed point on a boundary is not flagged. The test is an exact floating-point cross product, which is reliable for exactly representable coordinates and is the documented behaviour otherwise. The half-open ray rule counts a ray through a vertex once.
- Precision is read from the text. Floats forget trailing zeros, so decimal places come from the CSV text. Decimal-shift corruptions are written with 7 decimal places so that a x10 repair returns the original to within 1e-9.
- Territories are star-shaped, one per grid cell. Each fits inside a circle that fits its cell, so they cannot overlap. The western block is lat 30-40, lon -90 to -80 (the 4 x 4 grid of 2.5 degree cells spans 10 degrees of longitude, and the tests assert these bounds). It keeps swapped points far from every territory. The eastern block (lat 40-46, lon 40-46) has territories on the lat = lon diagonal, where a swapped point can stay inside its own territory, so some corruptions are undetectable by design and are reported as such rather than hidden.
repair --applyleaves unrepaired rows untouched. Rows without a repair are copied through byte for byte, so they have no values for the audit columns (original_latitude,original_longitude,repair_hypothesis) and are shorter than the header; CSV readers fill the missing trailing fields with empty values.latlonfixreads such files back.
142 tests, run with pytest -q (a few seconds). They cover:
- Point-in-polygon on a hand square, a concave L-shape and a diamond: interior, exterior, vertex, edge, and a ray through a vertex.
- Every hypothesis against its inverse on 1,000 seeded points plus a derandomized property test.
- Each check, including the 1e-6 degree centroid tolerance and text-based precision.
- Ambiguous and unresolved repair fixtures.
- On the seed-17 registry: null island flags exactly the planted rows, zero wrong repairs on the modelled, planted corruptions (each proposal equals the planted original within 1e-9), recall of 1.0 for western swaps and dropped signs, centroid clusters found with precision 1.0, and undetectable swaps never repaired.
- The CLI end to end, including byte-identical unrepaired rows in
repair --apply.
The generator corrupts about 4.7% of 20,000 assets: 1.5% swapped, 1% longitude sign dropped, 0.5% decimal shifts, 0.3% at (0, 0), 0.8% truncated to 2 decimals, and 3 clusters of 40 assets at a territory centroid.
src/latlonfix/
geometry.py point-in-polygon, bbox, haversine, centroid
territories.py GeoJSON loading, by-id lookup, grid-bucket `locate`
registry.py CSV reading and writing, decimal places from text
checks.py per-asset checks
hypotheses.py nine invertible transforms
repair.py exactly-one-hypothesis gate
generate.py seeded synthetic registry with ground truth
evaluate.py scoring against ground truth
report.py text reports
cli.py generate, check, repair, summary
data/territories.geojson
scripts/demo.py summary on the seed-17 registry
tests/
MIT
