-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathgeo.go
881 lines (802 loc) · 27.3 KB
/
geo.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
// 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 geo contains the base types for spatial data type operations.
package geo
import (
"bytes"
"encoding/binary"
"math"
"github.com/cockroachdb/cockroach/pkg/geo/geographiclib"
"github.com/cockroachdb/cockroach/pkg/geo/geopb"
"github.com/cockroachdb/cockroach/pkg/geo/geoprojbase"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/errors"
"github.com/golang/geo/r1"
"github.com/golang/geo/s1"
"github.com/golang/geo/s2"
"github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/ewkb"
)
// DefaultEWKBEncodingFormat is the default encoding format for EWKB.
var DefaultEWKBEncodingFormat = binary.LittleEndian
// EmptyBehavior is the behavior to adopt when an empty Geometry is encountered.
type EmptyBehavior uint8
const (
// EmptyBehaviorError will error with EmptyGeometryError when an empty geometry
// is encountered.
EmptyBehaviorError EmptyBehavior = 0
// EmptyBehaviorOmit will omit an entry when an empty geometry is encountered.
EmptyBehaviorOmit EmptyBehavior = 1
)
//
// Geospatial Type
//
// GeospatialType are functions that are common between all Geospatial types.
type GeospatialType interface {
// SRID returns the SRID of the given type.
SRID() geopb.SRID
// ShapeType returns the ShapeType of the given type.
ShapeType() geopb.ShapeType
}
var _ GeospatialType = (*Geometry)(nil)
var _ GeospatialType = (*Geography)(nil)
// GeospatialTypeFitsColumnMetadata determines whether a GeospatialType is compatible with the
// given SRID and Shape.
// Returns an error if the types does not fit.
func GeospatialTypeFitsColumnMetadata(
t GeospatialType, srid geopb.SRID, shapeType geopb.ShapeType,
) error {
// SRID 0 can take in any SRID. Otherwise SRIDs must match.
if srid != 0 && t.SRID() != srid {
return errors.Newf("object SRID %d does not match column SRID %d", t.SRID(), srid)
}
// Shape_Geometry/Shape_Unset can take in any kind of shape.
// Otherwise, shapes must match.
if shapeType != geopb.ShapeType_Unset && shapeType != geopb.ShapeType_Geometry && shapeType != t.ShapeType() {
return errors.Newf("object type %s does not match column type %s", t.ShapeType(), shapeType)
}
return nil
}
//
// Geometry
//
// Geometry is planar spatial object.
type Geometry struct {
spatialObject geopb.SpatialObject
}
// NewGeometry returns a new Geometry. Assumes the input EWKB is validated and in little endian.
func NewGeometry(spatialObject geopb.SpatialObject) (*Geometry, error) {
if spatialObject.SRID != 0 {
if _, ok := geoprojbase.Projection(spatialObject.SRID); !ok {
return nil, errors.Newf("unknown SRID for Geometry: %d", spatialObject.SRID)
}
}
if spatialObject.Type != geopb.SpatialObjectType_GeometryType {
return nil, errors.Newf("expected geometry type, found %s", spatialObject.Type)
}
return &Geometry{spatialObject: spatialObject}, nil
}
// NewGeometryUnsafe creates a geometry object that assumes spatialObject is from the DB.
// It assumes the spatialObject underneath is safe.
func NewGeometryUnsafe(spatialObject geopb.SpatialObject) *Geometry {
return &Geometry{spatialObject: spatialObject}
}
// NewGeometryFromPointCoords makes a point from x, y coordinates.
func NewGeometryFromPointCoords(x, y float64) (*Geometry, error) {
s, err := spatialObjectFromGeomT(geom.NewPointFlat(geom.XY, []float64{x, y}), geopb.SpatialObjectType_GeometryType)
if err != nil {
return nil, err
}
return NewGeometry(s)
}
// NewGeometryFromGeomT creates a new Geometry object from a geom.T object.
func NewGeometryFromGeomT(g geom.T) (*Geometry, error) {
spatialObject, err := spatialObjectFromGeomT(g, geopb.SpatialObjectType_GeometryType)
if err != nil {
return nil, err
}
return NewGeometry(spatialObject)
}
// ParseGeometry parses a Geometry from a given text.
func ParseGeometry(str string) (*Geometry, error) {
spatialObject, err := parseAmbiguousText(geopb.SpatialObjectType_GeometryType, str, geopb.DefaultGeometrySRID)
if err != nil {
return nil, err
}
return NewGeometry(spatialObject)
}
// MustParseGeometry behaves as ParseGeometry, but panics if there is an error.
func MustParseGeometry(str string) *Geometry {
g, err := ParseGeometry(str)
if err != nil {
panic(err)
}
return g
}
// ParseGeometryFromEWKT parses the EWKT into a Geometry.
func ParseGeometryFromEWKT(
ewkt geopb.EWKT, srid geopb.SRID, defaultSRIDOverwriteSetting defaultSRIDOverwriteSetting,
) (*Geometry, error) {
g, err := parseEWKT(geopb.SpatialObjectType_GeometryType, ewkt, srid, defaultSRIDOverwriteSetting)
if err != nil {
return nil, err
}
return NewGeometry(g)
}
// ParseGeometryFromEWKB parses the EWKB into a Geometry.
func ParseGeometryFromEWKB(ewkb geopb.EWKB) (*Geometry, error) {
g, err := parseEWKB(geopb.SpatialObjectType_GeometryType, ewkb, geopb.DefaultGeometrySRID, DefaultSRIDIsHint)
if err != nil {
return nil, err
}
return NewGeometry(g)
}
// ParseGeometryFromEWKBAndSRID parses the EWKB into a given Geometry with the given
// SRID set.
func ParseGeometryFromEWKBAndSRID(ewkb geopb.EWKB, srid geopb.SRID) (*Geometry, error) {
g, err := parseEWKB(geopb.SpatialObjectType_GeometryType, ewkb, srid, DefaultSRIDShouldOverwrite)
if err != nil {
return nil, err
}
return NewGeometry(g)
}
// MustParseGeometryFromEWKB behaves as ParseGeometryFromEWKB, but panics if an error occurs.
func MustParseGeometryFromEWKB(ewkb geopb.EWKB) *Geometry {
ret, err := ParseGeometryFromEWKB(ewkb)
if err != nil {
panic(err)
}
return ret
}
// ParseGeometryFromGeoJSON parses the GeoJSON into a given Geometry.
func ParseGeometryFromGeoJSON(json []byte) (*Geometry, error) {
g, err := parseGeoJSON(geopb.SpatialObjectType_GeometryType, json, geopb.DefaultGeometrySRID)
if err != nil {
return nil, err
}
return NewGeometry(g)
}
// ParseGeometryFromEWKBUnsafe returns a new Geometry from an EWKB, without any SRID checks.
// You should only do this if you trust the EWKB is setup correctly.
// You most likely want geo.ParseGeometryFromEWKB instead.
func ParseGeometryFromEWKBUnsafe(ewkb geopb.EWKB) (*Geometry, error) {
base, err := parseEWKBRaw(geopb.SpatialObjectType_GeometryType, ewkb)
if err != nil {
return nil, err
}
return NewGeometryUnsafe(base), nil
}
// AsGeography converts a given Geometry to its Geography form.
func (g *Geometry) AsGeography() (*Geography, error) {
srid := g.SRID()
if srid == 0 {
// Set a geography SRID if one is not already set.
srid = geopb.DefaultGeographySRID
}
spatialObject, err := adjustSpatialObject(g.spatialObject, srid, geopb.SpatialObjectType_GeographyType)
if err != nil {
return nil, err
}
return NewGeography(spatialObject)
}
// CloneWithSRID sets a given Geometry's SRID to another, without any transformations.
// Returns a new Geometry object.
func (g *Geometry) CloneWithSRID(srid geopb.SRID) (*Geometry, error) {
spatialObject, err := adjustSpatialObject(g.spatialObject, srid, geopb.SpatialObjectType_GeometryType)
if err != nil {
return nil, err
}
return NewGeometry(spatialObject)
}
// adjustSpatialObject returns the SpatialObject with new parameters.
func adjustSpatialObject(
so geopb.SpatialObject, srid geopb.SRID, soType geopb.SpatialObjectType,
) (geopb.SpatialObject, error) {
t, err := ewkb.Unmarshal(so.EWKB)
if err != nil {
return geopb.SpatialObject{}, err
}
adjustGeomSRID(t, srid)
return spatialObjectFromGeomT(t, soType)
}
// AsGeomT returns the geometry as a geom.T object.
func (g *Geometry) AsGeomT() (geom.T, error) {
return ewkb.Unmarshal(g.spatialObject.EWKB)
}
// Empty returns whether the given Geometry is empty.
func (g *Geometry) Empty() bool {
return g.spatialObject.BoundingBox == nil
}
// EWKB returns the EWKB representation of the Geometry.
func (g *Geometry) EWKB() geopb.EWKB {
return g.spatialObject.EWKB
}
// SpatialObject returns the SpatialObject representation of the Geometry.
func (g *Geometry) SpatialObject() geopb.SpatialObject {
return g.spatialObject
}
// EWKBHex returns the EWKBHex representation of the Geometry.
func (g *Geometry) EWKBHex() string {
return g.spatialObject.EWKBHex()
}
// SRID returns the SRID representation of the Geometry.
func (g *Geometry) SRID() geopb.SRID {
return g.spatialObject.SRID
}
// ShapeType returns the shape type of the Geometry.
func (g *Geometry) ShapeType() geopb.ShapeType {
return g.spatialObject.ShapeType
}
// CartesianBoundingBox returns a Cartesian bounding box.
func (g *Geometry) CartesianBoundingBox() *CartesianBoundingBox {
if g.spatialObject.BoundingBox == nil {
return nil
}
return &CartesianBoundingBox{BoundingBox: *g.spatialObject.BoundingBox}
}
// SpaceCurveIndex returns an uint64 index to use representing an index into a space-filling curve.
// This will return 0 for empty spatial objects, and math.MaxUint64 for any object outside
// the defined bounds of the given SRID projection.
func (g *Geometry) SpaceCurveIndex() uint64 {
bbox := g.CartesianBoundingBox()
if bbox == nil {
return 0
}
centerX := (bbox.BoundingBox.LoX + bbox.BoundingBox.HiX) / 2
centerY := (bbox.BoundingBox.LoY + bbox.BoundingBox.HiY) / 2
// By default, bound by MaxInt32 (we have not typically seen bounds greater than 1B).
bounds := geoprojbase.Bounds{
MinX: math.MinInt32,
MaxX: math.MaxInt32,
MinY: math.MinInt32,
MaxY: math.MaxInt32,
}
if proj, ok := geoprojbase.Projection(g.SRID()); ok {
bounds = proj.Bounds
}
// If we're out of bounds, give up and return a large number.
if centerX > bounds.MaxX || centerY > bounds.MaxY || centerX < bounds.MinX || centerY < bounds.MinY {
return math.MaxUint64
}
const boxLength = 1 << 32
// Add 1 to each bound so that we normalize the coordinates to [0, 1) before
// multiplying by boxLength to give coordinates that are integers in the interval [0, boxLength-1].
xBounds := (bounds.MaxX - bounds.MinX) + 1
yBounds := (bounds.MaxY - bounds.MinY) + 1
xPos := uint64(((centerX - bounds.MinX) / xBounds) * boxLength)
yPos := uint64(((centerY - bounds.MinY) / yBounds) * boxLength)
return hilbertInverse(boxLength, xPos, yPos)
}
// Compare compares a Geometry against another.
// It compares using SpaceCurveIndex, followed by the byte representation of the Geometry.
// This must produce the same ordering as the index mechanism.
func (g *Geometry) Compare(o *Geometry) int {
lhs := g.SpaceCurveIndex()
rhs := o.SpaceCurveIndex()
if lhs > rhs {
return 1
}
if lhs < rhs {
return -1
}
return compareSpatialObjectBytes(g.SpatialObject(), o.SpatialObject())
}
//
// Geography
//
// Geography is a spherical spatial object.
type Geography struct {
spatialObject geopb.SpatialObject
}
// NewGeography returns a new Geography. Assumes the input EWKB is validated and in little endian.
func NewGeography(spatialObject geopb.SpatialObject) (*Geography, error) {
projection, ok := geoprojbase.Projection(spatialObject.SRID)
if !ok {
return nil, errors.Newf("unknown SRID for Geography: %d", spatialObject.SRID)
}
if !projection.IsLatLng {
return nil, errors.Newf(
"SRID %d cannot be used for geography as it is not in a lon/lat coordinate system",
spatialObject.SRID,
)
}
if spatialObject.Type != geopb.SpatialObjectType_GeographyType {
return nil, errors.Newf("expected geography type, found %s", spatialObject.Type)
}
return &Geography{spatialObject: spatialObject}, nil
}
// NewGeographyUnsafe creates a geometry object that assumes spatialObject is from the DB.
// It assumes the spatialObject underneath is safe.
func NewGeographyUnsafe(spatialObject geopb.SpatialObject) *Geography {
return &Geography{spatialObject: spatialObject}
}
// NewGeographyFromGeomT creates a new Geography from a geom.T object.
func NewGeographyFromGeomT(g geom.T) (*Geography, error) {
spatialObject, err := spatialObjectFromGeomT(g, geopb.SpatialObjectType_GeographyType)
if err != nil {
return nil, err
}
return NewGeography(spatialObject)
}
// MustNewGeographyFromGeomT enforces no error from NewGeographyFromGeomT.
func MustNewGeographyFromGeomT(g geom.T) *Geography {
ret, err := NewGeographyFromGeomT(g)
if err != nil {
panic(err)
}
return ret
}
// ParseGeography parses a Geography from a given text.
func ParseGeography(str string) (*Geography, error) {
spatialObject, err := parseAmbiguousText(geopb.SpatialObjectType_GeographyType, str, geopb.DefaultGeographySRID)
if err != nil {
return nil, err
}
return NewGeography(spatialObject)
}
// MustParseGeography behaves as ParseGeography, but panics if there is an error.
func MustParseGeography(str string) *Geography {
g, err := ParseGeography(str)
if err != nil {
panic(err)
}
return g
}
// ParseGeographyFromEWKT parses the EWKT into a Geography.
func ParseGeographyFromEWKT(
ewkt geopb.EWKT, srid geopb.SRID, defaultSRIDOverwriteSetting defaultSRIDOverwriteSetting,
) (*Geography, error) {
g, err := parseEWKT(geopb.SpatialObjectType_GeographyType, ewkt, srid, defaultSRIDOverwriteSetting)
if err != nil {
return nil, err
}
return NewGeography(g)
}
// ParseGeographyFromEWKB parses the EWKB into a Geography.
func ParseGeographyFromEWKB(ewkb geopb.EWKB) (*Geography, error) {
g, err := parseEWKB(geopb.SpatialObjectType_GeographyType, ewkb, geopb.DefaultGeographySRID, DefaultSRIDIsHint)
if err != nil {
return nil, err
}
return NewGeography(g)
}
// ParseGeographyFromEWKBAndSRID parses the EWKB into a given Geography with the
// given SRID set.
func ParseGeographyFromEWKBAndSRID(ewkb geopb.EWKB, srid geopb.SRID) (*Geography, error) {
g, err := parseEWKB(geopb.SpatialObjectType_GeographyType, ewkb, srid, DefaultSRIDShouldOverwrite)
if err != nil {
return nil, err
}
return NewGeography(g)
}
// MustParseGeographyFromEWKB behaves as ParseGeographyFromEWKB, but panics if an error occurs.
func MustParseGeographyFromEWKB(ewkb geopb.EWKB) *Geography {
ret, err := ParseGeographyFromEWKB(ewkb)
if err != nil {
panic(err)
}
return ret
}
// ParseGeographyFromGeoJSON parses the GeoJSON into a given Geography.
func ParseGeographyFromGeoJSON(json []byte) (*Geography, error) {
g, err := parseGeoJSON(geopb.SpatialObjectType_GeographyType, json, geopb.DefaultGeographySRID)
if err != nil {
return nil, err
}
return NewGeography(g)
}
// ParseGeographyFromEWKBUnsafe returns a new Geography from an EWKB, without any SRID checks.
// You should only do this if you trust the EWKB is setup correctly.
// You most likely want ParseGeographyFromEWKB instead.
func ParseGeographyFromEWKBUnsafe(ewkb geopb.EWKB) (*Geography, error) {
base, err := parseEWKBRaw(geopb.SpatialObjectType_GeographyType, ewkb)
if err != nil {
return nil, err
}
return NewGeographyUnsafe(base), nil
}
// CloneWithSRID sets a given Geography's SRID to another, without any transformations.
// Returns a new Geography object.
func (g *Geography) CloneWithSRID(srid geopb.SRID) (*Geography, error) {
spatialObject, err := adjustSpatialObject(g.spatialObject, srid, geopb.SpatialObjectType_GeographyType)
if err != nil {
return nil, err
}
return NewGeography(spatialObject)
}
// AsGeometry converts a given Geography to its Geometry form.
func (g *Geography) AsGeometry() (*Geometry, error) {
spatialObject, err := adjustSpatialObject(g.spatialObject, g.SRID(), geopb.SpatialObjectType_GeometryType)
if err != nil {
return nil, err
}
return NewGeometry(spatialObject)
}
// AsGeomT returns the Geography as a geom.T object.
func (g *Geography) AsGeomT() (geom.T, error) {
return ewkb.Unmarshal(g.spatialObject.EWKB)
}
// EWKB returns the EWKB representation of the Geography.
func (g *Geography) EWKB() geopb.EWKB {
return g.spatialObject.EWKB
}
// SpatialObject returns the SpatialObject representation of the Geography.
func (g *Geography) SpatialObject() geopb.SpatialObject {
return g.spatialObject
}
// EWKBHex returns the EWKBHex representation of the Geography.
func (g *Geography) EWKBHex() string {
return g.spatialObject.EWKBHex()
}
// SRID returns the SRID representation of the Geography.
func (g *Geography) SRID() geopb.SRID {
return g.spatialObject.SRID
}
// ShapeType returns the shape type of the Geography.
func (g *Geography) ShapeType() geopb.ShapeType {
return g.spatialObject.ShapeType
}
// Spheroid returns the spheroid represented by the given Geography.
func (g *Geography) Spheroid() (*geographiclib.Spheroid, error) {
proj, ok := geoprojbase.Projection(g.SRID())
if !ok {
return nil, errors.Newf("expected spheroid for SRID %d", g.SRID())
}
return proj.Spheroid, nil
}
// AsS2 converts a given Geography into it's S2 form.
func (g *Geography) AsS2(emptyBehavior EmptyBehavior) ([]s2.Region, error) {
geomRepr, err := g.AsGeomT()
if err != nil {
return nil, err
}
// TODO(otan): convert by reading from EWKB to S2 directly.
return S2RegionsFromGeomT(geomRepr, emptyBehavior)
}
// BoundingRect returns the bounding s2.Rect of the given Geography.
func (g *Geography) BoundingRect() s2.Rect {
bbox := g.spatialObject.BoundingBox
if bbox == nil {
return s2.EmptyRect()
}
return s2.Rect{
Lat: r1.Interval{Lo: bbox.LoY, Hi: bbox.HiY},
Lng: s1.Interval{Lo: bbox.LoX, Hi: bbox.HiX},
}
}
// BoundingCap returns the bounding s2.Cap of the given Geography.
func (g *Geography) BoundingCap() s2.Cap {
return g.BoundingRect().CapBound()
}
// SpaceCurveIndex returns an uint64 index to use representing an index into a space-filling curve.
// This will return 0 for empty spatial objects.
func (g *Geography) SpaceCurveIndex() uint64 {
rect := g.BoundingRect()
if rect.IsEmpty() {
return 0
}
return uint64(s2.CellIDFromLatLng(rect.Center()))
}
// Compare compares a Geography against another.
// It compares using SpaceCurveIndex, followed by the byte representation of the Geography.
// This must produce the same ordering as the index mechanism.
func (g *Geography) Compare(o *Geography) int {
lhs := g.SpaceCurveIndex()
rhs := o.SpaceCurveIndex()
if lhs > rhs {
return 1
}
if lhs < rhs {
return -1
}
return compareSpatialObjectBytes(g.SpatialObject(), o.SpatialObject())
}
//
// Common
//
// IsLinearRingCCW returns whether a given linear ring is counter clock wise.
// See 2.07 of http://www.faqs.org/faqs/graphics/algorithms-faq/.
// "Find the lowest vertex (or, if there is more than one vertex with the same lowest coordinate,
// the rightmost of those vertices) and then take the cross product of the edges fore and aft of it."
func IsLinearRingCCW(linearRing *geom.LinearRing) bool {
smallestIdx := 0
smallest := linearRing.Coord(0)
for pointIdx := 1; pointIdx < linearRing.NumCoords()-1; pointIdx++ {
curr := linearRing.Coord(pointIdx)
if curr.Y() < smallest.Y() || (curr.Y() == smallest.Y() && curr.X() > smallest.X()) {
smallestIdx = pointIdx
smallest = curr
}
}
// prevIdx is the previous point. If we are at the 0th point, the last coordinate
// is also the 0th point, so take the second last point.
// Note we don't have to apply this for "nextIdx" as we cap the search above at the
// second last vertex.
prevIdx := smallestIdx - 1
if smallestIdx == 0 {
prevIdx = linearRing.NumCoords() - 2
}
a := linearRing.Coord(prevIdx)
b := smallest
c := linearRing.Coord(smallestIdx + 1)
// We could do the cross product, but we are only interested in the sign.
// To find the sign, reorganize into the orientation matrix:
// 1 x_a y_a
// 1 x_b y_b
// 1 x_c y_c
// and find the determinant.
// https://en.wikipedia.org/wiki/Curve_orientation#Orientation_of_a_simple_polygon
areaSign := a.X()*b.Y() - a.Y()*b.X() +
a.Y()*c.X() - a.X()*c.Y() +
b.X()*c.Y() - c.X()*b.Y()
// Note having an area sign of 0 means it is a flat polygon, which is invalid.
return areaSign > 0
}
// S2RegionsFromGeomT converts an geom representation of an object
// to s2 regions.
// As S2 does not really handle empty geometries well, we need to ingest emptyBehavior and
// react appropriately.
func S2RegionsFromGeomT(geomRepr geom.T, emptyBehavior EmptyBehavior) ([]s2.Region, error) {
var regions []s2.Region
if geomRepr.Empty() {
switch emptyBehavior {
case EmptyBehaviorOmit:
return nil, nil
case EmptyBehaviorError:
return nil, NewEmptyGeometryError()
default:
return nil, errors.Newf("programmer error: unknown behavior")
}
}
switch repr := geomRepr.(type) {
case *geom.Point:
regions = []s2.Region{
s2.PointFromLatLng(s2.LatLngFromDegrees(repr.Y(), repr.X())),
}
case *geom.LineString:
latLngs := make([]s2.LatLng, repr.NumCoords())
for i := 0; i < repr.NumCoords(); i++ {
p := repr.Coord(i)
latLngs[i] = s2.LatLngFromDegrees(p.Y(), p.X())
}
regions = []s2.Region{
s2.PolylineFromLatLngs(latLngs),
}
case *geom.Polygon:
loops := make([]*s2.Loop, repr.NumLinearRings())
// All loops must be oriented CCW for S2.
for ringIdx := 0; ringIdx < repr.NumLinearRings(); ringIdx++ {
linearRing := repr.LinearRing(ringIdx)
points := make([]s2.Point, linearRing.NumCoords())
isCCW := IsLinearRingCCW(linearRing)
for pointIdx := 0; pointIdx < linearRing.NumCoords(); pointIdx++ {
p := linearRing.Coord(pointIdx)
pt := s2.PointFromLatLng(s2.LatLngFromDegrees(p.Y(), p.X()))
if isCCW {
points[pointIdx] = pt
} else {
points[len(points)-pointIdx-1] = pt
}
}
loops[ringIdx] = s2.LoopFromPoints(points)
}
regions = []s2.Region{
s2.PolygonFromLoops(loops),
}
case *geom.GeometryCollection:
for _, geom := range repr.Geoms() {
subRegions, err := S2RegionsFromGeomT(geom, emptyBehavior)
if err != nil {
return nil, err
}
regions = append(regions, subRegions...)
}
case *geom.MultiPoint:
for i := 0; i < repr.NumPoints(); i++ {
subRegions, err := S2RegionsFromGeomT(repr.Point(i), emptyBehavior)
if err != nil {
return nil, err
}
regions = append(regions, subRegions...)
}
case *geom.MultiLineString:
for i := 0; i < repr.NumLineStrings(); i++ {
subRegions, err := S2RegionsFromGeomT(repr.LineString(i), emptyBehavior)
if err != nil {
return nil, err
}
regions = append(regions, subRegions...)
}
case *geom.MultiPolygon:
for i := 0; i < repr.NumPolygons(); i++ {
subRegions, err := S2RegionsFromGeomT(repr.Polygon(i), emptyBehavior)
if err != nil {
return nil, err
}
regions = append(regions, subRegions...)
}
}
return regions, nil
}
// normalizeLngLat normalizes geographical coordinates into a valid range.
func normalizeLngLat(lng float64, lat float64) (float64, float64) {
if lat > 90 || lat < -90 {
lat = NormalizeLatitudeDegrees(lat)
}
if lng > 180 || lng < -180 {
lng = NormalizeLongitudeDegrees(lng)
}
return lng, lat
}
// normalizeGeographyGeomT limits geography coordinates to spherical coordinates
// by converting geom.T cordinates inplace
func normalizeGeographyGeomT(t geom.T) {
switch repr := t.(type) {
case *geom.GeometryCollection:
for _, geom := range repr.Geoms() {
normalizeGeographyGeomT(geom)
}
default:
coords := repr.FlatCoords()
for i := 0; i < len(coords); i += repr.Stride() {
coords[i], coords[i+1] = normalizeLngLat(coords[i], coords[i+1])
}
}
}
// validateGeomT validates the geom.T object across valid geom.T objects,
// returning an error if it is invalid.
func validateGeomT(t geom.T) error {
if t.Empty() {
return nil
}
switch t := t.(type) {
case *geom.Point:
case *geom.LineString:
if t.NumCoords() < 2 {
return errors.Newf("LineString must have at least 2 coordinates")
}
case *geom.Polygon:
for i := 0; i < t.NumLinearRings(); i++ {
linearRing := t.LinearRing(i)
if linearRing.NumCoords() < 4 {
return errors.Newf("Polygon LinearRing must have at least 4 points, found %d at position %d", linearRing.NumCoords(), i+1)
}
if !linearRing.Coord(0).Equal(linearRing.Layout(), linearRing.Coord(linearRing.NumCoords()-1)) {
return errors.Newf("Polygon LinearRing at position %d is not closed", i+1)
}
}
case *geom.MultiPoint:
case *geom.MultiLineString:
for i := 0; i < t.NumLineStrings(); i++ {
if err := validateGeomT(t.LineString(i)); err != nil {
return errors.Wrapf(err, "invalid MultiLineString component at position %d", i+1)
}
}
case *geom.MultiPolygon:
for i := 0; i < t.NumPolygons(); i++ {
if err := validateGeomT(t.Polygon(i)); err != nil {
return errors.Wrapf(err, "invalid MultiPolygon component at position %d", i+1)
}
}
case *geom.GeometryCollection:
for i := 0; i < t.NumGeoms(); i++ {
if err := validateGeomT(t.Geom(i)); err != nil {
return errors.Wrapf(err, "invalid GeometryCollection component at position %d", i+1)
}
}
default:
return errors.Newf("unknown geom.T type: %T", t)
}
return nil
}
// spatialObjectFromGeomT creates a geopb.SpatialObject from a geom.T.
func spatialObjectFromGeomT(t geom.T, soType geopb.SpatialObjectType) (geopb.SpatialObject, error) {
if err := validateGeomT(t); err != nil {
return geopb.SpatialObject{}, err
}
if soType == geopb.SpatialObjectType_GeographyType {
normalizeGeographyGeomT(t)
}
ret, err := ewkb.Marshal(t, DefaultEWKBEncodingFormat)
if err != nil {
return geopb.SpatialObject{}, err
}
shapeType, err := shapeTypeFromGeomT(t)
if err != nil {
return geopb.SpatialObject{}, err
}
switch t.Layout() {
case geom.XY:
case geom.NoLayout:
if gc, ok := t.(*geom.GeometryCollection); !ok || !gc.Empty() {
return geopb.SpatialObject{}, errors.Newf("no layout found on object")
}
default:
return geopb.SpatialObject{}, errors.Newf("only 2D objects are currently supported")
}
bbox, err := boundingBoxFromGeomT(t, soType)
if err != nil {
return geopb.SpatialObject{}, err
}
return geopb.SpatialObject{
Type: soType,
EWKB: geopb.EWKB(ret),
SRID: geopb.SRID(t.SRID()),
ShapeType: shapeType,
BoundingBox: bbox,
}, nil
}
func shapeTypeFromGeomT(t geom.T) (geopb.ShapeType, error) {
switch t := t.(type) {
case *geom.Point:
return geopb.ShapeType_Point, nil
case *geom.LineString:
return geopb.ShapeType_LineString, nil
case *geom.Polygon:
return geopb.ShapeType_Polygon, nil
case *geom.MultiPoint:
return geopb.ShapeType_MultiPoint, nil
case *geom.MultiLineString:
return geopb.ShapeType_MultiLineString, nil
case *geom.MultiPolygon:
return geopb.ShapeType_MultiPolygon, nil
case *geom.GeometryCollection:
return geopb.ShapeType_GeometryCollection, nil
default:
return geopb.ShapeType_Unset, errors.Newf("unknown shape: %T", t)
}
}
// GeomTContainsEmpty returns whether a geom.T contains any empty element.
func GeomTContainsEmpty(g geom.T) bool {
if g.Empty() {
return true
}
switch g := g.(type) {
case *geom.MultiPoint:
for i := 0; i < g.NumPoints(); i++ {
if g.Point(i).Empty() {
return true
}
}
case *geom.MultiLineString:
for i := 0; i < g.NumLineStrings(); i++ {
if g.LineString(i).Empty() {
return true
}
}
case *geom.MultiPolygon:
for i := 0; i < g.NumPolygons(); i++ {
if g.Polygon(i).Empty() {
return true
}
}
case *geom.GeometryCollection:
for i := 0; i < g.NumGeoms(); i++ {
if GeomTContainsEmpty(g.Geom(i)) {
return true
}
}
}
return false
}
// compareSpatialObjectBytes compares the SpatialObject if they were serialized.
// This is used for comparison operations, and must be kept consistent with the indexing
// encoding.
func compareSpatialObjectBytes(lhs geopb.SpatialObject, rhs geopb.SpatialObject) int {
marshalledLHS, err := protoutil.Marshal(&lhs)
if err != nil {
panic(err)
}
marshalledRHS, err := protoutil.Marshal(&rhs)
if err != nil {
panic(err)
}
return bytes.Compare(marshalledLHS, marshalledRHS)
}