forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ogrsf.cpp
4221 lines (3719 loc) · 147 KB
/
test_ogrsf.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: Formal test harness for OGRLayer implementations.
* Author: Frank Warmerdam, [email protected]
*
******************************************************************************
* Copyright (c) 1999, Frank Warmerdam
* Copyright (c) 2009-2014, 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_conv.h"
#include "cpl_multiproc.h"
#include "gdal_version.h"
#include "ogr_api.h"
#include "ogr_p.h"
#include "ogrsf_frmts.h"
#include "ogr_swq.h"
#include "commonutils.h"
#include <algorithm>
#include <limits>
bool bReadOnly = false;
bool bVerbose = true;
const char *pszDataSource = nullptr;
char **papszLayers = nullptr;
const char *pszSQLStatement = nullptr;
const char *pszDialect = nullptr;
int nLoops = 1;
bool bFullSpatialFilter = false;
char **papszOpenOptions = nullptr;
const char *pszDriver = nullptr;
bool bAllDrivers = false;
const char *pszLogFilename = nullptr;
char **papszDSCO = nullptr;
char **papszLCO = nullptr;
typedef struct
{
CPLJoinableThread *hThread;
int bRet;
} ThreadContext;
static void Usage();
static void ThreadFunction(void *user_data);
static void ThreadFunctionInternal(ThreadContext *psContext);
static int TestDataset(GDALDriver **ppoDriver);
static int TestCreate(GDALDriver *poDriver, int bFromAllDrivers);
static int TestOGRLayer(GDALDataset *poDS, OGRLayer *poLayer, int bIsSQLLayer);
static int TestInterleavedReading(const char *pszDataSource,
char **papszLayers);
static int TestDSErrorConditions(GDALDataset *poDS);
static int TestVirtualIO(GDALDataset *poDS);
static const char *Log(const char *pszMsg, int nLineNumber)
{
if (pszLogFilename == nullptr)
return pszMsg;
FILE *f = fopen(pszLogFilename, "at");
if (f == nullptr)
return pszMsg;
fprintf(f, "%d: %s\n", nLineNumber, pszMsg);
fclose(f);
return pszMsg;
}
#define LOG_STR(str) (Log((str), __LINE__))
#define LOG_ACTION(action) ((void)Log(#action, __LINE__), (action))
/************************************************************************/
/* DestroyFeatureAndNullify() */
/************************************************************************/
static void DestroyFeatureAndNullify(OGRFeature *&poFeature)
{
OGRFeature::DestroyFeature(poFeature);
poFeature = nullptr;
}
/************************************************************************/
/* main() */
/************************************************************************/
MAIN_START(nArgc, papszArgv)
{
EarlySetConfigOptions(nArgc, papszArgv);
OGRRegisterAll();
/* -------------------------------------------------------------------- */
/* Processing command line arguments. */
/* -------------------------------------------------------------------- */
nArgc = OGRGeneralCmdLineProcessor(nArgc, &papszArgv, 0);
if (nArgc < 1)
exit(-nArgc);
int bRet = TRUE;
int nThreads = 1;
if (EQUAL(CPLGetConfigOption("DRIVER_WISHED", ""), "FileGDB"))
{
auto poFileGDB = GetGDALDriverManager()->GetDriverByName("FileGDB");
auto poOpenFileGDB =
GetGDALDriverManager()->GetDriverByName("OpenFileGDB");
if (poFileGDB && poOpenFileGDB)
{
GetGDALDriverManager()->DeregisterDriver(poFileGDB);
GetGDALDriverManager()->DeregisterDriver(poOpenFileGDB);
GetGDALDriverManager()->RegisterDriver(poFileGDB);
GetGDALDriverManager()->RegisterDriver(poOpenFileGDB);
}
}
/* -------------------------------------------------------------------- */
/* Processing command line arguments. */
/* -------------------------------------------------------------------- */
for (int iArg = 1; iArg < nArgc; iArg++)
{
if (EQUAL(papszArgv[iArg], "--utility_version"))
{
printf("%s was compiled against GDAL %s and "
"is running against GDAL %s\n",
papszArgv[0], GDAL_RELEASE_NAME,
GDALVersionInfo("RELEASE_NAME"));
CSLDestroy(papszArgv);
return 0;
}
else if (EQUAL(papszArgv[iArg], "-ro"))
{
bReadOnly = true;
}
else if (EQUAL(papszArgv[iArg], "-q") ||
EQUAL(papszArgv[iArg], "-quiet"))
{
bVerbose = false;
}
else if (EQUAL(papszArgv[iArg], "-sql") && iArg + 1 < nArgc)
{
pszSQLStatement = papszArgv[++iArg];
}
else if (EQUAL(papszArgv[iArg], "-dialect") &&
papszArgv[iArg + 1] != nullptr)
{
pszDialect = papszArgv[++iArg];
}
else if (EQUAL(papszArgv[iArg], "-threads") && iArg + 1 < nArgc)
{
nThreads = atoi(papszArgv[++iArg]);
}
else if (EQUAL(papszArgv[iArg], "-loops") && iArg + 1 < nArgc)
{
nLoops = atoi(papszArgv[++iArg]);
}
else if (EQUAL(papszArgv[iArg], "-fsf"))
{
bFullSpatialFilter = true;
}
else if (EQUAL(papszArgv[iArg], "-oo") && iArg + 1 < nArgc)
{
papszOpenOptions =
CSLAddString(papszOpenOptions, papszArgv[++iArg]);
}
else if (EQUAL(papszArgv[iArg], "-dsco") && iArg + 1 < nArgc)
{
papszDSCO = CSLAddString(papszDSCO, papszArgv[++iArg]);
}
else if (EQUAL(papszArgv[iArg], "-lco") && iArg + 1 < nArgc)
{
papszLCO = CSLAddString(papszLCO, papszArgv[++iArg]);
}
else if (EQUAL(papszArgv[iArg], "-log") && iArg + 1 < nArgc)
{
pszLogFilename = papszArgv[++iArg];
}
else if (EQUAL(papszArgv[iArg], "-driver") && iArg + 1 < nArgc)
{
pszDriver = papszArgv[++iArg];
}
else if (EQUAL(papszArgv[iArg], "-all_drivers"))
{
bAllDrivers = true;
}
else if (papszArgv[iArg][0] == '-')
{
Usage();
}
else if (pszDataSource == nullptr)
{
pszDataSource = papszArgv[iArg];
}
else
{
papszLayers = CSLAddString(papszLayers, papszArgv[iArg]);
}
}
if (pszDataSource == nullptr && pszDriver == nullptr && !bAllDrivers)
Usage();
if (nThreads > 1 && !bReadOnly && pszDataSource != nullptr)
{
fprintf(
stderr,
"-threads must be used with -ro or -driver/-all_drivers option.\n");
exit(1);
}
if (nThreads == 1)
{
ThreadContext sContext;
ThreadFunction(&sContext);
bRet = sContext.bRet;
}
else if (nThreads > 1)
{
ThreadContext *pasContext = new ThreadContext[nThreads];
for (int i = 0; i < nThreads; i++)
{
pasContext[i].hThread =
CPLCreateJoinableThread(ThreadFunction, &(pasContext[i]));
}
for (int i = 0; i < nThreads; i++)
{
CPLJoinThread(pasContext[i].hThread);
bRet &= pasContext[i].bRet;
}
delete[] pasContext;
}
OGRCleanupAll();
CSLDestroy(papszLayers);
CSLDestroy(papszArgv);
CSLDestroy(papszOpenOptions);
CSLDestroy(papszDSCO);
CSLDestroy(papszLCO);
#ifdef DBMALLOC
malloc_dump(1);
#endif
return (bRet) ? 0 : 1;
}
MAIN_END
/************************************************************************/
/* ThreadFunction() */
/************************************************************************/
static void ThreadFunction(void *user_data)
{
ThreadContext *psContext = static_cast<ThreadContext *>(user_data);
psContext->bRet = TRUE;
#ifdef __AFL_HAVE_MANUAL_CONTROL
while (__AFL_LOOP(1000))
{
#endif
for (int iLoop = 0; psContext->bRet && iLoop < nLoops; iLoop++)
{
ThreadFunctionInternal(psContext);
}
#ifdef __AFL_HAVE_MANUAL_CONTROL
}
#endif
}
/************************************************************************/
/* ThreadFunctionInternal() */
/************************************************************************/
static void ThreadFunctionInternal(ThreadContext *psContext)
{
int bRet = TRUE;
GDALDriver *poDriver = nullptr;
if (pszDataSource != nullptr)
{
bRet = TestDataset(&poDriver);
}
else if (pszDriver != nullptr)
{
poDriver = static_cast<GDALDriver *>(GDALGetDriverByName(pszDriver));
if (poDriver)
{
bRet &= TestCreate(poDriver, FALSE);
}
else
{
printf("ERROR: Cannot find driver %s\n", pszDriver);
bRet = FALSE;
}
}
else
{
const int nCount = GDALGetDriverCount();
for (int i = 0; i < nCount; i++)
{
poDriver = static_cast<GDALDriver *>(GDALGetDriver(i));
if (poDriver->GetMetadataItem(GDAL_DCAP_VECTOR) != nullptr)
bRet &= TestCreate(poDriver, TRUE);
}
}
psContext->bRet = bRet;
}
/************************************************************************/
/* TestDataset() */
/************************************************************************/
static int TestDataset(GDALDriver **ppoDriver)
{
int bRet = TRUE;
int bRetLocal;
/* -------------------------------------------------------------------- */
/* Open data source. */
/* -------------------------------------------------------------------- */
GDALDataset *poDS = static_cast<GDALDataset *>(GDALOpenEx(
pszDataSource,
(!bReadOnly ? GDAL_OF_UPDATE : GDAL_OF_READONLY) | GDAL_OF_VECTOR,
nullptr, papszOpenOptions, nullptr));
if (poDS == nullptr && !bReadOnly)
{
poDS = static_cast<GDALDataset *>(GDALOpenEx(
pszDataSource, GDAL_OF_VECTOR, nullptr, papszOpenOptions, nullptr));
if (poDS != nullptr && bVerbose)
{
printf("Had to open data source read-only.\n");
bReadOnly = true;
}
}
GDALDriver *poDriver = nullptr;
if (poDS != nullptr)
poDriver = poDS->GetDriver();
*ppoDriver = poDriver;
/* -------------------------------------------------------------------- */
/* Report failure */
/* -------------------------------------------------------------------- */
if (poDS == nullptr)
{
OGRSFDriverRegistrar *poR = OGRSFDriverRegistrar::GetRegistrar();
printf("FAILURE:\n"
"Unable to open datasource `%s' with the following drivers.\n",
pszDataSource);
for (int iDriver = 0; iDriver < poR->GetDriverCount(); iDriver++)
{
printf(" -> %s\n", poR->GetDriver(iDriver)->GetDescription());
}
return FALSE;
}
/* -------------------------------------------------------------------- */
/* Some information messages. */
/* -------------------------------------------------------------------- */
if (bVerbose)
printf("INFO: Open of `%s' using driver `%s' successful.\n",
pszDataSource, poDriver->GetDescription());
if (bVerbose && !EQUAL(pszDataSource, poDS->GetDescription()))
{
printf("INFO: Internal data source name `%s'\n"
" different from user name `%s'.\n",
poDS->GetDescription(), pszDataSource);
}
// Check that pszDomain == nullptr doesn't crash
poDS->GetMetadata(nullptr);
poDS->GetMetadataItem("", nullptr);
if (!poDriver->GetMetadataItem(GDAL_DCAP_VECTOR))
{
printf("FAILURE: Driver does not advertise GDAL_DCAP_VECTOR!\n");
bRet = false;
}
// Test consistency of datasource capabilities and driver metadata
if (poDS->TestCapability(ODsCCreateLayer) &&
!poDriver->GetMetadataItem(GDAL_DCAP_CREATE_LAYER))
{
printf("FAILURE: Dataset advertises ODsCCreateLayer capability but "
"driver metadata does not advertise GDAL_DCAP_CREATE_LAYER!\n");
bRet = false;
}
if (!bReadOnly &&
poDriver->GetMetadataItem(GDAL_DCAP_MULTIPLE_VECTOR_LAYERS) &&
poDriver->GetMetadataItem(GDAL_DCAP_CREATE_LAYER) &&
!poDS->TestCapability(ODsCCreateLayer))
{
printf("FAILURE: Driver advertises GDAL_DCAP_CREATE_LAYER and "
"GDAL_DCAP_MULTIPLE_VECTOR_LAYERS capability but dataset does "
"not advertise ODsCCreateLayer!\n");
bRet = false;
}
if (poDS->TestCapability(ODsCDeleteLayer) &&
!poDriver->GetMetadataItem(GDAL_DCAP_DELETE_LAYER))
{
printf("FAILURE: Dataset advertises ODsCDeleteLayer capability but "
"driver metadata does not advertise GDAL_DCAP_DELETE_LAYER!\n");
bRet = false;
}
if (poDriver->GetMetadataItem(GDAL_DCAP_CREATE_FIELD) &&
!poDriver->GetMetadataItem(GDAL_DMD_CREATIONFIELDDATATYPES))
{
bRet = FALSE;
printf("FAILURE: Driver metadata advertises GDAL_DCAP_CREATE_FIELD but "
"does not include GDAL_DMD_CREATIONFIELDDATATYPES!\n");
}
if (poDriver->GetMetadataItem(GDAL_DMD_CREATIONFIELDDATATYPES) &&
!poDriver->GetMetadataItem(GDAL_DCAP_CREATE_FIELD))
{
bRet = FALSE;
printf(
"FAILURE: Driver metadata includes GDAL_DMD_CREATIONFIELDDATATYPES "
"but does not advertise GDAL_DCAP_CREATE_FIELD!\n");
}
if (poDriver->GetMetadataItem(GDAL_DMD_CREATIONFIELDDATASUBTYPES) &&
!poDriver->GetMetadataItem(GDAL_DMD_CREATIONFIELDDATATYPES))
{
bRet = FALSE;
printf("FAILURE: Driver metadata includes "
"GDAL_DMD_CREATIONFIELDDATASUBTYPES but does not include "
"GDAL_DMD_CREATIONFIELDDATATYPES!\n");
}
/* -------------------------------------------------------------------- */
/* Process optional SQL request. */
/* -------------------------------------------------------------------- */
if (pszSQLStatement != nullptr)
{
OGRLayer *poResultSet =
poDS->ExecuteSQL(pszSQLStatement, nullptr, pszDialect);
if (poResultSet == nullptr)
{
GDALClose(poDS);
return FALSE;
}
if (bVerbose)
{
printf("INFO: Testing layer %s.\n", poResultSet->GetName());
}
bRet = TestOGRLayer(poDS, poResultSet, TRUE);
poDS->ReleaseResultSet(poResultSet);
bRetLocal = TestDSErrorConditions(poDS);
bRet &= bRetLocal;
bRetLocal = TestVirtualIO(poDS);
bRet &= bRetLocal;
}
/* -------------------------------------------------------------------- */
/* Process each data source layer. */
/* -------------------------------------------------------------------- */
else if (papszLayers == nullptr)
{
for (int iLayer = 0; iLayer < poDS->GetLayerCount(); iLayer++)
{
OGRLayer *poLayer = poDS->GetLayer(iLayer);
if (poLayer == nullptr)
{
printf("FAILURE: Couldn't fetch advertised layer %d!\n",
iLayer);
GDALClose(poDS);
return FALSE;
}
if (bVerbose)
{
printf("INFO: Testing layer %s.\n", poLayer->GetName());
}
bRet &= TestOGRLayer(poDS, poLayer, FALSE);
}
bRetLocal = TestDSErrorConditions(poDS);
bRet &= bRetLocal;
bRetLocal = TestVirtualIO(poDS);
bRet &= bRetLocal;
if (poDS->GetLayerCount() >= 2)
{
GDALClose(poDS);
poDS = nullptr;
bRetLocal = TestInterleavedReading(pszDataSource, nullptr);
bRet &= bRetLocal;
}
}
else
{
/* --------------------------------------------------------------------
*/
/* Or process layers specified by the user */
/* --------------------------------------------------------------------
*/
char **papszLayerIter = papszLayers;
while (*papszLayerIter)
{
OGRLayer *poLayer = poDS->GetLayerByName(*papszLayerIter);
if (poLayer == nullptr)
{
printf("FAILURE: Couldn't fetch requested layer %s!\n",
*papszLayerIter);
GDALClose(poDS);
return FALSE;
}
if (bVerbose)
{
printf("INFO: Testing layer %s.\n", poLayer->GetName());
}
bRet &= TestOGRLayer(poDS, poLayer, FALSE);
papszLayerIter++;
}
bRetLocal = TestDSErrorConditions(poDS);
bRet &= bRetLocal;
bRetLocal = TestVirtualIO(poDS);
bRet &= bRetLocal;
if (CSLCount(papszLayers) >= 2)
{
GDALClose(poDS);
poDS = nullptr;
bRetLocal = TestInterleavedReading(pszDataSource, papszLayers);
bRet &= bRetLocal;
}
}
/* -------------------------------------------------------------------- */
/* Close down. */
/* -------------------------------------------------------------------- */
if (poDS != nullptr)
GDALClose(poDS);
return bRet;
}
/************************************************************************/
/* GetWKT() */
/************************************************************************/
static const char *GetWKT(OGRwkbGeometryType eGeomType)
{
const char *pszWKT = nullptr;
if (eGeomType == wkbUnknown || eGeomType == wkbPoint)
pszWKT = "POINT (0 0)";
else if (eGeomType == wkbLineString)
pszWKT = "LINESTRING (0 0,1 1)";
else if (eGeomType == wkbPolygon)
pszWKT = "POLYGON ((0 0,0 1,1 1,1 0,0 0))";
else if (eGeomType == wkbMultiPoint)
pszWKT = "MULTIPOINT (0 0)";
else if (eGeomType == wkbMultiLineString)
pszWKT = "MULTILINESTRING ((0 0,1 1))";
else if (eGeomType == wkbMultiPolygon)
pszWKT = "MULTIPOLYGON (((0 0,0 1,1 1,1 0,0 0)))";
else if (eGeomType == wkbGeometryCollection)
pszWKT = "GEOMETRYCOLLECTION (POINT (0 0),LINESTRING (0 0,1 1),"
"POLYGON ((0 0,0 1,1 1,1 0,0 0)))";
else if (eGeomType == wkbPoint25D)
pszWKT = "POINT (0 0 10)";
else if (eGeomType == wkbLineString25D)
pszWKT = "LINESTRING (0 0 10,1 1 10)";
else if (eGeomType == wkbPolygon25D)
pszWKT = "POLYGON ((0 0 10,0 1 10,1 1 10,1 0 10,0 0 10))";
else if (eGeomType == wkbMultiPoint25D)
pszWKT = "MULTIPOINT (0 0 10)";
else if (eGeomType == wkbMultiLineString25D)
pszWKT = "MULTILINESTRING ((0 0 10,1 1 10))";
else if (eGeomType == wkbMultiPolygon25D)
pszWKT = "MULTIPOLYGON (((0 0 10,0 1 10,1 1 10,1 0 10,0 0 10)))";
else if (eGeomType == wkbGeometryCollection25D)
pszWKT =
"GEOMETRYCOLLECTION (POINT (0 0 10),LINESTRING (0 0 10,1 1 10),"
"POLYGON ((0 0 10,0 1 10,1 1 10,1 0 10,0 0 10)))";
return pszWKT;
}
/************************************************************************/
/* TestCreateLayer() */
/************************************************************************/
static int TestCreateLayer(GDALDriver *poDriver, OGRwkbGeometryType eGeomType)
{
int bRet = TRUE;
const char *pszExt = poDriver->GetMetadataItem(GDAL_DMD_EXTENSION);
static int nCounter = 0;
CPLString osFilename =
CPLFormFilename("/vsimem", CPLSPrintf("test%d", ++nCounter), pszExt);
GDALDataset *poDS = LOG_ACTION(
poDriver->Create(osFilename, 0, 0, 0, GDT_Unknown, papszDSCO));
if (poDS == nullptr)
{
if (bVerbose)
printf("INFO: %s: Creation of %s failed.\n",
poDriver->GetDescription(), osFilename.c_str());
return bRet;
}
CPLPushErrorHandler(CPLQuietErrorHandler);
int bCreateLayerCap = LOG_ACTION(poDS->TestCapability(ODsCCreateLayer));
OGRLayer *poLayer = LOG_ACTION(poDS->CreateLayer(
CPLGetFilename(osFilename), nullptr, eGeomType, papszLCO));
CPLPopErrorHandler();
CPLString osLayerNameToTest;
OGRwkbGeometryType eExpectedGeomType = wkbUnknown;
if (poLayer != nullptr)
{
if (bCreateLayerCap == FALSE)
{
printf("ERROR: %s: TestCapability(ODsCCreateLayer) returns FALSE "
"whereas layer creation was successful.\n",
poDriver->GetDescription());
bRet = FALSE;
}
if (LOG_ACTION(poLayer->GetLayerDefn()) == nullptr)
{
printf("ERROR: %s: GetLayerDefn() returns NUL just after layer "
"creation.\n",
poDriver->GetDescription());
bRet = FALSE;
}
// Create fields of various types
int bCreateField = LOG_ACTION(poLayer->TestCapability(OLCCreateField));
int iFieldStr = -1;
int iFieldInt = -1;
int iFieldReal = -1;
int iFieldDate = -1;
int iFieldDateTime = -1;
int bStrFieldOK;
{
OGRFieldDefn oFieldStr("str", OFTString);
CPLPushErrorHandler(CPLQuietErrorHandler);
bStrFieldOK =
LOG_ACTION(poLayer->CreateField(&oFieldStr)) == OGRERR_NONE;
CPLPopErrorHandler();
if (bStrFieldOK && (iFieldStr = LOG_ACTION(poLayer->GetLayerDefn())
->GetFieldIndex("str")) < 0)
{
printf("ERROR: %s: CreateField(str) returned OK "
"but field was not created.\n",
poDriver->GetDescription());
bRet = FALSE;
}
}
OGRFieldDefn oFieldInt("int", OFTInteger);
CPLPushErrorHandler(CPLQuietErrorHandler);
const bool bIntFieldOK =
LOG_ACTION(poLayer->CreateField(&oFieldInt)) == OGRERR_NONE;
CPLPopErrorHandler();
if (bIntFieldOK &&
(iFieldInt = poLayer->GetLayerDefn()->GetFieldIndex("int")) < 0)
{
printf("ERROR: %s: CreateField(int) returned OK "
"but field was not created.\n",
poDriver->GetDescription());
bRet = FALSE;
}
OGRFieldDefn oFieldReal("real", OFTReal);
CPLPushErrorHandler(CPLQuietErrorHandler);
const bool bRealFieldOK =
LOG_ACTION(poLayer->CreateField(&oFieldReal)) == OGRERR_NONE;
CPLPopErrorHandler();
if (bRealFieldOK &&
(iFieldReal = poLayer->GetLayerDefn()->GetFieldIndex("real")) < 0)
{
printf("ERROR: %s: CreateField(real) returned OK but field was not "
"created.\n",
poDriver->GetDescription());
bRet = FALSE;
}
OGRFieldDefn oFieldDate("mydate", OFTDate);
CPLPushErrorHandler(CPLQuietErrorHandler);
const bool bDateFieldOK =
LOG_ACTION(poLayer->CreateField(&oFieldDate)) == OGRERR_NONE;
CPLPopErrorHandler();
if (bDateFieldOK &&
(iFieldDate = poLayer->GetLayerDefn()->GetFieldIndex("mydate")) < 0)
{
printf("ERROR: %s: CreateField(mydate) returned OK but field was "
"not created.\n",
poDriver->GetDescription());
bRet = FALSE;
}
OGRFieldDefn oFieldDateTime("datetime", OFTDateTime);
CPLPushErrorHandler(CPLQuietErrorHandler);
const bool bDateTimeFieldOK =
LOG_ACTION(poLayer->CreateField(&oFieldDateTime)) == OGRERR_NONE;
CPLPopErrorHandler();
if (bDateTimeFieldOK &&
(iFieldDateTime =
poLayer->GetLayerDefn()->GetFieldIndex("datetime")) < 0)
{
printf("ERROR: %s: CreateField(datetime) returned OK but field was "
"not created.\n",
poDriver->GetDescription());
bRet = FALSE;
}
if (bCreateField == FALSE &&
(bStrFieldOK || bIntFieldOK || bRealFieldOK || bDateFieldOK ||
bDateTimeFieldOK))
{
printf("ERROR: %s: TestCapability(OLCCreateField) returns FALSE.\n",
poDriver->GetDescription());
bRet = FALSE;
}
if (LOG_ACTION(poLayer->TestCapability(OLCSequentialWrite)) == FALSE)
{
printf("ERROR: %s: TestCapability(OLCSequentialWrite) returns "
"FALSE.\n",
poDriver->GetDescription());
bRet = FALSE;
}
/* Test creating empty feature */
OGRFeature *poFeature = new OGRFeature(poLayer->GetLayerDefn());
CPLErrorReset();
CPLPushErrorHandler(CPLQuietErrorHandler);
OGRErr eErr = LOG_ACTION(poLayer->CreateFeature(poFeature));
CPLPopErrorHandler();
if (eErr != OGRERR_NONE && CPLGetLastErrorType() == 0)
{
printf("INFO: %s: CreateFeature() at line %d failed but without "
"explicit error.\n",
poDriver->GetDescription(), __LINE__);
}
if (eErr == OGRERR_NONE && poFeature->GetFID() < 0 &&
eGeomType == wkbUnknown)
{
printf("INFO: %s: CreateFeature() at line %d succeeded "
"but failed to assign FID to feature.\n",
poDriver->GetDescription(), __LINE__);
}
delete poFeature;
/* Test creating feature with all fields set */
poFeature = new OGRFeature(poLayer->GetLayerDefn());
if (bStrFieldOK)
poFeature->SetField(iFieldStr, "foo");
if (bIntFieldOK)
poFeature->SetField(iFieldInt, 123);
if (bRealFieldOK)
poFeature->SetField(iFieldReal, 1.23);
if (bDateFieldOK)
poFeature->SetField(iFieldDate, "2014/10/20");
if (bDateTimeFieldOK)
poFeature->SetField(iFieldDateTime, "2014/10/20 12:34:56");
CPLErrorReset();
CPLPushErrorHandler(CPLQuietErrorHandler);
eErr = LOG_ACTION(poLayer->CreateFeature(poFeature));
CPLPopErrorHandler();
if (eErr != OGRERR_NONE && CPLGetLastErrorType() == 0)
{
printf("INFO: %s: CreateFeature() at line %d failed "
"but without explicit error.\n",
poDriver->GetDescription(), __LINE__);
}
delete poFeature;
/* Test creating feature with all fields set as well as geometry */
poFeature = new OGRFeature(poLayer->GetLayerDefn());
if (bStrFieldOK)
poFeature->SetField(iFieldStr, "foo");
if (bIntFieldOK)
poFeature->SetField(iFieldInt, 123);
if (bRealFieldOK)
poFeature->SetField(iFieldReal, 1.23);
if (bDateFieldOK)
poFeature->SetField(iFieldDate, "2014/10/20");
if (bDateTimeFieldOK)
poFeature->SetField(iFieldDateTime, "2014/10/20 12:34:56");
const char *pszWKT = GetWKT(eGeomType);
if (pszWKT != nullptr)
{
OGRGeometry *poGeom = nullptr;
OGRGeometryFactory::createFromWkt(pszWKT, nullptr, &poGeom);
poFeature->SetGeometryDirectly(poGeom);
}
CPLErrorReset();
CPLPushErrorHandler(CPLQuietErrorHandler);
eErr = LOG_ACTION(poLayer->CreateFeature(poFeature));
CPLPopErrorHandler();
if (eErr != OGRERR_NONE && CPLGetLastErrorType() == 0)
{
printf("INFO: %s: CreateFeature() at line %d failed "
"but without explicit error.\n",
poDriver->GetDescription(), __LINE__);
}
delete poFeature;
/* Test feature with incompatible geometry */
poFeature = new OGRFeature(poLayer->GetLayerDefn());
if (bStrFieldOK)
poFeature->SetField(iFieldStr, "foo");
if (bIntFieldOK)
poFeature->SetField(iFieldInt, 123);
if (bRealFieldOK)
poFeature->SetField(iFieldReal, 1.23);
if (bDateFieldOK)
poFeature->SetField(iFieldDate, "2014/10/20");
if (bDateTimeFieldOK)
poFeature->SetField(iFieldDateTime, "2014/10/20 12:34:56");
OGRwkbGeometryType eOtherGeomType;
if (eGeomType == wkbUnknown || eGeomType == wkbNone)
eOtherGeomType = wkbLineString;
else if (wkbFlatten(eGeomType) == eGeomType)
eOtherGeomType = static_cast<OGRwkbGeometryType>(
(static_cast<int>(eGeomType) % 7) + 1);
else
eOtherGeomType = wkbSetZ(static_cast<OGRwkbGeometryType>(
((static_cast<int>(wkbFlatten(eGeomType)) % 7) + 1)));
pszWKT = GetWKT(eOtherGeomType);
if (pszWKT != nullptr)
{
OGRGeometry *poGeom = nullptr;
OGRGeometryFactory::createFromWkt(pszWKT, nullptr, &poGeom);
poFeature->SetGeometryDirectly(poGeom);
}
CPLErrorReset();
CPLPushErrorHandler(CPLQuietErrorHandler);
eErr = LOG_ACTION(poLayer->CreateFeature(poFeature));
CPLPopErrorHandler();
if (eErr != OGRERR_NONE && CPLGetLastErrorType() == 0)
{
printf("INFO: %s: CreateFeature() at line %d failed "
"but without explicit error.\n",
poDriver->GetDescription(), __LINE__);
}
delete poFeature;
/* Test reading a feature: write-only layers might not like this */
CPLPushErrorHandler(CPLQuietErrorHandler);
LOG_ACTION(poLayer->ResetReading());
delete LOG_ACTION(poLayer->GetNextFeature());
CPLPopErrorHandler();
osLayerNameToTest = poLayer->GetName();
eExpectedGeomType = poLayer->GetGeomType();
/* Some drivers don't like more than one layer per dataset */
CPLPushErrorHandler(CPLQuietErrorHandler);
const int bCreateLayerCap2 =
LOG_ACTION(poDS->TestCapability(ODsCCreateLayer));
OGRLayer *poLayer2 = LOG_ACTION(poDS->CreateLayer(
CPLSPrintf("%s2", CPLGetFilename(osFilename)), nullptr, eGeomType));
CPLPopErrorHandler();
if (poLayer2 == nullptr && bCreateLayerCap2)
{
printf("INFO: %s: Creation of second layer failed but "
"TestCapability(ODsCCreateLayer) succeeded.\n",
poDriver->GetDescription());
}
else if (!EQUAL(poDriver->GetDescription(), "CSV") &&
poLayer2 != nullptr)
{
OGRFieldDefn oFieldStr("str", OFTString);
CPLPushErrorHandler(CPLQuietErrorHandler);
LOG_ACTION(poLayer2->CreateField(&oFieldStr));
CPLPopErrorHandler();
poFeature = new OGRFeature(poLayer2->GetLayerDefn());
pszWKT = GetWKT(eGeomType);
if (pszWKT != nullptr)
{
OGRGeometry *poGeom = nullptr;
OGRGeometryFactory::createFromWkt(pszWKT, nullptr, &poGeom);
poFeature->SetGeometryDirectly(poGeom);
}
CPLErrorReset();
CPLPushErrorHandler(CPLQuietErrorHandler);
eErr = LOG_ACTION(poLayer2->CreateFeature(poFeature));
CPLPopErrorHandler();
delete poFeature;
if (eErr == OGRERR_NONE)
{
osLayerNameToTest = poLayer2->GetName();
eExpectedGeomType = poLayer2->GetGeomType();
}
}
/* Test deleting first layer */
const int bDeleteLayerCap =
LOG_ACTION(poDS->TestCapability(ODsCDeleteLayer));
CPLPushErrorHandler(CPLQuietErrorHandler);
eErr = LOG_ACTION(poDS->DeleteLayer(0));
CPLPopErrorHandler();
if (eErr == OGRERR_NONE)
{
if (!bDeleteLayerCap)
{
printf("ERROR: %s: TestCapability(ODsCDeleteLayer) "
"returns FALSE but layer deletion worked.\n",
poDriver->GetDescription());
bRet = FALSE;
}
if (LOG_ACTION(poDS->GetLayerByName(CPLGetFilename(osFilename))) !=
nullptr)
{
printf("ERROR: %s: DeleteLayer() declared success, "
"but layer can still be fetched.\n",
poDriver->GetDescription());
bRet = FALSE;
}
}
else
{
if (bDeleteLayerCap)
{
printf("ERROR: %s: TestCapability(ODsCDeleteLayer) "
"returns TRUE but layer deletion failed.\n",
poDriver->GetDescription());
bRet = FALSE;
}
}
}
/*else
{
if( bVerbose )
printf("INFO: %s: Creation of layer with geom_type = %s failed.\n",
poDriver->GetDescription(),
OGRGeometryTypeToName(eGeomType));
}*/
LOG_ACTION(GDALClose(poDS));
if (eExpectedGeomType != wkbUnknown &&
/* Those drivers are expected not to store a layer geometry type */
!EQUAL(poDriver->GetDescription(), "KML") &&
!EQUAL(poDriver->GetDescription(), "LIBKML") &&
!EQUAL(poDriver->GetDescription(), "PDF") &&
!EQUAL(poDriver->GetDescription(), "GeoJSON") &&
!EQUAL(poDriver->GetDescription(), "OGR_GMT") &&
!EQUAL(poDriver->GetDescription(), "PDS4") &&
!EQUAL(poDriver->GetDescription(), "FlatGeobuf"))
{
/* Reopen dataset */
poDS = LOG_ACTION(static_cast<GDALDataset *>(
GDALOpenEx(osFilename, GDAL_OF_VECTOR, nullptr, nullptr, nullptr)));
if (poDS != nullptr)
{
poLayer = LOG_ACTION(poDS->GetLayerByName(osLayerNameToTest));
if (poLayer != nullptr)
{
if (poLayer->GetGeomType() != eExpectedGeomType &&
!(eGeomType == wkbGeometryCollection25D &&
EQUAL(poDriver->GetDescription(), "OpenFileGDB")))
{
printf("ERROR: %s: GetGeomType() returns %d but %d "