-
Notifications
You must be signed in to change notification settings - Fork 23
/
geos_functions.jl
1701 lines (1519 loc) · 50.1 KB
/
geos_functions.jl
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
function _readgeom(
wktstring::String,
wktreader::WKTReader,
context::GEOSContext = get_global_context(),
)
result = GEOSWKTReader_read_r(context, wktreader, wktstring)
if result == C_NULL
error("LibGEOS: Error in GEOSWKTReader_read_r while reading $wktstring")
end
result
end
_readgeom(wktstring::String, context::GEOSContext = get_global_context()) =
_readgeom(wktstring, WKTReader(context), context)
function _readgeom(
wkbbuffer::Vector{Cuchar},
wkbreader::WKBReader,
context::GEOSContext = get_global_context(),
)
result = GEOSWKBReader_read_r(context, wkbreader, wkbbuffer, length(wkbbuffer))
if result == C_NULL
error("LibGEOS: Error in GEOSWKBReader_read_r")
end
result
end
_readgeom(wkbbuffer::Vector{Cuchar}, context::GEOSContext = get_global_context()) =
_readgeom(wkbbuffer, WKBReader(context), context)
readgeom(
wktstring::String,
wktreader::WKTReader,
context::GEOSContext = get_global_context(),
) = geomFromGEOS(_readgeom(wktstring, wktreader, context), context)
readgeom(wktstring::String, context::GEOSContext = get_global_context()) =
readgeom(wktstring, WKTReader(context), context)
readgeom(
wkbbuffer::Vector{Cuchar},
wkbreader::WKBReader,
context::GEOSContext = get_global_context(),
) = geomFromGEOS(_readgeom(wkbbuffer, wkbreader, context), context)
readgeom(wkbbuffer::Vector{Cuchar}, context::GEOSContext = get_global_context()) =
readgeom(wkbbuffer, WKBReader(context), context)
function writegeom(
obj::Geometry,
wktwriter::WKTWriter,
context::GEOSContext = get_context(obj),
)
GEOSWKTWriter_write_r(context, wktwriter, obj)
end
function writegeom(
obj::Geometry,
wkbwriter::WKBWriter,
context::GEOSContext = get_context(obj),
)
wkbsize = Ref{Csize_t}()
result = GEOSWKBWriter_write_r(context, wkbwriter, obj, wkbsize)
unsafe_wrap(Array, result, wkbsize[], own = true)
end
writegeom(obj::Geometry, context::GEOSContext = get_context(obj)) =
writegeom(obj, WKTWriter(context), context)
writegeom(obj::PreparedGeometry, context::GEOSContext = get_context(obj)) =
writegeom(obj.ownedby, WKTWriter(context), context)
# -----
# Coordinate Sequence functions
# -----
"""
createCoordSeq(size::Integer; ndim::Integer=2) -> Ptr{Ptr{Void}}
Create a Coordinate sequence with ``size'' coordinates of ``dims'' dimensions (Return NULL on exception)
"""
function createCoordSeq(
size::Integer,
context::GEOSContext = get_global_context();
ndim::Integer = 2,
)
@assert ndim >= 2
result = GEOSCoordSeq_create_r(context, size, ndim)
if result == C_NULL
error("LibGEOS: Error in GEOSCoordSeq_create")
end
result
end
# Clone a Coordinate Sequence (Return NULL on exception)
function cloneCoordSeq(ptr::GEOSCoordSeq, context::GEOSContext = get_global_context())
result = GEOSCoordSeq_clone_r(context, ptr)
if result == C_NULL
error("LibGEOS: Error in GEOSCoordSeq_clone")
end
result
end
function destroyCoordSeq(ptr::GEOSCoordSeq, context::GEOSContext = get_global_context())
result = GEOSCoordSeq_destroy_r(context, ptr)
if result == C_NULL
error("LibGEOS: Error in GEOSCoordSeq_destroy")
end
result
end
# Set ordinate values in a Coordinate Sequence (Return 0 on exception)
function setX!(
ptr::GEOSCoordSeq,
i::Integer,
value::Real,
context::GEOSContext = get_global_context(),
)
if !(0 < i <= getSize(ptr, context))
error(
"LibGEOS: i=$i is out of bounds for CoordSeq with size=$(getSize(ptr, context))",
)
end
result = GEOSCoordSeq_setX_r(context, ptr, i - 1, value)
if result == 0
error("LibGEOS: Error in GEOSCoordSeq_setX")
end
result
end
function setY!(
ptr::GEOSCoordSeq,
i::Integer,
value::Real,
context::GEOSContext = get_global_context(),
)
if !(0 < i <= getSize(ptr, context))
error(
"LibGEOS: i=$i is out of bounds for CoordSeq with size=$(getSize(ptr, context))",
)
end
result = GEOSCoordSeq_setY_r(context, ptr, i - 1, value)
if result == 0
error("LibGEOS: Error in GEOSCoordSeq_setY")
end
result
end
function setZ!(
ptr::GEOSCoordSeq,
i::Integer,
value::Real,
context::GEOSContext = get_global_context(),
)
if !(0 < i <= getSize(ptr, context))
error(
"LibGEOS: i=$i is out of bounds for CoordSeq with size=$(getSize(ptr, context))",
)
end
result = GEOSCoordSeq_setZ_r(context, ptr, i - 1, value)
if result == 0
error("LibGEOS: Error in GEOSCoordSeq_setZ")
end
result
end
"Get size info from a Coordinate Sequence (Return 0 on exception)"
function getSize(ptr::GEOSCoordSeq, context::GEOSContext = get_global_context())
out = Ref{UInt32}()
result = GEOSCoordSeq_getSize_r(context, ptr, out)
if result == 0
error("LibGEOS: Error in GEOSCoordSeq_getSize")
end
Int(out[])
end
"Get dimensions info from a Coordinate Sequence (Return 0 on exception)"
function getDimensions(ptr::GEOSCoordSeq, context::GEOSContext = get_global_context())
out = Ref{UInt32}()
result = GEOSCoordSeq_getDimensions_r(context, ptr, out)
if result == 0
error("LibGEOS: Error in GEOSCoordSeq_getDimensions")
end
Int(out[])
end
# convenience functions
# Use Tuple where possible
function setCoordSeq!(
ptr::GEOSCoordSeq,
i::Integer,
coords::Union{Vector{<:Real},Tuple},
context::GEOSContext = get_global_context(),
)
ndim = length(coords)
@assert ndim >= 2
if !(0 < i <= getSize(ptr, context))
error(
"LibGEOS: i=$i is out of bounds for CoordSeq with size=$(getSize(ptr, context))",
)
end
setX!(ptr, i, Float64(coords[1]), context)
setY!(ptr, i, Float64(coords[2]), context)
ndim >= 3 && setZ!(ptr, i, Float64(coords[3]), context)
ptr
end
"""
createCoordSeq(x::Real, y::Real) -> Ptr{Ptr{Void}}
Create a createCoordSeq of a single 2D coordinate
"""
function createCoordSeq(x::Real, y::Real, context::GEOSContext = get_global_context())
coordinates = createCoordSeq(1, context, ndim = 2)
setX!(coordinates, 1, x, context)
setY!(coordinates, 1, y, context)
coordinates
end
"""
createCoordSeq(x::Real, y::Real, z::Real) -> Ptr{Ptr{Void}}
Create a createCoordSeq of a single 3D coordinate
"""
function createCoordSeq(
x::Real,
y::Real,
z::Real,
context::GEOSContext = get_global_context(),
)
coordinates = createCoordSeq(1, context, ndim = 3)
setX!(coordinates, 1, x, context)
setY!(coordinates, 1, y, context)
setZ!(coordinates, 1, z, context)
coordinates
end
"""
createCoordSeq(coords::Vector{Float64}) -> Ptr{Ptr{Void}}
Create a createCoordSeq of a single N dimensional coordinate
"""
function createCoordSeq(
coords::Vector{Float64},
context::GEOSContext = get_global_context(),
)
ndim = length(coords)
@assert ndim >= 2
coordinates = createCoordSeq(1, context, ndim = ndim)
setCoordSeq!(coordinates, 1, coords, context)
end
"""
createCoordSeq(coords::Vector{Float64}) -> Ptr{Ptr{Void}}
Create a createCoordSeq of a multiple N dimensional coordinate
"""
function createCoordSeq(
coords::Vector{Vector{Float64}},
context::GEOSContext = get_global_context(),
)
ncoords = length(coords)
@assert ncoords > 0
ndim = length(coords[1])
coordinates = createCoordSeq(ncoords, context, ndim = ndim)
for (i, coord) in enumerate(coords)
setCoordSeq!(coordinates, i, coord, context)
end
coordinates
end
function getX(ptr::GEOSCoordSeq, i::Integer, context::GEOSContext = get_global_context())
if !(0 < i <= getSize(ptr, context))
error(
"LibGEOS: i=$i is out of bounds for CoordSeq with size=$(getSize(ptr, context))",
)
end
out = Ref{Float64}()
result = GEOSCoordSeq_getX_r(context, ptr, i - 1, out)
if result == 0
error("LibGEOS: Error in GEOSCoordSeq_getX")
end
out[]
end
function getX(ptr::GEOSCoordSeq, context::GEOSContext = get_global_context())
ncoords = getSize(ptr, context)
xcoords = Vector{Float64}(undef, ncoords)
start = pointer(xcoords)
floatsize = sizeof(Float64)
for i = 0:ncoords-1
GEOSCoordSeq_getX_r(context, ptr, i, start + i * floatsize)
end
xcoords
end
function getY(ptr::GEOSCoordSeq, i::Integer, context::GEOSContext = get_global_context())
if !(0 < i <= getSize(ptr, context))
error(
"LibGEOS: i=$i is out of bounds for CoordSeq with size=$(getSize(ptr, context))",
)
end
out = Ref{Float64}()
result = GEOSCoordSeq_getY_r(context, ptr, i - 1, out)
if result == 0
error("LibGEOS: Error in GEOSCoordSeq_getY")
end
out[]
end
function getY(ptr::GEOSCoordSeq, context::GEOSContext = get_global_context())
ncoords = getSize(ptr, context)
ycoords = Vector{Float64}(undef, ncoords)
start = pointer(ycoords)
floatsize = sizeof(Float64)
for i = 0:ncoords-1
GEOSCoordSeq_getY_r(context, ptr, i, start + i * floatsize)
end
ycoords
end
function getZ(ptr::GEOSCoordSeq, i::Integer, context::GEOSContext = get_global_context())
if !(0 < i <= getSize(ptr, context))
error(
"LibGEOS: i=$i is out of bounds for CoordSeq with size=$(getSize(ptr, context))",
)
end
out = Ref{Float64}()
result = GEOSCoordSeq_getZ_r(context, ptr, i - 1, out)
if result == 0
error("LibGEOS: Error in GEOSCoordSeq_getZ")
end
out[]
end
function getZ(ptr::GEOSCoordSeq, context::GEOSContext = get_global_context())
ncoords = getSize(ptr, context)
zcoords = Array{Float64}(undef, ncoords)
start = pointer(zcoords)
floatsize = sizeof(Float64)
for i = 0:ncoords-1
GEOSCoordSeq_getZ_r(context, ptr, i, start + i * floatsize)
end
zcoords
end
function getCoordinates!(out::Vector{Float64}, ptr::GEOSCoordSeq, i::Integer, context)
if !(0 < i <= getSize(ptr, context))
error(
"LibGEOS: i=$i is out of bounds for CoordSeq with size=$(getSize(ptr, context))",
)
end
ndim = getDimensions(ptr, context)
@assert length(out) == ndim
GC.@preserve out begin
start = pointer(out)
floatsize = sizeof(Float64)
GEOSCoordSeq_getX_r(context, ptr, i - 1, start)
GEOSCoordSeq_getY_r(context, ptr, i - 1, start + floatsize)
if ndim == 3
GEOSCoordSeq_getZ_r(context, ptr, i - 1, start + 2 * floatsize)
end
end
out
end
function getCoordinates(
ptr::GEOSCoordSeq,
i::Integer,
context::GEOSContext = get_global_context(),
)
ndim = getDimensions(ptr, context)
out = Array{Float64}(undef, ndim)
getCoordinates!(out, ptr, i, context)
out
end
function getCoordinates(ptr::GEOSCoordSeq, context::GEOSContext = get_global_context())
ndim = getDimensions(ptr, context)
ncoords = getSize(ptr, context)
coordseq = Vector{Float64}[]
sizehint!(coordseq, ncoords)
for i = 1:ncoords
push!(coordseq, getCoordinates(ptr, i, context))
end
coordseq
end
function isCCW(ptr::GEOSCoordSeq, context::GEOSContext = get_global_context())::Bool
d = [0x02]
GC.@preserve d begin
result = GEOSCoordSeq_isCCW_r(context, ptr, pointer(d))
if result == C_NULL
error("LibGEOS: Error in GEOSInterpolateNormalized")
end
Bool(d[1])
end
end
# -----
# Linear referencing functions -- there are more, but these are probably sufficient for most purposes
# -----
# (GEOSGeometry ownership is retained by caller)
# Return distance of point 'point' projected on 'line' from origin of 'line'. Geometry 'line' must be a lineal geometry
project(line::LineString, point::Point, context::GEOSContext = get_context(line)) =
GEOSProject_r(context, line, point)
# Return closest point to given distance within geometry (Geometry must be a LineString)
function interpolate(line::LineString, dist::Real, context::GEOSContext = get_context(line))
result = GEOSInterpolate_r(context, line, dist)
if result == C_NULL
error("LibGEOS: Error in GEOSInterpolate")
end
Point(result, context)
end
projectNormalized(
line::LineString,
point::Point,
context::GEOSContext = get_context(line),
) = GEOSProjectNormalized_r(context, line, point)
function interpolateNormalized(
line::LineString,
dist::Real,
context::GEOSContext = get_context(line),
)
result = GEOSInterpolateNormalized_r(context, line, dist)
if result == C_NULL
error("LibGEOS: Error in GEOSInterpolateNormalized")
end
Point(result, context)
end
# -----
# Buffer related functions
# -----
# Computes the buffer of a geometry, for both positive and negative buffer distances.
# Since true buffer curves may contain circular arcs, computed buffer polygons can only be approximations to the true geometry.
# The user can control the accuracy of the curve approximation by specifying the number of linear segments with which to approximate a curve.
# Always returns a polygon. The negative or zero-distance buffer of lines and points is always an empty Polygon.
buffer(
obj::Geometry,
dist::Real,
quadsegs::Integer = 8,
context::GEOSContext = get_context(obj),
) = geomFromGEOS(GEOSBuffer_r(context, obj, dist, Int32(quadsegs)), context)
bufferWithStyle(
obj::Geometry,
dist::Real;
quadsegs::Integer = 8,
endCapStyle::GEOSBufCapStyles = GEOSBUF_CAP_ROUND,
joinStyle::GEOSBufJoinStyles = GEOSBUF_JOIN_ROUND,
mitreLimit::Real = 5.0,
context::GEOSContext = get_context(obj),
) = geomFromGEOS(
GEOSBufferWithStyle_r(
context,
obj,
dist,
Int32(quadsegs),
Int32(endCapStyle),
Int32(joinStyle),
mitreLimit,
),
context,
)
# GEOSBufferParams_create
# GEOSBufferParams_destroy
# GEOSBufferParams_setEndCapStyle
# GEOSBufferParams_setJoinStyle
# GEOSBufferParams_setMitreLimit
# GEOSBufferParams_setQuadrantSegments
# GEOSBufferParams_setSingleSided
# GEOSBufferWithParams
# GEOSOffsetCurve
# -----
# Geometry Constructors -- All functions return NULL on exception
# -----
# (GEOSCoordSequence* arguments will become ownership of the returned object.)
function createPoint(ptr::GEOSCoordSeq, context::GEOSContext = get_global_context())
result = GEOSGeom_createPoint_r(context, ptr)
if result == C_NULL
error("LibGEOS: Error in GEOSGeom_createPoint")
end
result
end
createPoint(x::Real, y::Real, context::GEOSContext = get_global_context()) =
createPoint(createCoordSeq(x, y, context), context)
createPoint(x::Real, y::Real, z::Real, context::GEOSContext = get_global_context()) =
createPoint(createCoordSeq(x, y, z, context), context)
createPoint(coords::Vector{Vector{Float64}}, context::GEOSContext = get_global_context()) =
createPoint(createCoordSeq(coords, context), context)
createPoint(coords::Vector{Float64}, context::GEOSContext = get_global_context()) =
createPoint(createCoordSeq(Vector{Float64}[coords], context), context)
function createLinearRing(ptr::GEOSCoordSeq, context::GEOSContext = get_global_context())
result = GEOSGeom_createLinearRing_r(context, ptr)
if result == C_NULL
error("LibGEOS: Error in GEOSGeom_createLinearRing")
end
result
end
createLinearRing(
coords::Vector{Vector{Float64}},
context::GEOSContext = get_global_context(),
) = GEOSGeom_createLinearRing_r(context, createCoordSeq(coords, context))
function createLineString(ptr::GEOSCoordSeq, context::GEOSContext = get_global_context())
result = GEOSGeom_createLineString_r(context, ptr)
if result == C_NULL
error("LibGEOS: Error in GEOSGeom_createLineString")
end
result
end
createLineString(
coords::Vector{Vector{Float64}},
context::GEOSContext = get_global_context(),
) = GEOSGeom_createLineString_r(context, createCoordSeq(coords, context))
# Second argument is an array of GEOSGeometry* objects.
# The caller remains owner of the array, but pointed-to
# objects become ownership of the returned GEOSGeometry.
function createPolygon(
shell::Union{LinearRing,GEOSGeom},
holes::AbstractVector,
context::GEOSContext = get_global_context(),
)
result = GEOSGeom_createPolygon_r(context, shell, holes, length(holes))
if result == C_NULL
error("LibGEOS: Error in GEOSGeom_createPolygon")
end
result
end
# convenience function to create polygon without holes
createPolygon(
shell::Union{LinearRing,GEOSGeom},
context::GEOSContext = get_global_context(),
) = createPolygon(shell, GEOSGeom[], context)
function createCollection(
geomtype::GEOSGeomTypes,
geoms::AbstractVector,
context::GEOSContext = get_global_context(),
)
result = GEOSGeom_createCollection_r(context, geomtype, geoms, length(geoms))
if result == C_NULL
error("LibGEOS: Error in GEOSGeom_createCollection")
end
result
end
function createEmptyCollection(
geomtype::GEOSGeomTypes,
context::GEOSContext = get_global_context(),
)
result = GEOSGeom_createEmptyCollection_r(context, geomtype)
if result == C_NULL
error("LibGEOS: Error in GEOSGeom_createEmptyCollection")
end
result
end
function createEmptyPolygon(context::GEOSContext = get_global_context())::Polygon
result = GEOSGeom_createEmptyPolygon_r(context)
if result == C_NULL
error("LibGEOS: Error in GEOSGeom_createEmptyPolygon")
end
Polygon(result, context)
end
function reverse(
obj::Geometry,
context::GEOSContext = get_context(obj),
)::LibGEOS.AbstractGeometry
result = GEOSReverse_r(context, obj)
if result == C_NULL
error("LibGEOS: Error in GEOSReverse_r")
end
geomFromGEOS(result, context)
end
function makeValid(
obj::Geometry,
context::GEOSContext = get_context(obj),
)::LibGEOS.AbstractGeometry
result = GEOSMakeValid_r(context, obj)
if result == C_NULL
error("LibGEOS: Error in GEOSMakeValid_r")
end
geomFromGEOS(result, context)
end
# Memory management
# cloneGeom result needs to be wrapped in Geometry type
function cloneGeom(
ptr::Union{Geometry,GEOSGeom},
context::GEOSContext = get_global_context(),
)
result = GEOSGeom_clone_r(context, ptr)
if result == C_NULL
error("LibGEOS: Error in GEOSGeom_clone")
end
result
end
function destroyGeom(obj::Geometry, context::GEOSContext = get_context(obj))
GEOSGeom_destroy_r(context, obj)
obj.ptr = C_NULL
end
function destroyGeom(obj::PreparedGeometry, context::GEOSContext = get_context(obj))
destroyPreparedGeom(obj, context)
obj.ptr = C_NULL
end
# -----
# Topology operations - return NULL on exception.
# -----
function envelope(obj::Geometry, context::GEOSContext = get_context(obj))
result = GEOSEnvelope_r(context, obj)
if result == C_NULL
error("LibGEOS: Error in GEOSEnvelope")
end
geomFromGEOS(result, context)
end
function envelope(obj::PreparedGeometry, context::GEOSContext = get_context(obj))
envelope(obj.ownedby, context)
end
"""
minimumRotatedRectangle(geom)
Returns the minimum rotated rectangular POLYGON which encloses the input geometry. The rectangle
has width equal to the minimum diameter, and a longer length. If the convex hill of the input is
degenerate (a line or point) a LINESTRING or POINT is returned. The minimum rotated rectangle can
be used as an extremely generalized representation for the given geometry.
"""
function minimumRotatedRectangle(obj::Geometry, context::GEOSContext = get_context(obj))
result = GEOSMinimumRotatedRectangle_r(context, obj)
if result == C_NULL
error("LibGEOS: Error in GEOSMinimumRotatedRectangle")
end
geomFromGEOS(result, context)
end
function intersection(
obj1::Geometry,
obj2::Geometry,
context::GEOSContext = get_context(obj1),
)
result = GEOSIntersection_r(context, obj1, obj2)
if result == C_NULL
error("LibGEOS: Error in GEOSIntersection")
end
geomFromGEOS(result, context)
end
# Returns a Geometry that represents the convex hull of the input geometry. The returned geometry contains the minimal number of points needed to represent the convex hull. In particular, no more than two consecutive points will be collinear.
# Returns:
# if the convex hull contains 3 or more points, a Polygon; 2 points, a LineString; 1 point, a Point; 0 points, an empty GeometryCollection.
function convexhull(obj::Geometry, context::GEOSContext = get_context(obj))
result = GEOSConvexHull_r(context, obj)
if result == C_NULL
error("LibGEOS: Error in GEOSConvexHull")
end
geomFromGEOS(result, context)
end
function difference(
obj1::Geometry,
obj2::Geometry,
context::GEOSContext = get_context(obj1),
)
result = GEOSDifference_r(context, obj1, obj2)
if result == C_NULL
error("LibGEOS: Error in GEOSDifference")
end
geomFromGEOS(result, context)
end
function symmetricDifference(
obj1::Geometry,
obj2::Geometry,
context::GEOSContext = get_context(obj1),
)
result = GEOSSymDifference_r(context, obj1, obj2)
if result == C_NULL
error("LibGEOS: Error in GEOSSymDifference")
end
geomFromGEOS(result, context)
end
function boundary(obj::Geometry, context::GEOSContext = get_context(obj))
result = GEOSBoundary_r(context, obj)
if result == C_NULL
error("LibGEOS: Error in GEOSBoundary")
end
geomFromGEOS(result, context)
end
function union(obj1::Geometry, obj2::Geometry, context::GEOSContext = get_context(obj1))
result = GEOSUnion_r(context, obj1, obj2)
if result == C_NULL
error("LibGEOS: Error in GEOSUnion")
end
geomFromGEOS(result, context)
end
function unaryUnion(obj::Geometry, context::GEOSContext = get_context(obj))
result = GEOSUnaryUnion_r(context, obj)
if result == C_NULL
error("LibGEOS: Error in GEOSUnaryUnion")
end
geomFromGEOS(result, context)
end
function pointOnSurface(obj::Geometry, context::GEOSContext = get_context(obj))
result = GEOSPointOnSurface_r(context, obj)
if result == C_NULL
error("LibGEOS: Error in GEOSPointOnSurface")
end
Point(result, context)
end
function centroid(obj::Geometry, context::GEOSContext = get_context(obj))
result = GEOSGetCentroid_r(context, obj)
if result == C_NULL
error("LibGEOS: Error in GEOSGetCentroid")
end
Point(result, context)
end
function node(obj::Geometry, context::GEOSContext = get_context(obj))
result = GEOSNode_r(context, obj)
if result == C_NULL
error("LibGEOS: Error in GEOSNode")
end
geomFromGEOS(result, context)
end
# all arguments remain ownership of the caller (both Geometries and pointers)
function polygonize(geoms::Vector{GEOSGeom}, context::GEOSContext = get_global_context())
result = GEOSPolygonize_r(context, pointer(geoms), length(geoms))
if result == C_NULL
error("LibGEOS: Error in GEOSPolygonize")
end
result
end
# GEOSPolygonizer_getCutEdges
# GEOSPolygonize_full
function lineMerge(obj::Geometry, context::GEOSContext = get_context(obj))
result = GEOSLineMerge_r(context, obj)
if result == C_NULL
error("LibGEOS: Error in GEOSLineMerge")
end
geomFromGEOS(result, context)
end
function maximumInscribedCircle(
obj::Geometry,
tolerance,
context::GEOSContext = get_context(obj),
)
result = GEOSMaximumInscribedCircle_r(context, obj, tolerance)
if result == C_NULL
error("LibGEOS: Error in GEOSMaximumInscribedCircle")
end
geomFromGEOS(result, context)
end
function isValidReason(obj::Geometry, context::GEOSContext = get_context(obj))
result = GEOSisValidReason_r(context, obj)
if result == C_NULL
error("LibGEOS: Error in GEOSisValidReason")
end
result
end
function simplify(obj::Geometry, tol::Real, context::GEOSContext = get_context(obj))
result = GEOSSimplify_r(context, obj, tol)
if result == C_NULL
error("LibGEOS: Error in GEOSSimplify")
end
geomFromGEOS(result, context)
end
function topologyPreserveSimplify(
obj::Geometry,
tol::Real,
context::GEOSContext = get_context(obj),
)
result = GEOSTopologyPreserveSimplify_r(context, obj, tol)
if result == C_NULL
error("LibGEOS: Error in GEOSTopologyPreserveSimplify")
end
geomFromGEOS(result, context)
end
# Return all distinct vertices of input geometry as a MULTIPOINT.
# (Note that only 2 dimensions of the vertices are considered when testing for equality)
function uniquePoints(obj::Geometry, context::GEOSContext = get_context(obj))
result = GEOSGeom_extractUniquePoints_r(context, obj)
if result == C_NULL
error("LibGEOS: Error in GEOSGeom_extractUniquePoints")
end
MultiPoint(result, context)
end
# Find paths shared between the two given lineal geometries.
# Returns a GEOMETRYCOLLECTION having two elements:
# - first element is a MULTILINESTRING containing shared paths
# having the _same_ direction on both inputs
# - second element is a MULTILINESTRING containing shared paths
# having the _opposite_ direction on the two inputs
# (Returns NULL on exception)
function sharedPaths(
obj1::LineString,
obj2::LineString,
context::GEOSContext = get_context(obj1),
)
result = GEOSSharedPaths_r(context, obj1, obj2)
if result == C_NULL
error("LibGEOS: Error in GEOSSharedPaths")
end
GeometryCollection(result, context)
end
# Snap first geometry on to second with given tolerance
# (Returns a newly allocated geometry, or NULL on exception)
function snap(
obj1::Geometry,
obj2::Geometry,
tol::Real,
context::GEOSContext = get_context(obj1),
)
result = GEOSSnap_r(context, obj1, obj2, tol)
if result == C_NULL
error("LibGEOS: Error in GEOSSnap")
end
geomFromGEOS(result, context)
end
# Return a Delaunay triangulation of the vertex of the given geometry
#
# @param g the input geometry whose vertex will be used as "sites"
# @param tolerance optional snapping tolerance to use for improved robustness
# @param onlyEdges if non-zero will return a MULTILINESTRING, otherwise it will
# return a GEOMETRYCOLLECTION containing triangular POLYGONs.
#
# @return a newly allocated geometry, or NULL on exception
function delaunayTriangulation(
obj::Geometry,
tol::Real = 0.0,
onlyEdges::Bool = false,
context::GEOSContext = get_context(obj),
)
result = GEOSDelaunayTriangulation_r(context, obj, tol, Int32(onlyEdges))
if result == C_NULL
error("LibGEOS: Error in GEOSDelaunayTriangulation")
end
onlyEdges ? MultiLineString(result, context) : GeometryCollection(result, context)
end
function delaunayTriangulationEdges(
obj::Geometry,
tol::Real = 0.0,
context::GEOSContext = get_context(obj),
)
delaunayTriangulation(obj, tol, true, context)
end
function constrainedDelaunayTriangulation(
obj::Geometry,
context::GEOSContext = get_context(obj),
)
result = GEOSConstrainedDelaunayTriangulation_r(context, obj)
if result == C_NULL
error("LibGEOS: Error in GEOSConstrainedDelaunayTriangulation")
end
GeometryCollection(result, context)
end
# -----
# Binary predicates - return 2 on exception, 1 on true, 0 on false
# -----
function disjoint(obj1::Geometry, obj2::Geometry, context::GEOSContext = get_context(obj1))
result = GEOSDisjoint_r(context, obj1, obj2)
if result == 0x02
error("LibGEOS: Error in GEOSDisjoint")
end
result != 0x00
end
function touches(obj1::Geometry, obj2::Geometry, context::GEOSContext = get_context(obj1))
result = GEOSTouches_r(context, obj1, obj2)
if result == 0x02
error("LibGEOS: Error in GEOSTouches")
end
result != 0x00
end
function intersects(
obj1::Geometry,
obj2::Geometry,
context::GEOSContext = get_context(obj1),
)
result = GEOSIntersects_r(context, obj1, obj2)
if result == 0x02
error("LibGEOS: Error in GEOSIntersects")
end
result != 0x00
end
function crosses(obj1::Geometry, obj2::Geometry, context::GEOSContext = get_context(obj1))
result = GEOSCrosses_r(context, obj1, obj2)
if result == 0x02
error("LibGEOS: Error in GEOSCrosses")
end
result != 0x00
end
function within(obj1::Geometry, obj2::Geometry, context::GEOSContext = get_context(obj1))
result = GEOSWithin_r(context, obj1, obj2)
if result == 0x02
error("LibGEOS: Error in GEOSWithin")
end
result != 0x00
end
Base.contains(obj1::Geometry, obj2::Geometry, context::GEOSContext = get_context(obj1)) =
contains(obj1, obj2, context)
function contains(obj1::Geometry, obj2::Geometry, context::GEOSContext = get_context(obj1))
result = GEOSContains_r(context, obj1, obj2)
if result == 0x02
error("LibGEOS: Error in GEOSContains")
end
result != 0x00
end
function overlaps(obj1::Geometry, obj2::Geometry, context::GEOSContext = get_context(obj1))
result = GEOSOverlaps_r(context, obj1, obj2)
if result == 0x02
error("LibGEOS: Error in GEOSOverlaps")
end
result != 0x00
end
function equals(obj1::Geometry, obj2::Geometry, context::GEOSContext = get_context(obj1))
result = GEOSEquals_r(context, obj1, obj2)
if result == 0x02
error("LibGEOS: Error in GEOSEquals")
end
result != 0x00
end
function equalsexact(
obj1::Geometry,
obj2::Geometry,
tol::Real,
context::GEOSContext = get_context(obj1),
)
result = GEOSEqualsExact_r(context, obj1, obj2, tol)
if result == 0x02
error("LibGEOS: Error in GEOSEqualsExact")
end
result != 0x00
end
function covers(obj1::Geometry, obj2::Geometry, context::GEOSContext = get_context(obj1))
result = GEOSCovers_r(context, obj1, obj2)
if result == 0x02
error("LibGEOS: Error in GEOSCovers")
end
result != 0x00
end
function coveredby(obj1::Geometry, obj2::Geometry, context::GEOSContext = get_context(obj1))
result = GEOSCoveredBy_r(context, obj1, obj2)
if result == 0x02
error("LibGEOS: Error in GEOSCoveredBy")
end
result != 0x00
end
# -----
# Prepared Geometry Binary predicates - return 2 on exception, 1 on true, 0 on false
# -----
# GEOSGeometry ownership is retained by caller
function prepareGeom(obj::Geometry, context::GEOSContext = get_context(obj))
result = GEOSPrepare_r(context, obj)