Skip to content

Commit

Permalink
Ignore NaN and Inf when parsing floats (#395)
Browse files Browse the repository at this point in the history
* Ignore NaN and Inf when parsing floats

* Better handling of missing stop lon/lat
  • Loading branch information
irees authored Dec 11, 2024
1 parent d5e3e15 commit 95a4501
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
24 changes: 21 additions & 3 deletions gtfs/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,29 @@ func (ent *Stop) ToPoint() tlxy.Point {

func (ent *Stop) ConditionalErrors() []error {
var errs []error
// TODO: This should be an enum for exhaustive search
// TODO: LocationType should be an enum for exhaustive search
lt := ent.LocationType.Val
if (lt == 0 || lt == 1 || lt == 2) && len(ent.StopName.Val) == 0 {
errs = append(errs, causes.NewConditionallyRequiredFieldError("stop_name"))
if lt == 0 || lt == 1 || lt == 2 {
if len(ent.StopName.Val) == 0 {
errs = append(errs, causes.NewConditionallyRequiredFieldError("stop_name"))
}
// Handle geometry passed via csv stop_lon/stop_lat OR geometry
stopLon := ent.StopLon
stopLat := ent.StopLat
if ent.Geometry.Valid {
if g := ent.Geometry.FlatCoords(); len(g) >= 2 {
stopLon = tt.NewFloat(g[0])
stopLat = tt.NewFloat(g[1])
}
}
if !stopLon.Valid {
errs = append(errs, causes.NewConditionallyRequiredFieldError("stop_lon"))
}
if !stopLat.Valid {
errs = append(errs, causes.NewConditionallyRequiredFieldError("stop_lat"))
}
}

// Check for "0" value...
if lt == 1 && ent.ParentStation.Val != "" {
errs = append(errs, causes.NewInvalidFieldError("parent_station", ent.ParentStation.Val, fmt.Errorf("station cannot have parent_station")))
Expand Down
14 changes: 11 additions & 3 deletions testdata/bad-entities/stops.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@ ok_no_stop_lat_for_nodes,node stop_lat = 0,,0.0,-116.40094,,,3,,,,ok_station,
ok_no_stop_lon_for_nodes,node stop_lon = 0,,36.641496,0.0,,,3,,,,ok_station,
ok_no_stop_lat_for_boarding_areas,area stop_lat = 0,,,-116.40094,,,4,,,,ok_stop,
ok_no_stop_lon_for_boarding_areas,area stop_lon = 0,,36.641496,,,,4,,,,ok_stop,
parse_stop_lon,stop_lat parse error,,36.641496,xyz,,,,,,,,FieldParseError:stop_lon
parse_stop_lat,stop_lon parse error,,xyz,-116.40094,,,,,,,,FieldParseError:stop_lat
parse_stop_lon,stop_lat parse error,,36.641496,xyz,,,3,,,,station,FieldParseError:stop_lon
parse_stop_lat,stop_lon parse error,,xyz,-116.40094,,,3,,,,station,FieldParseError:stop_lat
invalid_location_type,location_type = 6,,36.641496,-116.40094,,,6,,,,,InvalidFieldError:location_type
parse_location_type,location_type parse error,,36.641496,-116.40094,,,xyz,,,,,FieldParseError:location_type
parse_wheelchair_boarding,wheelchair_boarding parse error,,36.641496,-116.40094,,,,xyz,,,,FieldParseError:wheelchair_boarding
invalid_wheelchair_boarding,wheelchair_boarding > 2,,36.641496,-116.40094,,,,3,,,,InvalidFieldError:wheelchair_boarding
error_location_type_without_parent_station,location_type = 2 without parent_station,,36.641496,-116.40094,,,2,,,,,ConditionallyRequiredFieldError:parent_station
error_station_with_parent_station,location_type = 1 with parent_station,,36.641496,-116.40094,,,1,,,,station,InvalidFieldError:parent_station
error_station_with_parent_station,location_type = 1 with parent_station,,36.641496,-116.40094,,,1,,,,station,InvalidFieldError:parent_station
error_lat_NaN,stop_lat = NaN,,NaN,-116.40094,,,0,,,,,ConditionallyRequiredFieldError:stop_lat
error_lon_NaN,stop_lat = NaN,,36.641,NaN,,,0,,,,,ConditionallyRequiredFieldError:stop_lon
error_lat_Inf,stop_lat = NaN,,Inf,-116.40094,,,0,,,,,ConditionallyRequiredFieldError:stop_lat
error_lon_Inf,stop_lat = NaN,,36.641,Inf,,,0,,,,,ConditionallyRequiredFieldError:stop_lon
error_lat_-Inf,stop_lat = NaN,,-Inf,-116.40094,,,0,,,,,ConditionallyRequiredFieldError:stop_lat
error_lon_-Inf,stop_lat = NaN,,36.641,-Inf,,,0,,,,,ConditionallyRequiredFieldError:stop_lon
error_lat_+Inf,stop_lat = NaN,,+Inf,-116.40094,,,0,,,,,ConditionallyRequiredFieldError:stop_lat
error_lon_+Inf,stop_lat = NaN,,36.641,+Inf,,,0,,,,,ConditionallyRequiredFieldError:stop_lon
4 changes: 3 additions & 1 deletion tlcsv/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ func (reader *Reader) Stops() (out chan gtfs.Stop) {
reader.Adapter.ReadRows(ent.Filename(), func(row Row) {
e := gtfs.Stop{}
loadRow(&e, row)
e.SetCoordinates([2]float64{e.StopLon.Val, e.StopLat.Val})
if e.StopLon.Valid && e.StopLat.Valid {
e.SetCoordinates([2]float64{e.StopLon.Val, e.StopLat.Val})
}
out <- e
})
close(out)
Expand Down
4 changes: 4 additions & 0 deletions tt/option_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ func convertAssign(dest any, src any) (bool, error) {
default:
err = cannotConvert(dest, src)
}
if math.IsNaN(*d) || math.IsInf(*d, 0) {
*d = 0.0
ok = false
}
case *bool:
switch s := src.(type) {
case []byte:
Expand Down

0 comments on commit 95a4501

Please sign in to comment.