|
| 1 | +// Copyright (c) 2019 Couchbase, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package searcher |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/blevesearch/bleve/geo" |
| 19 | + "github.com/blevesearch/bleve/index" |
| 20 | + "github.com/blevesearch/bleve/numeric" |
| 21 | + "github.com/blevesearch/bleve/search" |
| 22 | + "math" |
| 23 | +) |
| 24 | + |
| 25 | +func NewGeoBoundedPolygonSearcher(indexReader index.IndexReader, |
| 26 | + polygon []geo.Point, field string, boost float64, |
| 27 | + options search.SearcherOptions) (search.Searcher, error) { |
| 28 | + |
| 29 | + // compute the bounding box enclosing the polygon |
| 30 | + topLeftLon, topLeftLat, bottomRightLon, bottomRightLat, err := |
| 31 | + geo.BoundingRectangleForPolygon(polygon) |
| 32 | + if err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + |
| 36 | + // build a searcher for the bounding box on the polygon |
| 37 | + boxSearcher, err := boxSearcher(indexReader, |
| 38 | + topLeftLon, topLeftLat, bottomRightLon, bottomRightLat, |
| 39 | + field, boost, options, true) |
| 40 | + if err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + |
| 44 | + dvReader, err := indexReader.DocValueReader([]string{field}) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + |
| 49 | + // wrap it in a filtering searcher that checks for the polygon inclusivity |
| 50 | + return NewFilteringSearcher(boxSearcher, |
| 51 | + buildPolygonFilter(dvReader, field, polygon)), nil |
| 52 | +} |
| 53 | + |
| 54 | +const float64EqualityThreshold = 1e-6 |
| 55 | + |
| 56 | +func almostEqual(a, b float64) bool { |
| 57 | + return math.Abs(a-b) <= float64EqualityThreshold |
| 58 | +} |
| 59 | + |
| 60 | +// buildPolygonFilter returns true if the point lies inside the |
| 61 | +// polygon. It is based on the ray-casting technique as referred |
| 62 | +// here: https://wrf.ecse.rpi.edu/nikola/pubdetails/pnpoly.html |
| 63 | +func buildPolygonFilter(dvReader index.DocValueReader, field string, |
| 64 | + polygon []geo.Point) FilterFunc { |
| 65 | + return func(d *search.DocumentMatch) bool { |
| 66 | + var lon, lat float64 |
| 67 | + var found bool |
| 68 | + |
| 69 | + err := dvReader.VisitDocValues(d.IndexInternalID, func(field string, term []byte) { |
| 70 | + // only consider the values which are shifted 0 |
| 71 | + prefixCoded := numeric.PrefixCoded(term) |
| 72 | + shift, err := prefixCoded.Shift() |
| 73 | + if err == nil && shift == 0 { |
| 74 | + i64, err := prefixCoded.Int64() |
| 75 | + if err == nil { |
| 76 | + lon = geo.MortonUnhashLon(uint64(i64)) |
| 77 | + lat = geo.MortonUnhashLat(uint64(i64)) |
| 78 | + found = true |
| 79 | + } |
| 80 | + } |
| 81 | + }) |
| 82 | + |
| 83 | + // Note: this approach works for points which are strictly inside |
| 84 | + // the polygon. ie it might fail for certain points on the polygon boundaries. |
| 85 | + if err == nil && found { |
| 86 | + nVertices := len(polygon) |
| 87 | + var inside bool |
| 88 | + // check for a direct vertex match |
| 89 | + if almostEqual(polygon[0].Lat, lat) && |
| 90 | + almostEqual(polygon[0].Lon, lon) { |
| 91 | + return true |
| 92 | + } |
| 93 | + |
| 94 | + for i := 1; i < nVertices; i++ { |
| 95 | + if almostEqual(polygon[i].Lat, lat) && |
| 96 | + almostEqual(polygon[i].Lon, lon) { |
| 97 | + return true |
| 98 | + } |
| 99 | + if (polygon[i].Lat > lat) != (polygon[i-1].Lat > lat) && |
| 100 | + lon < (polygon[i-1].Lon-polygon[i].Lon)*(lat-polygon[i].Lat)/ |
| 101 | + (polygon[i-1].Lat-polygon[i].Lat)+polygon[i].Lon { |
| 102 | + inside = !inside |
| 103 | + } |
| 104 | + } |
| 105 | + return inside |
| 106 | + |
| 107 | + } |
| 108 | + return false |
| 109 | + } |
| 110 | +} |
0 commit comments