-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
ogr_mvt.py
1547 lines (1221 loc) · 57.8 KB
/
ogr_mvt.py
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
#!/usr/bin/env pytest
###############################################################################
# $Id$
#
# Project: GDAL/OGR Test Suite
# Purpose: Test OGR MVT driver functionality.
# Author: Even Rouault <even dot rouault at spatialys dot com>
#
###############################################################################
# Copyright (c) 2018, Even Rouault <even dot rouault at spatialys dot com>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
###############################################################################
import json
import sys
import gdaltest
import ogrtest
import webserver
from osgeo import gdal
from osgeo import ogr
from osgeo import osr
import pytest
###############################################################################
def test_ogr_mvt_datatypes():
# With metadata.json
ds = ogr.Open('data/mvt/datatypes/0/0/0.pbf')
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
if f['bool_false'] != 0 or \
f['bool_true'] != 1 or \
f['pos_int_value'] != 1 or \
f['pos_int64_value'] != 123456789012345 or \
f['neg_int_value'] != -1 or \
f['neg_int64_value'] != -123456789012345 or \
f['pos_sint_value'] != 1 or \
f['pos_sint64_value'] != 123456789012345 or \
f['neg_sint_value'] != -1 or \
f['neg_sint64_value'] != -123456789012345 or \
f['uint_value'] != 2000000000 or \
f['uint64_value'] != 4000000000 or \
f['float_value'] != 1.25 or \
f['real_value'] != 1.23456789 or \
f['string_value'] != 'str':
f.DumpReadable()
pytest.fail()
# Without metadata.json
ds = gdal.OpenEx('data/mvt/datatypes/0/0/0.pbf',
open_options=['METADATA_FILE='])
lyr = ds.GetLayer(0)
count = lyr.GetLayerDefn().GetFieldCount()
assert count == 16
tab = []
for i in range(lyr.GetLayerDefn().GetFieldCount()):
fld_defn = lyr.GetLayerDefn().GetFieldDefn(i)
tab += [(fld_defn.GetName(), fld_defn.GetType(),
fld_defn.GetSubType())]
expected_tab = [
('mvt_id', ogr.OFTInteger64, ogr.OFSTNone),
('bool_true', ogr.OFTInteger, ogr.OFSTBoolean),
('bool_false', ogr.OFTInteger, ogr.OFSTBoolean),
('pos_int_value', ogr.OFTInteger, ogr.OFSTNone),
('pos_int64_value', ogr.OFTInteger64, ogr.OFSTNone),
('neg_int_value', ogr.OFTInteger, ogr.OFSTNone),
('neg_int64_value', ogr.OFTInteger64, ogr.OFSTNone),
('pos_sint_value', ogr.OFTInteger, ogr.OFSTNone),
('pos_sint64_value', ogr.OFTInteger64, ogr.OFSTNone),
('neg_sint_value', ogr.OFTInteger, ogr.OFSTNone),
('neg_sint64_value', ogr.OFTInteger64, ogr.OFSTNone),
('uint_value', ogr.OFTInteger, ogr.OFSTNone),
('uint64_value', ogr.OFTInteger64, ogr.OFSTNone),
('float_value', ogr.OFTReal, ogr.OFSTFloat32),
('real_value', ogr.OFTReal, ogr.OFSTNone),
('string_value', ogr.OFTString, ogr.OFSTNone),
]
assert tab == expected_tab
f = lyr.GetNextFeature()
if f['bool_false'] != 0 or \
f['bool_true'] != 1 or \
f['pos_int_value'] != 1 or \
f['pos_int64_value'] != 123456789012345 or \
f['neg_int_value'] != -1 or \
f['neg_int64_value'] != -123456789012345 or \
f['pos_sint_value'] != 1 or \
f['pos_sint64_value'] != 123456789012345 or \
f['neg_sint_value'] != -1 or \
f['neg_sint64_value'] != -123456789012345 or \
f['uint_value'] != 2000000000 or \
f['uint64_value'] != 4000000000 or \
f['float_value'] != 1.25 or \
f['real_value'] != 1.23456789 or \
f['string_value'] != 'str':
f.DumpReadable()
pytest.fail()
###############################################################################
def test_ogr_mvt_datatype_promotion():
ds = ogr.Open('data/mvt/datatype_promotion.pbf')
tab = [('int_to_int64', ogr.OFTInteger64),
('int_to_real', ogr.OFTReal),
('int64_to_real', ogr.OFTReal),
('bool_to_int', ogr.OFTInteger),
('bool_to_str', ogr.OFTString),
('float_to_double', ogr.OFTReal)]
for layer_name, dt in tab:
lyr = ds.GetLayerByName(layer_name)
fld_defn = lyr.GetLayerDefn().GetFieldDefn(1)
assert fld_defn.GetType() == dt, layer_name
assert fld_defn.GetSubType() == ogr.OFSTNone, layer_name
###############################################################################
def test_ogr_mvt_limit_cases():
with gdaltest.error_handler():
ds = ogr.Open('data/mvt/limit_cases.pbf')
lyr = ds.GetLayerByName('empty')
assert lyr.GetFeatureCount() == 0
lyr = ds.GetLayerByName('layer1')
assert lyr.GetFeatureCount() == 7
f = lyr.GetFeature(1)
assert f['mvt_id'] == 1
with gdaltest.error_handler():
f = lyr.GetFeature(6)
assert f['b'] == 1
lyr = ds.GetLayerByName('layer2')
assert lyr.GetFeatureCount() == 0
lyr = ds.GetLayerByName('layer3')
assert lyr.GetFeatureCount() == 0
lyr = ds.GetLayerByName('layer4')
assert lyr.GetFeatureCount() == 0
lyr = ds.GetLayerByName('layer5')
assert lyr.GetFeatureCount() == 1
f = lyr.GetNextFeature()
assert f.GetGeometryRef().ExportToWkt() == 'POINT (2070 2690)'
###############################################################################
def test_ogr_mvt_with_extension_fields():
ds = ogr.Open('data/mvt/with_extension_fields.pbf')
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f, 'LINESTRING (2070 2690,2082 2707)') != 0:
f.DumpReadable()
pytest.fail()
###############################################################################
def test_ogr_mvt_mixed():
ds = ogr.Open('data/mvt/mixed/0/0/0.pbf')
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f, 'MULTIPOINT ((215246.671651058 6281289.23636264),(332653.947097085 6447616.20991119))') != 0:
f.DumpReadable()
pytest.fail()
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f, 'LINESTRING (215246.671651058 6281289.23636264,332653.947097085 6447616.20991119)') != 0:
f.DumpReadable()
pytest.fail()
###############################################################################
def test_ogr_mvt_linestring():
ds = ogr.Open('data/mvt/linestring/0/0/0.pbf')
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f, 'MULTILINESTRING ((215246.671651058 6281289.23636264,332653.947097085 6447616.20991119))') != 0:
f.DumpReadable()
pytest.fail()
ds = gdal.OpenEx('data/mvt/linestring/0/0/0.pbf', open_options=['METADATA_FILE='])
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f, 'LINESTRING (215246.671651058 6281289.23636264,332653.947097085 6447616.20991119)') != 0:
f.DumpReadable()
pytest.fail()
###############################################################################
def test_ogr_mvt_multilinestring():
ds = ogr.Open('data/mvt/multilinestring/0/0/0.pbf')
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f, 'MULTILINESTRING ((215246.671651058 6281289.23636264,332653.947097085 6447616.20991119),(440277.282922614 6623727.12308023,547900.618748143 6809621.97586978),(665307.894194175 6985732.88903883,772931.230019704 7171627.74182838))') != 0:
f.DumpReadable()
pytest.fail()
ds = gdal.OpenEx('data/mvt/multilinestring/0/0/0.pbf', open_options=['METADATA_FILE='])
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f, 'MULTILINESTRING ((215246.671651058 6281289.23636264,332653.947097085 6447616.20991119),(440277.282922614 6623727.12308023,547900.618748143 6809621.97586978),(665307.894194175 6985732.88903883,772931.230019704 7171627.74182838))') != 0:
f.DumpReadable()
pytest.fail()
###############################################################################
def test_ogr_mvt_polygon():
ds = ogr.Open('data/mvt/polygon/0/0/0.pbf')
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f, 'MULTIPOLYGON (((332653.947097085 6447616.20991119,332653.947097085 6281289.23636264,215246.671651058 6281289.23636264,215246.671651058 6447616.20991119,332653.947097085 6447616.20991119)))') != 0:
f.DumpReadable()
pytest.fail()
ds = gdal.OpenEx('data/mvt/polygon/0/0/0.pbf', open_options=['METADATA_FILE='])
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f, 'POLYGON ((332653.947097085 6447616.20991119,332653.947097085 6281289.23636264,215246.671651058 6281289.23636264,215246.671651058 6447616.20991119,332653.947097085 6447616.20991119))') != 0:
f.DumpReadable()
pytest.fail()
###############################################################################
def test_ogr_mvt_point_polygon():
ds = ogr.Open('data/mvt/point_polygon/0')
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f, 'MULTIPOINT ((215246.671651058 6281289.23636264))') != 0:
f.DumpReadable()
pytest.fail()
lyr = ds.GetLayer(1)
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f, 'MULTIPOLYGON (((440277.282922614 450061.222543117,440277.282922614 -440277.282922614,0.0 -440277.282922614,0.0 -215246.671651058,215246.671651058 -215246.671651058,215246.671651058 225030.61127156,0.0 225030.61127156,0.0 450061.222543117,440277.282922614 450061.222543117)),((0.0 117407.275446031,0.0 -107623.335825529,-117407.275446031 -107623.335825529,-117407.275446031 117407.275446031,0.0 117407.275446031)),((107623.335825529 58703.6377230138,107623.335825529 -48919.6981025115,48919.6981025115 -48919.6981025115,48919.6981025115 58703.6377230138,107623.335825529 58703.6377230138)))') != 0:
f.DumpReadable()
pytest.fail()
###############################################################################
def test_ogr_mvt_point_polygon_clip():
if not ogrtest.have_geos() or gdal.GetConfigOption('OGR_MVT_CLIP') is not None:
pytest.skip()
if gdal.GetConfigOption('APPVEYOR') is not None:
pytest.skip()
if sys.platform == 'darwin' and gdal.GetConfigOption('TRAVIS', None) is not None:
pytest.skip()
ds = ogr.Open('data/mvt/point_polygon/1')
lyr = ds.GetLayer(1)
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f, 'MULTIPOLYGON (((445169.252732867 450061.222543117,445169.252732867 0.0,220138.641461308 0.0,220138.641461308 225030.61127156,0.0 225030.61127156,0.0 450061.222543117,445169.252732867 450061.222543117)),((107623.335825528 58703.6377230138,107623.335825528 0.0,53811.6679127641 0.0,53811.6679127641 58703.6377230138,107623.335825528 58703.6377230138)))') != 0 and \
ogrtest.check_feature_geometry(f, 'MULTIPOLYGON (((445169.252732867 0.0,445169.252732867 -445169.252732867,0.0 -445169.252732867,0.0 -220138.641461308,220138.641461308 -220138.641461308,220138.641461308 0.0,445169.252732867 0.0)),((107623.335825528 0.0,107623.335825528 -53811.6679127641,53811.6679127641 -53811.6679127641,53811.6679127641 0.0,107623.335825528 0.0)))') != 0:
f.DumpReadable()
pytest.fail()
###############################################################################
def test_ogr_mvt_tileset_without_readdir():
with gdaltest.config_option('MVT_USE_READDIR', 'NO'):
ds = gdal.OpenEx('data/mvt/linestring/0')
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
assert f is not None
###############################################################################
def test_ogr_mvt_tileset_tilegl():
ds = ogr.Open('data/mvt/linestring_tilejson_gl/0')
lyr = ds.GetLayer(0)
assert lyr.GetLayerDefn().GetFieldCount() == 2
f = lyr.GetNextFeature()
assert f is not None
###############################################################################
def test_ogr_mvt_tileset_without_metadata_file():
ds = gdal.OpenEx('data/mvt/point_polygon/1',
open_options=['METADATA_FILE=', 'CLIP=NO'])
lyr = ds.GetLayerByName('point')
assert lyr.GetGeomType() == ogr.wkbMultiPoint
lyr = ds.GetLayerByName('polygon2')
assert lyr.GetGeomType() == ogr.wkbMultiPolygon
assert lyr.GetLayerDefn().GetFieldCount() == 2
###############################################################################
def test_ogr_mvt_tileset_json_field():
ds = gdal.OpenEx('data/mvt/datatypes/0',
open_options=['METADATA_FILE=', 'JSON_FIELD=YES', 'CLIP=NO'])
lyr = ds.GetLayer(0)
assert lyr.GetLayerDefn().GetFieldCount() == 2
f = lyr.GetNextFeature()
d = json.loads(f.GetFieldAsString("json"))
assert (d == {
"bool_true": True,
"bool_false": False,
"pos_int_value": 1,
"pos_int64_value": 123456789012345,
"neg_int_value": -1,
"neg_int64_value": -123456789012345,
"pos_sint_value": 1,
"pos_sint64_value": 123456789012345,
"neg_sint_value": -1,
"neg_sint64_value": -123456789012345,
"uint_value": 2000000000,
"uint64_value": 4000000000,
"float_value": 1.25,
"real_value": 1.23456789,
"string_value": "str"
}), f.GetFieldAsString("json")
###############################################################################
def test_ogr_mvt_open_variants():
expected_geom = 'MULTILINESTRING ((215246.671651058 6281289.23636264,332653.947097085 6447616.20991119))'
ds = ogr.Open('MVT:data/mvt/linestring/0/0/0.pbf')
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f, expected_geom) != 0:
f.DumpReadable()
pytest.fail()
ds = ogr.Open('MVT:data/mvt/linestring/0')
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f, expected_geom) != 0:
f.DumpReadable()
pytest.fail()
ds = ogr.Open('/vsigzip/data/mvt/linestring/0/0/0.pbf')
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f, expected_geom) != 0:
f.DumpReadable()
pytest.fail()
ds = ogr.Open('MVT:/vsigzip/data/mvt/linestring/0/0/0.pbf')
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f, expected_geom) != 0:
f.DumpReadable()
pytest.fail()
###############################################################################
def test_ogr_mvt_xyz_options():
ds = gdal.OpenEx('data/mvt/datatypes/0/0/0.pbf',
open_options=['X=1', 'Y=2', 'Z=3'])
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f, 'POINT (-12496536.8802869 8299226.7830913)') != 0:
f.DumpReadable()
pytest.fail()
###############################################################################
def test_ogr_mvt_test_ogrsf_pbf():
import test_cli_utilities
if test_cli_utilities.get_test_ogrsf_path() is None:
pytest.skip()
ret = gdaltest.runexternal(test_cli_utilities.get_test_ogrsf_path() +
' -ro data/mvt/datatypes/0/0/0.pbf')
assert ret.find('INFO') != -1 and ret.find('ERROR') == -1
###############################################################################
def test_ogr_mvt_test_ogrsf_directory():
import test_cli_utilities
if test_cli_utilities.get_test_ogrsf_path() is None:
pytest.skip()
ret = gdaltest.runexternal(test_cli_utilities.get_test_ogrsf_path() +
' -ro data/mvt/datatypes/0')
assert ret.find('INFO') != -1 and ret.find('ERROR') == -1
###############################################################################
def test_ogr_mvt_mbtiles():
if ogr.GetDriverByName('MBTILES') is None:
pytest.skip()
ds = ogr.Open('data/mvt/point_polygon.mbtiles')
lyr = ds.GetLayerByName('point')
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f, 'MULTIPOINT ((220138.641461308 6276397.26655239))') != 0:
f.DumpReadable()
pytest.fail()
lyr.SetSpatialFilterRect(0, 0, 10000000, 10000000)
lyr.ResetReading()
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f, 'MULTIPOINT ((220138.641461308 6276397.26655239))') != 0:
f.DumpReadable()
pytest.fail()
###############################################################################
def test_ogr_mvt_mbtiles_json_field():
if ogr.GetDriverByName('MBTILES') is None:
pytest.skip()
ds = gdal.OpenEx('data/mvt/datatypes.mbtiles',
open_options=['JSON_FIELD=YES', 'CLIP=NO'])
lyr = ds.GetLayer(0)
assert lyr.GetLayerDefn().GetFieldCount() == 2
f = lyr.GetNextFeature()
d = json.loads(f.GetFieldAsString("json"))
assert (d == {'int64_value': 123456789012345,
'string_value': 'str',
'real_value': 1.23456789,
'bool_false': False,
'pos_int_value': 1,
'neg_int_value': -1,
'bool_true': True,
'float_value': 1.25
}), f.GetFieldAsString("json")
###############################################################################
def test_ogr_mvt_mbtiles_json_field_auto():
if ogr.GetDriverByName('MBTILES') is None:
pytest.skip()
ds = gdal.OpenEx('data/mvt/datatypes_json_field_auto.mbtiles',
open_options=['CLIP=NO'])
lyr = ds.GetLayer(0)
assert lyr.GetLayerDefn().GetFieldCount() == 2
f = lyr.GetNextFeature()
d = json.loads(f.GetFieldAsString("json"))
assert (d == {'int64_value': 123456789012345,
'string_value': 'str',
'real_value': 1.23456789,
'bool_false': False,
'pos_int_value': 1,
'neg_int_value': -1,
'bool_true': True,
'float_value': 1.25
}), f.GetFieldAsString("json")
###############################################################################
def test_ogr_mvt_mbtiles_test_ogrsf():
import test_cli_utilities
if test_cli_utilities.get_test_ogrsf_path() is None:
pytest.skip()
if ogr.GetDriverByName('MBTILES') is None:
pytest.skip()
ret = gdaltest.runexternal(test_cli_utilities.get_test_ogrsf_path() +
' -ro data/mvt/point_polygon.mbtiles polygon2')
assert ret.find('INFO') != -1 and ret.find('ERROR') == -1
###############################################################################
def test_ogr_mvt_mbtiles_open_vector_in_raster_mode():
if ogr.GetDriverByName('MBTILES') is None:
pytest.skip()
ds = gdal.OpenEx('data/mvt/datatypes.mbtiles', gdal.OF_RASTER)
assert ds is None
###############################################################################
def test_ogr_mvt_x_y_z_filename_scheme():
tmpfilename = '/vsimem/0-0-0.pbf'
gdal.FileFromMemBuffer(tmpfilename,
open('data/mvt/linestring/0/0/0.pbf', 'rb').read())
ds = ogr.Open(tmpfilename)
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
if ogrtest.check_feature_geometry(f, 'LINESTRING (215246.671651058 6281289.23636264,332653.947097085 6447616.20991119)') != 0:
f.DumpReadable()
pytest.fail()
ds = None
gdal.Unlink(tmpfilename)
###############################################################################
def test_ogr_mvt_polygon_larger_than_header():
ds = gdal.OpenEx('data/mvt/polygon_larger_than_header.pbf',
open_options=['CLIP=NO'])
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
assert f is not None
###############################################################################
def test_ogr_mvt_errors():
assert ogr.Open('MVT:/i_do_not/exist') is None
# Cannot detect Z in directory name
assert ogr.Open('MVT:data') is None
# Invalid Z
gdal.Mkdir('/vsimem/33', 0)
assert ogr.Open('MVT:/vsimem/33') is None
gdal.Rmdir('/vsimem/33')
# Inexisting metadata
with gdaltest.error_handler():
assert (gdal.OpenEx('data/mvt/linestring/0/0/0.pbf',
open_options=['METADATA_FILE=/i_do_not/exist']) is not None)
# Invalid metadata
with gdaltest.error_handler():
assert (gdal.OpenEx('data/mvt/linestring/0/0/0.pbf',
open_options=['METADATA_FILE=ogr_mvt.py']) is not None)
# Invalid metadata
gdal.FileFromMemBuffer('/vsimem/my.json', '{}')
with gdaltest.error_handler():
assert (gdal.OpenEx('data/mvt/linestring/0/0/0.pbf',
open_options=['METADATA_FILE=/vsimem/my.json']) is not None)
gdal.Unlink('/vsimem/my.json')
# Invalid metadata
gdal.FileFromMemBuffer('/vsimem/my.json', '{ "json": "x y" }')
with gdaltest.error_handler():
assert (gdal.OpenEx('data/mvt/linestring/0/0/0.pbf',
open_options=['METADATA_FILE=/vsimem/my.json']) is not None)
gdal.Unlink('/vsimem/my.json')
# Too big file
tmpfilename = '/vsimem/foo.pbf'
gdal.FileFromMemBuffer(tmpfilename,
open('data/mvt/polygon_larger_than_header.pbf', 'rb').read())
f = gdal.VSIFOpenL(tmpfilename, 'rb+')
gdal.VSIFSeekL(f, 20 * 1024 * 1024, 0)
gdal.VSIFWriteL(' ', 1, 1, f)
gdal.VSIFCloseL(f)
ds = ogr.Open(tmpfilename)
gdal.Unlink(tmpfilename)
assert ds is None
###############################################################################
def test_ogr_mvt_http_start():
gdaltest.webserver_process = None
gdaltest.webserver_port = 0
if not gdaltest.built_against_curl():
pytest.skip()
(gdaltest.webserver_process, gdaltest.webserver_port) = webserver.launch(handler=webserver.DispatcherHttpHandler)
if gdaltest.webserver_port == 0:
pytest.skip()
###############################################################################
def test_ogr_mvt_http():
if gdaltest.webserver_port == 0:
pytest.skip()
handler = webserver.SequentialHandler()
handler.add('GET', '/linestring/metadata.json', 200, {},
open('data/mvt/linestring/metadata.json', 'rb').read())
handler.add('GET', '/linestring/0/0/0.pbf', 200, {},
open('data/mvt/linestring/0/0/0.pbf', 'rb').read())
handler.add('GET', '/linestring/0/0/0.pbf', 200, {},
open('data/mvt/linestring/0/0/0.pbf', 'rb').read())
with webserver.install_http_handler(handler):
ds = ogr.Open('MVT:http://127.0.0.1:%d/linestring/0' % gdaltest.webserver_port)
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
assert f is not None
# No metadata file nor tile
handler = webserver.SequentialHandler()
handler.add('GET', '/linestring/metadata.json', 404, {})
handler.add('GET', '/linestring.json', 404, {})
handler.add('GET', '/linestring/0/0/0.pbf', 404, {})
with webserver.install_http_handler(handler):
with gdaltest.error_handler():
ds = ogr.Open('MVT:http://127.0.0.1:%d/linestring/0' % gdaltest.webserver_port)
assert ds is None
# No metadata file, but tiles
handler = webserver.SequentialHandler()
handler.add('GET', '/linestring/metadata.json', 404, {})
handler.add('GET', '/linestring.json', 404, {})
handler.add('GET', '/linestring/0/0/0.pbf', 200, {},
open('data/mvt/linestring/0/0/0.pbf', 'rb').read())
handler.add('GET', '/linestring/0/0/0.pbf', 200, {},
open('data/mvt/linestring/0/0/0.pbf', 'rb').read())
with webserver.install_http_handler(handler):
ds = ogr.Open('MVT:http://127.0.0.1:%d/linestring/0' % gdaltest.webserver_port)
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
assert f is not None
# metadata.json file, but no tiles
handler = webserver.SequentialHandler()
handler.add('GET', '/linestring/metadata.json', 200, {},
open('data/mvt/linestring/metadata.json', 'rb').read())
handler.add('GET', '/linestring/0/0/0.pbf', 404, {})
handler.add('GET', '/linestring/0/0/0.pbf', 404, {})
with webserver.install_http_handler(handler):
ds = ogr.Open('MVT:http://127.0.0.1:%d/linestring/0' % gdaltest.webserver_port)
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
assert f is None
# No metadata.json file, but a linestring.json and no tiles
handler = webserver.SequentialHandler()
handler.add('GET', '/linestring/metadata.json', 404, {})
handler.add('GET', '/linestring.json', 200, {},
open('data/mvt/linestring/metadata.json', 'rb').read())
handler.add('GET', '/linestring/0/0/0.pbf', 404, {})
handler.add('GET', '/linestring/0/0/0.pbf', 404, {})
with webserver.install_http_handler(handler):
ds = ogr.Open('MVT:http://127.0.0.1:%d/linestring/0' % gdaltest.webserver_port)
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
assert f is None
# Open pbf file
handler = webserver.SequentialHandler()
handler.add('GET', '/linestring/0/0/0.pbf', 200, {},
open('data/mvt/linestring/0/0/0.pbf', 'rb').read())
with webserver.install_http_handler(handler):
ds = ogr.Open('MVT:http://127.0.0.1:%d/linestring/0/0/0.pbf' % gdaltest.webserver_port)
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
assert f is not None
###############################################################################
def test_ogr_mvt_http_stop():
if gdaltest.webserver_port == 0:
pytest.skip()
webserver.server_stop(gdaltest.webserver_process, gdaltest.webserver_port)
###############################################################################
def test_ogr_mvt_write_one_layer():
if not ogrtest.have_geos() or ogr.GetDriverByName('SQLITE') is None:
pytest.skip()
src_ds = gdal.GetDriverByName('Memory').Create('', 0, 0, 0, gdal.GDT_Unknown)
lyr = src_ds.CreateLayer('mylayer')
lyr.CreateField(ogr.FieldDefn('strfield', ogr.OFTString))
lyr.CreateField(ogr.FieldDefn('intfield', ogr.OFTInteger))
lyr.CreateField(ogr.FieldDefn('int64field', ogr.OFTInteger64))
lyr.CreateField(ogr.FieldDefn('realfield', ogr.OFTReal))
lyr.CreateField(ogr.FieldDefn('datefield', ogr.OFTDate))
lyr.CreateField(ogr.FieldDefn('datetimefield', ogr.OFTDateTime))
boolfield = ogr.FieldDefn('boolfield', ogr.OFTInteger)
boolfield.SetSubType(ogr.OFSTBoolean)
lyr.CreateField(boolfield)
# Test empty layer: OK
with gdaltest.error_handler():
out_ds = gdal.VectorTranslate('/vsimem/outmvt', src_ds, format='MVT')
assert out_ds is not None
# Cannot create directory
with gdaltest.error_handler():
out_ds = gdal.VectorTranslate('/i_dont/exist/outmvt', src_ds, format='MVT')
assert out_ds is None
# Directory already exists
with gdaltest.error_handler():
out_ds = gdal.VectorTranslate('/vsimem/outmvt', src_ds, format='MVT')
assert out_ds is None
gdal.RmdirRecursive('/vsimem/outmvt')
f = ogr.Feature(lyr.GetLayerDefn())
f.SetFID(123)
f['strfield'] = 'foo'
f['intfield'] = -1
f['int64field'] = 123456789012345
f['realfield'] = 1.25
f['datefield'] = '2018/02/01'
f['datetimefield'] = '2018/02/01 12:34:56'
f['boolfield'] = True
f.SetGeometry(ogr.CreateGeometryFromWkt('POINT(500000 1000000)'))
lyr.CreateFeature(f)
f = ogr.Feature(lyr.GetLayerDefn())
f.SetFID(124)
f['strfield'] = 'foo'
f['intfield'] = -1
f['int64field'] = 123456789012345
f['realfield'] = 1.25
f['datefield'] = '2018/02/01'
f['datetimefield'] = '2018/02/01 12:34:56'
f['boolfield'] = True
f.SetGeometry(ogr.CreateGeometryFromWkt('LINESTRING(500000 1000000,510000 1010000,520000 1020000)'))
lyr.CreateFeature(f)
f = ogr.Feature(lyr.GetLayerDefn())
f.SetFID(125)
f['strfield'] = 'foobarbazbaw'
f['intfield'] = 1
f['int64field'] = -123456789012345
f['realfield'] = -1.25678
f.SetGeometry(ogr.CreateGeometryFromWkt('POLYGON((500000 1000000,510000 1000000,510000 1010000,500000 1000000))'))
lyr.CreateFeature(f)
f = ogr.Feature(lyr.GetLayerDefn())
f.SetFID(126)
f.SetGeometry(ogr.CreateGeometryFromWkt('MULTIPOINT(500000 1000000,510000 1010000)'))
lyr.CreateFeature(f)
f = ogr.Feature(lyr.GetLayerDefn())
f.SetFID(127)
f.SetGeometry(ogr.CreateGeometryFromWkt('MULTILINESTRING((500000 1000000,510000 1010000),(510000 1010000,510000 1000000))'))
lyr.CreateFeature(f)
f = ogr.Feature(lyr.GetLayerDefn())
f.SetFID(128)
f.SetGeometry(ogr.CreateGeometryFromWkt('MULTIPOLYGON(((500000 1000000,510000 1000000,510000 1010000,500000 1000000)),((-500000 1000000,-510000 1000000,-510000 1010000,-500000 1000000),(-502000 1001000,-509000 1001000,-509000 1008500,-502000 1001000)))'))
lyr.CreateFeature(f)
f = ogr.Feature(lyr.GetLayerDefn())
f.SetFID(129)
f.SetGeometry(ogr.CreateGeometryFromWkt('GEOMETRYCOLLECTION(POINT(500000 1000000),LINESTRING(500000 1000000,510000 1010000,520000 1020000))'))
lyr.CreateFeature(f)
with gdaltest.error_handler():
out_ds = gdal.VectorTranslate('/vsimem/outmvt', src_ds,
options='-f MVT -preserve_fid')
assert out_ds is not None
out_ds = None
out_ds = ogr.Open('/vsimem/outmvt/0')
assert out_ds is not None
out_lyr = out_ds.GetLayerByName('mylayer')
out_f = out_lyr.GetNextFeature()
if out_f['mvt_id'] != 123 or \
out_f['strfield'] != 'foo' or \
out_f['intfield'] != -1 or \
out_f['int64field'] != 123456789012345 or \
out_f['realfield'] != 1.25 or \
out_f['datefield'] != '2018-02-01' or \
out_f['datetimefield'] != '2018-02-01T12:34:56' or \
out_f['boolfield'] is False or \
ogrtest.check_feature_geometry(out_f, 'POINT (498980.920645632 997961.84129126)') != 0:
out_f.DumpReadable()
pytest.fail()
out_f = out_lyr.GetNextFeature()
if out_f['strfield'] != 'foo' or \
out_f['intfield'] != -1 or \
out_f['int64field'] != 123456789012345 or \
out_f['realfield'] != 1.25 or \
out_f['datefield'] != '2018-02-01' or \
out_f['datetimefield'] != '2018-02-01T12:34:56' or \
out_f['boolfield'] is False or \
ogrtest.check_feature_geometry(out_f, 'MULTILINESTRING ((498980.920645632 997961.84129126,508764.860266134 1007745.78091176,518548.799886636 1017529.72053226))') != 0:
out_f.DumpReadable()
pytest.fail()
out_f = out_lyr.GetNextFeature()
if out_f['strfield'] != 'foobarbazbaw' or \
out_f['intfield'] != 1 or \
out_f['int64field'] != -123456789012345 or \
out_f['realfield'] != -1.25678 or \
out_f.IsFieldSet('datefield') or \
out_f.IsFieldSet('datetimefield') or \
out_f.IsFieldSet('boolfield') or \
ogrtest.check_feature_geometry(out_f, 'POLYGON ((498980.920645632 997961.84129126,508764.860266134 1007745.78091176,508764.860266134 997961.84129126,498980.920645632 997961.84129126))') != 0:
out_f.DumpReadable()
pytest.fail()
out_f = out_lyr.GetNextFeature()
if ogrtest.check_feature_geometry(out_f, 'MULTIPOINT ((498980.920645632 997961.84129126),(508764.860266134 1007745.78091176))') != 0:
out_f.DumpReadable()
pytest.fail()
out_f = out_lyr.GetNextFeature()
if ogrtest.check_feature_geometry(out_f, 'MULTILINESTRING ((498980.920645632 997961.84129126,508764.860266134 1007745.78091176),(508764.860266134 1007745.78091176,508764.860266134 997961.84129126))') != 0:
out_f.DumpReadable()
pytest.fail()
out_f = out_lyr.GetNextFeature()
if ogrtest.check_feature_geometry(out_f, 'MULTIPOLYGON (((498980.920645632 997961.84129126,508764.860266134 1007745.78091176,508764.860266134 997961.84129126,498980.920645632 997961.84129126)),((-498980.920645632 997961.84129126,-508764.860266134 997961.84129126,-508764.860266134 1007745.78091176,-498980.920645632 997961.84129126)))') != 0:
out_f.DumpReadable()
pytest.fail()
out_f = out_lyr.GetNextFeature()
if (out_f.GetGeometryRef().GetGeometryType() == ogr.wkbPoint and ogrtest.check_feature_geometry(out_f, 'POINT (498980.920645632 997961.84129126)') != 0) or \
(out_f.GetGeometryRef().GetGeometryType() == ogr.wkbMultiLineString and ogrtest.check_feature_geometry(out_f, 'MULTILINESTRING ((498980.920645632 997961.84129126,508764.860266134 1007745.78091176,518548.799886636 1017529.72053226))') != 0) or \
out_f.GetGeometryRef().GetGeometryType() not in (ogr.wkbPoint, ogr.wkbMultiLineString):
out_f.DumpReadable()
pytest.fail()
out_f = out_lyr.GetNextFeature()
if (out_f.GetGeometryRef().GetGeometryType() == ogr.wkbPoint and ogrtest.check_feature_geometry(out_f, 'POINT (498980.920645632 997961.84129126)') != 0) or \
(out_f.GetGeometryRef().GetGeometryType() == ogr.wkbMultiLineString and ogrtest.check_feature_geometry(out_f, 'MULTILINESTRING ((498980.920645632 997961.84129126,508764.860266134 1007745.78091176,518548.799886636 1017529.72053226))') != 0) or \
out_f.GetGeometryRef().GetGeometryType() not in (ogr.wkbPoint, ogr.wkbMultiLineString):
out_f.DumpReadable()
pytest.fail()
out_ds = ogr.Open('/vsimem/outmvt/5')
assert out_ds is not None
out_lyr = out_ds.GetLayerByName('mylayer')
assert out_lyr.GetFeatureCount() == 9
f = gdal.VSIFOpenL('/vsimem/outmvt/metadata.json', 'rb')
assert f is not None
data = gdal.VSIFReadL(1, 100000, f).decode('ASCII')
gdal.VSIFCloseL(f)
data_json = json.loads(data)
expected_json = {
"name": "outmvt",
"description": "",
"version": 2,
"minzoom": 0,
"maxzoom": 5,
"center": "0.0449158,9.0352907,0",
"bounds": "-4.5814079,8.9465739,4.6712395,9.1240075",
"type": "overlay",
"format": "pbf",
}
for k in expected_json:
assert k in data_json and data_json[k] == expected_json[k], data
json_json = json.loads(data_json['json'])
expected_json_json = {
"vector_layers": [
{
"id": "mylayer",
"description": "",
"minzoom": 0,
"maxzoom": 5,
"fields": {
"strfield": "String",
"intfield": "Number",
"int64field": "Number",
"realfield": "Number",
"datefield": "String",
"datetimefield": "String",
"boolfield": "Boolean"
}
}
],
"tilestats": {
"layerCount": 1,
"layers": [
{
"layer": "mylayer",
"count": 7,
"geometry": "LineString",
"attributeCount": 7,
"attributes": [
{
"attribute": "strfield",
"count": 2,
"type": "string",
"values": [
"foo",
"foobarbazbaw"
]
},
{
"attribute": "intfield",
"count": 2,
"type": "numeric",
"values": [
-1,
1
],
"min":-1,
"max":1
},
{
"attribute": "int64field",
"count": 2,
"type": "numeric",
"values": [
-123456789012345,
123456789012345
],
"min":-123456789012345,
"max":123456789012345
},
{
"attribute": "realfield",
"count": 2,
"type": "numeric",
"values": [
-1.256780,
1.250000
],
"min":-1.25678,
"max":1.25
},
{
"attribute": "datefield",
"count": 1,
"type": "string",
"values": [
"2018-02-01"