-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathbinary_predicates.go
342 lines (313 loc) · 10.7 KB
/
binary_predicates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package geomfn
import (
"github.com/cockroachdb/cockroach/pkg/geo"
"github.com/cockroachdb/cockroach/pkg/geo/geopb"
"github.com/cockroachdb/cockroach/pkg/geo/geos"
"github.com/cockroachdb/errors"
"github.com/twpayne/go-geom"
)
// Covers returns whether geometry A covers geometry B.
func Covers(a geo.Geometry, b geo.Geometry) (bool, error) {
if a.SRID() != b.SRID() {
return false, geo.NewMismatchingSRIDsError(a.SpatialObject(), b.SpatialObject())
}
if !a.CartesianBoundingBox().Covers(b.CartesianBoundingBox()) {
return false, nil
}
// Optimization for point in polygon calculations.
pointPolygonPair, pointKind, polygonKind := PointKindAndPolygonKind(a, b)
switch pointPolygonPair {
case PointAndPolygon:
// A point cannot cover a polygon.
return false, nil
case PolygonAndPoint:
// Computing whether a polygon covers a point is equivalent
// to computing whether the point is covered by the polygon.
return PointKindCoveredByPolygonKind(pointKind, polygonKind)
}
return geos.Covers(a.EWKB(), b.EWKB())
}
// CoveredBy returns whether geometry A is covered by geometry B.
func CoveredBy(a geo.Geometry, b geo.Geometry) (bool, error) {
if a.SRID() != b.SRID() {
return false, geo.NewMismatchingSRIDsError(a.SpatialObject(), b.SpatialObject())
}
if !b.CartesianBoundingBox().Covers(a.CartesianBoundingBox()) {
return false, nil
}
// Optimization for point in polygon calculations.
pointPolygonPair, pointKind, polygonKind := PointKindAndPolygonKind(a, b)
switch pointPolygonPair {
case PolygonAndPoint:
// A polygon cannot be covered by a point.
return false, nil
case PointAndPolygon:
return PointKindCoveredByPolygonKind(pointKind, polygonKind)
}
return geos.CoveredBy(a.EWKB(), b.EWKB())
}
// Contains returns whether geometry A contains geometry B.
func Contains(a geo.Geometry, b geo.Geometry) (bool, error) {
if a.SRID() != b.SRID() {
return false, geo.NewMismatchingSRIDsError(a.SpatialObject(), b.SpatialObject())
}
if !a.CartesianBoundingBox().Covers(b.CartesianBoundingBox()) {
return false, nil
}
// Optimization for point in polygon calculations.
pointPolygonPair, pointKind, polygonKind := PointKindAndPolygonKind(a, b)
switch pointPolygonPair {
case PointAndPolygon:
// A point cannot contain a polygon.
return false, nil
case PolygonAndPoint:
// Computing whether a polygon contains a point is equivalent
// to computing whether the point is contained within the polygon.
return PointKindWithinPolygonKind(pointKind, polygonKind)
}
return geos.Contains(a.EWKB(), b.EWKB())
}
// ContainsProperly returns whether geometry A properly contains geometry B.
func ContainsProperly(a geo.Geometry, b geo.Geometry) (bool, error) {
if a.SRID() != b.SRID() {
return false, geo.NewMismatchingSRIDsError(a.SpatialObject(), b.SpatialObject())
}
if !a.CartesianBoundingBox().Covers(b.CartesianBoundingBox()) {
return false, nil
}
return geos.RelatePattern(a.EWKB(), b.EWKB(), "T**FF*FF*")
}
// Crosses returns whether geometry A crosses geometry B.
func Crosses(a geo.Geometry, b geo.Geometry) (bool, error) {
if a.SRID() != b.SRID() {
return false, geo.NewMismatchingSRIDsError(a.SpatialObject(), b.SpatialObject())
}
if !a.CartesianBoundingBox().Intersects(b.CartesianBoundingBox()) {
return false, nil
}
return geos.Crosses(a.EWKB(), b.EWKB())
}
// Disjoint returns whether geometry A is disjoint from geometry B.
func Disjoint(a geo.Geometry, b geo.Geometry) (bool, error) {
if a.SRID() != b.SRID() {
return false, geo.NewMismatchingSRIDsError(a.SpatialObject(), b.SpatialObject())
}
return geos.Disjoint(a.EWKB(), b.EWKB())
}
// Equals returns whether geometry A equals geometry B.
func Equals(a geo.Geometry, b geo.Geometry) (bool, error) {
if a.SRID() != b.SRID() {
return false, geo.NewMismatchingSRIDsError(a.SpatialObject(), b.SpatialObject())
}
// Empty items are equal to each other.
// Do this check before the BoundingBoxIntersects check, as we would otherwise
// return false.
if a.Empty() && b.Empty() {
return true, nil
}
if !a.CartesianBoundingBox().Covers(b.CartesianBoundingBox()) {
return false, nil
}
return geos.Equals(a.EWKB(), b.EWKB())
}
// Intersects returns whether geometry A intersects geometry B.
func Intersects(a geo.Geometry, b geo.Geometry) (bool, error) {
if a.SRID() != b.SRID() {
return false, geo.NewMismatchingSRIDsError(a.SpatialObject(), b.SpatialObject())
}
if !a.CartesianBoundingBox().Intersects(b.CartesianBoundingBox()) {
return false, nil
}
// Optimization for point in polygon calculations.
pointPolygonPair, pointKind, polygonKind := PointKindAndPolygonKind(a, b)
switch pointPolygonPair {
case PointAndPolygon, PolygonAndPoint:
return PointKindIntersectsPolygonKind(pointKind, polygonKind)
}
return geos.Intersects(a.EWKB(), b.EWKB())
}
// OrderingEquals returns whether geometry A is equal to B, with all constituents
// and coordinates in the same order.
func OrderingEquals(a, b geo.Geometry) (bool, error) {
if a.SRID() != b.SRID() {
return false, nil
}
aBox, bBox := a.CartesianBoundingBox(), b.CartesianBoundingBox()
switch {
case aBox == nil && bBox == nil:
case aBox == nil || bBox == nil:
return false, nil
case aBox.Compare(bBox) != 0:
return false, nil
}
geomA, err := a.AsGeomT()
if err != nil {
return false, err
}
geomB, err := b.AsGeomT()
if err != nil {
return false, err
}
return orderingEqualsFromGeomT(geomA, geomB)
}
// orderingEqualsFromGeomT returns whether geometry A is equal to B.
func orderingEqualsFromGeomT(a, b geom.T) (bool, error) {
if a.Layout() != b.Layout() {
return false, nil
}
switch a := a.(type) {
case *geom.Point:
if b, ok := b.(*geom.Point); ok {
// Point.Coords() panics on empty points
switch {
case a.Empty() && b.Empty():
return true, nil
case a.Empty() || b.Empty():
return false, nil
default:
return a.Coords().Equal(b.Layout(), b.Coords()), nil
}
}
case *geom.LineString:
if b, ok := b.(*geom.LineString); ok && a.NumCoords() == b.NumCoords() {
for i := 0; i < a.NumCoords(); i++ {
if !a.Coord(i).Equal(b.Layout(), b.Coord(i)) {
return false, nil
}
}
return true, nil
}
case *geom.Polygon:
if b, ok := b.(*geom.Polygon); ok && a.NumLinearRings() == b.NumLinearRings() {
for i := 0; i < a.NumLinearRings(); i++ {
for j := 0; j < a.LinearRing(i).NumCoords(); j++ {
if !a.LinearRing(i).Coord(j).Equal(b.Layout(), b.LinearRing(i).Coord(j)) {
return false, nil
}
}
}
return true, nil
}
case *geom.MultiPoint:
if b, ok := b.(*geom.MultiPoint); ok && a.NumPoints() == b.NumPoints() {
for i := 0; i < a.NumPoints(); i++ {
if eq, err := orderingEqualsFromGeomT(a.Point(i), b.Point(i)); err != nil || !eq {
return false, err
}
}
return true, nil
}
case *geom.MultiLineString:
if b, ok := b.(*geom.MultiLineString); ok && a.NumLineStrings() == b.NumLineStrings() {
for i := 0; i < a.NumLineStrings(); i++ {
if eq, err := orderingEqualsFromGeomT(a.LineString(i), b.LineString(i)); err != nil || !eq {
return false, err
}
}
return true, nil
}
case *geom.MultiPolygon:
if b, ok := b.(*geom.MultiPolygon); ok && a.NumPolygons() == b.NumPolygons() {
for i := 0; i < a.NumPolygons(); i++ {
if eq, err := orderingEqualsFromGeomT(a.Polygon(i), b.Polygon(i)); err != nil || !eq {
return false, err
}
}
return true, nil
}
case *geom.GeometryCollection:
if b, ok := b.(*geom.GeometryCollection); ok && a.NumGeoms() == b.NumGeoms() {
for i := 0; i < a.NumGeoms(); i++ {
if eq, err := orderingEqualsFromGeomT(a.Geom(i), b.Geom(i)); err != nil || !eq {
return false, err
}
}
return true, nil
}
default:
return false, errors.AssertionFailedf("unknown geometry type: %T", a)
}
return false, nil
}
// Overlaps returns whether geometry A overlaps geometry B.
func Overlaps(a geo.Geometry, b geo.Geometry) (bool, error) {
if a.SRID() != b.SRID() {
return false, geo.NewMismatchingSRIDsError(a.SpatialObject(), b.SpatialObject())
}
if !a.CartesianBoundingBox().Intersects(b.CartesianBoundingBox()) {
return false, nil
}
return geos.Overlaps(a.EWKB(), b.EWKB())
}
// PointPolygonOrder represents the order of a point and a polygon
// in an ordered pair of geometries.
type PointPolygonOrder int
const (
// NotPointAndPolygon signifies that a pair of geometries is
// not a point and a polygon.
NotPointAndPolygon PointPolygonOrder = iota
// PointAndPolygon signifies that the point appears first
// in an ordered pair of a point and a polygon.
PointAndPolygon
// PolygonAndPoint signifies that the polygon appears first
// in an ordered pair of a point and a polygon.
PolygonAndPoint
)
// PointKindAndPolygonKind returns whether a pair of geometries contains
// a (multi)point and a (multi)polygon. It is used to determine if the
// point in polygon optimization can be applied.
func PointKindAndPolygonKind(
a geo.Geometry, b geo.Geometry,
) (PointPolygonOrder, geo.Geometry, geo.Geometry) {
switch a.ShapeType2D() {
case geopb.ShapeType_Point, geopb.ShapeType_MultiPoint:
switch b.ShapeType2D() {
case geopb.ShapeType_Polygon, geopb.ShapeType_MultiPolygon:
return PointAndPolygon, a, b
}
case geopb.ShapeType_Polygon, geopb.ShapeType_MultiPolygon:
switch b.ShapeType2D() {
case geopb.ShapeType_Point, geopb.ShapeType_MultiPoint:
return PolygonAndPoint, b, a
}
}
return NotPointAndPolygon, a, b
}
// Touches returns whether geometry A touches geometry B.
func Touches(a geo.Geometry, b geo.Geometry) (bool, error) {
if a.SRID() != b.SRID() {
return false, geo.NewMismatchingSRIDsError(a.SpatialObject(), b.SpatialObject())
}
if !a.CartesianBoundingBox().Intersects(b.CartesianBoundingBox()) {
return false, nil
}
return geos.Touches(a.EWKB(), b.EWKB())
}
// Within returns whether geometry A is within geometry B.
func Within(a geo.Geometry, b geo.Geometry) (bool, error) {
if a.SRID() != b.SRID() {
return false, geo.NewMismatchingSRIDsError(a.SpatialObject(), b.SpatialObject())
}
if !b.CartesianBoundingBox().Covers(a.CartesianBoundingBox()) {
return false, nil
}
// Optimization for point in polygon calculations.
pointPolygonPair, pointKind, polygonKind := PointKindAndPolygonKind(a, b)
switch pointPolygonPair {
case PolygonAndPoint:
// A polygon cannot be contained within a point.
return false, nil
case PointAndPolygon:
return PointKindWithinPolygonKind(pointKind, polygonKind)
}
return geos.Within(a.EWKB(), b.EWKB())
}