Skip to content

Commit

Permalink
Merge ae3a555 into b7a0cb6
Browse files Browse the repository at this point in the history
  • Loading branch information
sreekanth-cb committed Feb 25, 2020
2 parents b7a0cb6 + ae3a555 commit b4892b6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
15 changes: 11 additions & 4 deletions search/searcher/search_geopolygon.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,17 @@ func buildPolygonFilter(dvReader index.DocValueReader, field string,
// the polygon. ie it might fail for certain points on the polygon boundaries.
if err == nil && found {
nVertices := len(polygon)
if len(polygon) < 3 {
return false
}
rayIntersectsSegment := func(point, a, b geo.Point) bool {
return (a.Lat > point.Lat) != (b.Lat > point.Lat) &&
point.Lon < (b.Lon-a.Lon)*(point.Lat-a.Lat)/(b.Lat-a.Lat)+a.Lon
}

for i := range lons {
var inside bool
pt := geo.Point{Lon: lons[i], Lat: lats[i]}
inside := rayIntersectsSegment(pt, polygon[len(polygon)-1], polygon[0])
// check for a direct vertex match
if almostEqual(polygon[0].Lat, lats[i]) &&
almostEqual(polygon[0].Lon, lons[i]) {
Expand All @@ -98,9 +107,7 @@ func buildPolygonFilter(dvReader index.DocValueReader, field string,
almostEqual(polygon[j].Lon, lons[i]) {
return true
}
if (polygon[j].Lat > lats[i]) != (polygon[j-1].Lat > lats[i]) &&
lons[i] < (polygon[j-1].Lon-polygon[j].Lon)*(lats[i]-polygon[j].Lat)/
(polygon[j-1].Lat-polygon[j].Lat)+polygon[j].Lon {
if rayIntersectsSegment(pt, polygon[j-1], polygon[j]) {
inside = !inside
}
}
Expand Down
5 changes: 3 additions & 2 deletions search/searcher/search_geopolygon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ func TestGeoRectanglePolygon(t *testing.T) {
field string
want []string
}{
{[]geo.Point{{Lon: 0.001, Lat: 0.001}, {Lon: 85.002, Lat: 38.002}}, "loc",
[]string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}},
{[]geo.Point{{Lon: 0, Lat: 0}, {Lon: 0, Lat: 50}, {Lon: 50, Lat: 50}, {Lon: 50, Lat: 0}, {Lon: 0, Lat: 0}}, "loc",
[]string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"},
},
}

i := setupGeo(t)
Expand Down

0 comments on commit b4892b6

Please sign in to comment.