Why jurisdiction miles go missing at state lines
Splitting a trip at a border is a geometry problem, and the obvious implementation loses distance at every crossing.
Fuel tax reporting needs miles per jurisdiction. You have a stream of GPS positions and a set of state polygons. It looks like a point-in-polygon problem. It is actually a line-segment problem, and conflating the two quietly loses distance on every border crossing in the fleet.
The obvious implementation#
Attribute each segment to the state the vehicle was in when the position arrived:
for a, b in pairwise(positions):
state = locate(a) # the bug
miles[state] += haversine(a, b)
Every term here is defensible and the result is wrong. When a is in Nevada and
b is in Arizona, the entire segment is credited to Nevada. Nothing looks
anomalous on any single row.
Why it compounds#
Positions arrive every 30–60 seconds. At highway speed that is roughly half a mile to a mile of travel between fixes, so each crossing misattributes up to a mile. A truck running the I-15 corridor crosses four jurisdictions in a day.
The error is also directional. It does not average out, because the misattribution always favours the state you were leaving.
Splitting at the boundary#
The fix is to treat the segment as a line and clip it:
for a, b in pairwise(positions):
for state, piece in clip(segment(a, b), boundaries):
miles[state] += length(piece)
clip returns the portions of the segment falling inside each polygon. Two
things matter in practice:
| Concern | What we do |
|---|---|
| Great-circle vs straight line | Interpolate along the geodesic; a straight line in lat/lon is wrong at speed |
| Polygons that share an edge | Assign the boundary itself to exactly one side, consistently |
| Positions that jump | Reject segments implying impossible speeds before clipping |
Checking it#
The test we actually trust is not a unit test. It is: total jurisdiction miles should match odometer delta within a percent or two.
If one state's calculated MPG is far off the fleet average, the miles for that state are wrong. The fuel almost never is.
Deadhead, personal conveyance and yard moves explain a small gap. They do not explain ten percent.
