fix: validate coordinates with isValidLatLon before S2 clustering - #88
fix: validate coordinates with isValidLatLon before S2 clustering#88Vishmayraj wants to merge 2 commits into
Conversation
ac10569 to
aa709b6
Compare
0xaboomar
left a comment
There was a problem hiding this comment.
Hi @Vishmayraj , thanks for the PR and for taking a close look at this.
The current behavior of getClusterID() is correct. Since Stop.Latitude and Stop.Longitude are pointers, we check whether they are set by verifying they are not nil.
The two lines you’re attempting to change already correctly represent the intended behavior, as clarified in the comments:
- Line 46: If a stop has no parent but has lat/lon, it is clustered by S2 cell.
- Line 64: If a parent exists but the grandparent is missing, we fall back to clustering by S2 using the stop’s lat/lon.
That said, you’ve raised a good point regarding how we treat coordinates (0, 0) as invalid. I’ll revisit this decision and verify whether it still makes sense. From a quick check, it doesn’t appear to be used elsewhere, but I’ll investigate further before deciding whether to keep or remove this constraint.
Thanks again for the thoughtful observation.
0xaboomar
left a comment
There was a problem hiding this comment.
Thanks for the PR, @Vishmayraj. Please take a look at the requested changes and let me know once they’re updated.
As an additional improvement, you could update the parameters of isValidLatLon() to use *float64 and adjust the callers accordingly. This additional improvement isn’t a blocker for merging, so we can proceed without it and address it later.
| return "", "", false // malformed hierarchy | ||
| } else if stop.Latitude != nil && stop.Longitude != nil { | ||
| } else if stop.Latitude != nil && stop.Longitude != nil && | ||
| isValidLatLon(*stop.Latitude, *stop.Longitude) { |
There was a problem hiding this comment.
I recommend moving the guard isValidLatLon() and the check of lat and lon equal nil before the switch case so we reject malformed stops early.
| func float64Ptr(f float64) *float64 { return &f } | ||
|
|
||
| func TestGetClusterID_ZeroCoordinates(t *testing.T) { | ||
| t.Run("Stop with (0,0) coordinates should not S2 cluster", func(t *testing.T) { |
There was a problem hiding this comment.
Update the test to make sure that malformed stops (ones with later lat | lon or lat not between (-90 , 90) or lon not between (-180 , 180)) is rejected. Skip the (0,0) checking because we will change this making isValidLatLon() depends on *float type to check whether lat/lon exist or not.
Code reviewFound 1 issue:
watchdog/internal/geo/geo_cluster.go Lines 45 to 48 in aa709b6 🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
The getClusterID function was producing S2 cluster IDs for stops with coordinates (0,0), which is typically a placeholder for improperly formatted GTFS data.
I noticed that the geo package already includes a validation check with the function “isValidLatLon,” but this is not currently used in the S2 clustering path for the aforementioned stops.
This PR includes the validation check in the 0 and 4 cases of the S2 fallback path, as well as a test to ensure that the previous incorrect behavior is now working as expected.