/
geos.go
1130 lines (1038 loc) · 32.3 KB
/
geos.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
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 geos is a wrapper around the spatial data types between the geo
// package and the GEOS C library. The GEOS library is dynamically loaded
// at init time.
// Operations will error if the GEOS library was not found.
package geos
import (
"fmt"
"os"
"path"
"path/filepath"
"runtime"
"sync"
"unsafe"
"github.com/cockroachdb/cockroach/pkg/build/bazel"
"github.com/cockroachdb/cockroach/pkg/docs"
"github.com/cockroachdb/cockroach/pkg/geo/geopb"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/errors"
)
// #cgo CXXFLAGS: -std=c++14
// #cgo !windows LDFLAGS: -ldl -lm
//
// #include "geos.h"
import "C"
// EnsureInitErrorDisplay is used to control the error message displayed by
// EnsureInit.
type EnsureInitErrorDisplay int
const (
// EnsureInitErrorDisplayPrivate displays the full error message, including
// path info. It is intended for log messages.
EnsureInitErrorDisplayPrivate EnsureInitErrorDisplay = iota
// EnsureInitErrorDisplayPublic displays a redacted error message, excluding
// path info. It is intended for errors to display for the client.
EnsureInitErrorDisplayPublic
)
// maxArrayLen is the maximum safe length for this architecture.
const maxArrayLen = 1<<31 - 1
// geosOnce contains the global instance of CR_GEOS, to be initialized
// during at a maximum of once.
// If it has failed to open, the error will be populated in "err".
// This should only be touched by "fetchGEOSOrError".
var geosOnce struct {
geos *C.CR_GEOS
loc string
err error
once sync.Once
}
// PreparedGeometry is an instance of a GEOS PreparedGeometry.
type PreparedGeometry *C.CR_GEOS_PreparedGeometry
// EnsureInit attempts to start GEOS if it has not been opened already
// and returns the location if found, and an error if the CR_GEOS is not valid.
func EnsureInit(
errDisplay EnsureInitErrorDisplay, flagLibraryDirectoryValue string,
) (string, error) {
crdbBinaryLoc := ""
if len(os.Args) > 0 {
crdbBinaryLoc = os.Args[0]
}
_, err := ensureInit(errDisplay, flagLibraryDirectoryValue, crdbBinaryLoc)
return geosOnce.loc, err
}
// ensureInitInternal ensures initialization has been done, always displaying
// errors privately and not assuming a flag has been set if initialized
// for the first time.
func ensureInitInternal() (*C.CR_GEOS, error) {
return ensureInit(EnsureInitErrorDisplayPrivate, "", "")
}
// ensureInits behaves as described in EnsureInit, but also returns the GEOS
// C object which should be hidden from the public eye.
func ensureInit(
errDisplay EnsureInitErrorDisplay, flagLibraryDirectoryValue string, crdbBinaryLoc string,
) (*C.CR_GEOS, error) {
geosOnce.once.Do(func() {
geosOnce.geos, geosOnce.loc, geosOnce.err = initGEOS(
findLibraryDirectories(flagLibraryDirectoryValue, crdbBinaryLoc),
)
})
if geosOnce.err != nil && errDisplay == EnsureInitErrorDisplayPublic {
return nil, pgerror.Newf(pgcode.System, "geos: this operation is not available")
}
return geosOnce.geos, geosOnce.err
}
// appendLibraryExt appends the extension expected for the running OS.
func getLibraryExt(base string) string {
switch runtime.GOOS {
case "darwin":
return base + ".dylib"
case "windows":
return base + ".dll"
default:
return base + ".so"
}
}
const (
libgeosFileName = "libgeos"
libgeoscFileName = "libgeos_c"
)
// findLibraryDirectories returns the default locations where GEOS is installed.
func findLibraryDirectories(flagLibraryDirectoryValue string, crdbBinaryLoc string) []string {
// Try path by trying to find all parenting paths and appending
// `lib/libgeos_c.<ext>` to the current working directory, as well
// as the directory in which the cockroach binary is initialized.
locs := []string{}
if flagLibraryDirectoryValue != "" {
locs = append(locs, flagLibraryDirectoryValue)
}
// Account for the libraries to be in a bazel runfile path.
if bazel.BuiltWithBazel() {
pathsToCheck := []string{
path.Join("c-deps", "libgeos_foreign", "lib"),
path.Join("external", "archived_cdep_libgeos_linux", "lib"),
path.Join("external", "archived_cdep_libgeos_linuxarm", "lib"),
path.Join("external", "archived_cdep_libgeos_macos", "lib"),
path.Join("external", "archived_cdep_libgeos_macosarm", "lib"),
path.Join("external", "archived_cdep_libgeos_windows", "bin"),
}
for _, path := range pathsToCheck {
if p, err := bazel.Runfile(path); err == nil {
locs = append(locs, p)
}
}
}
locs = append(
locs,
findLibraryDirectoriesInParentingDirectories(crdbBinaryLoc)...,
)
cwd, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "warning: cannot retrieve cwd: %v", err)
} else {
locs = append(
locs,
findLibraryDirectoriesInParentingDirectories(cwd)...,
)
}
return locs
}
// findLibraryDirectoriesInParentingDirectories attempts to find GEOS by looking at
// parenting folders and looking inside `lib/libgeos_c.*`.
// This is basically only useful for CI runs.
func findLibraryDirectoriesInParentingDirectories(dir string) []string {
locs := []string{}
for {
checkDir := filepath.Join(dir, "lib")
found := true
for _, file := range []string{
filepath.Join(checkDir, getLibraryExt(libgeoscFileName)),
filepath.Join(checkDir, getLibraryExt(libgeosFileName)),
} {
if _, err := os.Stat(file); err != nil {
found = false
break
}
}
if found {
locs = append(locs, checkDir)
}
parentDir := filepath.Dir(dir)
if parentDir == dir {
break
}
dir = parentDir
}
return locs
}
// initGEOS initializes the CR_GEOS by attempting to dlopen all
// the paths as parsed in by locs.
func initGEOS(dirs []string) (*C.CR_GEOS, string, error) {
var err error
for _, dir := range dirs {
var ret *C.CR_GEOS
newErr := statusToError(
C.CR_GEOS_Init(
goToCSlice([]byte(filepath.Join(dir, getLibraryExt(libgeoscFileName)))),
goToCSlice([]byte(filepath.Join(dir, getLibraryExt(libgeosFileName)))),
&ret,
),
)
if newErr == nil {
return ret, dir, nil
}
err = errors.CombineErrors(
err,
errors.Wrapf(
newErr,
"geos: cannot load GEOS from dir %q",
dir,
),
)
}
if err != nil {
return nil, "", wrapGEOSInitError(errors.Wrap(err, "geos: error during GEOS init"))
}
return nil, "", wrapGEOSInitError(errors.Newf("geos: no locations to init GEOS"))
}
func wrapGEOSInitError(err error) error {
page := "linux"
switch runtime.GOOS {
case "darwin":
page = "mac"
case "windows":
page = "windows"
}
return pgerror.WithCandidateCode(
errors.WithHintf(
err,
"Ensure you have the spatial libraries installed as per the instructions in %s",
docs.URL("install-cockroachdb-"+page),
),
pgcode.ConfigFile,
)
}
// goToCSlice returns a CR_GEOS_Slice from a given Go byte slice.
func goToCSlice(b []byte) C.CR_GEOS_Slice {
if len(b) == 0 {
return C.CR_GEOS_Slice{data: nil, len: 0}
}
return C.CR_GEOS_Slice{
data: (*C.char)(unsafe.Pointer(&b[0])),
len: C.size_t(len(b)),
}
}
// cStringToUnsafeGoBytes convert a CR_GEOS_String to a Go
// byte slice that refer to the underlying C memory.
func cStringToUnsafeGoBytes(s C.CR_GEOS_String) []byte {
return cToUnsafeGoBytes(s.data, s.len)
}
func cToUnsafeGoBytes(data *C.char, len C.size_t) []byte {
if data == nil {
return nil
}
// Interpret the C pointer as a pointer to a Go array, then slice.
return (*[maxArrayLen]byte)(unsafe.Pointer(data))[:len:len]
}
// cStringToSafeGoBytes converts a CR_GEOS_String to a Go byte slice.
// Additionally, it frees the C memory.
func cStringToSafeGoBytes(s C.CR_GEOS_String) []byte {
unsafeBytes := cStringToUnsafeGoBytes(s)
b := make([]byte, len(unsafeBytes))
copy(b, unsafeBytes)
C.free(unsafe.Pointer(s.data))
return b
}
// A Error wraps an error returned from a GEOS operation.
type Error struct {
msg string
}
// Error implements the error interface.
func (err *Error) Error() string {
return err.msg
}
func statusToError(s C.CR_GEOS_Status) error {
if s.data == nil {
return nil
}
return &Error{msg: string(cStringToSafeGoBytes(s))}
}
// Version returns the GEOS version used.
func Version() (string, error) {
g, err := ensureInitInternal()
if err != nil {
return "", err
}
var version C.CR_GEOS_String
C.CR_GEOS_Version(g, &version)
// Returns a `const char*`, so we don't have to free anything.
return string(cStringToUnsafeGoBytes(version)), nil
}
// BufferParamsJoinStyle maps to the GEOSBufJoinStyles enum in geos_c.h.in.
type BufferParamsJoinStyle int
// These should be kept in sync with the geos_c.h.in corresponding enum definition.
const (
BufferParamsJoinStyleRound = 1
BufferParamsJoinStyleMitre = 2
BufferParamsJoinStyleBevel = 3
)
// BufferParamsEndCapStyle maps to the GEOSBufCapStyles enum in geos_c.h.in.
type BufferParamsEndCapStyle int
// These should be kept in sync with the geos_c.h.in corresponding enum definition.
const (
BufferParamsEndCapStyleRound = 1
BufferParamsEndCapStyleFlat = 2
BufferParamsEndCapStyleSquare = 3
)
// BufferParams are parameters to provide into the GEOS buffer function.
type BufferParams struct {
JoinStyle BufferParamsJoinStyle
EndCapStyle BufferParamsEndCapStyle
SingleSided bool
QuadrantSegments int
MitreLimit float64
}
// Buffer buffers the given geometry by the given distance and params.
func Buffer(ewkb geopb.EWKB, params BufferParams, distance float64) (geopb.EWKB, error) {
g, err := ensureInitInternal()
if err != nil {
return nil, err
}
singleSided := 0
if params.SingleSided {
singleSided = 1
}
cParams := C.CR_GEOS_BufferParamsInput{
endCapStyle: C.int(params.EndCapStyle),
joinStyle: C.int(params.JoinStyle),
singleSided: C.int(singleSided),
quadrantSegments: C.int(params.QuadrantSegments),
mitreLimit: C.double(params.MitreLimit),
}
var cEWKB C.CR_GEOS_String
if err := statusToError(C.CR_GEOS_Buffer(g, goToCSlice(ewkb), cParams, C.double(distance), &cEWKB)); err != nil {
return nil, err
}
return cStringToSafeGoBytes(cEWKB), nil
}
// Area returns the area of an EWKB.
func Area(ewkb geopb.EWKB) (float64, error) {
g, err := ensureInitInternal()
if err != nil {
return 0, err
}
var area C.double
if err := statusToError(C.CR_GEOS_Area(g, goToCSlice(ewkb), &area)); err != nil {
return 0, err
}
return float64(area), nil
}
// Boundary returns the boundary of an EWKB.
func Boundary(ewkb geopb.EWKB) (geopb.EWKB, error) {
g, err := ensureInitInternal()
if err != nil {
return nil, err
}
var cEWKB C.CR_GEOS_String
if err := statusToError(C.CR_GEOS_Boundary(g, goToCSlice(ewkb), &cEWKB)); err != nil {
return nil, err
}
return cStringToSafeGoBytes(cEWKB), nil
}
// Difference returns the difference between two EWKB.
func Difference(ewkb1 geopb.EWKB, ewkb2 geopb.EWKB) (geopb.EWKB, error) {
g, err := ensureInitInternal()
if err != nil {
return nil, err
}
var diffEWKB C.CR_GEOS_String
if err := statusToError(C.CR_GEOS_Difference(g, goToCSlice(ewkb1), goToCSlice(ewkb2), &diffEWKB)); err != nil {
return nil, err
}
return cStringToSafeGoBytes(diffEWKB), nil
}
// Length returns the length of an EWKB.
func Length(ewkb geopb.EWKB) (float64, error) {
g, err := ensureInitInternal()
if err != nil {
return 0, err
}
var length C.double
if err := statusToError(C.CR_GEOS_Length(g, goToCSlice(ewkb), &length)); err != nil {
return 0, err
}
return float64(length), nil
}
// Normalize returns the geometry in its normalized form.
func Normalize(a geopb.EWKB) (geopb.EWKB, error) {
g, err := ensureInitInternal()
if err != nil {
return nil, err
}
var cEWKB C.CR_GEOS_String
if err := statusToError(C.CR_GEOS_Normalize(g, goToCSlice(a), &cEWKB)); err != nil {
return nil, err
}
return cStringToSafeGoBytes(cEWKB), nil
}
// LineMerge merges multilinestring constituents.
func LineMerge(a geopb.EWKB) (geopb.EWKB, error) {
g, err := ensureInitInternal()
if err != nil {
return nil, err
}
var cEWKB C.CR_GEOS_String
if err := statusToError(C.CR_GEOS_LineMerge(g, goToCSlice(a), &cEWKB)); err != nil {
return nil, err
}
return cStringToSafeGoBytes(cEWKB), nil
}
// IsSimple returns whether the EWKB is simple.
func IsSimple(ewkb geopb.EWKB) (bool, error) {
g, err := ensureInitInternal()
if err != nil {
return false, err
}
var ret C.char
if err := statusToError(C.CR_GEOS_IsSimple(g, goToCSlice(ewkb), &ret)); err != nil {
return false, err
}
return ret == 1, nil
}
// Centroid returns the centroid of an EWKB.
func Centroid(ewkb geopb.EWKB) (geopb.EWKB, error) {
g, err := ensureInitInternal()
if err != nil {
return nil, err
}
var cEWKB C.CR_GEOS_String
if err := statusToError(C.CR_GEOS_Centroid(g, goToCSlice(ewkb), &cEWKB)); err != nil {
return nil, err
}
return cStringToSafeGoBytes(cEWKB), nil
}
// MinimumBoundingCircle returns minimum bounding circle of an EWKB
func MinimumBoundingCircle(ewkb geopb.EWKB) (geopb.EWKB, geopb.EWKB, float64, error) {
g, err := ensureInitInternal()
if err != nil {
return nil, nil, 0, err
}
var centerEWKB C.CR_GEOS_String
var polygonEWKB C.CR_GEOS_String
var radius C.double
if err := statusToError(C.CR_GEOS_MinimumBoundingCircle(g, goToCSlice(ewkb), &radius, ¢erEWKB, &polygonEWKB)); err != nil {
return nil, nil, 0, err
}
return cStringToSafeGoBytes(polygonEWKB), cStringToSafeGoBytes(centerEWKB), float64(radius), nil
}
// ConvexHull returns an EWKB which returns the convex hull of the given EWKB.
func ConvexHull(ewkb geopb.EWKB) (geopb.EWKB, error) {
g, err := ensureInitInternal()
if err != nil {
return nil, err
}
var cEWKB C.CR_GEOS_String
if err := statusToError(C.CR_GEOS_ConvexHull(g, goToCSlice(ewkb), &cEWKB)); err != nil {
return nil, err
}
return cStringToSafeGoBytes(cEWKB), nil
}
// Simplify returns an EWKB which returns the simplified EWKB.
func Simplify(ewkb geopb.EWKB, tolerance float64) (geopb.EWKB, error) {
g, err := ensureInitInternal()
if err != nil {
return nil, err
}
var cEWKB C.CR_GEOS_String
if err := statusToError(
C.CR_GEOS_Simplify(g, goToCSlice(ewkb), &cEWKB, C.double(tolerance)),
); err != nil {
return nil, err
}
return cStringToSafeGoBytes(cEWKB), nil
}
// TopologyPreserveSimplify returns an EWKB which returns the simplified EWKB
// with the topology preserved.
func TopologyPreserveSimplify(ewkb geopb.EWKB, tolerance float64) (geopb.EWKB, error) {
g, err := ensureInitInternal()
if err != nil {
return nil, err
}
var cEWKB C.CR_GEOS_String
if err := statusToError(
C.CR_GEOS_TopologyPreserveSimplify(g, goToCSlice(ewkb), &cEWKB, C.double(tolerance)),
); err != nil {
return nil, err
}
return cStringToSafeGoBytes(cEWKB), nil
}
// PointOnSurface returns an EWKB with a point that is on the surface of the given EWKB.
func PointOnSurface(ewkb geopb.EWKB) (geopb.EWKB, error) {
g, err := ensureInitInternal()
if err != nil {
return nil, err
}
var cEWKB C.CR_GEOS_String
if err := statusToError(C.CR_GEOS_PointOnSurface(g, goToCSlice(ewkb), &cEWKB)); err != nil {
return nil, err
}
return cStringToSafeGoBytes(cEWKB), nil
}
// Intersection returns an EWKB which contains the geometries of intersection between A and B.
func Intersection(a geopb.EWKB, b geopb.EWKB) (geopb.EWKB, error) {
g, err := ensureInitInternal()
if err != nil {
return nil, err
}
var cEWKB C.CR_GEOS_String
if err := statusToError(C.CR_GEOS_Intersection(g, goToCSlice(a), goToCSlice(b), &cEWKB)); err != nil {
return nil, err
}
return cStringToSafeGoBytes(cEWKB), nil
}
// UnaryUnion Returns an EWKB which is a union of input geometry components.
func UnaryUnion(a geopb.EWKB) (geopb.EWKB, error) {
g, err := ensureInitInternal()
if err != nil {
return nil, err
}
var unionEWKB C.CR_GEOS_String
if err := statusToError(C.CR_GEOS_UnaryUnion(g, goToCSlice(a), &unionEWKB)); err != nil {
return nil, err
}
return cStringToSafeGoBytes(unionEWKB), nil
}
// Union returns an EWKB which is a union of shapes A and B.
func Union(a geopb.EWKB, b geopb.EWKB) (geopb.EWKB, error) {
g, err := ensureInitInternal()
if err != nil {
return nil, err
}
var cEWKB C.CR_GEOS_String
if err := statusToError(C.CR_GEOS_Union(g, goToCSlice(a), goToCSlice(b), &cEWKB)); err != nil {
return nil, err
}
return cStringToSafeGoBytes(cEWKB), nil
}
// SymDifference returns an EWKB which is the symmetric difference of shapes A and B.
func SymDifference(a geopb.EWKB, b geopb.EWKB) (geopb.EWKB, error) {
g, err := ensureInitInternal()
if err != nil {
return nil, err
}
var cEWKB C.CR_GEOS_String
if err := statusToError(C.CR_GEOS_SymDifference(g, goToCSlice(a), goToCSlice(b), &cEWKB)); err != nil {
return nil, err
}
return cStringToSafeGoBytes(cEWKB), nil
}
// InterpolateLine returns the point along the given LineString which is at
// a given distance from starting point.
// Note: For distance less than 0 it returns start point similarly for distance
// greater LineString's length.
// InterpolateLine also works with (Multi)LineString. However, the result is
// not appropriate as it combines all the LineString present in (MULTI)LineString,
// considering all the corner points of LineString overlaps each other.
func InterpolateLine(ewkb geopb.EWKB, distance float64) (geopb.EWKB, error) {
g, err := ensureInitInternal()
if err != nil {
return nil, err
}
var cEWKB C.CR_GEOS_String
if err := statusToError(C.CR_GEOS_Interpolate(g, goToCSlice(ewkb), C.double(distance), &cEWKB)); err != nil {
return nil, err
}
return cStringToSafeGoBytes(cEWKB), nil
}
// MinDistance returns the minimum distance between two EWKBs.
func MinDistance(a geopb.EWKB, b geopb.EWKB) (float64, error) {
g, err := ensureInitInternal()
if err != nil {
return 0, err
}
var distance C.double
if err := statusToError(C.CR_GEOS_Distance(g, goToCSlice(a), goToCSlice(b), &distance)); err != nil {
return 0, err
}
return float64(distance), nil
}
// MinimumClearance returns the minimum distance a vertex can move to result in an
// invalid geometry.
func MinimumClearance(ewkb geopb.EWKB) (float64, error) {
g, err := ensureInitInternal()
if err != nil {
return 0, err
}
var distance C.double
if err := statusToError(C.CR_GEOS_MinimumClearance(g, goToCSlice(ewkb), &distance)); err != nil {
return 0, err
}
return float64(distance), nil
}
// MinimumClearanceLine returns the line spanning the minimum clearance a vertex can
// move before producing an invalid geometry.
func MinimumClearanceLine(ewkb geopb.EWKB) (geopb.EWKB, error) {
g, err := ensureInitInternal()
if err != nil {
return nil, err
}
var clearanceEWKB C.CR_GEOS_String
if err := statusToError(C.CR_GEOS_MinimumClearanceLine(g, goToCSlice(ewkb), &clearanceEWKB)); err != nil {
return nil, err
}
return cStringToSafeGoBytes(clearanceEWKB), nil
}
// ClipByRect clips a EWKB to the specified rectangle.
func ClipByRect(
ewkb geopb.EWKB, xMin float64, yMin float64, xMax float64, yMax float64,
) (geopb.EWKB, error) {
g, err := ensureInitInternal()
if err != nil {
return nil, err
}
var cEWKB C.CR_GEOS_String
if err := statusToError(C.CR_GEOS_ClipByRect(g, goToCSlice(ewkb), C.double(xMin),
C.double(yMin), C.double(xMax), C.double(yMax), &cEWKB)); err != nil {
return nil, err
}
return cStringToSafeGoBytes(cEWKB), nil
}
//
// PreparedGeometry
//
// PrepareGeometry prepares a geometry in GEOS.
func PrepareGeometry(a geopb.EWKB) (PreparedGeometry, error) {
g, err := ensureInitInternal()
if err != nil {
return nil, err
}
var ret *C.CR_GEOS_PreparedGeometry
if err := statusToError(C.CR_GEOS_Prepare(g, goToCSlice(a), &ret)); err != nil {
return nil, err
}
return PreparedGeometry(ret), nil
}
// PreparedGeomDestroy destroys a prepared geometry.
func PreparedGeomDestroy(a PreparedGeometry) {
g, err := ensureInitInternal()
if err != nil {
panic(errors.NewAssertionErrorWithWrappedErrf(err, "trying to destroy PreparedGeometry with no GEOS"))
}
ap := (*C.CR_GEOS_PreparedGeometry)(unsafe.Pointer(a))
if err := statusToError(C.CR_GEOS_PreparedGeometryDestroy(g, ap)); err != nil {
panic(errors.NewAssertionErrorWithWrappedErrf(err, "PreparedGeometryDestroy returned an error"))
}
}
//
// Binary predicates.
//
// Covers returns whether the EWKB provided by A covers the EWKB provided by B.
func Covers(a geopb.EWKB, b geopb.EWKB) (bool, error) {
g, err := ensureInitInternal()
if err != nil {
return false, err
}
var ret C.char
if err := statusToError(C.CR_GEOS_Covers(g, goToCSlice(a), goToCSlice(b), &ret)); err != nil {
return false, err
}
return ret == 1, nil
}
// CoveredBy returns whether the EWKB provided by A is covered by the EWKB provided by B.
func CoveredBy(a geopb.EWKB, b geopb.EWKB) (bool, error) {
g, err := ensureInitInternal()
if err != nil {
return false, err
}
var ret C.char
if err := statusToError(C.CR_GEOS_CoveredBy(g, goToCSlice(a), goToCSlice(b), &ret)); err != nil {
return false, err
}
return ret == 1, nil
}
// Contains returns whether the EWKB provided by A contains the EWKB provided by B.
func Contains(a geopb.EWKB, b geopb.EWKB) (bool, error) {
g, err := ensureInitInternal()
if err != nil {
return false, err
}
var ret C.char
if err := statusToError(C.CR_GEOS_Contains(g, goToCSlice(a), goToCSlice(b), &ret)); err != nil {
return false, err
}
return ret == 1, nil
}
// Crosses returns whether the EWKB provided by A crosses the EWKB provided by B.
func Crosses(a geopb.EWKB, b geopb.EWKB) (bool, error) {
g, err := ensureInitInternal()
if err != nil {
return false, err
}
var ret C.char
if err := statusToError(C.CR_GEOS_Crosses(g, goToCSlice(a), goToCSlice(b), &ret)); err != nil {
return false, err
}
return ret == 1, nil
}
// Disjoint returns whether the EWKB provided by A is disjoint from the EWKB provided by B.
func Disjoint(a geopb.EWKB, b geopb.EWKB) (bool, error) {
g, err := ensureInitInternal()
if err != nil {
return false, err
}
var ret C.char
if err := statusToError(C.CR_GEOS_Disjoint(g, goToCSlice(a), goToCSlice(b), &ret)); err != nil {
return false, err
}
return ret == 1, nil
}
// Equals returns whether the EWKB provided by A equals the EWKB provided by B.
func Equals(a geopb.EWKB, b geopb.EWKB) (bool, error) {
g, err := ensureInitInternal()
if err != nil {
return false, err
}
var ret C.char
if err := statusToError(C.CR_GEOS_Equals(g, goToCSlice(a), goToCSlice(b), &ret)); err != nil {
return false, err
}
return ret == 1, nil
}
// PreparedIntersects returns whether the EWKB provided by A intersects the EWKB provided by B.
func PreparedIntersects(a PreparedGeometry, b geopb.EWKB) (bool, error) {
// Double check - since PreparedGeometry is actually a pointer to C type.
if a == nil {
return false, errors.New("provided PreparedGeometry is nil")
}
g, err := ensureInitInternal()
if err != nil {
return false, err
}
var ret C.char
ap := (*C.CR_GEOS_PreparedGeometry)(unsafe.Pointer(a))
if err := statusToError(C.CR_GEOS_PreparedIntersects(g, ap, goToCSlice(b), &ret)); err != nil {
return false, err
}
return ret == 1, nil
}
// Intersects returns whether the EWKB provided by A intersects the EWKB provided by B.
func Intersects(a geopb.EWKB, b geopb.EWKB) (bool, error) {
g, err := ensureInitInternal()
if err != nil {
return false, err
}
var ret C.char
if err := statusToError(C.CR_GEOS_Intersects(g, goToCSlice(a), goToCSlice(b), &ret)); err != nil {
return false, err
}
return ret == 1, nil
}
// Overlaps returns whether the EWKB provided by A overlaps the EWKB provided by B.
func Overlaps(a geopb.EWKB, b geopb.EWKB) (bool, error) {
g, err := ensureInitInternal()
if err != nil {
return false, err
}
var ret C.char
if err := statusToError(C.CR_GEOS_Overlaps(g, goToCSlice(a), goToCSlice(b), &ret)); err != nil {
return false, err
}
return ret == 1, nil
}
// Touches returns whether the EWKB provided by A touches the EWKB provided by B.
func Touches(a geopb.EWKB, b geopb.EWKB) (bool, error) {
g, err := ensureInitInternal()
if err != nil {
return false, err
}
var ret C.char
if err := statusToError(C.CR_GEOS_Touches(g, goToCSlice(a), goToCSlice(b), &ret)); err != nil {
return false, err
}
return ret == 1, nil
}
// Within returns whether the EWKB provided by A is within the EWKB provided by B.
func Within(a geopb.EWKB, b geopb.EWKB) (bool, error) {
g, err := ensureInitInternal()
if err != nil {
return false, err
}
var ret C.char
if err := statusToError(C.CR_GEOS_Within(g, goToCSlice(a), goToCSlice(b), &ret)); err != nil {
return false, err
}
return ret == 1, nil
}
// FrechetDistance returns the Frechet distance between the geometries.
func FrechetDistance(a, b geopb.EWKB) (float64, error) {
g, err := ensureInitInternal()
if err != nil {
return 0, err
}
var distance C.double
if err := statusToError(
C.CR_GEOS_FrechetDistance(g, goToCSlice(a), goToCSlice(b), &distance),
); err != nil {
return 0, err
}
return float64(distance), nil
}
// FrechetDistanceDensify returns the Frechet distance between the geometries.
func FrechetDistanceDensify(a, b geopb.EWKB, densifyFrac float64) (float64, error) {
g, err := ensureInitInternal()
if err != nil {
return 0, err
}
var distance C.double
if err := statusToError(
C.CR_GEOS_FrechetDistanceDensify(g, goToCSlice(a), goToCSlice(b), C.double(densifyFrac), &distance),
); err != nil {
return 0, err
}
return float64(distance), nil
}
// HausdorffDistance returns the Hausdorff distance between the geometries.
func HausdorffDistance(a, b geopb.EWKB) (float64, error) {
g, err := ensureInitInternal()
if err != nil {
return 0, err
}
var distance C.double
if err := statusToError(
C.CR_GEOS_HausdorffDistance(g, goToCSlice(a), goToCSlice(b), &distance),
); err != nil {
return 0, err
}
return float64(distance), nil
}
// HausdorffDistanceDensify returns the Hausdorff distance between the geometries.
func HausdorffDistanceDensify(a, b geopb.EWKB, densifyFrac float64) (float64, error) {
g, err := ensureInitInternal()
if err != nil {
return 0, err
}
var distance C.double
if err := statusToError(
C.CR_GEOS_HausdorffDistanceDensify(g, goToCSlice(a), goToCSlice(b), C.double(densifyFrac), &distance),
); err != nil {
return 0, err
}
return float64(distance), nil
}
// EqualsExact returns whether two geometry objects are equal with some epsilon
func EqualsExact(lhs, rhs geopb.EWKB, epsilon float64) (bool, error) {
g, err := ensureInitInternal()
if err != nil {
return false, err
}
var ret C.char
if err := statusToError(
C.CR_GEOS_EqualsExact(g, goToCSlice(lhs), goToCSlice(rhs), C.double(epsilon), &ret),
); err != nil {
return false, err
}
return ret == 1, nil
}
//
// DE-9IM related
//
// Relate returns the DE-9IM relation between A and B.
func Relate(a geopb.EWKB, b geopb.EWKB) (string, error) {
g, err := ensureInitInternal()
if err != nil {
return "", err
}
var ret C.CR_GEOS_String
if err := statusToError(C.CR_GEOS_Relate(g, goToCSlice(a), goToCSlice(b), &ret)); err != nil {
return "", err
}
if ret.data == nil {
return "", errors.Newf("expected DE-9IM string but found nothing")
}
return string(cStringToSafeGoBytes(ret)), nil
}
// RelateBoundaryNodeRule returns the DE-9IM relation between A and B given a boundary node rule.
func RelateBoundaryNodeRule(a geopb.EWKB, b geopb.EWKB, bnr int) (string, error) {
g, err := ensureInitInternal()
if err != nil {
return "", err
}
var ret C.CR_GEOS_String
if err := statusToError(C.CR_GEOS_RelateBoundaryNodeRule(g, goToCSlice(a), goToCSlice(b), C.int(bnr), &ret)); err != nil {
return "", err
}
if ret.data == nil {
return "", errors.Newf("expected DE-9IM string but found nothing")
}
return string(cStringToSafeGoBytes(ret)), nil
}
// RelatePattern whether A and B have a DE-9IM relation matching the given pattern.
func RelatePattern(a geopb.EWKB, b geopb.EWKB, pattern string) (bool, error) {
g, err := ensureInitInternal()
if err != nil {
return false, err
}
var ret C.char
if err := statusToError(
C.CR_GEOS_RelatePattern(g, goToCSlice(a), goToCSlice(b), goToCSlice([]byte(pattern)), &ret),
); err != nil {
return false, err
}
return ret == 1, nil
}
//
// Validity checking.
//
// IsValid returns whether the given geometry is valid.
func IsValid(ewkb geopb.EWKB) (bool, error) {
g, err := ensureInitInternal()
if err != nil {
return false, err
}
var ret C.char
if err := statusToError(
C.CR_GEOS_IsValid(g, goToCSlice(ewkb), &ret),
); err != nil {
return false, err
}
return ret == 1, nil
}
// IsValidReason the reasoning for whether the Geometry is valid or invalid.
func IsValidReason(ewkb geopb.EWKB) (string, error) {
g, err := ensureInitInternal()
if err != nil {
return "", err
}
var ret C.CR_GEOS_String
if err := statusToError(
C.CR_GEOS_IsValidReason(g, goToCSlice(ewkb), &ret),