-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathogrgeometryfactory.cpp
6241 lines (5599 loc) · 225 KB
/
ogrgeometryfactory.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: Factory for converting geometry to and from well known binary
* format.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 1999, Frank Warmerdam
* Copyright (c) 2008-2014, Even Rouault <even dot rouault at spatialys dot 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 "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_string.h"
#include "ogr_geometry.h"
#include "ogr_api.h"
#include "ogr_core.h"
#include "ogr_geos.h"
#include "ogr_sfcgal.h"
#include "ogr_p.h"
#include "ogr_spatialref.h"
#include "ogr_srs_api.h"
#ifdef HAVE_GEOS
#include "geos_c.h"
#endif
#include "ogrgeojsonreader.h"
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstddef>
#include <algorithm>
#include <limits>
#include <new>
#include <utility>
#include <vector>
#ifndef HAVE_GEOS
#define UNUSED_IF_NO_GEOS CPL_UNUSED
#else
#define UNUSED_IF_NO_GEOS
#endif
/************************************************************************/
/* createFromWkb() */
/************************************************************************/
/**
* \brief Create a geometry object of the appropriate type from its
* well known binary representation.
*
* Note that if nBytes is passed as zero, no checking can be done on whether
* the pabyData is sufficient. This can result in a crash if the input
* data is corrupt. This function returns no indication of the number of
* bytes from the data source actually used to represent the returned
* geometry object. Use OGRGeometry::WkbSize() on the returned geometry to
* establish the number of bytes it required in WKB format.
*
* Also note that this is a static method, and that there
* is no need to instantiate an OGRGeometryFactory object.
*
* The C function OGR_G_CreateFromWkb() is the same as this method.
*
* @param pabyData pointer to the input BLOB data.
* @param poSR pointer to the spatial reference to be assigned to the
* created geometry object. This may be NULL.
* @param ppoReturn the newly created geometry object will be assigned to the
* indicated pointer on return. This will be NULL in case
* of failure. If not NULL, *ppoReturn should be freed with
* OGRGeometryFactory::destroyGeometry() after use.
* @param nBytes the number of bytes available in pabyData, or -1 if it isn't
* known
* @param eWkbVariant WKB variant.
*
* @return OGRERR_NONE if all goes well, otherwise any of
* OGRERR_NOT_ENOUGH_DATA, OGRERR_UNSUPPORTED_GEOMETRY_TYPE, or
* OGRERR_CORRUPT_DATA may be returned.
*/
OGRErr OGRGeometryFactory::createFromWkb(const void *pabyData,
const OGRSpatialReference *poSR,
OGRGeometry **ppoReturn, size_t nBytes,
OGRwkbVariant eWkbVariant)
{
size_t nBytesConsumedOutIgnored = 0;
return createFromWkb(pabyData, poSR, ppoReturn, nBytes, eWkbVariant,
nBytesConsumedOutIgnored);
}
/**
* \brief Create a geometry object of the appropriate type from its
* well known binary representation.
*
* Note that if nBytes is passed as zero, no checking can be done on whether
* the pabyData is sufficient. This can result in a crash if the input
* data is corrupt. This function returns no indication of the number of
* bytes from the data source actually used to represent the returned
* geometry object. Use OGRGeometry::WkbSize() on the returned geometry to
* establish the number of bytes it required in WKB format.
*
* Also note that this is a static method, and that there
* is no need to instantiate an OGRGeometryFactory object.
*
* The C function OGR_G_CreateFromWkb() is the same as this method.
*
* @param pabyData pointer to the input BLOB data.
* @param poSR pointer to the spatial reference to be assigned to the
* created geometry object. This may be NULL.
* @param ppoReturn the newly created geometry object will be assigned to the
* indicated pointer on return. This will be NULL in case
* of failure. If not NULL, *ppoReturn should be freed with
* OGRGeometryFactory::destroyGeometry() after use.
* @param nBytes the number of bytes available in pabyData, or -1 if it isn't
* known
* @param eWkbVariant WKB variant.
* @param nBytesConsumedOut output parameter. Number of bytes consumed.
*
* @return OGRERR_NONE if all goes well, otherwise any of
* OGRERR_NOT_ENOUGH_DATA, OGRERR_UNSUPPORTED_GEOMETRY_TYPE, or
* OGRERR_CORRUPT_DATA may be returned.
* @since GDAL 2.3
*/
OGRErr OGRGeometryFactory::createFromWkb(const void *pabyData,
const OGRSpatialReference *poSR,
OGRGeometry **ppoReturn, size_t nBytes,
OGRwkbVariant eWkbVariant,
size_t &nBytesConsumedOut)
{
const GByte *l_pabyData = static_cast<const GByte *>(pabyData);
nBytesConsumedOut = 0;
*ppoReturn = nullptr;
if (nBytes < 9 && nBytes != static_cast<size_t>(-1))
return OGRERR_NOT_ENOUGH_DATA;
/* -------------------------------------------------------------------- */
/* Get the byte order byte. The extra tests are to work around */
/* bug sin the WKB of DB2 v7.2 as identified by Safe Software. */
/* -------------------------------------------------------------------- */
const int nByteOrder = DB2_V72_FIX_BYTE_ORDER(*l_pabyData);
if (nByteOrder != wkbXDR && nByteOrder != wkbNDR)
{
CPLDebug("OGR",
"OGRGeometryFactory::createFromWkb() - got corrupt data.\n"
"%02X%02X%02X%02X%02X%02X%02X%02X%02X",
l_pabyData[0], l_pabyData[1], l_pabyData[2], l_pabyData[3],
l_pabyData[4], l_pabyData[5], l_pabyData[6], l_pabyData[7],
l_pabyData[8]);
return OGRERR_CORRUPT_DATA;
}
/* -------------------------------------------------------------------- */
/* Get the geometry feature type. For now we assume that */
/* geometry type is between 0 and 255 so we only have to fetch */
/* one byte. */
/* -------------------------------------------------------------------- */
OGRwkbGeometryType eGeometryType = wkbUnknown;
const OGRErr err =
OGRReadWKBGeometryType(l_pabyData, eWkbVariant, &eGeometryType);
if (err != OGRERR_NONE)
return err;
/* -------------------------------------------------------------------- */
/* Instantiate a geometry of the appropriate type, and */
/* initialize from the input stream. */
/* -------------------------------------------------------------------- */
OGRGeometry *poGeom = createGeometry(eGeometryType);
if (poGeom == nullptr)
return OGRERR_UNSUPPORTED_GEOMETRY_TYPE;
/* -------------------------------------------------------------------- */
/* Import from binary. */
/* -------------------------------------------------------------------- */
const OGRErr eErr = poGeom->importFromWkb(l_pabyData, nBytes, eWkbVariant,
nBytesConsumedOut);
if (eErr != OGRERR_NONE)
{
delete poGeom;
return eErr;
}
/* -------------------------------------------------------------------- */
/* Assign spatial reference system. */
/* -------------------------------------------------------------------- */
if (poGeom->hasCurveGeometry() &&
CPLTestBool(CPLGetConfigOption("OGR_STROKE_CURVE", "FALSE")))
{
OGRGeometry *poNewGeom = poGeom->getLinearGeometry();
delete poGeom;
poGeom = poNewGeom;
}
poGeom->assignSpatialReference(poSR);
*ppoReturn = poGeom;
return OGRERR_NONE;
}
/************************************************************************/
/* OGR_G_CreateFromWkb() */
/************************************************************************/
/**
* \brief Create a geometry object of the appropriate type from its
* well known binary representation.
*
* Note that if nBytes is passed as zero, no checking can be done on whether
* the pabyData is sufficient. This can result in a crash if the input
* data is corrupt. This function returns no indication of the number of
* bytes from the data source actually used to represent the returned
* geometry object. Use OGR_G_WkbSize() on the returned geometry to
* establish the number of bytes it required in WKB format.
*
* The OGRGeometryFactory::createFromWkb() CPP method is the same as this
* function.
*
* @param pabyData pointer to the input BLOB data.
* @param hSRS handle to the spatial reference to be assigned to the
* created geometry object. This may be NULL.
* @param phGeometry the newly created geometry object will
* be assigned to the indicated handle on return. This will be NULL in case
* of failure. If not NULL, *phGeometry should be freed with
* OGR_G_DestroyGeometry() after use.
* @param nBytes the number of bytes of data available in pabyData, or -1
* if it is not known, but assumed to be sufficient.
*
* @return OGRERR_NONE if all goes well, otherwise any of
* OGRERR_NOT_ENOUGH_DATA, OGRERR_UNSUPPORTED_GEOMETRY_TYPE, or
* OGRERR_CORRUPT_DATA may be returned.
*/
OGRErr CPL_DLL OGR_G_CreateFromWkb(const void *pabyData,
OGRSpatialReferenceH hSRS,
OGRGeometryH *phGeometry, int nBytes)
{
return OGRGeometryFactory::createFromWkb(
pabyData, OGRSpatialReference::FromHandle(hSRS),
reinterpret_cast<OGRGeometry **>(phGeometry), nBytes);
}
/************************************************************************/
/* OGR_G_CreateFromWkbEx() */
/************************************************************************/
/**
* \brief Create a geometry object of the appropriate type from its
* well known binary representation.
*
* Note that if nBytes is passed as zero, no checking can be done on whether
* the pabyData is sufficient. This can result in a crash if the input
* data is corrupt. This function returns no indication of the number of
* bytes from the data source actually used to represent the returned
* geometry object. Use OGR_G_WkbSizeEx() on the returned geometry to
* establish the number of bytes it required in WKB format.
*
* The OGRGeometryFactory::createFromWkb() CPP method is the same as this
* function.
*
* @param pabyData pointer to the input BLOB data.
* @param hSRS handle to the spatial reference to be assigned to the
* created geometry object. This may be NULL.
* @param phGeometry the newly created geometry object will
* be assigned to the indicated handle on return. This will be NULL in case
* of failure. If not NULL, *phGeometry should be freed with
* OGR_G_DestroyGeometry() after use.
* @param nBytes the number of bytes of data available in pabyData, or -1
* if it is not known, but assumed to be sufficient.
*
* @return OGRERR_NONE if all goes well, otherwise any of
* OGRERR_NOT_ENOUGH_DATA, OGRERR_UNSUPPORTED_GEOMETRY_TYPE, or
* OGRERR_CORRUPT_DATA may be returned.
* @since GDAL 3.3
*/
OGRErr CPL_DLL OGR_G_CreateFromWkbEx(const void *pabyData,
OGRSpatialReferenceH hSRS,
OGRGeometryH *phGeometry, size_t nBytes)
{
return OGRGeometryFactory::createFromWkb(
pabyData, OGRSpatialReference::FromHandle(hSRS),
reinterpret_cast<OGRGeometry **>(phGeometry), nBytes);
}
/************************************************************************/
/* createFromWkt() */
/************************************************************************/
/**
* \brief Create a geometry object of the appropriate type from its
* well known text representation.
*
* The C function OGR_G_CreateFromWkt() is the same as this method.
*
* @param ppszData input zero terminated string containing well known text
* representation of the geometry to be created. The pointer
* is updated to point just beyond that last character consumed.
* @param poSR pointer to the spatial reference to be assigned to the
* created geometry object. This may be NULL.
* @param ppoReturn the newly created geometry object will be assigned to the
* indicated pointer on return. This will be NULL if the
* method fails. If not NULL, *ppoReturn should be freed with
* OGRGeometryFactory::destroyGeometry() after use.
*
* <b>Example:</b>
*
* \code{.cpp}
* const char* wkt= "POINT(0 0)";
*
* // cast because OGR_G_CreateFromWkt will move the pointer
* char* pszWkt = (char*) wkt;
* OGRSpatialReferenceH ref = OSRNewSpatialReference(NULL);
* OGRGeometryH new_geom;
* OSRSetAxisMappingStrategy(poSR, OAMS_TRADITIONAL_GIS_ORDER);
* OGRErr err = OGR_G_CreateFromWkt(&pszWkt, ref, &new_geom);
* \endcode
*
*
*
* @return OGRERR_NONE if all goes well, otherwise any of
* OGRERR_NOT_ENOUGH_DATA, OGRERR_UNSUPPORTED_GEOMETRY_TYPE, or
* OGRERR_CORRUPT_DATA may be returned.
*/
OGRErr OGRGeometryFactory::createFromWkt(const char **ppszData,
const OGRSpatialReference *poSR,
OGRGeometry **ppoReturn)
{
const char *pszInput = *ppszData;
*ppoReturn = nullptr;
/* -------------------------------------------------------------------- */
/* Get the first token, which should be the geometry type. */
/* -------------------------------------------------------------------- */
char szToken[OGR_WKT_TOKEN_MAX] = {};
if (OGRWktReadToken(pszInput, szToken) == nullptr)
return OGRERR_CORRUPT_DATA;
/* -------------------------------------------------------------------- */
/* Instantiate a geometry of the appropriate type. */
/* -------------------------------------------------------------------- */
OGRGeometry *poGeom = nullptr;
if (STARTS_WITH_CI(szToken, "POINT"))
{
poGeom = new OGRPoint();
}
else if (STARTS_WITH_CI(szToken, "LINESTRING"))
{
poGeom = new OGRLineString();
}
else if (STARTS_WITH_CI(szToken, "POLYGON"))
{
poGeom = new OGRPolygon();
}
else if (STARTS_WITH_CI(szToken, "TRIANGLE"))
{
poGeom = new OGRTriangle();
}
else if (STARTS_WITH_CI(szToken, "GEOMETRYCOLLECTION"))
{
poGeom = new OGRGeometryCollection();
}
else if (STARTS_WITH_CI(szToken, "MULTIPOLYGON"))
{
poGeom = new OGRMultiPolygon();
}
else if (STARTS_WITH_CI(szToken, "MULTIPOINT"))
{
poGeom = new OGRMultiPoint();
}
else if (STARTS_WITH_CI(szToken, "MULTILINESTRING"))
{
poGeom = new OGRMultiLineString();
}
else if (STARTS_WITH_CI(szToken, "CIRCULARSTRING"))
{
poGeom = new OGRCircularString();
}
else if (STARTS_WITH_CI(szToken, "COMPOUNDCURVE"))
{
poGeom = new OGRCompoundCurve();
}
else if (STARTS_WITH_CI(szToken, "CURVEPOLYGON"))
{
poGeom = new OGRCurvePolygon();
}
else if (STARTS_WITH_CI(szToken, "MULTICURVE"))
{
poGeom = new OGRMultiCurve();
}
else if (STARTS_WITH_CI(szToken, "MULTISURFACE"))
{
poGeom = new OGRMultiSurface();
}
else if (STARTS_WITH_CI(szToken, "POLYHEDRALSURFACE"))
{
poGeom = new OGRPolyhedralSurface();
}
else if (STARTS_WITH_CI(szToken, "TIN"))
{
poGeom = new OGRTriangulatedSurface();
}
else
{
return OGRERR_UNSUPPORTED_GEOMETRY_TYPE;
}
/* -------------------------------------------------------------------- */
/* Do the import. */
/* -------------------------------------------------------------------- */
const OGRErr eErr = poGeom->importFromWkt(&pszInput);
/* -------------------------------------------------------------------- */
/* Assign spatial reference system. */
/* -------------------------------------------------------------------- */
if (eErr == OGRERR_NONE)
{
if (poGeom->hasCurveGeometry() &&
CPLTestBool(CPLGetConfigOption("OGR_STROKE_CURVE", "FALSE")))
{
OGRGeometry *poNewGeom = poGeom->getLinearGeometry();
delete poGeom;
poGeom = poNewGeom;
}
poGeom->assignSpatialReference(poSR);
*ppoReturn = poGeom;
*ppszData = pszInput;
}
else
{
delete poGeom;
}
return eErr;
}
/**
* \brief Create a geometry object of the appropriate type from its
* well known text representation.
*
* The C function OGR_G_CreateFromWkt() is the same as this method.
*
* @param pszData input zero terminated string containing well known text
* representation of the geometry to be created.
* @param poSR pointer to the spatial reference to be assigned to the
* created geometry object. This may be NULL.
* @param ppoReturn the newly created geometry object will be assigned to the
* indicated pointer on return. This will be NULL if the
* method fails. If not NULL, *ppoReturn should be freed with
* OGRGeometryFactory::destroyGeometry() after use.
* @return OGRERR_NONE if all goes well, otherwise any of
* OGRERR_NOT_ENOUGH_DATA, OGRERR_UNSUPPORTED_GEOMETRY_TYPE, or
* OGRERR_CORRUPT_DATA may be returned.
* @since GDAL 2.3
*/
OGRErr OGRGeometryFactory::createFromWkt(const char *pszData,
const OGRSpatialReference *poSR,
OGRGeometry **ppoReturn)
{
return createFromWkt(&pszData, poSR, ppoReturn);
}
/************************************************************************/
/* OGR_G_CreateFromWkt() */
/************************************************************************/
/**
* \brief Create a geometry object of the appropriate type from its well known
* text representation.
*
* The OGRGeometryFactory::createFromWkt CPP method is the same as this
* function.
*
* @param ppszData input zero terminated string containing well known text
* representation of the geometry to be created. The pointer
* is updated to point just beyond that last character consumed.
* @param hSRS handle to the spatial reference to be assigned to the
* created geometry object. This may be NULL.
* @param phGeometry the newly created geometry object will be assigned to the
* indicated handle on return. This will be NULL if the
* method fails. If not NULL, *phGeometry should be freed with
* OGR_G_DestroyGeometry() after use.
*
* @return OGRERR_NONE if all goes well, otherwise any of
* OGRERR_NOT_ENOUGH_DATA, OGRERR_UNSUPPORTED_GEOMETRY_TYPE, or
* OGRERR_CORRUPT_DATA may be returned.
*/
OGRErr CPL_DLL OGR_G_CreateFromWkt(char **ppszData, OGRSpatialReferenceH hSRS,
OGRGeometryH *phGeometry)
{
return OGRGeometryFactory::createFromWkt(
const_cast<const char **>(ppszData),
OGRSpatialReference::FromHandle(hSRS),
reinterpret_cast<OGRGeometry **>(phGeometry));
}
/************************************************************************/
/* createGeometry() */
/************************************************************************/
/**
* \brief 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.
*
* This method is the same as the C function OGR_G_CreateGeometry().
*
* @param eGeometryType the type code of the geometry class to be instantiated.
*
* @return the newly create geometry or NULL on failure. Should be freed with
* OGRGeometryFactory::destroyGeometry() after use.
*/
OGRGeometry *
OGRGeometryFactory::createGeometry(OGRwkbGeometryType eGeometryType)
{
OGRGeometry *poGeom = nullptr;
switch (wkbFlatten(eGeometryType))
{
case wkbPoint:
poGeom = new (std::nothrow) OGRPoint();
break;
case wkbLineString:
poGeom = new (std::nothrow) OGRLineString();
break;
case wkbPolygon:
poGeom = new (std::nothrow) OGRPolygon();
break;
case wkbGeometryCollection:
poGeom = new (std::nothrow) OGRGeometryCollection();
break;
case wkbMultiPolygon:
poGeom = new (std::nothrow) OGRMultiPolygon();
break;
case wkbMultiPoint:
poGeom = new (std::nothrow) OGRMultiPoint();
break;
case wkbMultiLineString:
poGeom = new (std::nothrow) OGRMultiLineString();
break;
case wkbLinearRing:
poGeom = new (std::nothrow) OGRLinearRing();
break;
case wkbCircularString:
poGeom = new (std::nothrow) OGRCircularString();
break;
case wkbCompoundCurve:
poGeom = new (std::nothrow) OGRCompoundCurve();
break;
case wkbCurvePolygon:
poGeom = new (std::nothrow) OGRCurvePolygon();
break;
case wkbMultiCurve:
poGeom = new (std::nothrow) OGRMultiCurve();
break;
case wkbMultiSurface:
poGeom = new (std::nothrow) OGRMultiSurface();
break;
case wkbTriangle:
poGeom = new (std::nothrow) OGRTriangle();
break;
case wkbPolyhedralSurface:
poGeom = new (std::nothrow) OGRPolyhedralSurface();
break;
case wkbTIN:
poGeom = new (std::nothrow) OGRTriangulatedSurface();
break;
case wkbUnknown:
break;
default:
CPLAssert(false);
break;
}
if (poGeom)
{
if (OGR_GT_HasZ(eGeometryType))
poGeom->set3D(true);
if (OGR_GT_HasM(eGeometryType))
poGeom->setMeasured(true);
}
return poGeom;
}
/************************************************************************/
/* OGR_G_CreateGeometry() */
/************************************************************************/
/**
* \brief 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.
*
* This function is the same as the CPP method
* OGRGeometryFactory::createGeometry.
*
* @param eGeometryType the type code of the geometry to be created.
*
* @return handle to the newly create geometry or NULL on failure. Should be
* freed with OGR_G_DestroyGeometry() after use.
*/
OGRGeometryH OGR_G_CreateGeometry(OGRwkbGeometryType eGeometryType)
{
return OGRGeometry::ToHandle(
OGRGeometryFactory::createGeometry(eGeometryType));
}
/************************************************************************/
/* destroyGeometry() */
/************************************************************************/
/**
* \brief Destroy geometry object.
*
* Equivalent to invoking delete on a geometry, but it guaranteed to take
* place within the context of the GDAL/OGR heap.
*
* This method is the same as the C function OGR_G_DestroyGeometry().
*
* @param poGeom the geometry to deallocate.
*/
void OGRGeometryFactory::destroyGeometry(OGRGeometry *poGeom)
{
delete poGeom;
}
/************************************************************************/
/* OGR_G_DestroyGeometry() */
/************************************************************************/
/**
* \brief Destroy geometry object.
*
* Equivalent to invoking delete on a geometry, but it guaranteed to take
* place within the context of the GDAL/OGR heap.
*
* This function is the same as the CPP method
* OGRGeometryFactory::destroyGeometry.
*
* @param hGeom handle to the geometry to delete.
*/
void OGR_G_DestroyGeometry(OGRGeometryH hGeom)
{
delete OGRGeometry::FromHandle(hGeom);
}
/************************************************************************/
/* forceToPolygon() */
/************************************************************************/
/**
* \brief Convert to polygon.
*
* Tries to force the provided geometry to be a polygon. This effects a change
* on multipolygons.
* Starting with GDAL 2.0, curve polygons or closed curves will be changed to
* polygons. The passed in geometry is consumed and a new one returned (or
* potentially the same one).
*
* Note: the resulting polygon may break the Simple Features rules for polygons,
* for example when converting from a multi-part multipolygon.
*
* @param poGeom the input geometry - ownership is passed to the method.
* @return new geometry.
*/
OGRGeometry *OGRGeometryFactory::forceToPolygon(OGRGeometry *poGeom)
{
if (poGeom == nullptr)
return nullptr;
OGRwkbGeometryType eGeomType = wkbFlatten(poGeom->getGeometryType());
if (eGeomType == wkbCurvePolygon)
{
OGRCurvePolygon *poCurve = poGeom->toCurvePolygon();
if (!poGeom->hasCurveGeometry(TRUE))
return OGRSurface::CastToPolygon(poCurve);
OGRPolygon *poPoly = poCurve->CurvePolyToPoly();
delete poGeom;
return poPoly;
}
// base polygon or triangle
if (OGR_GT_IsSubClassOf(eGeomType, wkbPolygon))
{
return OGRSurface::CastToPolygon(poGeom->toSurface());
}
if (OGR_GT_IsCurve(eGeomType))
{
OGRCurve *poCurve = poGeom->toCurve();
if (poCurve->getNumPoints() >= 3 && poCurve->get_IsClosed())
{
OGRPolygon *poPolygon = new OGRPolygon();
poPolygon->assignSpatialReference(poGeom->getSpatialReference());
if (!poGeom->hasCurveGeometry(TRUE))
{
poPolygon->addRingDirectly(OGRCurve::CastToLinearRing(poCurve));
}
else
{
OGRLineString *poLS = poCurve->CurveToLine();
poPolygon->addRingDirectly(OGRCurve::CastToLinearRing(poLS));
delete poGeom;
}
return poPolygon;
}
}
if (OGR_GT_IsSubClassOf(eGeomType, wkbPolyhedralSurface))
{
OGRPolyhedralSurface *poPS = poGeom->toPolyhedralSurface();
if (poPS->getNumGeometries() == 1)
{
poGeom = OGRSurface::CastToPolygon(
poPS->getGeometryRef(0)->clone()->toSurface());
delete poPS;
return poGeom;
}
}
if (eGeomType != wkbGeometryCollection && eGeomType != wkbMultiPolygon &&
eGeomType != wkbMultiSurface)
return poGeom;
// Build an aggregated polygon from all the polygon rings in the container.
OGRPolygon *poPolygon = new OGRPolygon();
OGRGeometryCollection *poGC = poGeom->toGeometryCollection();
if (poGeom->hasCurveGeometry())
{
OGRGeometryCollection *poNewGC =
poGC->getLinearGeometry()->toGeometryCollection();
delete poGC;
poGeom = poNewGC;
poGC = poNewGC;
}
poPolygon->assignSpatialReference(poGeom->getSpatialReference());
for (int iGeom = 0; iGeom < poGC->getNumGeometries(); iGeom++)
{
if (wkbFlatten(poGC->getGeometryRef(iGeom)->getGeometryType()) !=
wkbPolygon)
continue;
OGRPolygon *poOldPoly = poGC->getGeometryRef(iGeom)->toPolygon();
if (poOldPoly->getExteriorRing() == nullptr)
continue;
poPolygon->addRingDirectly(poOldPoly->stealExteriorRing());
for (int iRing = 0; iRing < poOldPoly->getNumInteriorRings(); iRing++)
poPolygon->addRingDirectly(poOldPoly->stealInteriorRing(iRing));
}
delete poGC;
return poPolygon;
}
/************************************************************************/
/* OGR_G_ForceToPolygon() */
/************************************************************************/
/**
* \brief Convert to polygon.
*
* This function is the same as the C++ method
* OGRGeometryFactory::forceToPolygon().
*
* @param hGeom handle to the geometry to convert (ownership surrendered).
* @return the converted geometry (ownership to caller).
*
* @since GDAL/OGR 1.8.0
*/
OGRGeometryH OGR_G_ForceToPolygon(OGRGeometryH hGeom)
{
return OGRGeometry::ToHandle(
OGRGeometryFactory::forceToPolygon(OGRGeometry::FromHandle(hGeom)));
}
/************************************************************************/
/* forceToMultiPolygon() */
/************************************************************************/
/**
* \brief Convert to multipolygon.
*
* Tries to force the provided geometry to be a multipolygon. Currently
* this just effects a change on polygons. The passed in geometry is
* consumed and a new one returned (or potentially the same one).
*
* @return new geometry.
*/
OGRGeometry *OGRGeometryFactory::forceToMultiPolygon(OGRGeometry *poGeom)
{
if (poGeom == nullptr)
return nullptr;
OGRwkbGeometryType eGeomType = wkbFlatten(poGeom->getGeometryType());
/* -------------------------------------------------------------------- */
/* If this is already a MultiPolygon, nothing to do */
/* -------------------------------------------------------------------- */
if (eGeomType == wkbMultiPolygon)
{
return poGeom;
}
/* -------------------------------------------------------------------- */
/* If this is already a MultiSurface with compatible content, */
/* just cast */
/* -------------------------------------------------------------------- */
if (eGeomType == wkbMultiSurface)
{
OGRMultiSurface *poMS = poGeom->toMultiSurface();
if (!poMS->hasCurveGeometry(TRUE))
{
return OGRMultiSurface::CastToMultiPolygon(poMS);
}
}
/* -------------------------------------------------------------------- */
/* Check for the case of a geometrycollection that can be */
/* promoted to MultiPolygon. */
/* -------------------------------------------------------------------- */
if (eGeomType == wkbGeometryCollection || eGeomType == wkbMultiSurface)
{
bool bAllPoly = true;
OGRGeometryCollection *poGC = poGeom->toGeometryCollection();
if (poGeom->hasCurveGeometry())
{
OGRGeometryCollection *poNewGC =
poGC->getLinearGeometry()->toGeometryCollection();
delete poGC;
poGeom = poNewGC;
poGC = poNewGC;
}
bool bCanConvertToMultiPoly = true;
for (int iGeom = 0; iGeom < poGC->getNumGeometries(); iGeom++)
{
OGRwkbGeometryType eSubGeomType =
wkbFlatten(poGC->getGeometryRef(iGeom)->getGeometryType());
if (eSubGeomType != wkbPolygon)
bAllPoly = false;
if (eSubGeomType != wkbMultiPolygon && eSubGeomType != wkbPolygon &&
eSubGeomType != wkbPolyhedralSurface && eSubGeomType != wkbTIN)
{
bCanConvertToMultiPoly = false;
}
}
if (!bCanConvertToMultiPoly)
return poGeom;
OGRMultiPolygon *poMP = new OGRMultiPolygon();
poMP->assignSpatialReference(poGeom->getSpatialReference());
while (poGC->getNumGeometries() > 0)
{
OGRGeometry *poSubGeom = poGC->getGeometryRef(0);
poGC->removeGeometry(0, FALSE);
if (bAllPoly)
{
poMP->addGeometryDirectly(poSubGeom);
}
else
{
poSubGeom = forceToMultiPolygon(poSubGeom);
OGRMultiPolygon *poSubMP = poSubGeom->toMultiPolygon();
while (poSubMP != nullptr && poSubMP->getNumGeometries() > 0)
{
poMP->addGeometryDirectly(poSubMP->getGeometryRef(0));
poSubMP->removeGeometry(0, FALSE);
}
delete poSubMP;
}
}
delete poGC;
return poMP;
}
if (eGeomType == wkbCurvePolygon)
{
OGRPolygon *poPoly = poGeom->toCurvePolygon()->CurvePolyToPoly();
OGRMultiPolygon *poMP = new OGRMultiPolygon();
poMP->assignSpatialReference(poGeom->getSpatialReference());
poMP->addGeometryDirectly(poPoly);
delete poGeom;
return poMP;
}
/* -------------------------------------------------------------------- */
/* If it is PolyhedralSurface or TIN, then pretend it is a */
/* multipolygon. */
/* -------------------------------------------------------------------- */
if (OGR_GT_IsSubClassOf(eGeomType, wkbPolyhedralSurface))
{
return OGRPolyhedralSurface::CastToMultiPolygon(
poGeom->toPolyhedralSurface());
}
if (eGeomType == wkbTriangle)
{
return forceToMultiPolygon(forceToPolygon(poGeom));
}
/* -------------------------------------------------------------------- */
/* Eventually we should try to split the polygon into component */
/* island polygons. But that is a lot of work and can be put off. */
/* -------------------------------------------------------------------- */
if (eGeomType != wkbPolygon)
return poGeom;
OGRMultiPolygon *poMP = new OGRMultiPolygon();
poMP->assignSpatialReference(poGeom->getSpatialReference());
poMP->addGeometryDirectly(poGeom);
return poMP;
}
/************************************************************************/
/* OGR_G_ForceToMultiPolygon() */
/************************************************************************/