Skip to content

Commit

Permalink
feat: fail fast if a lat/lng is not found
Browse files Browse the repository at this point in the history
Stop the search if the first 5 timezone objects found by the index
do not contain the input lat/lng thus saving computing resources
  • Loading branch information
noandrea committed Jun 25, 2024
1 parent 1c7fe85 commit c67fef8
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions db/rtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,17 @@ func NewGeo2TzRTreeIndexFromGeoJSON(geoJSONPath string) (*Geo2TzRTreeIndex, erro
// if the timezone is not found, it returns an error
// It first searches in the land index, if not found, it searches in the sea index
func (g *Geo2TzRTreeIndex) Lookup(lat, lng float64) (tzID string, err error) {

chances := 5
// search the land index
g.land.Search(
[2]float64{lat, lng},
[2]float64{lat, lng},
func(min, max [2]float64, data timezoneGeo) bool {
chances--
if chances == 0 {
return false
}
for _, p := range data.Polygons {
if isPointInPolygonPIP(vertex{lat, lng}, p) {
tzID = data.Name
Expand All @@ -82,10 +88,15 @@ func (g *Geo2TzRTreeIndex) Lookup(lat, lng float64) (tzID string, err error) {

if tzID == "" {
// if not found, search the sea index
chances = 5
g.sea.Search(
[2]float64{lat, lng},
[2]float64{lat, lng},
func(min, max [2]float64, data timezoneGeo) bool {
chances--
if chances == 0 {
return false
}
for _, p := range data.Polygons {
if isPointInPolygonPIP(vertex{lat, lng}, p) {
tzID = data.Name
Expand Down

0 comments on commit c67fef8

Please sign in to comment.