forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathogrgeometrycollection.cpp
1301 lines (1097 loc) · 42.6 KB
/
ogrgeometrycollection.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: The OGRGeometryCollection class.
* 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 <cstddef>
#include <cstring>
#include <new>
#include "cpl_conv.h"
#include "cpl_error.h"
#include "cpl_string.h"
#include "cpl_vsi.h"
#include "ogr_api.h"
#include "ogr_core.h"
#include "ogr_p.h"
#include "ogr_spatialref.h"
CPL_CVSID("$Id$")
/************************************************************************/
/* OGRGeometryCollection() */
/************************************************************************/
/**
* \brief Create an empty geometry collection.
*/
OGRGeometryCollection::OGRGeometryCollection() = default;
/************************************************************************/
/* OGRGeometryCollection( const OGRGeometryCollection& ) */
/************************************************************************/
/**
* \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
*/
OGRGeometryCollection::OGRGeometryCollection(
const OGRGeometryCollection& other ) :
OGRGeometry(other)
{
// Do not use addGeometry() as it is virtual.
papoGeoms = static_cast<OGRGeometry **>(
VSI_CALLOC_VERBOSE(sizeof(void*), other.nGeomCount));
if( papoGeoms )
{
nGeomCount = other.nGeomCount;
for( int i = 0; i < other.nGeomCount; i++ )
{
papoGeoms[i] = other.papoGeoms[i]->clone();
}
}
}
/************************************************************************/
/* ~OGRGeometryCollection() */
/************************************************************************/
OGRGeometryCollection::~OGRGeometryCollection()
{
OGRGeometryCollection::empty();
}
/************************************************************************/
/* operator=( const OGRGeometryCollection&) */
/************************************************************************/
/**
* \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
*/
OGRGeometryCollection& OGRGeometryCollection::operator=(
const OGRGeometryCollection& other )
{
if( this != &other)
{
empty();
OGRGeometry::operator=( other );
for( int i = 0; i < other.nGeomCount; i++ )
{
addGeometry( other.papoGeoms[i] );
}
}
return *this;
}
/************************************************************************/
/* empty() */
/************************************************************************/
void OGRGeometryCollection::empty()
{
if( papoGeoms != nullptr )
{
for( auto&& poSubGeom: *this )
{
delete poSubGeom;
}
CPLFree( papoGeoms );
}
nGeomCount = 0;
papoGeoms = nullptr;
}
/************************************************************************/
/* clone() */
/************************************************************************/
OGRGeometryCollection *OGRGeometryCollection::clone() const
{
return new (std::nothrow) OGRGeometryCollection(*this);
}
/************************************************************************/
/* getGeometryType() */
/************************************************************************/
OGRwkbGeometryType OGRGeometryCollection::getGeometryType() const
{
if( (flags & OGR_G_3D) && (flags & OGR_G_MEASURED) )
return wkbGeometryCollectionZM;
else if( flags & OGR_G_MEASURED )
return wkbGeometryCollectionM;
else if( flags & OGR_G_3D )
return wkbGeometryCollection25D;
else
return wkbGeometryCollection;
}
/************************************************************************/
/* getDimension() */
/************************************************************************/
int OGRGeometryCollection::getDimension() const
{
int nDimension = 0;
// FIXME? Not sure if it is really appropriate to take the max in case
// of geometries of different dimension.
for( auto&& poSubGeom: *this )
{
int nSubGeomDimension = poSubGeom->getDimension();
if( nSubGeomDimension > nDimension )
{
nDimension = nSubGeomDimension;
if( nDimension == 2 )
break;
}
}
return nDimension;
}
/************************************************************************/
/* flattenTo2D() */
/************************************************************************/
void OGRGeometryCollection::flattenTo2D()
{
for( auto&& poSubGeom: *this )
{
poSubGeom->flattenTo2D();
}
flags &= ~OGR_G_3D;
flags &= ~OGR_G_MEASURED;
}
/************************************************************************/
/* getGeometryName() */
/************************************************************************/
const char * OGRGeometryCollection::getGeometryName() const
{
return "GEOMETRYCOLLECTION";
}
/************************************************************************/
/* getNumGeometries() */
/************************************************************************/
/**
* \brief Fetch number of geometries in container.
*
* This method relates to the SFCOM IGeometryCollect::get_NumGeometries()
* method.
*
* @return count of children geometries. May be zero.
*/
int OGRGeometryCollection::getNumGeometries() const
{
return nGeomCount;
}
/************************************************************************/
/* getGeometryRef() */
/************************************************************************/
/**
* \brief Fetch geometry from container.
*
* This method returns a pointer to a geometry within the container. The
* returned geometry remains owned by the container, and should not be
* modified. The pointer is only valid until the next change to the
* geometry container. Use IGeometry::clone() to make a copy.
*
* This method relates to the SFCOM IGeometryCollection::get_Geometry() method.
*
* @param i the index of the geometry to fetch, between 0 and
* getNumGeometries() - 1.
* @return pointer to requested geometry.
*/
OGRGeometry * OGRGeometryCollection::getGeometryRef( int i )
{
if( i < 0 || i >= nGeomCount )
return nullptr;
return papoGeoms[i];
}
/**
* \brief Fetch geometry from container.
*
* This method returns a pointer to a geometry within the container. The
* returned geometry remains owned by the container, and should not be
* modified. The pointer is only valid until the next change to the
* geometry container. Use IGeometry::clone() to make a copy.
*
* This method relates to the SFCOM IGeometryCollection::get_Geometry() method.
*
* @param i the index of the geometry to fetch, between 0 and
* getNumGeometries() - 1.
* @return pointer to requested geometry.
*/
const OGRGeometry * OGRGeometryCollection::getGeometryRef( int i ) const
{
if( i < 0 || i >= nGeomCount )
return nullptr;
return papoGeoms[i];
}
/************************************************************************/
/* addGeometry() */
/* */
/* Add a new geometry to a collection. Subclasses should */
/* override this to verify the type of the new geometry, and */
/* then call this method to actually add it. */
/************************************************************************/
/**
* \brief Add a geometry to the container.
*
* Some subclasses of OGRGeometryCollection restrict the types of geometry
* that can be added, and may return an error. The passed geometry is cloned
* to make an internal copy.
*
* There is no SFCOM analog to this method.
*
* This method is the same as the C function OGR_G_AddGeometry().
*
* @param poNewGeom geometry to add to the container.
*
* @return OGRERR_NONE if successful, or OGRERR_UNSUPPORTED_GEOMETRY_TYPE if
* the geometry type is illegal for the type of geometry container.
*/
OGRErr OGRGeometryCollection::addGeometry( const OGRGeometry * poNewGeom )
{
OGRGeometry *poClone = poNewGeom->clone();
if( poClone == nullptr )
return OGRERR_FAILURE;
const OGRErr eErr = addGeometryDirectly( poClone );
if( eErr != OGRERR_NONE )
delete poClone;
return eErr;
}
/************************************************************************/
/* addGeometryDirectly() */
/* */
/* Add a new geometry to a collection. Subclasses should */
/* override this to verify the type of the new geometry, and */
/* then call this method to actually add it. */
/************************************************************************/
/**
* \brief Add a geometry directly to the container.
*
* Some subclasses of OGRGeometryCollection restrict the types of geometry
* that can be added, and may return an error. Ownership of the passed
* geometry is taken by the container rather than cloning as addGeometry()
* does.
*
* This method is the same as the C function OGR_G_AddGeometryDirectly().
*
* There is no SFCOM analog to this method.
*
* @param poNewGeom geometry to add to the container.
*
* @return OGRERR_NONE if successful, or OGRERR_UNSUPPORTED_GEOMETRY_TYPE if
* the geometry type is illegal for the type of geometry container.
*/
OGRErr OGRGeometryCollection::addGeometryDirectly( OGRGeometry * poNewGeom )
{
if( !isCompatibleSubType(poNewGeom->getGeometryType()) )
return OGRERR_UNSUPPORTED_GEOMETRY_TYPE;
HomogenizeDimensionalityWith(poNewGeom);
OGRGeometry** papoNewGeoms = static_cast<OGRGeometry **>(
VSI_REALLOC_VERBOSE(papoGeoms, sizeof(void*) * (nGeomCount + 1)));
if( papoNewGeoms == nullptr )
return OGRERR_FAILURE;
papoGeoms = papoNewGeoms;
papoGeoms[nGeomCount] = poNewGeom;
nGeomCount++;
return OGRERR_NONE;
}
/************************************************************************/
/* removeGeometry() */
/************************************************************************/
/**
* \brief Remove a geometry from the container.
*
* Removing a geometry will cause the geometry count to drop by one, and all
* "higher" geometries will shuffle down one in index.
*
* There is no SFCOM analog to this method.
*
* This method is the same as the C function OGR_G_RemoveGeometry().
*
* @param iGeom the index of the geometry to delete. A value of -1 is a
* special flag meaning that all geometries should be removed.
*
* @param bDelete if TRUE the geometry will be deallocated, otherwise it will
* not. The default is TRUE as the container is considered to own the
* geometries in it.
*
* @return OGRERR_NONE if successful, or OGRERR_FAILURE if the index is
* out of range.
*/
OGRErr OGRGeometryCollection::removeGeometry( int iGeom, int bDelete )
{
if( iGeom < -1 || iGeom >= nGeomCount )
return OGRERR_FAILURE;
// Special case.
if( iGeom == -1 )
{
while( nGeomCount > 0 )
removeGeometry( nGeomCount-1, bDelete );
return OGRERR_NONE;
}
if( bDelete )
delete papoGeoms[iGeom];
memmove( papoGeoms + iGeom, papoGeoms + iGeom + 1,
sizeof(void*) * (nGeomCount-iGeom-1) );
nGeomCount--;
return OGRERR_NONE;
}
/************************************************************************/
/* WkbSize() */
/* */
/* Return the size of this object in well known binary */
/* representation including the byte order, and type information. */
/************************************************************************/
size_t OGRGeometryCollection::WkbSize() const
{
size_t nSize = 9;
for( const auto& poGeom: *this )
{
nSize += poGeom->WkbSize();
}
return nSize;
}
/************************************************************************/
/* importFromWkbInternal() */
/************************************************************************/
OGRErr OGRGeometryCollection::importFromWkbInternal( const unsigned char * pabyData,
size_t nSize, int nRecLevel,
OGRwkbVariant eWkbVariant,
size_t& nBytesConsumedOut )
{
nBytesConsumedOut = 0;
// Arbitrary value, but certainly large enough for reasonable use cases.
if( nRecLevel == 32 )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Too many recursion levels (%d) while parsing WKB geometry.",
nRecLevel );
return OGRERR_CORRUPT_DATA;
}
nGeomCount = 0;
OGRwkbByteOrder eByteOrder = wkbXDR;
size_t nDataOffset = 0;
OGRErr eErr = importPreambleOfCollectionFromWkb( pabyData,
nSize,
nDataOffset,
eByteOrder,
9,
nGeomCount,
eWkbVariant );
if( eErr != OGRERR_NONE )
return eErr;
// coverity[tainted_data]
papoGeoms = static_cast<OGRGeometry **>(
VSI_CALLOC_VERBOSE(sizeof(void*), nGeomCount));
if( nGeomCount != 0 && papoGeoms == nullptr )
{
nGeomCount = 0;
return OGRERR_NOT_ENOUGH_MEMORY;
}
/* -------------------------------------------------------------------- */
/* Get the Geoms. */
/* -------------------------------------------------------------------- */
for( int iGeom = 0; iGeom < nGeomCount; iGeom++ )
{
// Parses sub-geometry.
const unsigned char* pabySubData = pabyData + nDataOffset;
if( nSize < 9 && nSize != static_cast<size_t>(-1) )
return OGRERR_NOT_ENOUGH_DATA;
OGRwkbGeometryType eSubGeomType = wkbUnknown;
eErr = OGRReadWKBGeometryType( pabySubData, eWkbVariant,
&eSubGeomType );
if( eErr != OGRERR_NONE )
return eErr;
if( !isCompatibleSubType(eSubGeomType) )
{
nGeomCount = iGeom;
CPLDebug(
"OGR",
"Cannot add geometry of type (%d) to geometry of type (%d)",
eSubGeomType, getGeometryType());
return OGRERR_CORRUPT_DATA;
}
OGRGeometry* poSubGeom = nullptr;
size_t nSubGeomBytesConsumed = 0;
if( OGR_GT_IsSubClassOf(eSubGeomType, wkbGeometryCollection) )
{
poSubGeom = OGRGeometryFactory::createGeometry( eSubGeomType );
if( poSubGeom == nullptr )
eErr = OGRERR_FAILURE;
else
eErr = poSubGeom->toGeometryCollection()->
importFromWkbInternal( pabySubData, nSize,
nRecLevel + 1, eWkbVariant,
nSubGeomBytesConsumed );
}
else
{
eErr = OGRGeometryFactory::
createFromWkb( pabySubData, nullptr,
&poSubGeom, nSize, eWkbVariant,
nSubGeomBytesConsumed );
}
if( eErr != OGRERR_NONE )
{
nGeomCount = iGeom;
delete poSubGeom;
return eErr;
}
papoGeoms[iGeom] = poSubGeom;
if( papoGeoms[iGeom]->Is3D() )
flags |= OGR_G_3D;
if( papoGeoms[iGeom]->IsMeasured() )
flags |= OGR_G_MEASURED;
CPLAssert( nSubGeomBytesConsumed > 0 );
if( nSize != static_cast<size_t>(-1) )
{
CPLAssert( nSize >= nSubGeomBytesConsumed );
nSize -= nSubGeomBytesConsumed;
}
nDataOffset += nSubGeomBytesConsumed;
}
nBytesConsumedOut = nDataOffset;
return OGRERR_NONE;
}
/************************************************************************/
/* importFromWkb() */
/* */
/* Initialize from serialized stream in well known binary */
/* format. */
/************************************************************************/
OGRErr OGRGeometryCollection::importFromWkb( const unsigned char * pabyData,
size_t nSize,
OGRwkbVariant eWkbVariant,
size_t& nBytesConsumedOut )
{
return importFromWkbInternal(pabyData, nSize, 0, eWkbVariant,
nBytesConsumedOut);
}
/************************************************************************/
/* exportToWkb() */
/* */
/* Build a well known binary representation of this object. */
/************************************************************************/
OGRErr OGRGeometryCollection::exportToWkb( OGRwkbByteOrder eByteOrder,
unsigned char * pabyData,
OGRwkbVariant eWkbVariant ) const
{
if( eWkbVariant == wkbVariantOldOgc &&
(wkbFlatten(getGeometryType()) == wkbMultiCurve ||
wkbFlatten(getGeometryType()) == wkbMultiSurface) )
{
// Does not make sense for new geometries, so patch it.
eWkbVariant = wkbVariantIso;
}
/* -------------------------------------------------------------------- */
/* Set the byte order. */
/* -------------------------------------------------------------------- */
pabyData[0] = DB2_V72_UNFIX_BYTE_ORDER(static_cast<unsigned char>(eByteOrder));
/* -------------------------------------------------------------------- */
/* Set the geometry feature type, ensuring that 3D flag is */
/* preserved. */
/* -------------------------------------------------------------------- */
GUInt32 nGType = getGeometryType();
if( eWkbVariant == wkbVariantIso )
nGType = getIsoGeometryType();
else if( eWkbVariant == wkbVariantPostGIS1 )
{
const bool bIs3D = wkbHasZ(static_cast<OGRwkbGeometryType>(nGType));
nGType = wkbFlatten(nGType);
if( nGType == wkbMultiCurve )
nGType = POSTGIS15_MULTICURVE;
else if( nGType == wkbMultiSurface )
nGType = POSTGIS15_MULTISURFACE;
if( bIs3D )
// Yes, explicitly set wkb25DBit.
nGType = static_cast<OGRwkbGeometryType>(nGType | wkb25DBitInternalUse);
}
if( OGR_SWAP( eByteOrder ) )
{
nGType = CPL_SWAP32(nGType);
}
memcpy( pabyData + 1, &nGType, 4 );
/* -------------------------------------------------------------------- */
/* Copy in the raw data. */
/* -------------------------------------------------------------------- */
if( OGR_SWAP( eByteOrder ) )
{
int nCount = CPL_SWAP32( nGeomCount );
memcpy( pabyData+5, &nCount, 4 );
}
else
{
memcpy( pabyData+5, &nGeomCount, 4 );
}
size_t nOffset = 9;
/* ==================================================================== */
/* Serialize each of the Geoms. */
/* ==================================================================== */
int iGeom = 0;
for( auto&& poSubGeom: *this )
{
poSubGeom->exportToWkb( eByteOrder, pabyData + nOffset,
eWkbVariant );
// Should normally not happen if everyone else does its job,
// but has happened sometimes. (#6332)
if( poSubGeom->getCoordinateDimension() !=
getCoordinateDimension() )
{
CPLError(CE_Warning, CPLE_AppDefined,
"Sub-geometry %d has coordinate dimension %d, "
"but container has %d",
iGeom,
poSubGeom->getCoordinateDimension(),
getCoordinateDimension() );
}
nOffset += poSubGeom->WkbSize();
iGeom ++;
}
return OGRERR_NONE;
}
/************************************************************************/
/* importFromWktInternal() */
/************************************************************************/
OGRErr OGRGeometryCollection::importFromWktInternal( const char ** ppszInput,
int nRecLevel )
{
// Arbitrary value, but certainly large enough for reasonable usages.
if( nRecLevel == 32 )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Too many recursion levels (%d) while parsing WKT geometry.",
nRecLevel );
return OGRERR_CORRUPT_DATA;
}
int bHasZ = FALSE;
int bHasM = FALSE;
bool bIsEmpty = false;
OGRErr eErr = importPreambleFromWkt(ppszInput, &bHasZ, &bHasM, &bIsEmpty);
if( eErr != OGRERR_NONE )
return eErr;
if( bHasZ ) flags |= OGR_G_3D;
if( bHasM ) flags |= OGR_G_MEASURED;
if( bIsEmpty )
return OGRERR_NONE;
char szToken[OGR_WKT_TOKEN_MAX] = {};
const char *pszInput = *ppszInput;
// Skip first '('.
pszInput = OGRWktReadToken( pszInput, szToken );
/* ==================================================================== */
/* Read each subgeometry in turn. */
/* ==================================================================== */
do
{
OGRGeometry *poGeom = nullptr;
/* -------------------------------------------------------------------- */
/* Get the first token, which should be the geometry type. */
/* -------------------------------------------------------------------- */
OGRWktReadToken( pszInput, szToken );
/* -------------------------------------------------------------------- */
/* Do the import. */
/* -------------------------------------------------------------------- */
if( STARTS_WITH_CI(szToken, "GEOMETRYCOLLECTION") )
{
OGRGeometryCollection* poGC = new OGRGeometryCollection();
poGeom = poGC;
eErr = poGC->importFromWktInternal( &pszInput,
nRecLevel + 1 );
}
else
eErr = OGRGeometryFactory::createFromWkt( &pszInput,
nullptr, &poGeom );
if( eErr == OGRERR_NONE )
{
// If this has M, but not Z, it is an error if poGeom does
// not have M.
if( !Is3D() && IsMeasured() && !poGeom->IsMeasured() )
eErr = OGRERR_CORRUPT_DATA;
else
eErr = addGeometryDirectly( poGeom );
}
if( eErr != OGRERR_NONE )
{
delete poGeom;
return eErr;
}
/* -------------------------------------------------------------------- */
/* Read the delimiter following the ring. */
/* -------------------------------------------------------------------- */
pszInput = OGRWktReadToken( pszInput, szToken );
} while( szToken[0] == ',' );
/* -------------------------------------------------------------------- */
/* freak if we don't get a closing bracket. */
/* -------------------------------------------------------------------- */
if( szToken[0] != ')' )
return OGRERR_CORRUPT_DATA;
*ppszInput = pszInput;
return OGRERR_NONE;
}
/************************************************************************/
/* importFromWkt() */
/************************************************************************/
OGRErr OGRGeometryCollection::importFromWkt( const char ** ppszInput )
{
return importFromWktInternal(ppszInput, 0);
}
/************************************************************************/
/* exportToWkt() */
/* */
/* Translate this structure into its well known text format */
/* equivalent. */
/************************************************************************/
std::string OGRGeometryCollection::exportToWkt(const OGRWktOptions& opts,
OGRErr *err) const
{
return exportToWktInternal(opts, err);
}
//! @cond Doxygen_Suppress
std::string OGRGeometryCollection::exportToWktInternal(const OGRWktOptions& opts,
OGRErr *err, std::string exclude) const
{
bool first = true;
const size_t excludeSize = exclude.size();
std::string wkt(getGeometryName());
wkt += wktTypeString(opts.variant);
try
{
for (int i = 0; i < nGeomCount; ++i)
{
OGRGeometry *geom = papoGeoms[i];
OGRErr subgeomErr = OGRERR_NONE;
std::string tempWkt = geom->exportToWkt(opts, &subgeomErr);
if (subgeomErr != OGRERR_NONE)
{
if (err)
*err = subgeomErr;
return std::string();
}
// For some strange reason we exclude the typename leader when using
// some geometries as part of a collection.
if (excludeSize && (tempWkt.compare(0, excludeSize, exclude) == 0))
{
auto pos = tempWkt.find('(');
// We won't have an opening paren if the geom is empty.
if (pos == std::string::npos)
continue;
tempWkt = tempWkt.substr(pos);
}
// Also strange, we allow the inclusion of ISO-only geometries (see
// OGRPolyhedralSurface) in a non-iso geometry collection. In order
// to facilitate this, we need to rip the ISO bit from the string.
if (opts.variant != wkbVariantIso)
{
std::string::size_type pos;
if ((pos = tempWkt.find(" Z ")) != std::string::npos)
tempWkt.erase(pos + 1, 2);
else if ((pos = tempWkt.find(" M ")) != std::string::npos)
tempWkt.erase(pos + 1, 2);
else if ((pos = tempWkt.find(" ZM ")) != std::string::npos)
tempWkt.erase(pos + 1, 3);
}
if (first)
wkt += '(';
else
wkt += ',';
first = false;
wkt += tempWkt;
}
if (err)
*err = OGRERR_NONE;
if (first)
wkt += "EMPTY";
else
wkt += ')';
return wkt;
}
catch( const std::bad_alloc& e )
{
CPLError(CE_Failure, CPLE_OutOfMemory, "%s", e.what());
if (err)
*err = OGRERR_FAILURE;
return std::string();
}
}
//! @endcond
/************************************************************************/
/* getEnvelope() */
/************************************************************************/
void OGRGeometryCollection::getEnvelope( OGREnvelope * psEnvelope ) const
{
OGREnvelope3D oEnv3D;
getEnvelope(&oEnv3D);
psEnvelope->MinX = oEnv3D.MinX;
psEnvelope->MinY = oEnv3D.MinY;
psEnvelope->MaxX = oEnv3D.MaxX;
psEnvelope->MaxY = oEnv3D.MaxY;
}
/************************************************************************/
/* getEnvelope() */
/************************************************************************/
void OGRGeometryCollection::getEnvelope( OGREnvelope3D * psEnvelope ) const
{
OGREnvelope3D oGeomEnv;
bool bExtentSet = false;
*psEnvelope = OGREnvelope3D();
for( auto&& poSubGeom: *this )
{
if( !poSubGeom->IsEmpty() )
{
bExtentSet = true;
poSubGeom->getEnvelope( &oGeomEnv );
psEnvelope->Merge( oGeomEnv );
}
}
if( !bExtentSet )
{
// To be backward compatible when called on empty geom
psEnvelope->MinX = 0.0;
psEnvelope->MinY = 0.0;
psEnvelope->MinZ = 0.0;
psEnvelope->MaxX = 0.0;
psEnvelope->MaxY = 0.0;
psEnvelope->MaxZ = 0.0;
}
}
/************************************************************************/
/* Equals() */
/************************************************************************/
OGRBoolean OGRGeometryCollection::Equals( const OGRGeometry * poOther ) const
{
if( poOther == this )
return TRUE;
if( poOther->getGeometryType() != getGeometryType() )
return FALSE;
if( IsEmpty() && poOther->IsEmpty() )
return TRUE;
auto poOGC = poOther->toGeometryCollection();
if( getNumGeometries() != poOGC->getNumGeometries() )
return FALSE;
// TODO(schwehr): Should test the SRS.
for( int iGeom = 0; iGeom < nGeomCount; iGeom++ )
{
if( !getGeometryRef(iGeom)->Equals(poOGC->getGeometryRef(iGeom)) )
return FALSE;
}
return TRUE;
}
/************************************************************************/
/* transform() */
/************************************************************************/
OGRErr OGRGeometryCollection::transform( OGRCoordinateTransformation *poCT )
{
int iGeom = 0;
for( auto&& poSubGeom: *this )
{
const OGRErr eErr = poSubGeom->transform( poCT );
if( eErr != OGRERR_NONE )
{
if( iGeom != 0 )
{
CPLDebug("OGR",
"OGRGeometryCollection::transform() failed for a "
"geometry other than the first, meaning some "
"geometries are transformed and some are not." );
return OGRERR_FAILURE;
}
return eErr;
}
iGeom ++;
}
assignSpatialReference( poCT->GetTargetCS() );
return OGRERR_NONE;
}
/************************************************************************/
/* closeRings() */
/************************************************************************/
void OGRGeometryCollection::closeRings()
{
for( auto&& poSubGeom: *this )
{
if( OGR_GT_IsSubClassOf(
wkbFlatten(poSubGeom->getGeometryType()),
wkbCurvePolygon ) )
{
OGRCurvePolygon *poPoly = poSubGeom->toCurvePolygon();
poPoly->closeRings();
}
}