forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathogrgeometry.cpp
7713 lines (6669 loc) · 245 KB
/
ogrgeometry.cpp
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
/******************************************************************************
*
* Project: OpenGIS Simple Features Reference Implementation
* Purpose: Implements a few base methods on OGRGeometry.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 1999, Frank Warmerdam
* Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "cpl_port.h"
#include "ogr_geometry.h"
#include <climits>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <memory>
#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_multiproc.h"
#include "cpl_string.h"
#include "ogr_api.h"
#include "ogr_core.h"
#include "ogr_geos.h"
#include "ogr_sfcgal.h"
#include "ogr_libs.h"
#include "ogr_p.h"
#include "ogr_spatialref.h"
#include "ogr_srs_api.h"
#include "ogr_wkb.h"
#ifndef HAVE_GEOS
#define UNUSED_IF_NO_GEOS CPL_UNUSED
#else
#define UNUSED_IF_NO_GEOS
#endif
//! @cond Doxygen_Suppress
int OGRGeometry::bGenerate_DB2_V72_BYTE_ORDER = FALSE;
//! @endcond
#ifdef HAVE_GEOS
static void OGRGEOSErrorHandler(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
CPLErrorV(CE_Failure, CPLE_AppDefined, fmt, args);
va_end(args);
}
static void OGRGEOSWarningHandler(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
CPLErrorV(CE_Warning, CPLE_AppDefined, fmt, args);
va_end(args);
}
#endif
/************************************************************************/
/* OGRWktOptions() */
/************************************************************************/
int OGRWktOptions::getDefaultPrecision()
{
return atoi(CPLGetConfigOption("OGR_WKT_PRECISION", "15"));
}
bool OGRWktOptions::getDefaultRound()
{
return CPLTestBool(CPLGetConfigOption("OGR_WKT_ROUND", "TRUE"));
}
/************************************************************************/
/* OGRGeometry() */
/************************************************************************/
OGRGeometry::OGRGeometry() = default;
/************************************************************************/
/* OGRGeometry( const OGRGeometry& ) */
/************************************************************************/
/**
* \brief Copy constructor.
*
* Note: before GDAL 2.1, only the default implementation of the constructor
* existed, which could be unsafe to use.
*
* @since GDAL 2.1
*/
OGRGeometry::OGRGeometry(const OGRGeometry &other)
: poSRS(other.poSRS), flags(other.flags)
{
if (poSRS != nullptr)
const_cast<OGRSpatialReference *>(poSRS)->Reference();
}
/************************************************************************/
/* ~OGRGeometry() */
/************************************************************************/
OGRGeometry::~OGRGeometry()
{
if (poSRS != nullptr)
const_cast<OGRSpatialReference *>(poSRS)->Release();
}
/************************************************************************/
/* operator=( const OGRGeometry&) */
/************************************************************************/
/**
* \brief Assignment operator.
*
* Note: before GDAL 2.1, only the default implementation of the operator
* existed, which could be unsafe to use.
*
* @since GDAL 2.1
*/
OGRGeometry &OGRGeometry::operator=(const OGRGeometry &other)
{
if (this != &other)
{
assignSpatialReference(other.getSpatialReference());
flags = other.flags;
}
return *this;
}
/************************************************************************/
/* dumpReadable() */
/************************************************************************/
/**
* \brief Dump geometry in well known text format to indicated output file.
*
* A few options can be defined to change the default dump :
* <ul>
* <li>DISPLAY_GEOMETRY=NO : to hide the dump of the geometry</li>
* <li>DISPLAY_GEOMETRY=WKT or YES (default) : dump the geometry as a WKT</li>
* <li>DISPLAY_GEOMETRY=SUMMARY : to get only a summary of the geometry</li>
* </ul>
*
* This method is the same as the C function OGR_G_DumpReadable().
*
* @param fp the text file to write the geometry to.
* @param pszPrefix the prefix to put on each line of output.
* @param papszOptions NULL terminated list of options (may be NULL)
*/
void OGRGeometry::dumpReadable(FILE *fp, const char *pszPrefix,
CSLConstList papszOptions) const
{
if (fp == nullptr)
fp = stdout;
const auto osStr = dumpReadable(pszPrefix, papszOptions);
fprintf(fp, "%s", osStr.c_str());
}
/************************************************************************/
/* dumpReadable() */
/************************************************************************/
/**
* \brief Dump geometry in well known text format to indicated output file.
*
* A few options can be defined to change the default dump :
* <ul>
* <li>DISPLAY_GEOMETRY=NO : to hide the dump of the geometry</li>
* <li>DISPLAY_GEOMETRY=WKT or YES (default) : dump the geometry as a WKT</li>
* <li>DISPLAY_GEOMETRY=SUMMARY : to get only a summary of the geometry</li>
* </ul>
*
* @param pszPrefix the prefix to put on each line of output.
* @param papszOptions NULL terminated list of options (may be NULL)
* @return a string with the geometry representation.
* @since GDAL 3.7
*/
std::string OGRGeometry::dumpReadable(const char *pszPrefix,
CSLConstList papszOptions) const
{
if (pszPrefix == nullptr)
pszPrefix = "";
std::string osRet;
const char *pszDisplayGeometry =
CSLFetchNameValue(papszOptions, "DISPLAY_GEOMETRY");
if (pszDisplayGeometry != nullptr && EQUAL(pszDisplayGeometry, "SUMMARY"))
{
osRet += CPLOPrintf("%s%s : ", pszPrefix, getGeometryName());
switch (getGeometryType())
{
case wkbUnknown:
case wkbNone:
case wkbPoint:
case wkbPoint25D:
case wkbPointM:
case wkbPointZM:
break;
case wkbPolyhedralSurface:
case wkbTIN:
case wkbPolyhedralSurfaceZ:
case wkbTINZ:
case wkbPolyhedralSurfaceM:
case wkbTINM:
case wkbPolyhedralSurfaceZM:
case wkbTINZM:
{
const OGRPolyhedralSurface *poPS = toPolyhedralSurface();
osRet +=
CPLOPrintf("%d geometries:\n", poPS->getNumGeometries());
for (auto &&poSubGeom : *poPS)
{
osRet += pszPrefix;
osRet += poSubGeom->dumpReadable(pszPrefix, papszOptions);
}
break;
}
case wkbLineString:
case wkbLineString25D:
case wkbLineStringM:
case wkbLineStringZM:
case wkbCircularString:
case wkbCircularStringZ:
case wkbCircularStringM:
case wkbCircularStringZM:
{
const OGRSimpleCurve *poSC = toSimpleCurve();
osRet += CPLOPrintf("%d points\n", poSC->getNumPoints());
break;
}
case wkbPolygon:
case wkbTriangle:
case wkbTriangleZ:
case wkbTriangleM:
case wkbTriangleZM:
case wkbPolygon25D:
case wkbPolygonM:
case wkbPolygonZM:
case wkbCurvePolygon:
case wkbCurvePolygonZ:
case wkbCurvePolygonM:
case wkbCurvePolygonZM:
{
const OGRCurvePolygon *poPoly = toCurvePolygon();
const OGRCurve *poRing = poPoly->getExteriorRingCurve();
const int nRings = poPoly->getNumInteriorRings();
if (poRing == nullptr)
{
osRet += "empty";
}
else
{
osRet += CPLOPrintf("%d points", poRing->getNumPoints());
if (wkbFlatten(poRing->getGeometryType()) ==
wkbCompoundCurve)
{
osRet += " (";
osRet += poRing->dumpReadable(nullptr, papszOptions);
osRet += ")";
}
if (nRings)
{
osRet += CPLOPrintf(", %d inner rings (", nRings);
for (int ir = 0; ir < nRings; ir++)
{
poRing = poPoly->getInteriorRingCurve(ir);
if (ir)
osRet += ", ";
osRet +=
CPLOPrintf("%d points", poRing->getNumPoints());
if (wkbFlatten(poRing->getGeometryType()) ==
wkbCompoundCurve)
{
osRet += " (";
osRet +=
poRing->dumpReadable(nullptr, papszOptions);
osRet += ")";
}
}
osRet += ")";
}
}
osRet += "\n";
break;
}
case wkbCompoundCurve:
case wkbCompoundCurveZ:
case wkbCompoundCurveM:
case wkbCompoundCurveZM:
{
const OGRCompoundCurve *poCC = toCompoundCurve();
if (poCC->getNumCurves() == 0)
{
osRet += "empty";
}
else
{
for (int i = 0; i < poCC->getNumCurves(); i++)
{
if (i)
osRet += ", ";
osRet +=
CPLOPrintf("%s (%d points)",
poCC->getCurve(i)->getGeometryName(),
poCC->getCurve(i)->getNumPoints());
}
}
break;
}
case wkbMultiPoint:
case wkbMultiLineString:
case wkbMultiPolygon:
case wkbMultiCurve:
case wkbMultiSurface:
case wkbGeometryCollection:
case wkbMultiPoint25D:
case wkbMultiLineString25D:
case wkbMultiPolygon25D:
case wkbMultiCurveZ:
case wkbMultiSurfaceZ:
case wkbGeometryCollection25D:
case wkbMultiPointM:
case wkbMultiLineStringM:
case wkbMultiPolygonM:
case wkbMultiCurveM:
case wkbMultiSurfaceM:
case wkbGeometryCollectionM:
case wkbMultiPointZM:
case wkbMultiLineStringZM:
case wkbMultiPolygonZM:
case wkbMultiCurveZM:
case wkbMultiSurfaceZM:
case wkbGeometryCollectionZM:
{
const OGRGeometryCollection *poColl = toGeometryCollection();
osRet +=
CPLOPrintf("%d geometries:\n", poColl->getNumGeometries());
for (auto &&poSubGeom : *poColl)
{
osRet += pszPrefix;
osRet += poSubGeom->dumpReadable(pszPrefix, papszOptions);
}
break;
}
case wkbLinearRing:
case wkbCurve:
case wkbSurface:
case wkbCurveZ:
case wkbSurfaceZ:
case wkbCurveM:
case wkbSurfaceM:
case wkbCurveZM:
case wkbSurfaceZM:
break;
}
}
else if (pszDisplayGeometry != nullptr && EQUAL(pszDisplayGeometry, "WKT"))
{
OGRErr err(OGRERR_NONE);
std::string wkt = exportToWkt(OGRWktOptions(), &err);
if (err == OGRERR_NONE)
{
osRet += pszPrefix;
osRet += wkt.data();
osRet += '\n';
}
}
else if (pszDisplayGeometry == nullptr || CPLTestBool(pszDisplayGeometry) ||
EQUAL(pszDisplayGeometry, "ISO_WKT"))
{
OGRErr err(OGRERR_NONE);
OGRWktOptions opts;
opts.variant = wkbVariantIso;
std::string wkt = exportToWkt(opts, &err);
if (err == OGRERR_NONE)
{
osRet += pszPrefix;
osRet += wkt.data();
osRet += '\n';
}
}
return osRet;
}
/************************************************************************/
/* OGR_G_DumpReadable() */
/************************************************************************/
/**
* \brief Dump geometry in well known text format to indicated output file.
*
* This method is the same as the CPP method OGRGeometry::dumpReadable.
*
* @param hGeom handle on the geometry to dump.
* @param fp the text file to write the geometry to.
* @param pszPrefix the prefix to put on each line of output.
*/
void OGR_G_DumpReadable(OGRGeometryH hGeom, FILE *fp, const char *pszPrefix)
{
VALIDATE_POINTER0(hGeom, "OGR_G_DumpReadable");
OGRGeometry::FromHandle(hGeom)->dumpReadable(fp, pszPrefix);
}
/************************************************************************/
/* assignSpatialReference() */
/************************************************************************/
/**
* \brief 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).
*
* This is similar to the SFCOM IGeometry::put_SpatialReference() method.
*
* This method is the same as the C function OGR_G_AssignSpatialReference().
*
* @param poSR new spatial reference system to apply.
*/
void OGRGeometry::assignSpatialReference(const OGRSpatialReference *poSR)
{
// Do in that order to properly handle poSR == poSRS
if (poSR != nullptr)
const_cast<OGRSpatialReference *>(poSR)->Reference();
if (poSRS != nullptr)
const_cast<OGRSpatialReference *>(poSRS)->Release();
poSRS = poSR;
}
/************************************************************************/
/* OGR_G_AssignSpatialReference() */
/************************************************************************/
/**
* \brief 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).
*
* This is similar to the SFCOM IGeometry::put_SpatialReference() method.
*
* This function is the same as the CPP method
* OGRGeometry::assignSpatialReference.
*
* @param hGeom handle on the geometry to apply the new spatial reference
* system.
* @param hSRS handle on the new spatial reference system to apply.
*/
void OGR_G_AssignSpatialReference(OGRGeometryH hGeom, OGRSpatialReferenceH hSRS)
{
VALIDATE_POINTER0(hGeom, "OGR_G_AssignSpatialReference");
OGRGeometry::FromHandle(hGeom)->assignSpatialReference(
OGRSpatialReference::FromHandle(hSRS));
}
/************************************************************************/
/* Intersects() */
/************************************************************************/
/**
* \brief Do these features 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.
*
* The poOtherGeom argument may be safely NULL, but in this case the method
* will always return TRUE. That is, a NULL geometry is treated as being
* everywhere.
*
* This method is the same as the C function OGR_G_Intersects().
*
* @param poOtherGeom the other geometry to test against.
*
* @return TRUE if the geometries intersect, otherwise FALSE.
*/
OGRBoolean OGRGeometry::Intersects(const OGRGeometry *poOtherGeom) const
{
if (poOtherGeom == nullptr)
return TRUE;
OGREnvelope oEnv1;
getEnvelope(&oEnv1);
OGREnvelope oEnv2;
poOtherGeom->getEnvelope(&oEnv2);
if (oEnv1.MaxX < oEnv2.MinX || oEnv1.MaxY < oEnv2.MinY ||
oEnv2.MaxX < oEnv1.MinX || oEnv2.MaxY < oEnv1.MinY)
return FALSE;
#ifndef HAVE_GEOS
// Without GEOS we assume that envelope overlap is equivalent to
// actual intersection.
return TRUE;
#else
GEOSContextHandle_t hGEOSCtxt = createGEOSContext();
GEOSGeom hThisGeosGeom = exportToGEOS(hGEOSCtxt);
GEOSGeom hOtherGeosGeom = poOtherGeom->exportToGEOS(hGEOSCtxt);
OGRBoolean bResult = FALSE;
if (hThisGeosGeom != nullptr && hOtherGeosGeom != nullptr)
{
bResult =
GEOSIntersects_r(hGEOSCtxt, hThisGeosGeom, hOtherGeosGeom) != 0;
}
GEOSGeom_destroy_r(hGEOSCtxt, hThisGeosGeom);
GEOSGeom_destroy_r(hGEOSCtxt, hOtherGeosGeom);
freeGEOSContext(hGEOSCtxt);
return bResult;
#endif // HAVE_GEOS
}
// Old API compatibility function.
//! @cond Doxygen_Suppress
OGRBoolean OGRGeometry::Intersect(OGRGeometry *poOtherGeom) const
{
return Intersects(poOtherGeom);
}
//! @endcond
/************************************************************************/
/* OGR_G_Intersects() */
/************************************************************************/
/**
* \brief Do these features 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.
*
* This function is the same as the CPP method OGRGeometry::Intersects.
*
* @param hGeom handle on the first geometry.
* @param hOtherGeom handle on the other geometry to test against.
*
* @return TRUE if the geometries intersect, otherwise FALSE.
*/
int OGR_G_Intersects(OGRGeometryH hGeom, OGRGeometryH hOtherGeom)
{
VALIDATE_POINTER1(hGeom, "OGR_G_Intersects", FALSE);
VALIDATE_POINTER1(hOtherGeom, "OGR_G_Intersects", FALSE);
return OGRGeometry::FromHandle(hGeom)->Intersects(
OGRGeometry::FromHandle(hOtherGeom));
}
//! @cond Doxygen_Suppress
int OGR_G_Intersect(OGRGeometryH hGeom, OGRGeometryH hOtherGeom)
{
VALIDATE_POINTER1(hGeom, "OGR_G_Intersect", FALSE);
VALIDATE_POINTER1(hOtherGeom, "OGR_G_Intersect", FALSE);
return OGRGeometry::FromHandle(hGeom)->Intersects(
OGRGeometry::FromHandle(hOtherGeom));
}
//! @endcond
/************************************************************************/
/* transformTo() */
/************************************************************************/
/**
* \brief Transform geometry to new spatial reference system.
*
* This method 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 method 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 method requires internal creation and initialization of an
* OGRCoordinateTransformation object it is significantly more expensive to
* use this method to transform many geometries than it is to create the
* OGRCoordinateTransformation in advance, and call transform() with that
* transformation. This method exists primarily for convenience when only
* transforming a single geometry.
*
* This method is the same as the C function OGR_G_TransformTo().
*
* @param poSR spatial reference system to transform to.
*
* @return OGRERR_NONE on success, or an error code.
*/
OGRErr OGRGeometry::transformTo(const OGRSpatialReference *poSR)
{
if (getSpatialReference() == nullptr)
{
CPLError(CE_Failure, CPLE_AppDefined, "Geometry has no SRS");
return OGRERR_FAILURE;
}
if (poSR == nullptr)
{
CPLError(CE_Failure, CPLE_AppDefined, "Target SRS is NULL");
return OGRERR_FAILURE;
}
OGRCoordinateTransformation *poCT =
OGRCreateCoordinateTransformation(getSpatialReference(), poSR);
if (poCT == nullptr)
return OGRERR_FAILURE;
const OGRErr eErr = transform(poCT);
delete poCT;
return eErr;
}
/************************************************************************/
/* OGR_G_TransformTo() */
/************************************************************************/
/**
* \brief 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.
*
* This function is the same as the CPP method OGRGeometry::transformTo.
*
* @param hGeom handle on the geometry to apply the transform to.
* @param hSRS handle on the spatial reference system to apply.
*
* @return OGRERR_NONE on success, or an error code.
*/
OGRErr OGR_G_TransformTo(OGRGeometryH hGeom, OGRSpatialReferenceH hSRS)
{
VALIDATE_POINTER1(hGeom, "OGR_G_TransformTo", OGRERR_FAILURE);
return OGRGeometry::FromHandle(hGeom)->transformTo(
OGRSpatialReference::FromHandle(hSRS));
}
/**
* \fn OGRErr OGRGeometry::transform( OGRCoordinateTransformation *poCT );
*
* \brief Apply arbitrary coordinate transformation to geometry.
*
* This method 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.
*
* Note that this method does not require that the geometry already
* have a spatial reference system. It will be assumed that they can
* be treated as having the source spatial reference system of the
* OGRCoordinateTransformation object, and the actual SRS of the geometry
* will be ignored. On successful completion the output OGRSpatialReference
* of the OGRCoordinateTransformation will be assigned to the geometry.
*
* This method only does reprojection on a point-by-point basis. It does not
* include advanced logic to deal with discontinuities at poles or antimeridian.
* For that, use the OGRGeometryFactory::transformWithOptions() method.
*
* This method is the same as the C function OGR_G_Transform().
*
* @param poCT the transformation to apply.
*
* @return OGRERR_NONE on success or an error code.
*/
/************************************************************************/
/* OGR_G_Transform() */
/************************************************************************/
/**
* \brief Apply arbitrary coordinate transformation to geometry.
*
* 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.
*
* Note that this function does not require that the geometry already
* have a spatial reference system. It will be assumed that they can
* be treated as having the source spatial reference system of the
* OGRCoordinateTransformation object, and the actual SRS of the geometry
* will be ignored. On successful completion the output OGRSpatialReference
* of the OGRCoordinateTransformation will be assigned to the geometry.
*
* This function only does reprojection on a point-by-point basis. It does not
* include advanced logic to deal with discontinuities at poles or antimeridian.
* For that, use the OGR_GeomTransformer_Create() and
* OGR_GeomTransformer_Transform() functions.
*
* This function is the same as the CPP method OGRGeometry::transform.
*
* @param hGeom handle on the geometry to apply the transform to.
* @param hTransform handle on the transformation to apply.
*
* @return OGRERR_NONE on success or an error code.
*/
OGRErr OGR_G_Transform(OGRGeometryH hGeom,
OGRCoordinateTransformationH hTransform)
{
VALIDATE_POINTER1(hGeom, "OGR_G_Transform", OGRERR_FAILURE);
return OGRGeometry::FromHandle(hGeom)->transform(
OGRCoordinateTransformation::FromHandle(hTransform));
}
/**
* \fn int OGRGeometry::getDimension() const;
*
* \brief Get the dimension of this object.
*
* This method corresponds to the SFCOM IGeometry::GetDimension() method.
* It indicates the dimension of the object, but does not indicate the
* dimension of the underlying space (as indicated by
* OGRGeometry::getCoordinateDimension()).
*
* This method is the same as the C function OGR_G_GetDimension().
*
* @return 0 for points, 1 for lines and 2 for surfaces.
*/
/**
* \brief Get the geometry type that conforms with ISO SQL/MM Part3
*
* @return the geometry type that conforms with ISO SQL/MM Part3
*/
OGRwkbGeometryType OGRGeometry::getIsoGeometryType() const
{
OGRwkbGeometryType nGType = wkbFlatten(getGeometryType());
if (flags & OGR_G_3D)
nGType = static_cast<OGRwkbGeometryType>(nGType + 1000);
if (flags & OGR_G_MEASURED)
nGType = static_cast<OGRwkbGeometryType>(nGType + 2000);
return nGType;
}
/************************************************************************/
/* OGRGeometry::segmentize() */
/************************************************************************/
/**
*
* \brief Modify the geometry such it has no segment longer then the
* given distance.
*
* This method modifies the geometry to add intermediate vertices if necessary
* so that the maximum length between 2 consecutive vertices is lower than
* dfMaxLength.
*
* Interpolated points will have Z and M values (if needed) set to 0.
* Distance computation is performed in 2d only
*
* This function is the same as the C function OGR_G_Segmentize()
*
* @param dfMaxLength the maximum distance between 2 points after segmentization
*/
void OGRGeometry::segmentize(CPL_UNUSED double dfMaxLength)
{
// Do nothing.
}
/************************************************************************/
/* OGR_G_Segmentize() */
/************************************************************************/
/**
*
* \brief Modify the geometry such it has no segment longer then the given
* distance.
*
* Interpolated points will have Z and M values (if needed) set to 0.
* Distance computation is performed in 2d only.
*
* This function is the same as the CPP method OGRGeometry::segmentize().
*
* @param hGeom handle on the geometry to segmentize
* @param dfMaxLength the maximum distance between 2 points after segmentization
*/
void CPL_DLL OGR_G_Segmentize(OGRGeometryH hGeom, double dfMaxLength)
{
VALIDATE_POINTER0(hGeom, "OGR_G_Segmentize");
if (dfMaxLength <= 0)
{
CPLError(CE_Failure, CPLE_AppDefined,
"dfMaxLength must be strictly positive");
return;
}
OGRGeometry::FromHandle(hGeom)->segmentize(dfMaxLength);
}
/************************************************************************/
/* OGR_G_GetDimension() */
/************************************************************************/
/**
*
* \brief Get the dimension of this geometry.
*
* 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).
*
* This function is the same as the CPP method OGRGeometry::getDimension().
*
* @param hGeom handle on the geometry to get the dimension from.
* @return 0 for points, 1 for lines and 2 for surfaces.
*/
int OGR_G_GetDimension(OGRGeometryH hGeom)
{
VALIDATE_POINTER1(hGeom, "OGR_G_GetDimension", 0);
return OGRGeometry::FromHandle(hGeom)->getDimension();
}
/************************************************************************/
/* getCoordinateDimension() */
/************************************************************************/
/**
* \brief Get the dimension of the coordinates in this object.
*
* This method is the same as the C function OGR_G_GetCoordinateDimension().
*
* @deprecated use CoordinateDimension().
*
* @return this will return 2 or 3.
*/
int OGRGeometry::getCoordinateDimension() const
{
return (flags & OGR_G_3D) ? 3 : 2;
}
/************************************************************************/
/* CoordinateDimension() */
/************************************************************************/
/**
* \brief Get the dimension of the coordinates in this object.
*
* This method is the same as the C function OGR_G_CoordinateDimension().
*
* @return this will return 2 for XY, 3 for XYZ and XYM, and 4 for XYZM data.
*
* @since GDAL 2.1
*/
int OGRGeometry::CoordinateDimension() const
{
if ((flags & OGR_G_3D) && (flags & OGR_G_MEASURED))
return 4;
else if ((flags & OGR_G_3D) || (flags & OGR_G_MEASURED))
return 3;
else
return 2;
}
/************************************************************************/
/* OGR_G_GetCoordinateDimension() */
/************************************************************************/
/**
*
* \brief Get the dimension of the coordinates in this geometry.
*
* This function is the same as the CPP method
* OGRGeometry::getCoordinateDimension().
*
* @param hGeom handle on the geometry to get the dimension of the
* coordinates from.
*
* @deprecated use OGR_G_CoordinateDimension(), OGR_G_Is3D(), or
* OGR_G_IsMeasured().
*
* @return this will return 2 or 3.
*/
int OGR_G_GetCoordinateDimension(OGRGeometryH hGeom)
{
VALIDATE_POINTER1(hGeom, "OGR_G_GetCoordinateDimension", 0);
return OGRGeometry::FromHandle(hGeom)->getCoordinateDimension();
}
/************************************************************************/
/* OGR_G_CoordinateDimension() */
/************************************************************************/
/**
*
* \brief Get the dimension of the coordinates in this geometry.
*
* This function is the same as the CPP method
* OGRGeometry::CoordinateDimension().
*
* @param hGeom handle on the geometry to get the dimension of the
* coordinates from.
*
* @return this will return 2 for XY, 3 for XYZ and XYM, and 4 for XYZM data.
*
* @since GDAL 2.1
*/
int OGR_G_CoordinateDimension(OGRGeometryH hGeom)
{
VALIDATE_POINTER1(hGeom, "OGR_G_CoordinateDimension", 0);