-
Notifications
You must be signed in to change notification settings - Fork 26
/
geometry.jl
1858 lines (1547 loc) · 57.5 KB
/
geometry.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
"""
fromWKB(data)
Create a geometry object of the appropriate type from it's well known
binary (WKB) representation.
### Parameters
* `data`: pointer to the input BLOB data.
"""
function fromWKB(data)::IGeometry
geom = Ref{GDAL.OGRGeometryH}()
result = @gdal(
OGR_G_CreateFromWkb::GDAL.OGRErr,
data::Ptr{Cuchar},
C_NULL::GDAL.OGRSpatialReferenceH,
geom::Ptr{GDAL.OGRGeometryH},
sizeof(data)::Cint
)
@ogrerr result "Failed to create geometry from WKB"
return IGeometry(geom[])
end
function unsafe_fromWKB(data)::Geometry
geom = Ref{GDAL.OGRGeometryH}()
result = @gdal(
OGR_G_CreateFromWkb::GDAL.OGRErr,
data::Ptr{Cuchar},
C_NULL::GDAL.OGRSpatialReferenceH,
geom::Ptr{GDAL.OGRGeometryH},
sizeof(data)::Cint
)
@ogrerr result "Failed to create geometry from WKB"
return Geometry(geom[])
end
"""
fromWKT(data::Vector{String})
Create a geometry object of the appropriate type from its well known text
(WKT) representation.
### Parameters
* `data`: input zero terminated string containing WKT representation of the
geometry to be created. The pointer is updated to point just beyond that
last character consumed.
"""
function fromWKT(data::Vector{String})::IGeometry
geom = Ref{GDAL.OGRGeometryH}()
result = @gdal(
OGR_G_CreateFromWkt::GDAL.OGRErr,
data::StringList,
C_NULL::GDAL.OGRSpatialReferenceH,
geom::Ptr{GDAL.OGRGeometryH}
)
@ogrerr result "Failed to create geometry from WKT"
return IGeometry(geom[])
end
function unsafe_fromWKT(data::Vector{String})::Geometry
geom = Ref{GDAL.OGRGeometryH}()
result = @gdal(
OGR_G_CreateFromWkt::GDAL.OGRErr,
data::StringList,
C_NULL::GDAL.OGRSpatialReferenceH,
geom::Ptr{GDAL.OGRGeometryH}
)
@ogrerr result "Failed to create geometry from WKT"
return Geometry(geom[])
end
fromWKT(data::String, args...)::IGeometry = fromWKT([data], args...)
unsafe_fromWKT(data::String, args...)::Geometry =
unsafe_fromWKT([data], args...)
"""
Destroy geometry object.
Equivalent to invoking delete on a geometry, but it guaranteed to take place
within the context of the GDAL/OGR heap.
"""
function destroy(geom::AbstractGeometry)::Nothing
GDAL.ogr_g_destroygeometry(geom.ptr)
geom.ptr = C_NULL
return nothing
end
"""
Destroy prepared geometry object.
Equivalent to invoking delete on a prepared geometry, but it guaranteed to take place
within the context of the GDAL/OGR heap.
"""
function destroy(geom::AbstractPreparedGeometry)::Nothing
GDAL.ogrdestroypreparedgeometry(geom.ptr)
geom.ptr = C_NULL
return nothing
end
"""
clone(geom::AbstractGeometry)
Returns a copy of the geometry with the original spatial reference system.
"""
function clone(geom::AbstractGeometry{T}) where {T}
if geom.ptr == C_NULL
return IGeometry{wkbUnknown}()
else
return IGeometry{T}(GDAL.ogr_g_clone(geom.ptr))
end
end
function unsafe_clone(geom::AbstractGeometry{T}) where {T}
if geom.ptr == C_NULL
return Geometry{wkbUnknown}()
else
return Geometry{T}(GDAL.ogr_g_clone(geom.ptr))
end
end
"""
creategeom(geomtype::OGRwkbGeometryType)
Create an empty geometry of desired type.
This is equivalent to allocating the desired geometry with new, but the
allocation is guaranteed to take place in the context of the GDAL/OGR heap.
"""
creategeom(geomtype::OGRwkbGeometryType)::IGeometry =
IGeometry(GDAL.ogr_g_creategeometry(geomtype))
unsafe_creategeom(geomtype::OGRwkbGeometryType)::Geometry =
Geometry(GDAL.ogr_g_creategeometry(geomtype))
# When the geometry type `T` is known, pass it wrapped in `Val` for type
# stability. `T` is usually equal to `geomtype`, except in the case
# of `geomtype == wkbLinearRing`, in which case `T` is `wkbLineString`
creategeom(::Val{T}) where {T} = IGeometry{T}(GDAL.ogr_g_creategeometry(T))
function unsafe_creategeom(::Val{T}) where {T}
return Geometry{T}(GDAL.ogr_g_creategeometry(T))
end
# Special-case createlinearring, because we need to pass
# wkbLinearRing create but gdal returns a wkbLineString
# we also don't know the type because there is no wkbLinearRing25D
function creategeom(::Val{wkbLinearRing})
return IGeometry(
GDAL.ogr_g_creategeometry(wkbLinearRing),
)::Union{IGeometry{wkbLineString},IGeometry{wkbLineString25D}}
end
function unsafe_creategeom(::Val{wkbLinearRing})
return Geometry(
GDAL.ogr_g_creategeometry(wkbLinearRing),
)::Union{Geometry{wkbLineString},Geometry{wkbLineString25D}}
end
"""
haspreparedgeomsupport()
Check whether the current GDAL instance has support for prepared geometries.
"""
has_preparedgeom_support() = Bool(GDAL.ogrhaspreparedgeometrysupport())
"""
preparegeom(geom::AbstractGeometry)
Create an prepared geometry of a geometry. This can speed up operations which interact
with the geometry multiple times, by storing caches of calculated geometry information.
"""
function preparegeom(geom::AbstractGeometry{T}) where {T}
return IPreparedGeometry{T}(GDAL.ogrcreatepreparedgeometry(geom.ptr))
end
function unsafe_preparegeom(geom::AbstractGeometry{T}) where {T}
return PreparedGeometry{T}(GDAL.ogrcreatepreparedgeometry(geom.ptr))
end
"""
forceto(geom::AbstractGeometry, targettype::OGRwkbGeometryType, [options])
Tries to force the provided geometry to the specified geometry type.
### Parameters
* `geom`: the input geometry.
* `targettype`: target output geometry type.
# `options`: (optional) options as a null-terminated vector of strings
It can promote 'single' geometry type to their corresponding collection type
(see OGR_GT_GetCollection()) or the reverse. non-linear geometry type to their
corresponding linear geometry type (see OGR_GT_GetLinear()), by possibly
approximating circular arcs they may contain. Regarding conversion from linear
geometry types to curve geometry types, only "wraping" will be done. No attempt
to retrieve potential circular arcs by de-approximating stroking will be done.
For that, OGRGeometry::getCurveGeometry() can be used.
The passed in geometry is cloned and a new one returned.
"""
function forceto(
geom::AbstractGeometry,
targettype::OGRwkbGeometryType,
options = StringList(C_NULL),
)::IGeometry
return IGeometry(
GDAL.ogr_g_forceto(unsafe_clone(geom).ptr, targettype, options),
)
end
function unsafe_forceto(
geom::AbstractGeometry,
targettype::OGRwkbGeometryType,
options = StringList(C_NULL),
)::Geometry
return Geometry(
GDAL.ogr_g_forceto(unsafe_clone(geom).ptr, targettype, options),
)
end
"""
geomdim(geom::AbstractGeometry)
Get the dimension of the geometry. 0 for points, 1 for lines and 2 for surfaces.
This function corresponds to the SFCOM IGeometry::GetDimension() method. It
indicates the dimension of the geometry, but does not indicate the dimension of
the underlying space (as indicated by OGR_G_GetCoordinateDimension() function).
"""
geomdim(geom::AbstractGeometry)::Integer = GDAL.ogr_g_getdimension(geom.ptr)
"""
getcoorddim(geom::AbstractGeometry)
Get the dimension of the coordinates in this geometry.
### Returns
This will return 2 or 3.
"""
getcoorddim(geom::AbstractGeometry)::Integer =
GDAL.ogr_g_getcoordinatedimension(geom.ptr)
"""
setcoorddim!(geom::AbstractGeometry, dim::Integer)
Set the coordinate dimension.
This method sets the explicit coordinate dimension. Setting the coordinate
dimension of a geometry to 2 should zero out any existing Z values. Setting the
dimension of a geometry collection, a compound curve, a polygon, etc. will
affect the children geometries. This will also remove the M dimension if present
before this call.
"""
function setcoorddim!(geom::G, dim::Integer)::G where {G<:AbstractGeometry}
# TODO change the geometry type here
GDAL.ogr_g_setcoordinatedimension(geom.ptr, dim)
return geom
end
"""
envelope(geom::AbstractGeometry)
Computes and returns the bounding envelope for this geometry.
"""
function envelope(geom::AbstractGeometry)::GDAL.OGREnvelope
envelope = Ref{GDAL.OGREnvelope}(GDAL.OGREnvelope(0, 0, 0, 0))
GDAL.ogr_g_getenvelope(geom.ptr, envelope)
return envelope[]
end
"""
envelope3d(geom::AbstractGeometry)
Computes and returns the bounding envelope (3D) for this geometry
"""
function envelope3d(geom::AbstractGeometry)::GDAL.OGREnvelope3D
envelope = Ref{GDAL.OGREnvelope3D}(GDAL.OGREnvelope3D(0, 0, 0, 0, 0, 0))
GDAL.ogr_g_getenvelope3d(geom.ptr, envelope)
return envelope[]
end
"""
boundingbox(geom::AbstractGeometry)
Returns a bounding box polygon (CW) built from envelope coordinates
"""
function boundingbox(geom::AbstractGeometry)::IGeometry
coordinates = envelope(geom)
MinX, MaxX = coordinates.MinX, coordinates.MaxX
MinY, MaxY = coordinates.MinY, coordinates.MaxY
# creates a CW closed ring polygon
return createpolygon([
[MinX, MaxY],
[MaxX, MaxY],
[MaxX, MinY],
[MinX, MinY],
[MinX, MaxY],
])
end
"""
toWKB(geom::AbstractGeometry, order::OGRwkbByteOrder = wkbNDR)
Convert a geometry well known binary format.
### Parameters
* `geom`: handle on the geometry to convert to a well know binary data from.
* `order`: One of wkbXDR or [wkbNDR] indicating MSB or LSB byte order resp.
"""
function toWKB(
geom::AbstractGeometry,
order::OGRwkbByteOrder = wkbNDR,
)::Vector{Cuchar}
buffer = Vector{Cuchar}(undef, wkbsize(geom))
result = GDAL.ogr_g_exporttowkb(geom.ptr, order, buffer)
@ogrerr result "Failed to export geometry to WKB"
return buffer
end
"""
toISOWKB(geom::AbstractGeometry, order::OGRwkbByteOrder = wkbNDR)
Convert a geometry into SFSQL 1.2 / ISO SQL/MM Part 3 well known binary format.
### Parameters
* `geom`: handle on the geometry to convert to a well know binary data from.
* `order`: One of wkbXDR or [wkbNDR] indicating MSB or LSB byte order resp.
"""
function toISOWKB(
geom::AbstractGeometry,
order::OGRwkbByteOrder = wkbNDR,
)::Vector{Cuchar}
buffer = Array{Cuchar}(undef, wkbsize(geom))
result = GDAL.ogr_g_exporttoisowkb(geom.ptr, order, buffer)
@ogrerr result "Failed to export geometry to ISO WKB"
return buffer
end
"""
wkbsize(geom::AbstractGeometry)
Returns size (in bytes) of related binary representation.
"""
wkbsize(geom::AbstractGeometry)::Integer = GDAL.ogr_g_wkbsize(geom.ptr)
"""
toWKT(geom::AbstractGeometry)
Convert a geometry into well known text format.
"""
function toWKT(geom::AbstractGeometry)::String
wkt_ptr = Ref(Cstring(C_NULL))
result = GDAL.ogr_g_exporttowkt(geom.ptr, wkt_ptr)
@ogrerr result "OGRErr $result: failed to export geometry to WKT"
wkt = unsafe_string(wkt_ptr[])
GDAL.vsifree(pointer(wkt_ptr[]))
return wkt
end
"""
toISOWKT(geom::AbstractGeometry)
Convert a geometry into SFSQL 1.2 / ISO SQL/MM Part 3 well known text format.
"""
function toISOWKT(geom::AbstractGeometry)::String
isowkt_ptr = Ref(Cstring(C_NULL))
result = GDAL.ogr_g_exporttoisowkt(geom.ptr, isowkt_ptr)
@ogrerr result "OGRErr $result: failed to export geometry to ISOWKT"
wkt = unsafe_string(isowkt_ptr[])
GDAL.vsifree(pointer(isowkt_ptr[]))
return wkt
end
"""
getgeomtype(geom::AbstractGeometry)
Fetch geometry type code
"""
getgeomtype(geom::AbstractGeometry)::OGRwkbGeometryType = _geomtype(geom)
"""
geomname(geom::AbstractGeometry)
Fetch WKT name for geometry type.
"""
function geomname(geom::AbstractGeometry)::Union{String,Missing}
return if geom.ptr == C_NULL
missing
else
GDAL.ogr_g_getgeometryname(geom.ptr)
end
end
"""
flattento2d!(geom::AbstractGeometry)
Convert geometry to strictly 2D.
The return value will have a new type, do not continue using the original object.
"""
function flattento2d!(geom::G)::G where {G<:AbstractGeometry}
# TODO change the geometry type here
GDAL.ogr_g_flattento2d(geom.ptr)
return geom
end
"""
closerings!(geom::AbstractGeometry)
Force rings to be closed.
If this geometry, or any contained geometries has polygon rings that are not
closed, they will be closed by adding the starting point at the end.
"""
function closerings!(geom::G)::G where {G<:AbstractGeometry}
GDAL.ogr_g_closerings(geom.ptr)
return geom
end
"""
fromGML(data)
Create geometry from GML.
This method translates a fragment of GML containing only the geometry portion
into a corresponding OGRGeometry. There are many limitations on the forms of GML
geometries supported by this parser, but they are too numerous to list here.
The following GML2 elements are parsed : Point, LineString, Polygon, MultiPoint,
MultiLineString, MultiPolygon, MultiGeometry.
"""
fromGML(data)::IGeometry = IGeometry(GDAL.ogr_g_createfromgml(data))
unsafe_fromGML(data)::Geometry = Geometry(GDAL.ogr_g_createfromgml(data))
"""
toGML(geom::AbstractGeometry)
Convert a geometry into GML format.
"""
toGML(geom::AbstractGeometry)::String = GDAL.ogr_g_exporttogml(geom.ptr)
"""
toKML(geom::AbstractGeometry, altitudemode = C_NULL)
Convert a geometry into KML format.
"""
toKML(geom::AbstractGeometry, altitudemode = C_NULL)::String =
GDAL.ogr_g_exporttokml(geom.ptr, altitudemode)
# ↑ * `altitudemode`: value to write in altitudeMode element, or NULL.
"""
toJSON(geom::AbstractGeometry; kwargs...)
Convert a geometry into GeoJSON format.
* The following options are supported :
* `COORDINATE_PRECISION=number`: maximum number of figures after decimal
separator to write in coordinates.
* `SIGNIFICANT_FIGURES=number`: maximum number of significant figures.
*
* If COORDINATE_PRECISION is defined, SIGNIFICANT_FIGURES will be ignored if
* specified.
* When none are defined, the default is COORDINATE_PRECISION=15.
### Parameters
* `geom`: handle to the geometry.
### Returns
A GeoJSON fragment or NULL in case of error.
"""
toJSON(geom::AbstractGeometry; kwargs...)::String =
GDAL.ogr_g_exporttojsonex(geom.ptr, String["$k=$v" for (k, v) in kwargs])
toJSON(geom::AbstractGeometry, options::Vector{String})::String =
GDAL.ogr_g_exporttojsonex(geom.ptr, options)
"""
fromJSON(data::String)
Create a geometry object from its GeoJSON representation.
"""
fromJSON(data::String)::IGeometry =
IGeometry(GDAL.ogr_g_creategeometryfromjson(data))
unsafe_fromJSON(data::String)::Geometry =
Geometry(GDAL.ogr_g_creategeometryfromjson(data))
# """
# Assign spatial reference to this object.
# Any existing spatial reference is replaced, but under no circumstances
# does this result in the object being reprojected. It is just changing
# the interpretation of the existing geometry. Note that assigning a
# spatial reference increments the reference count on the
# OGRSpatialReference, but does not copy it.
# Starting with GDAL 2.3, this will also assign the spatial reference to
# potential sub-geometries of the geometry (OGRGeometryCollection,
# OGRCurvePolygon/OGRPolygon, OGRCompoundCurve, OGRPolyhedralSurface and
# their derived classes).
# """
# function setspatialref!(geom::Geometry, spatialref::AbstractSpatialRef)
# GDAL.assignspatialreference(geom.ptr, spatialref.ptr)
# return geom
# end
"""
getspatialref(geom::AbstractGeometry)
Returns a clone of the spatial reference system for the geometry.
(The original SRS may be shared with many objects, and should not be modified.)
"""
function getspatialref(geom::AbstractGeometry)::ISpatialRef
if geom.ptr == C_NULL
return ISpatialRef()
end
result = GDAL.ogr_g_getspatialreference(geom.ptr)
return if result == C_NULL
ISpatialRef()
else
ISpatialRef(GDAL.osrclone(result))
end
end
function unsafe_getspatialref(geom::AbstractGeometry)::SpatialRef
if geom.ptr == C_NULL
return SpatialRef()
end
result = GDAL.ogr_g_getspatialreference(geom.ptr)
return if result == C_NULL
SpatialRef()
else
SpatialRef(GDAL.osrclone(result))
end
end
"""
transform!(geom::AbstractGeometry, coordtransform::CoordTransform)
Apply arbitrary coordinate transformation to geometry.
### Parameters
* `geom`: handle on the geometry to apply the transform to.
* `coordtransform`: handle on the transformation to apply.
"""
function transform!(
geom::G,
coordtransform::CoordTransform,
)::G where {G<:AbstractGeometry}
result = GDAL.ogr_g_transform(geom.ptr, coordtransform.ptr)
@ogrerr result "Failed to transform geometry"
return geom
end
# """
# Transform geometry to new spatial reference system.
# This function will transform the coordinates of a geometry from their
# current spatial reference system to a new target spatial reference
# system. Normally this means reprojecting the vectors, but it could
# include datum shifts, and changes of units.
# This function will only work if the geometry already has an assigned
# spatial reference system, and if it is transformable to the target
# coordinate system.
# Because this function requires internal creation and initialization of
# an OGRCoordinateTransformation object it is significantly more
# expensive to use this function to transform many geometries than it is
# to create the OGRCoordinateTransformation in advance, and call
# transform() with that transformation. This function exists primarily
# for convenience when only transforming a single geometry.
# ### Parameters
# * `geom`: handle on the geometry to apply the transformation.
# * `spatialref`: Target spatial reference system.
# """
# function transform!(geom::AbstractGeometry, spatialref::AbstractSpatialRef)
# result = GDAL.ogr_g_transformto(geom.ptr, spatialref.ptr)
# @ogrerr result "Failed to transform geometry to the new SRS"
# return geom
# end
"""
simplify(geom::AbstractGeometry, tol::Real)
Compute a simplified geometry.
### Parameters
* `geom`: the geometry.
* `tol`: the distance tolerance for the simplification.
"""
simplify(geom::AbstractGeometry, tol::Real)::IGeometry =
IGeometry(GDAL.ogr_g_simplify(geom.ptr, tol))
unsafe_simplify(geom::AbstractGeometry, tol::Real)::Geometry =
Geometry(GDAL.ogr_g_simplify(geom.ptr, tol))
"""
simplifypreservetopology(geom::AbstractGeometry, tol::Real)
Simplify the geometry while preserving topology.
### Parameters
* `geom`: the geometry.
* `tol`: the distance tolerance for the simplification.
"""
simplifypreservetopology(geom::AbstractGeometry, tol::Real)::IGeometry =
IGeometry(GDAL.ogr_g_simplifypreservetopology(geom.ptr, tol))
unsafe_simplifypreservetopology(geom::AbstractGeometry, tol::Real)::Geometry =
Geometry(GDAL.ogr_g_simplifypreservetopology(geom.ptr, tol))
"""
delaunaytriangulation(geom::AbstractGeometry, tol::Real, onlyedges::Bool)
Return a Delaunay triangulation of the vertices of the geometry.
### Parameters
* `geom`: the geometry.
* `tol`: optional snapping tolerance to use for improved robustness
* `onlyedges`: if `true`, will return a MULTILINESTRING, otherwise it
will return a GEOMETRYCOLLECTION containing triangular POLYGONs.
"""
function delaunaytriangulation(
geom::AbstractGeometry,
tol::Real,
onlyedges::Bool,
)::IGeometry
return IGeometry(GDAL.ogr_g_delaunaytriangulation(geom.ptr, tol, onlyedges))
end
function unsafe_delaunaytriangulation(
geom::AbstractGeometry,
tol::Real,
onlyedges::Bool,
)::Geometry
return Geometry(GDAL.ogr_g_delaunaytriangulation(geom.ptr, tol, onlyedges))
end
"""
segmentize!(geom::AbstractGeometry, maxlength::Real)
Modify the geometry such it has no segment longer than the given distance.
Interpolated points will have Z and M values (if needed) set to 0. Distance
computation is performed in 2d only
### Parameters
* `geom`: the geometry to segmentize
* `maxlength`: the maximum distance between 2 points after segmentization
"""
function segmentize!(geom::G, maxlength::Real)::G where {G<:AbstractGeometry}
GDAL.ogr_g_segmentize(geom.ptr, maxlength)
return geom
end
"""
intersects(g1::AbstractGeometry, g2::AbstractGeometry)
Returns whether the geometries intersect
Determines whether two geometries intersect. If GEOS is enabled, then this is
done in rigorous fashion otherwise `true` is returned if the envelopes (bounding
boxes) of the two geometries overlap.
"""
intersects(g1::AbstractGeometry, g2::AbstractGeometry)::Bool =
Bool(GDAL.ogr_g_intersects(g1.ptr, g2.ptr))
intersects(g1::AbstractPreparedGeometry, g2::AbstractGeometry)::Bool =
Bool(GDAL.ogrpreparedgeometryintersects(g1.ptr, g2.ptr))
"""
equals(g1::AbstractGeometry, g2::AbstractGeometry)
Returns `true` if the geometries are equivalent.
"""
equals(g1::AbstractGeometry, g2::AbstractGeometry)::Bool =
Bool(GDAL.ogr_g_equals(g1.ptr, g2.ptr))
function Base.:(==)(g1::AbstractGeometry, g2::AbstractGeometry)
return equals(g1, g2)
end
"""
disjoint(g1::AbstractGeometry, g2::AbstractGeometry)
Returns `true` if the geometries are disjoint.
"""
disjoint(g1::AbstractGeometry, g2::AbstractGeometry)::Bool =
Bool(GDAL.ogr_g_disjoint(g1.ptr, g2.ptr))
"""
touches(g1::AbstractGeometry, g2::AbstractGeometry)
Returns `true` if the geometries are touching.
"""
touches(g1::AbstractGeometry, g2::AbstractGeometry)::Bool =
Bool(GDAL.ogr_g_touches(g1.ptr, g2.ptr))
"""
crosses(g1::AbstractGeometry, g2::AbstractGeometry)
Returns `true` if the geometries are crossing.
"""
crosses(g1::AbstractGeometry, g2::AbstractGeometry)::Bool =
Bool(GDAL.ogr_g_crosses(g1.ptr, g2.ptr))
"""
within(g1::AbstractGeometry, g2::AbstractGeometry)
Returns `true` if g1 is contained within g2.
"""
within(g1::AbstractGeometry, g2::AbstractGeometry)::Bool =
Bool(GDAL.ogr_g_within(g1.ptr, g2.ptr))
"""
contains(g1::AbstractGeometry, g2::AbstractGeometry)
Returns `true` if g1 contains g2.
"""
contains(g1::AbstractGeometry, g2::AbstractGeometry)::Bool =
Bool(GDAL.ogr_g_contains(g1.ptr, g2.ptr))
contains(g1::AbstractPreparedGeometry, g2::AbstractGeometry)::Bool =
Bool(GDAL.ogrpreparedgeometrycontains(g1.ptr, g2.ptr))
"""
overlaps(g1::AbstractGeometry, g2::AbstractGeometry)
Returns `true` if the geometries overlap.
"""
overlaps(g1::AbstractGeometry, g2::AbstractGeometry)::Bool =
Bool(GDAL.ogr_g_overlaps(g1.ptr, g2.ptr))
"""
boundary(geom::AbstractGeometry)
Returns the boundary of the geometry.
A new geometry object is created and returned containing the boundary of the
geometry on which the method is invoked.
"""
boundary(geom::AbstractGeometry)::IGeometry =
IGeometry(GDAL.ogr_g_boundary(geom.ptr))
unsafe_boundary(geom::AbstractGeometry)::Geometry =
Geometry(GDAL.ogr_g_boundary(geom.ptr))
"""
convexhull(geom::AbstractGeometry)
Returns the convex hull of the geometry.
A new geometry object is created and returned containing the convex hull of the
geometry on which the method is invoked.
"""
convexhull(geom::AbstractGeometry)::IGeometry =
IGeometry(GDAL.ogr_g_convexhull(geom.ptr))
unsafe_convexhull(geom::AbstractGeometry)::Geometry =
Geometry(GDAL.ogr_g_convexhull(geom.ptr))
"""
buffer(geom::AbstractGeometry, dist::Real, quadsegs::Integer = 30)
Compute buffer of geometry.
Builds a new geometry containing the buffer region around the geometry on which
it is invoked. The buffer is a polygon containing the region within the buffer
distance of the original geometry.
Some buffer sections are properly described as curves, but are converted to
approximate polygons. The nQuadSegs parameter can be used to control how many
segments should be used to define a 90 degree curve - a quadrant of a circle.
A value of 30 is a reasonable default. Large values result in large numbers of
vertices in the resulting buffer geometry while small numbers reduce the
accuracy of the result.
### Parameters
* `geom`: the geometry.
* `dist`: the buffer distance to be applied. Should be expressed into the
same unit as the coordinates of the geometry.
* `quadsegs`: the number of segments used to approximate a 90 degree
(quadrant) of curvature.
"""
buffer(geom::AbstractGeometry, dist::Real, quadsegs::Integer = 30)::IGeometry =
IGeometry(GDAL.ogr_g_buffer(geom.ptr, dist, quadsegs))
function unsafe_buffer(
geom::AbstractGeometry,
dist::Real,
quadsegs::Integer = 30,
)::Geometry
return Geometry(GDAL.ogr_g_buffer(geom.ptr, dist, quadsegs))
end
"""
intersection(g1::AbstractGeometry, g2::AbstractGeometry)
Returns a new geometry representing the intersection of the geometries, or NULL
if there is no intersection or an error occurs.
Generates a new geometry which is the region of intersection of the two
geometries operated on. The OGR_G_Intersects() function can be used to test if
two geometries intersect.
"""
intersection(g1::AbstractGeometry, g2::AbstractGeometry)::IGeometry =
IGeometry(GDAL.ogr_g_intersection(g1.ptr, g2.ptr))
unsafe_intersection(g1::AbstractGeometry, g2::AbstractGeometry)::Geometry =
Geometry(GDAL.ogr_g_intersection(g1.ptr, g2.ptr))
"""
union(g1::AbstractGeometry, g2::AbstractGeometry)
Returns a new geometry representing the union of the geometries.
"""
union(g1::AbstractGeometry, g2::AbstractGeometry)::IGeometry =
IGeometry(GDAL.ogr_g_union(g1.ptr, g2.ptr))
unsafe_union(g1::AbstractGeometry, g2::AbstractGeometry)::Geometry =
Geometry(GDAL.ogr_g_union(g1.ptr, g2.ptr))
"""
pointonsurface(geom::AbstractGeometry)
Returns a point guaranteed to lie on the surface.
This method relates to the SFCOM ISurface::get_PointOnSurface() method however
the current implementation based on GEOS can operate on other geometry types
than the types that are supported by SQL/MM-Part 3 : surfaces (polygons) and
multisurfaces (multipolygons).
"""
pointonsurface(geom::AbstractGeometry)::IGeometry =
IGeometry(GDAL.ogr_g_pointonsurface(geom.ptr))
unsafe_pointonsurface(geom::AbstractGeometry)::Geometry =
Geometry(GDAL.ogr_g_pointonsurface(geom.ptr))
"""
difference(g1::AbstractGeometry, g2::AbstractGeometry)
Generates a new geometry which is the region of this geometry with the region of
the other geometry removed.
### Returns
A new geometry representing the difference of the geometries, or NULL
if the difference is empty.
"""
difference(g1::AbstractGeometry, g2::AbstractGeometry)::IGeometry =
IGeometry(GDAL.ogr_g_difference(g1.ptr, g2.ptr))
unsafe_difference(g1::AbstractGeometry, g2::AbstractGeometry)::Geometry =
Geometry(GDAL.ogr_g_difference(g1.ptr, g2.ptr))
"""
symdifference(g1::AbstractGeometry, g2::AbstractGeometry)
Returns a new geometry representing the symmetric difference of the geometries
or NULL if the difference is empty or an error occurs.
"""
symdifference(g1::AbstractGeometry, g2::AbstractGeometry)::IGeometry =
IGeometry(GDAL.ogr_g_symdifference(g1.ptr, g2.ptr))
unsafe_symdifference(g1::AbstractGeometry, g2::AbstractGeometry)::Geometry =
Geometry(GDAL.ogr_g_symdifference(g1.ptr, g2.ptr))
"""
distance(g1::AbstractGeometry, g2::AbstractGeometry)
Returns the distance between the geometries or -1 if an error occurs.
"""
distance(g1::AbstractGeometry, g2::AbstractGeometry)::Float64 =
GDAL.ogr_g_distance(g1.ptr, g2.ptr)
"""
geomlength(geom::AbstractGeometry)
Returns the length of the geometry, or 0.0 for unsupported geometry types.
"""
geomlength(geom::AbstractGeometry)::Float64 = GDAL.ogr_g_length(geom.ptr)
"""
geomarea(geom::AbstractGeometry)
Returns the area of the geometry or 0.0 for unsupported geometry types.
"""
geomarea(geom::AbstractGeometry)::Float64 = GDAL.ogr_g_area(geom.ptr)
"""
centroid!(geom::AbstractGeometry, centroid::AbstractGeometry)
Compute the geometry centroid.
The centroid location is applied to the passed in OGRPoint object. The centroid
is not necessarily within the geometry.
This method relates to the SFCOM ISurface::get_Centroid() method however the
current implementation based on GEOS can operate on other geometry types such as
multipoint, linestring, geometrycollection such as multipolygons. OGC SF SQL 1.1
defines the operation for surfaces (polygons). SQL/MM-Part 3 defines the
operation for surfaces and multisurfaces (multipolygons).
"""
function centroid!(
geom::AbstractGeometry,
centroid::G,
)::G where {G<:AbstractGeometry}
result = GDAL.ogr_g_centroid(geom.ptr, centroid.ptr)
@ogrerr result "Failed to compute the geometry centroid"
return centroid
end
"""
centroid(geom::AbstractGeometry)
Compute the geometry centroid.
The centroid is not necessarily within the geometry.
(This method relates to the SFCOM ISurface::get_Centroid() method however the
current implementation based on GEOS can operate on other geometry types such as
multipoint, linestring, geometrycollection such as multipolygons. OGC SF SQL 1.1
defines the operation for surfaces (polygons). SQL/MM-Part 3 defines the
operation for surfaces and multisurfaces (multipolygons).)
"""
function centroid(geom::AbstractGeometry)::IGeometry
# TODO should this handle 25D?
point = createpoint()
centroid!(geom, point)
return point
end
function unsafe_centroid(geom::AbstractGeometry)::Geometry
point = unsafe_createpoint()
centroid!(geom, point)
return point
end
"""
pointalongline(geom::AbstractGeometry, distance::Real)
Fetch point at given distance along curve.
### Parameters
* `geom`: curve geometry.
* `distance`: distance along the curve at which to sample position. This
distance should be between zero and geomlength() for this curve.
### Returns
a point or NULL.
"""
pointalongline(geom::AbstractGeometry, distance::Real)::IGeometry =
IGeometry(GDAL.ogr_g_value(geom.ptr, distance))
unsafe_pointalongline(geom::AbstractGeometry, distance::Real)::Geometry =
Geometry(GDAL.ogr_g_value(geom.ptr, distance))
"""
empty!(geom::AbstractGeometry)
Clear geometry information.
This restores the geometry to its initial state after construction, and before
assignment of actual geometry.
"""
function empty!(geom::G)::G where {G<:AbstractGeometry}
GDAL.ogr_g_empty(geom.ptr)
return geom
end
const wkbEnums = (
wkbPoint,
wkbLineString,
wkbPolygon,
wkbMultiPoint,
wkbMultiLineString,
wkbMultiPolygon,
wkbGeometryCollection,
wkbCircularString,
wkbCompoundCurve,
wkbCurvePolygon,
wkbMultiCurve,
wkbMultiSurface,
wkbCurve,
wkbSurface,
wkbPolyhedralSurface,
wkbTIN,
wkbTriangle,
wkbNone,
wkbLinearRing,
)
const wkbEnumsM = (
wkbPointM,
wkbLineStringM,
wkbPolygonM,
wkbMultiPointM,
wkbMultiLineStringM,
wkbMultiPolygonM,
wkbGeometryCollectionM,