-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathogrmvtdataset.cpp
6357 lines (5803 loc) · 239 KB
/
ogrmvtdataset.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: MVT Translator
* Purpose: Mapbox Vector Tile decoder
* Author: Even Rouault, Even Rouault <even dot rouault at spatialys dot com>
*
******************************************************************************
* Copyright (c) 2018, Even Rouault <even dot rouault at spatialys dot com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#if defined(HAVE_SQLITE) && defined(HAVE_GEOS)
// Needed by mvtutils.h
#define HAVE_MVT_WRITE_SUPPORT
#endif
#include "ogrsf_frmts.h"
#include "cpl_conv.h"
#include "cpl_json.h"
#include "cpl_http.h"
#include "ogr_p.h"
#include "mvt_tile.h"
#include "mvtutils.h"
#include "ogr_geos.h"
#include "gpb.h"
#include <algorithm>
#include <memory>
#include <vector>
#include <set>
const char *SRS_EPSG_3857 =
"PROJCS[\"WGS 84 / Pseudo-Mercator\",GEOGCS[\"WGS "
"84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS "
"84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY["
"\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
"UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
"AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Mercator_1SP\"],PARAMETER["
"\"central_meridian\",0],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_"
"easting\",0],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1,AUTHORITY["
"\"EPSG\",\"9001\"]],AXIS[\"X\",EAST],AXIS[\"Y\",NORTH],EXTENSION["
"\"PROJ4\",\"+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 "
"+x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext "
"+no_defs\"],AUTHORITY[\"EPSG\",\"3857\"]]";
// WebMercator related constants
constexpr double kmSPHERICAL_RADIUS = 6378137.0;
constexpr int knMAX_FILES_PER_DIR = 10000;
#ifdef HAVE_MVT_WRITE_SUPPORT
#include <sqlite3.h>
#include "../sqlite/ogrsqliteutility.h"
#include "../sqlite/ogrsqlitevfs.h"
#include "cpl_worker_thread_pool.h"
#include <mutex>
// Limitations from https://github.com/mapbox/mapbox-geostats
constexpr size_t knMAX_COUNT_LAYERS = 1000;
constexpr size_t knMAX_REPORT_LAYERS = 100;
constexpr size_t knMAX_COUNT_FIELDS = 1000;
constexpr size_t knMAX_REPORT_FIELDS = 100;
constexpr size_t knMAX_COUNT_VALUES = 1000;
constexpr size_t knMAX_REPORT_VALUES = 100;
constexpr size_t knMAX_STRING_VALUE_LENGTH = 256;
constexpr size_t knMAX_LAYER_NAME_LENGTH = 256;
constexpr size_t knMAX_FIELD_NAME_LENGTH = 256;
#undef SQLITE_STATIC
#define SQLITE_STATIC ((sqlite3_destructor_type) nullptr)
#endif
/************************************************************************/
/* InitWebMercatorTilingScheme() */
/************************************************************************/
static void InitWebMercatorTilingScheme(OGRSpatialReference *poSRS,
double &dfTopX, double &dfTopY,
double &dfTileDim0)
{
constexpr double kmMAX_GM =
kmSPHERICAL_RADIUS * M_PI; // 20037508.342789244
poSRS->SetFromUserInput(SRS_EPSG_3857);
dfTopX = -kmMAX_GM;
dfTopY = kmMAX_GM;
dfTileDim0 = 2 * kmMAX_GM;
}
/************************************************************************/
/* GetCmdId() */
/************************************************************************/
/* For a drawing instruction combining a command id and a command count,
* return the command id */
static unsigned GetCmdId(unsigned int nCmdCountCombined)
{
return nCmdCountCombined & 0x7;
}
/************************************************************************/
/* GetCmdCount() */
/************************************************************************/
/* For a drawing instruction combining a command id and a command count,
* return the command count */
static unsigned GetCmdCount(unsigned int nCmdCountCombined)
{
return nCmdCountCombined >> 3;
}
/************************************************************************/
/* OGRMVTLayerBase */
/************************************************************************/
class OGRMVTLayerBase CPL_NON_FINAL
: public OGRLayer,
public OGRGetNextFeatureThroughRaw<OGRMVTLayerBase>
{
virtual OGRFeature *GetNextRawFeature() = 0;
protected:
OGRFeatureDefn *m_poFeatureDefn = nullptr;
void InitFields(const CPLJSONObject &oFields,
const CPLJSONArray &oAttributesFromTileStats);
public:
virtual ~OGRMVTLayerBase();
virtual OGRFeatureDefn *GetLayerDefn() override
{
return m_poFeatureDefn;
}
DEFINE_GET_NEXT_FEATURE_THROUGH_RAW(OGRMVTLayerBase)
virtual int TestCapability(const char *) override;
};
/************************************************************************/
/* OGRMVTLayer */
/************************************************************************/
class OGRMVTDataset;
class OGRMVTLayer final : public OGRMVTLayerBase
{
OGRMVTDataset *m_poDS;
const GByte *m_pabyDataStart;
const GByte *m_pabyDataEnd;
const GByte *m_pabyDataCur = nullptr;
const GByte *m_pabyDataFeatureStart = nullptr;
bool m_bError = false;
unsigned int m_nExtent = knDEFAULT_EXTENT;
std::vector<CPLString> m_aosKeys;
typedef struct
{
OGRFieldType eType;
OGRFieldSubType eSubType;
OGRField sValue;
} Value;
std::vector<Value> m_asValues;
GIntBig m_nFID = 0;
GIntBig m_nFeatureCount = -1;
OGRPolygon m_oClipPoly;
double m_dfTileMinX = 0;
double m_dfTileMinY = 0;
double m_dfTileMaxX = 0;
double m_dfTileMaxY = 0;
bool m_bEnforceExternalIsClockwise = false;
void Init(const CPLJSONObject &oFields,
const CPLJSONArray &oAttributesFromTileStats);
bool QuickScanFeature(const GByte *pabyData,
const GByte *pabyDataFeatureEnd, bool bScanFields,
bool bScanGeometries, bool &bGeomTypeSet);
void GetXY(int nX, int nY, double &dfX, double &dfY);
OGRGeometry *ParseGeometry(unsigned int nGeomType,
const GByte *pabyDataGeometryEnd);
void SanitizeClippedGeometry(OGRGeometry *&poGeom);
virtual OGRFeature *GetNextRawFeature() override;
public:
OGRMVTLayer(OGRMVTDataset *poDS, const char *pszLayerName,
const GByte *pabyData, int nLayerSize,
const CPLJSONObject &oFields,
const CPLJSONArray &oAttributesFromTileStats,
OGRwkbGeometryType eGeomType);
virtual ~OGRMVTLayer();
virtual void ResetReading() override;
virtual GIntBig GetFeatureCount(int bForce) override;
GDALDataset *GetDataset() override;
};
/************************************************************************/
/* OGRMVTDirectoryLayer */
/************************************************************************/
class OGRMVTDirectoryLayer final : public OGRMVTLayerBase
{
OGRMVTDataset *m_poDS;
int m_nZ = 0;
bool m_bUseReadDir = true;
CPLString m_osDirName;
CPLStringList m_aosDirContent;
CPLString m_aosSubDirName;
CPLStringList m_aosSubDirContent;
bool m_bEOF = false;
int m_nXIndex = 0;
int m_nYIndex = 0;
GDALDataset *m_poCurrentTile = nullptr;
bool m_bJsonField = false;
GIntBig m_nFIDBase = 0;
OGREnvelope m_sExtent;
int m_nFilterMinX = 0;
int m_nFilterMinY = 0;
int m_nFilterMaxX = 0;
int m_nFilterMaxY = 0;
virtual OGRFeature *GetNextRawFeature() override;
OGRFeature *CreateFeatureFrom(OGRFeature *poSrcFeature);
void ReadNewSubDir();
void OpenTile();
void OpenTileIfNeeded();
public:
OGRMVTDirectoryLayer(OGRMVTDataset *poDS, const char *pszLayerName,
const char *pszDirectoryName,
const CPLJSONObject &oFields,
const CPLJSONArray &oAttributesFromTileStats,
bool bJsonField, OGRwkbGeometryType eGeomType,
const OGREnvelope *psExtent);
virtual ~OGRMVTDirectoryLayer();
virtual void ResetReading() override;
virtual GIntBig GetFeatureCount(int bForce) override;
OGRErr GetExtent(OGREnvelope *psExtent, int bForce) override;
virtual OGRErr GetExtent(int iGeomField, OGREnvelope *psExtent,
int bForce) override
{
return OGRLayer::GetExtent(iGeomField, psExtent, bForce);
}
virtual void SetSpatialFilter(OGRGeometry *) override;
virtual void SetSpatialFilter(int iGeomField, OGRGeometry *poGeom) override
{
OGRLayer::SetSpatialFilter(iGeomField, poGeom);
}
virtual OGRFeature *GetFeature(GIntBig nFID) override;
virtual int TestCapability(const char *) override;
GDALDataset *GetDataset() override;
};
/************************************************************************/
/* OGRMVTDataset */
/************************************************************************/
class OGRMVTDataset final : public GDALDataset
{
friend class OGRMVTLayer;
friend class OGRMVTDirectoryLayer;
GByte *m_pabyData;
std::vector<std::unique_ptr<OGRLayer>> m_apoLayers;
bool m_bGeoreferenced = false;
double m_dfTileDimX = 0.0;
double m_dfTileDimY = 0.0;
double m_dfTopX = 0.0;
double m_dfTopY = 0.0;
CPLString m_osMetadataMemFilename;
bool m_bClip = true;
CPLString m_osTileExtension{"pbf"};
OGRSpatialReference *m_poSRS = nullptr;
double m_dfTileDim0 = 0.0;
double m_dfTopXOrigin = 0.0;
double m_dfTopYOrigin = 0.0;
static GDALDataset *OpenDirectory(GDALOpenInfo *);
public:
explicit OGRMVTDataset(GByte *pabyData);
virtual ~OGRMVTDataset();
virtual int GetLayerCount() override
{
return static_cast<int>(m_apoLayers.size());
}
virtual OGRLayer *GetLayer(int) override;
virtual int TestCapability(const char *) override
{
return FALSE;
}
static GDALDataset *Open(GDALOpenInfo *);
static GDALDataset *Open(GDALOpenInfo *, bool bRecurseAllowed);
OGRSpatialReference *GetSRS()
{
return m_poSRS;
}
double GetTileDim0() const
{
return m_dfTileDim0;
}
double GetTopXOrigin() const
{
return m_dfTopXOrigin;
}
double GetTopYOrigin() const
{
return m_dfTopYOrigin;
}
};
/************************************************************************/
/* ~OGRMVTLayerBase() */
/************************************************************************/
OGRMVTLayerBase::~OGRMVTLayerBase()
{
m_poFeatureDefn->Release();
}
/************************************************************************/
/* InitFields() */
/************************************************************************/
void OGRMVTLayerBase::InitFields(const CPLJSONObject &oFields,
const CPLJSONArray &oAttributesFromTileStats)
{
OGRMVTInitFields(m_poFeatureDefn, oFields, oAttributesFromTileStats);
}
/************************************************************************/
/* TestCapability() */
/************************************************************************/
int OGRMVTLayerBase::TestCapability(const char *pszCap)
{
if (EQUAL(pszCap, OLCStringsAsUTF8) || EQUAL(pszCap, OLCFastSpatialFilter))
{
return TRUE;
}
return FALSE;
}
/************************************************************************/
/* OGRMVTLayer() */
/************************************************************************/
OGRMVTLayer::OGRMVTLayer(OGRMVTDataset *poDS, const char *pszLayerName,
const GByte *pabyData, int nLayerSize,
const CPLJSONObject &oFields,
const CPLJSONArray &oAttributesFromTileStats,
OGRwkbGeometryType eGeomType)
: m_poDS(poDS), m_pabyDataStart(pabyData),
m_pabyDataEnd(pabyData + nLayerSize)
{
m_poFeatureDefn = new OGRFeatureDefn(pszLayerName);
SetDescription(m_poFeatureDefn->GetName());
m_poFeatureDefn->SetGeomType(eGeomType);
m_poFeatureDefn->Reference();
if (m_poDS->m_bGeoreferenced)
{
m_poFeatureDefn->GetGeomFieldDefn(0)->SetSpatialRef(m_poDS->GetSRS());
}
Init(oFields, oAttributesFromTileStats);
GetXY(0, 0, m_dfTileMinX, m_dfTileMaxY);
GetXY(m_nExtent, m_nExtent, m_dfTileMaxX, m_dfTileMinY);
OGRLinearRing *poLR = new OGRLinearRing();
poLR->addPoint(m_dfTileMinX, m_dfTileMinY);
poLR->addPoint(m_dfTileMinX, m_dfTileMaxY);
poLR->addPoint(m_dfTileMaxX, m_dfTileMaxY);
poLR->addPoint(m_dfTileMaxX, m_dfTileMinY);
poLR->addPoint(m_dfTileMinX, m_dfTileMinY);
m_oClipPoly.addRingDirectly(poLR);
// Config option only for tests for now. When set, it ensures that
// the first ring (exterior ring) of a polygon is clockwise oriented,
// as per the MVT spec.
// By default, we are more tolerant and only use reversal of winding order
// to detect inner rings.
m_bEnforceExternalIsClockwise = CPLTestBool(
CPLGetConfigOption("OGR_MVT_ENFORE_EXTERNAL_RING_IS_CLOCKWISE", "NO"));
}
/************************************************************************/
/* ~OGRMVTLayer() */
/************************************************************************/
OGRMVTLayer::~OGRMVTLayer()
{
for (auto &sValue : m_asValues)
{
if (sValue.eType == OFTString)
{
CPLFree(sValue.sValue.String);
}
}
}
/************************************************************************/
/* Init() */
/************************************************************************/
void OGRMVTLayer::Init(const CPLJSONObject &oFields,
const CPLJSONArray &oAttributesFromTileStats)
{
// First pass to collect keys and values
const GByte *pabyData = m_pabyDataStart;
const GByte *pabyDataLimit = m_pabyDataEnd;
unsigned int nKey = 0;
bool bGeomTypeSet = false;
const bool bScanFields = !oFields.IsValid();
const bool bScanGeometries = m_poFeatureDefn->GetGeomType() == wkbUnknown;
const bool bQuickScanFeature = bScanFields || bScanGeometries;
try
{
while (pabyData < pabyDataLimit)
{
READ_FIELD_KEY(nKey);
if (nKey == MAKE_KEY(knLAYER_KEYS, WT_DATA))
{
char *pszKey = nullptr;
READ_TEXT(pabyData, pabyDataLimit, pszKey);
m_aosKeys.push_back(pszKey);
CPLFree(pszKey);
}
else if (nKey == MAKE_KEY(knLAYER_VALUES, WT_DATA))
{
unsigned int nValueLength = 0;
READ_SIZE(pabyData, pabyDataLimit, nValueLength);
const GByte *pabyDataValueEnd = pabyData + nValueLength;
READ_VARUINT32(pabyData, pabyDataLimit, nKey);
if (nKey == MAKE_KEY(knVALUE_STRING, WT_DATA))
{
char *pszValue = nullptr;
READ_TEXT(pabyData, pabyDataLimit, pszValue);
Value sValue;
sValue.eType = OFTString;
sValue.eSubType = OFSTNone;
sValue.sValue.String = pszValue;
m_asValues.push_back(sValue);
}
else if (nKey == MAKE_KEY(knVALUE_FLOAT, WT_32BIT))
{
Value sValue;
sValue.eType = OFTReal;
sValue.eSubType = OFSTFloat32;
sValue.sValue.Real = ReadFloat32(&pabyData, pabyDataLimit);
m_asValues.push_back(sValue);
}
else if (nKey == MAKE_KEY(knVALUE_DOUBLE, WT_64BIT))
{
Value sValue;
sValue.eType = OFTReal;
sValue.eSubType = OFSTNone;
sValue.sValue.Real = ReadFloat64(&pabyData, pabyDataLimit);
m_asValues.push_back(sValue);
}
else if (nKey == MAKE_KEY(knVALUE_INT, WT_VARINT))
{
GIntBig nVal = 0;
READ_VARINT64(pabyData, pabyDataLimit, nVal);
Value sValue;
sValue.eType = (nVal >= INT_MIN && nVal <= INT_MAX)
? OFTInteger
: OFTInteger64;
sValue.eSubType = OFSTNone;
if (sValue.eType == OFTInteger)
sValue.sValue.Integer = static_cast<int>(nVal);
else
sValue.sValue.Integer64 = nVal;
m_asValues.push_back(sValue);
}
else if (nKey == MAKE_KEY(knVALUE_UINT, WT_VARINT))
{
GUIntBig nVal = 0;
READ_VARUINT64(pabyData, pabyDataLimit, nVal);
Value sValue;
sValue.eType =
(nVal <= INT_MAX) ? OFTInteger : OFTInteger64;
sValue.eSubType = OFSTNone;
if (sValue.eType == OFTInteger)
sValue.sValue.Integer = static_cast<int>(nVal);
else
sValue.sValue.Integer64 = static_cast<GIntBig>(nVal);
m_asValues.push_back(sValue);
}
else if (nKey == MAKE_KEY(knVALUE_SINT, WT_VARINT))
{
GIntBig nVal = 0;
READ_VARSINT64(pabyData, pabyDataLimit, nVal);
Value sValue;
sValue.eType = (nVal >= INT_MIN && nVal <= INT_MAX)
? OFTInteger
: OFTInteger64;
sValue.eSubType = OFSTNone;
if (sValue.eType == OFTInteger)
sValue.sValue.Integer = static_cast<int>(nVal);
else
sValue.sValue.Integer64 = nVal;
m_asValues.push_back(sValue);
}
else if (nKey == MAKE_KEY(knVALUE_BOOL, WT_VARINT))
{
unsigned nVal = 0;
READ_VARUINT32(pabyData, pabyDataLimit, nVal);
Value sValue;
sValue.eType = OFTInteger;
sValue.eSubType = OFSTBoolean;
sValue.sValue.Integer = static_cast<int>(nVal);
m_asValues.push_back(sValue);
}
pabyData = pabyDataValueEnd;
}
else if (nKey == MAKE_KEY(knLAYER_EXTENT, WT_VARINT))
{
GUInt32 nExtent = 0;
READ_VARUINT32(pabyData, pabyDataLimit, nExtent);
m_nExtent = std::max(1U, nExtent); // to avoid divide by zero
}
else
{
SKIP_UNKNOWN_FIELD(pabyData, pabyDataLimit, FALSE);
}
}
InitFields(oFields, oAttributesFromTileStats);
m_nFeatureCount = 0;
pabyData = m_pabyDataStart;
// Second pass to iterate over features to figure out the geometry type
// and attribute schema
while (pabyData < pabyDataLimit)
{
const GByte *pabyDataBefore = pabyData;
READ_FIELD_KEY(nKey);
if (nKey == MAKE_KEY(knLAYER_FEATURES, WT_DATA))
{
if (m_pabyDataFeatureStart == nullptr)
{
m_pabyDataFeatureStart = pabyDataBefore;
m_pabyDataCur = pabyDataBefore;
}
unsigned int nFeatureLength = 0;
READ_SIZE(pabyData, pabyDataLimit, nFeatureLength);
const GByte *pabyDataFeatureEnd = pabyData + nFeatureLength;
if (bQuickScanFeature)
{
if (!QuickScanFeature(pabyData, pabyDataFeatureEnd,
bScanFields, bScanGeometries,
bGeomTypeSet))
{
return;
}
}
pabyData = pabyDataFeatureEnd;
m_nFeatureCount++;
}
else
{
SKIP_UNKNOWN_FIELD(pabyData, pabyDataLimit, FALSE);
}
}
}
catch (const GPBException &e)
{
CPLError(CE_Failure, CPLE_AppDefined, "%s", e.what());
}
}
/************************************************************************/
/* MergeFieldDefn() */
/************************************************************************/
static void MergeFieldDefn(OGRFieldDefn *poFieldDefn, OGRFieldType eSrcType,
OGRFieldSubType eSrcSubType)
{
if (eSrcType == OFTString)
{
poFieldDefn->SetSubType(OFSTNone);
poFieldDefn->SetType(OFTString);
}
else if (poFieldDefn->GetType() == OFTInteger && eSrcType == OFTInteger64)
{
poFieldDefn->SetSubType(OFSTNone);
poFieldDefn->SetType(OFTInteger64);
}
else if ((poFieldDefn->GetType() == OFTInteger ||
poFieldDefn->GetType() == OFTInteger64) &&
eSrcType == OFTReal)
{
poFieldDefn->SetSubType(OFSTNone);
poFieldDefn->SetType(OFTReal);
poFieldDefn->SetSubType(eSrcSubType);
}
else if (poFieldDefn->GetType() == OFTReal && eSrcType == OFTReal &&
eSrcSubType == OFSTNone)
{
poFieldDefn->SetSubType(OFSTNone);
}
else if (poFieldDefn->GetType() == OFTInteger && eSrcType == OFTInteger &&
eSrcSubType == OFSTNone)
{
poFieldDefn->SetSubType(OFSTNone);
}
}
/************************************************************************/
/* QuickScanFeature() */
/************************************************************************/
bool OGRMVTLayer::QuickScanFeature(const GByte *pabyData,
const GByte *pabyDataFeatureEnd,
bool bScanFields, bool bScanGeometries,
bool &bGeomTypeSet)
{
unsigned int nKey = 0;
unsigned int nGeomType = 0;
try
{
while (pabyData < pabyDataFeatureEnd)
{
READ_VARUINT32(pabyData, pabyDataFeatureEnd, nKey);
if (nKey == MAKE_KEY(knFEATURE_TYPE, WT_VARINT))
{
READ_VARUINT32(pabyData, pabyDataFeatureEnd, nGeomType);
}
else if (nKey == MAKE_KEY(knFEATURE_TAGS, WT_DATA) && bScanFields)
{
unsigned int nTagsSize = 0;
READ_SIZE(pabyData, pabyDataFeatureEnd, nTagsSize);
const GByte *pabyDataTagsEnd = pabyData + nTagsSize;
while (pabyData < pabyDataTagsEnd)
{
unsigned int nKeyIdx = 0;
unsigned int nValIdx = 0;
READ_VARUINT32(pabyData, pabyDataTagsEnd, nKeyIdx);
READ_VARUINT32(pabyData, pabyDataTagsEnd, nValIdx);
if (nKeyIdx >= m_aosKeys.size())
{
CPLError(CE_Failure, CPLE_AppDefined,
"Invalid tag key index: %u", nKeyIdx);
m_bError = true;
return false;
}
if (nValIdx >= m_asValues.size())
{
CPLError(CE_Failure, CPLE_AppDefined,
"Invalid tag value index: %u", nValIdx);
m_bError = true;
return false;
}
const int nFieldIdx =
m_poFeatureDefn->GetFieldIndex(m_aosKeys[nKeyIdx]);
if (nFieldIdx < 0)
{
OGRFieldDefn oFieldDefn(m_aosKeys[nKeyIdx],
m_asValues[nValIdx].eType);
oFieldDefn.SetSubType(m_asValues[nValIdx].eSubType);
m_poFeatureDefn->AddFieldDefn(&oFieldDefn);
}
else if (m_poFeatureDefn->GetFieldDefn(nFieldIdx)
->GetType() != m_asValues[nValIdx].eType ||
m_poFeatureDefn->GetFieldDefn(nFieldIdx)
->GetSubType() !=
m_asValues[nValIdx].eSubType)
{
OGRFieldDefn *poFieldDefn =
m_poFeatureDefn->GetFieldDefn(nFieldIdx);
OGRFieldType eSrcType(m_asValues[nValIdx].eType);
OGRFieldSubType eSrcSubType(
m_asValues[nValIdx].eSubType);
MergeFieldDefn(poFieldDefn, eSrcType, eSrcSubType);
}
}
}
else if (nKey == MAKE_KEY(knFEATURE_GEOMETRY, WT_DATA) &&
bScanGeometries && nGeomType >= knGEOM_TYPE_POINT &&
nGeomType <= knGEOM_TYPE_POLYGON)
{
unsigned int nGeometrySize = 0;
READ_SIZE(pabyData, pabyDataFeatureEnd, nGeometrySize);
const GByte *pabyDataGeometryEnd = pabyData + nGeometrySize;
OGRwkbGeometryType eType = wkbUnknown;
if (nGeomType == knGEOM_TYPE_POINT)
{
eType = wkbPoint;
unsigned int nCmdCountCombined = 0;
READ_VARUINT32(pabyData, pabyDataGeometryEnd,
nCmdCountCombined);
if (GetCmdId(nCmdCountCombined) == knCMD_MOVETO &&
GetCmdCount(nCmdCountCombined) > 1)
{
eType = wkbMultiPoint;
}
}
else if (nGeomType == knGEOM_TYPE_LINESTRING)
{
eType = wkbLineString;
for (int iIter = 0; pabyData < pabyDataGeometryEnd; iIter++)
{
if (iIter == 1)
{
eType = wkbMultiLineString;
break;
}
unsigned int nCmdCountCombined = 0;
unsigned int nLineToCount;
// Should be a moveto
SKIP_VARINT(pabyData, pabyDataGeometryEnd);
SKIP_VARINT(pabyData, pabyDataGeometryEnd);
SKIP_VARINT(pabyData, pabyDataGeometryEnd);
READ_VARUINT32(pabyData, pabyDataGeometryEnd,
nCmdCountCombined);
nLineToCount = GetCmdCount(nCmdCountCombined);
for (unsigned i = 0; i < 2 * nLineToCount; i++)
{
SKIP_VARINT(pabyData, pabyDataGeometryEnd);
}
}
}
else /* if( nGeomType == knGEOM_TYPE_POLYGON ) */
{
eType = wkbPolygon;
for (int iIter = 0; pabyData < pabyDataGeometryEnd; iIter++)
{
if (iIter == 1)
{
eType = wkbMultiPolygon;
break;
}
unsigned int nCmdCountCombined = 0;
unsigned int nLineToCount;
// Should be a moveto
SKIP_VARINT(pabyData, pabyDataGeometryEnd);
SKIP_VARINT(pabyData, pabyDataGeometryEnd);
SKIP_VARINT(pabyData, pabyDataGeometryEnd);
READ_VARUINT32(pabyData, pabyDataGeometryEnd,
nCmdCountCombined);
nLineToCount = GetCmdCount(nCmdCountCombined);
for (unsigned i = 0; i < 2 * nLineToCount; i++)
{
SKIP_VARINT(pabyData, pabyDataGeometryEnd);
}
// Should be a closepath
SKIP_VARINT(pabyData, pabyDataGeometryEnd);
}
}
if (bGeomTypeSet && m_poFeatureDefn->GetGeomType() ==
OGR_GT_GetCollection(eType))
{
// do nothing
}
else if (bGeomTypeSet &&
eType == OGR_GT_GetCollection(
m_poFeatureDefn->GetGeomType()))
{
m_poFeatureDefn->SetGeomType(eType);
}
else if (bGeomTypeSet &&
m_poFeatureDefn->GetGeomType() != eType)
{
m_poFeatureDefn->SetGeomType(wkbUnknown);
}
else
{
m_poFeatureDefn->SetGeomType(eType);
}
bGeomTypeSet = true;
pabyData = pabyDataGeometryEnd;
}
else
{
SKIP_UNKNOWN_FIELD(pabyData, pabyDataFeatureEnd, FALSE);
}
}
return true;
}
catch (const GPBException &e)
{
CPLError(CE_Failure, CPLE_AppDefined, "%s", e.what());
return false;
}
}
/************************************************************************/
/* GetFeatureCount() */
/************************************************************************/
GIntBig OGRMVTLayer::GetFeatureCount(int bForce)
{
if (m_poFilterGeom == nullptr && m_poAttrQuery == nullptr &&
m_nFeatureCount >= 0)
{
return m_nFeatureCount;
}
return OGRLayer::GetFeatureCount(bForce);
}
/************************************************************************/
/* ResetReading() */
/************************************************************************/
void OGRMVTLayer::ResetReading()
{
m_nFID = 0;
m_pabyDataCur = m_pabyDataFeatureStart;
}
/************************************************************************/
/* GetXY() */
/************************************************************************/
void OGRMVTLayer::GetXY(int nX, int nY, double &dfX, double &dfY)
{
if (m_poDS->m_bGeoreferenced)
{
dfX = m_poDS->m_dfTopX + nX * m_poDS->m_dfTileDimX / m_nExtent;
dfY = m_poDS->m_dfTopY - nY * m_poDS->m_dfTileDimY / m_nExtent;
}
else
{
dfX = nX;
dfY = static_cast<double>(m_nExtent) - nY;
}
}
/************************************************************************/
/* AddWithOverflowAccepted() */
/************************************************************************/
CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW
static int AddWithOverflowAccepted(int a, int b)
{
// In fact in normal situations a+b should not overflow. That can only
// happen with corrupted datasets. But we don't really want to add code
// to detect that situation, so basically this is just a trick to perform
// the addition without the various sanitizers to yell about the overflow.
//
// Assumes complement-to-two signed integer representation and that
// the compiler will safely cast a big unsigned to negative integer.
return static_cast<int>(static_cast<unsigned>(a) +
static_cast<unsigned>(b));
}
/************************************************************************/
/* ParseGeometry() */
/************************************************************************/
OGRGeometry *OGRMVTLayer::ParseGeometry(unsigned int nGeomType,
const GByte *pabyDataGeometryEnd)
{
OGRMultiPoint *poMultiPoint = nullptr;
OGRMultiLineString *poMultiLS = nullptr;
OGRLineString *poLine = nullptr;
OGRMultiPolygon *poMultiPoly = nullptr;
OGRPolygon *poPoly = nullptr;
OGRLinearRing *poRing = nullptr;
try
{
if (nGeomType == knGEOM_TYPE_POINT)
{
unsigned int nCmdCountCombined = 0;
unsigned int nCount;
READ_VARUINT32(m_pabyDataCur, pabyDataGeometryEnd,
nCmdCountCombined);
nCount = GetCmdCount(nCmdCountCombined);
if (GetCmdId(nCmdCountCombined) == knCMD_MOVETO && nCount == 1)
{
int nX = 0;
int nY = 0;
READ_VARSINT32(m_pabyDataCur, pabyDataGeometryEnd, nX);
READ_VARSINT32(m_pabyDataCur, pabyDataGeometryEnd, nY);
double dfX;
double dfY;
GetXY(nX, nY, dfX, dfY);
OGRPoint *poPoint = new OGRPoint(dfX, dfY);
if (m_poFeatureDefn->GetGeomType() == wkbMultiPoint)
{
poMultiPoint = new OGRMultiPoint();
poMultiPoint->addGeometryDirectly(poPoint);
return poMultiPoint;
}
else
{
return poPoint;
}
}
else if (GetCmdId(nCmdCountCombined) == knCMD_MOVETO && nCount > 1)
{
int nX = 0;
int nY = 0;
poMultiPoint = new OGRMultiPoint();
for (unsigned i = 0; i < nCount; i++)
{
int nDX = 0;
int nDY = 0;
READ_VARSINT32(m_pabyDataCur, pabyDataGeometryEnd, nDX);
READ_VARSINT32(m_pabyDataCur, pabyDataGeometryEnd, nDY);
// if( nDX != 0 || nDY != 0 )
{
nX = AddWithOverflowAccepted(nX, nDX);
nY = AddWithOverflowAccepted(nY, nDY);
double dfX;
double dfY;
GetXY(nX, nY, dfX, dfY);
OGRPoint *poPoint = new OGRPoint(dfX, dfY);
if (i == 0 && nCount == 2 &&
m_pabyDataCur == pabyDataGeometryEnd)
{
// Current versions of Mapserver at time of writing
// wrongly encode a point with nCount = 2
static bool bWarned = false;
if (!bWarned)
{
CPLDebug(
"MVT",
"Reading likely a broken point as "
"produced by some versions of Mapserver");
bWarned = true;
}
delete poMultiPoint;
return poPoint;
}
poMultiPoint->addGeometryDirectly(poPoint);
}
}
return poMultiPoint;
}
}
else if (nGeomType == knGEOM_TYPE_LINESTRING)
{
int nX = 0;
int nY = 0;
while (m_pabyDataCur < pabyDataGeometryEnd)
{
unsigned int nCmdCountCombined = 0;
unsigned int nLineToCount;
// Should be a moveto
SKIP_VARINT(m_pabyDataCur, pabyDataGeometryEnd);
int nDX = 0;
int nDY = 0;
READ_VARSINT32(m_pabyDataCur, pabyDataGeometryEnd, nDX);
READ_VARSINT32(m_pabyDataCur, pabyDataGeometryEnd, nDY);
nX = AddWithOverflowAccepted(nX, nDX);
nY = AddWithOverflowAccepted(nY, nDY);
double dfX;
double dfY;
GetXY(nX, nY, dfX, dfY);
if (poLine != nullptr)
{
if (poMultiLS == nullptr)
{
poMultiLS = new OGRMultiLineString();
poMultiLS->addGeometryDirectly(poLine);
}
poLine = new OGRLineString();
poMultiLS->addGeometryDirectly(poLine);
}
else
{