-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
test_gdalwarp_lib.py
executable file
·3130 lines (2412 loc) · 100 KB
/
test_gdalwarp_lib.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
# -*- coding: utf-8 -*-
###############################################################################
# $Id$
#
# Project: GDAL/OGR Test Suite
# Purpose: test librarified gdalwarp
# Author: Faza Mahamood <fazamhd @ gmail dot com>
#
###############################################################################
# Copyright (c) 2015, Faza Mahamood <fazamhd at gmail dot com>
# Copyright (c) 2015, Even Rouault <even.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.
###############################################################################
import os
import shutil
import struct
import gdaltest
import ogrtest
import pytest
from osgeo import gdal, ogr, osr
###############################################################################
# Simple test
def test_gdalwarp_lib_1():
ds1 = gdal.Open("../gcore/data/byte.tif")
dstDS = gdal.Warp("tmp/testgdalwarp1.tif", ds1)
assert dstDS.GetRasterBand(1).Checksum() == 4672, "Bad checksum"
dstDS = None
###############################################################################
# Test -of option
def test_gdalwarp_lib_2():
ds1 = gdal.Open("../gcore/data/byte.tif")
dstDS = gdal.Warp(
"tmp/testgdalwarp2.tif".encode("ascii").decode("ascii"), [ds1], format="GTiff"
)
assert dstDS.GetRasterBand(1).Checksum() == 4672, "Bad checksum"
dstDS = None
###############################################################################
# Test -ot option
def test_gdalwarp_lib_3():
ds1 = gdal.Open("../gcore/data/byte.tif")
dstDS = gdal.Warp("", ds1, format="MEM", outputType=gdal.GDT_Int16)
assert dstDS.GetRasterBand(1).DataType == gdal.GDT_Int16, "Bad data type"
assert dstDS.GetRasterBand(1).Checksum() == 4672, "Bad checksum"
dstDS = None
###############################################################################
# Test -t_srs option
def test_gdalwarp_lib_4():
ds1 = gdal.Open("../gcore/data/byte.tif")
dstDS = gdal.Warp("", ds1, format="MEM", dstSRS="EPSG:32611")
assert dstDS.GetRasterBand(1).Checksum() == 4672, "Bad checksum"
dstDS = None
###############################################################################
# Test warping from GCPs without any explicit option
def test_gdalwarp_lib_5():
ds = gdal.Open("../gcore/data/byte.tif")
gcpList = [
gdal.GCP(440720.000, 3751320.000, 0, 0, 0),
gdal.GCP(441920.000, 3751320.000, 0, 20, 0),
gdal.GCP(441920.000, 3750120.000, 0, 20, 20),
gdal.GCP(440720.000, 3750120.000, 0, 0, 20),
]
ds1 = gdal.Translate(
"tmp/testgdalwarp_gcp.tif", ds, outputSRS="EPSG:26711", GCPs=gcpList
)
dstDS = gdal.Warp("", ds1, format="MEM", tps=True)
assert dstDS.GetRasterBand(1).Checksum() == 4672, "Bad checksum"
assert gdaltest.geotransform_equals(
ds.GetGeoTransform(), dstDS.GetGeoTransform(), 1e-9
), "Bad geotransform"
dstDS = None
###############################################################################
# Test warping from GCPs with -tps
def test_gdalwarp_lib_6():
ds1 = gdal.Open("tmp/testgdalwarp_gcp.tif")
dstDS = gdal.Warp("", ds1, format="MEM", tps=True)
assert dstDS.GetRasterBand(1).Checksum() == 4672, "Bad checksum"
assert gdaltest.geotransform_equals(
gdal.Open("../gcore/data/byte.tif").GetGeoTransform(),
dstDS.GetGeoTransform(),
1e-9,
), "Bad geotransform"
dstDS = None
###############################################################################
# Test -tr
def test_gdalwarp_lib_7():
ds1 = gdal.Open("tmp/testgdalwarp_gcp.tif")
dstDS = gdal.Warp("", [ds1], format="MEM", xRes=120, yRes=120)
assert dstDS is not None
expected_gt = (440720.0, 120.0, 0.0, 3751320.0, 0.0, -120.0)
assert gdaltest.geotransform_equals(
expected_gt, dstDS.GetGeoTransform(), 1e-9
), "Bad geotransform"
dstDS = None
###############################################################################
# Test -ts
def test_gdalwarp_lib_8():
ds1 = gdal.Open("tmp/testgdalwarp_gcp.tif")
dstDS = gdal.Warp("", [ds1], format="MEM", width=10, height=10)
assert dstDS is not None
expected_gt = (440720.0, 120.0, 0.0, 3751320.0, 0.0, -120.0)
assert gdaltest.geotransform_equals(
expected_gt, dstDS.GetGeoTransform(), 1e-9
), "Bad geotransform"
dstDS = None
###############################################################################
# Test -te
def test_gdalwarp_lib_9():
ds = gdal.Warp(
"",
"../gcore/data/byte.tif",
format="MEM",
outputBounds=[440720.000, 3750120.000, 441920.000, 3751320.000],
)
assert gdaltest.geotransform_equals(
gdal.Open("../gcore/data/byte.tif").GetGeoTransform(),
ds.GetGeoTransform(),
1e-9,
), "Bad geotransform"
ds = None
###############################################################################
# Test -rn
def test_gdalwarp_lib_10():
ds = gdal.Warp(
"",
"../gcore/data/byte.tif",
format="MEM",
width=40,
height=40,
resampleAlg=gdal.GRA_NearestNeighbour,
)
assert ds.GetRasterBand(1).Checksum() == 18784, "Bad checksum"
ds = None
###############################################################################
# Test -rb
def test_gdalwarp_lib_11():
ds = gdal.Warp(
"",
"../gcore/data/byte.tif",
format="MEM",
width=40,
height=40,
resampleAlg=gdal.GRA_Bilinear,
)
ref_ds = gdal.Open("ref_data/testgdalwarp11.tif")
maxdiff = gdaltest.compare_ds(ds, ref_ds, verbose=0)
ref_ds = None
if maxdiff > 1:
gdaltest.compare_ds(ds, ref_ds, verbose=1)
pytest.fail("Image too different from reference")
ds = None
###############################################################################
# Test -rc
def test_gdalwarp_lib_12():
ds = gdal.Warp(
"",
"../gcore/data/byte.tif",
format="MEM",
width=40,
height=40,
resampleAlg=gdal.GRA_Cubic,
)
ref_ds = gdal.Open("ref_data/testgdalwarp12.tif")
maxdiff = gdaltest.compare_ds(ds, ref_ds, verbose=0)
ref_ds = None
if maxdiff > 1:
gdaltest.compare_ds(ds, ref_ds, verbose=1)
pytest.fail("Image too different from reference")
ds = None
###############################################################################
# Test -rcs
def test_gdalwarp_lib_13():
ds = gdal.Warp(
"",
"../gcore/data/byte.tif",
format="MEM",
width=40,
height=40,
resampleAlg=gdal.GRA_CubicSpline,
)
ref_ds = gdal.Open("ref_data/testgdalwarp13.tif")
maxdiff = gdaltest.compare_ds(ds, ref_ds, verbose=0)
ref_ds = None
if maxdiff > 1:
gdaltest.compare_ds(ds, ref_ds, verbose=1)
pytest.fail("Image too different from reference")
ds = None
###############################################################################
# Test -r lanczos
def test_gdalwarp_lib_14():
ds = gdal.Warp(
"",
"../gcore/data/byte.tif",
format="MEM",
width=40,
height=40,
resampleAlg=gdal.GRA_Lanczos,
)
ref_ds = gdal.Open("ref_data/testgdalwarp14.tif")
maxdiff = gdaltest.compare_ds(ds, ref_ds, verbose=0)
ref_ds = None
if maxdiff > 1:
gdaltest.compare_ds(ds, ref_ds, verbose=1)
pytest.fail("Image too different from reference")
ds = None
###############################################################################
# Test parsing all resampling methods
@pytest.mark.parametrize(
"resampleAlg,resampleAlgStr",
[
(gdal.GRA_NearestNeighbour, "near"),
(gdal.GRA_Cubic, "cubic"),
(gdal.GRA_CubicSpline, "cubicspline"),
(gdal.GRA_Lanczos, "lanczos"),
(gdal.GRA_Average, "average"),
(gdal.GRA_RMS, "rms"),
(gdal.GRA_Mode, "mode"),
(gdal.GRA_Max, "max"),
(gdal.GRA_Min, "min"),
(gdal.GRA_Med, "med"),
(gdal.GRA_Q1, "q1"),
(gdal.GRA_Q3, "q3"),
(gdal.GRA_Sum, "sum"),
],
)
def test_gdalwarp_lib_resampling_methods(resampleAlg, resampleAlgStr):
option_list = gdal.WarpOptions(
resampleAlg=resampleAlg, options="__RETURN_OPTION_LIST__"
)
assert option_list == ["-r", resampleAlgStr]
assert (
gdal.Warp(
"",
"../gcore/data/byte.tif",
format="MEM",
width=2,
height=2,
resampleAlg=resampleAlg,
)
is not None
)
###############################################################################
# Test -dstnodata
def test_gdalwarp_lib_15():
ds = gdal.Warp(
"", "tmp/testgdalwarp_gcp.tif", format="MEM", dstSRS="EPSG:32610", dstNodata=1
)
assert ds.GetRasterBand(1).GetNoDataValue() == 1, "Bad nodata value"
assert ds.GetRasterBand(1).Checksum() in (4523, 4547) # 4547 with HPGN grids
ds = None
###############################################################################
# Test -of VRT which is a special case
def test_gdalwarp_lib_16():
ds = gdal.Warp(
"/vsimem/test_gdalwarp_lib_16.vrt", "tmp/testgdalwarp_gcp.tif", format="VRT"
)
assert ds is not None
assert ds.GetRasterBand(1).Checksum() == 4672, "Bad checksum"
ds = None
gdal.Unlink("/vsimem/test_gdalwarp_lib_16.vrt")
# Cannot write file
with gdaltest.error_handler():
ds = gdal.Warp(
"/i_dont/exist/test_gdalwarp_lib_16.vrt",
"tmp/testgdalwarp_gcp.tif",
format="VRT",
)
assert ds is None
###############################################################################
# Test -dstalpha
def test_gdalwarp_lib_17():
ds = gdal.Warp("", "../gcore/data/rgbsmall.tif", format="MEM", dstAlpha=True)
assert ds is not None
assert ds.GetRasterBand(4) is not None, "No alpha band generated"
ds = None
###############################################################################
# Test -et 0 which is a special case
def test_gdalwarp_lib_19():
ds = gdal.Warp("", "tmp/testgdalwarp_gcp.tif", format="MEM", errorThreshold=0)
assert ds is not None
assert ds.GetRasterBand(1).Checksum() == 4672, "Bad checksum"
ds = None
###############################################################################
# Test cutline from OGR datasource.
def test_gdalwarp_lib_21():
ds = gdal.Warp(
"",
"../gcore/data/utmsmall.tif",
format="MEM",
cutlineDSName="data/cutline.vrt",
cutlineLayer="cutline",
)
assert ds is not None
assert ds.GetRasterBand(1).Checksum() == 19139, "Bad checksum"
ds = None
###############################################################################
# Test cutline whose extent is larger than the source data
@pytest.mark.parametrize(
"options", [{}, {"GDALWARP_SKIP_CUTLINE_CONTAINMENT_TEST": "YES"}]
)
def test_gdalwarp_lib_cutline_larger_source_dataset(options):
srs = osr.SpatialReference()
srs.ImportFromEPSG(26711)
cutline_ds = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource(
"/vsimem/cutline.shp"
)
cutline_lyr = cutline_ds.CreateLayer("cutline", srs=srs)
f = ogr.Feature(cutline_lyr.GetLayerDefn())
f.SetGeometry(
ogr.CreateGeometryFromWkt(
"POLYGON((400000 3000000,400000 4000000,500000 4000000,500000 3000000,400000 3000000))"
)
)
cutline_lyr.CreateFeature(f)
cutline_ds = None
with gdaltest.config_options(options):
ds = gdal.Warp(
"",
"../gcore/data/byte.tif",
format="MEM",
cutlineDSName="/vsimem/cutline.shp",
cutlineLayer="cutline",
)
assert ds is not None
assert ds.GetRasterBand(1).Checksum() == 4672
ds = None
ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("/vsimem/cutline.shp")
###############################################################################
# Test cutline with ALL_TOUCHED enabled.
def test_gdalwarp_lib_23():
ds = gdal.Warp(
"",
"../gcore/data/utmsmall.tif",
format="MEM",
warpOptions=["CUTLINE_ALL_TOUCHED=TRUE"],
cutlineDSName="data/cutline.vrt",
cutlineLayer="cutline",
)
assert ds is not None
assert ds.GetRasterBand(1).Checksum() == 20123, "Bad checksum"
ds = None
###############################################################################
# Test -tap
def test_gdalwarp_lib_32():
ds = gdal.Warp(
"",
"../gcore/data/byte.tif",
format="MEM",
targetAlignedPixels=True,
xRes=100,
yRes=50,
)
assert ds is not None
expected_gt = (440700.0, 100.0, 0.0, 3751350.0, 0.0, -50.0)
got_gt = ds.GetGeoTransform()
assert gdaltest.geotransform_equals(expected_gt, got_gt, 1e-9), "Bad geotransform"
assert (
ds.RasterXSize == 13 and ds.RasterYSize == 25
), "Wrong raster dimensions : %d x %d" % (ds.RasterXSize, ds.RasterYSize)
ds = None
###############################################################################
# Test warping multiple sources
def test_gdalwarp_lib_34():
srcds1 = gdal.Translate(
"", "../gcore/data/byte.tif", format="MEM", srcWin=[0, 0, 10, 20]
)
srcds2 = gdal.Translate(
"", "../gcore/data/byte.tif", format="MEM", srcWin=[10, 0, 10, 20]
)
ds = gdal.Warp("", [srcds1, srcds2], format="MEM")
cs = ds.GetRasterBand(1).Checksum()
gt = ds.GetGeoTransform()
xsize = ds.RasterXSize
ysize = ds.RasterYSize
ds = None
assert xsize == 20 and ysize == 20, "bad dimensions"
assert cs == 4672, "bad checksum"
expected_gt = (440720.0, 60.0, 0.0, 3751320.0, 0.0, -60.0)
for i in range(6):
assert gt[i] == pytest.approx(expected_gt[i], abs=1e-5), "bad gt"
###############################################################################
# Test -te_srs
def test_gdalwarp_lib_45():
ds = gdal.Warp(
"",
["../gcore/data/byte.tif"],
format="MEM",
outputBounds=[
-117.641087629972,
33.8915301685897,
-117.628190189534,
33.9024195619201,
],
outputBoundsSRS="EPSG:4267",
)
assert ds.GetRasterBand(1).Checksum() == 4672
ds = None
###############################################################################
# Test -crop_to_cutline
def test_gdalwarp_lib_46():
ds = gdal.Warp(
"",
["../gcore/data/utmsmall.tif"],
format="MEM",
cutlineDSName="data/cutline.vrt",
cropToCutline=True,
)
assert ds.GetRasterBand(1).Checksum() == 18837, "Bad checksum"
ds = None
# Precisely test output raster bounds in no raster reprojection ccase
src_ds = gdal.Translate(
"",
"../gcore/data/byte.tif",
format="MEM",
outputBounds=[2, 49, 3, 48],
outputSRS="EPSG:4326",
)
cutlineDSName = "/vsimem/test_gdalwarp_lib_46.json"
cutline_ds = ogr.GetDriverByName("GeoJSON").CreateDataSource(cutlineDSName)
cutline_lyr = cutline_ds.CreateLayer("cutline")
f = ogr.Feature(cutline_lyr.GetLayerDefn())
f.SetGeometry(
ogr.CreateGeometryFromWkt(
"POLYGON((2.13 48.13,2.83 48.13,2.83 48.83,2.13 48.83,2.13 48.13))"
)
)
cutline_lyr.CreateFeature(f)
f = None
cutline_lyr = None
cutline_ds = None
# No CUTLINE_ALL_TOUCHED: the extent should be smaller than the cutline
ds = gdal.Warp(
"", src_ds, format="MEM", cutlineDSName=cutlineDSName, cropToCutline=True
)
got_gt = ds.GetGeoTransform()
expected_gt = (2.15, 0.05, 0.0, 48.8, 0.0, -0.05)
assert max([abs(got_gt[i] - expected_gt[i]) for i in range(6)]) <= 1e-8
assert ds.RasterXSize == 13 and ds.RasterYSize == 13
# Same but with CUTLINE_ALL_TOUCHED=YES: the extent should be larger
# than the cutline
ds = gdal.Warp(
"",
src_ds,
format="MEM",
cutlineDSName=cutlineDSName,
cropToCutline=True,
warpOptions=["CUTLINE_ALL_TOUCHED=YES"],
)
got_gt = ds.GetGeoTransform()
expected_gt = (2.1, 0.05, 0.0, 48.85, 0.0, -0.05)
assert max([abs(got_gt[i] - expected_gt[i]) for i in range(6)]) <= 1e-8
assert ds.RasterXSize == 15 and ds.RasterYSize == 15
# Test numeric stability when the cutline is exactly on pixel boundaries
cutline_ds = ogr.GetDriverByName("GeoJSON").CreateDataSource(cutlineDSName)
cutline_lyr = cutline_ds.CreateLayer("cutline")
f = ogr.Feature(cutline_lyr.GetLayerDefn())
f.SetGeometry(
ogr.CreateGeometryFromWkt(
"POLYGON((2.15 48.15,2.85 48.15,2.85 48.85,2.15 48.85,2.15 48.15))"
)
)
cutline_lyr.CreateFeature(f)
f = None
cutline_lyr = None
cutline_ds = None
for warpOptions in [[], ["CUTLINE_ALL_TOUCHED=YES"]]:
ds = gdal.Warp(
"",
src_ds,
format="MEM",
cutlineDSName=cutlineDSName,
cropToCutline=True,
warpOptions=warpOptions,
)
got_gt = ds.GetGeoTransform()
expected_gt = (2.15, 0.05, 0.0, 48.85, 0.0, -0.05)
assert max([abs(got_gt[i] - expected_gt[i]) for i in range(6)]) <= 1e-8
assert ds.RasterXSize == 14 and ds.RasterYSize == 14
gdal.Unlink(cutlineDSName)
###############################################################################
# Test -crop_to_cutline -tr X Y -wo CUTLINE_ALL_TOUCHED=YES (fixes for #1360)
def test_gdalwarp_lib_cutline_all_touched_single_pixel():
cutlineDSName = "/vsimem/test_gdalwarp_lib_cutline_all_touched_single_pixel.json"
cutline_ds = ogr.GetDriverByName("GeoJSON").CreateDataSource(cutlineDSName)
cutline_lyr = cutline_ds.CreateLayer("cutline")
f = ogr.Feature(cutline_lyr.GetLayerDefn())
f.SetGeometry(
ogr.CreateGeometryFromWkt(
"POLYGON((2.15 48.15,2.15000001 48.15000001,2.15 48.15000001,2.15 48.15))"
)
)
cutline_lyr.CreateFeature(f)
f = None
cutline_lyr = None
cutline_ds = None
src_ds = gdal.Translate(
"",
"../gcore/data/byte.tif",
format="MEM",
outputBounds=[2, 49, 3, 48],
outputSRS="EPSG:4326",
)
ds = gdal.Warp(
"",
src_ds,
format="MEM",
cutlineDSName=cutlineDSName,
cropToCutline=True,
warpOptions=["CUTLINE_ALL_TOUCHED=YES"],
xRes=0.001,
yRes=0.001,
)
got_gt = ds.GetGeoTransform()
expected_gt = (2.15, 0.001, 0.0, 48.151, 0.0, -0.001)
assert max([abs(got_gt[i] - expected_gt[i]) for i in range(6)]) <= 1e-8, got_gt
assert ds.RasterXSize == 1 and ds.RasterYSize == 1
gdal.Unlink(cutlineDSName)
###############################################################################
# Test callback
def mycallback(pct, msg, user_data):
# pylint: disable=unused-argument
user_data[0] = pct
return 1
def test_gdalwarp_lib_100():
tab = [0]
ds = gdal.Warp(
"",
"../gcore/data/byte.tif",
format="MEM",
callback=mycallback,
callback_data=tab,
)
assert ds is not None
assert ds.GetRasterBand(1).Checksum() == 4672, "Bad checksum"
assert tab[0] == 1.0, "Bad percentage"
ds = None
###############################################################################
# Test with color table
def test_gdalwarp_lib_101():
ds = gdal.Warp("", "../gdrivers/data/small_world_pct.tif", format="MEM")
assert ds.GetRasterBand(1).GetColorTable() is not None, "Did not get color table"
###############################################################################
# Test with a dataset with no bands
def test_gdalwarp_lib_102():
no_band_ds = gdal.GetDriverByName("MEM").Create("no band", 1, 1, 0)
with gdaltest.error_handler():
ds = gdal.Warp(
"", ["../gdrivers/data/small_world_pct.tif", no_band_ds], format="MEM"
)
assert ds is None, "Did not expected dataset"
###############################################################################
# Test failed transformer
def test_gdalwarp_lib_103():
with gdaltest.error_handler():
ds = gdal.Warp(
"",
[
"../gdrivers/data/small_world_pct.tif",
"../gcore/data/stefan_full_rgba.tif",
],
format="MEM",
)
assert ds is None, "Did not expected dataset"
###############################################################################
# Test no usable source image
def test_gdalwarp_lib_104():
with gdaltest.error_handler():
ds = gdal.Warp("", [], format="MEM")
assert ds is None, "Did not expected dataset"
###############################################################################
# Test failure in GDALSuggestedWarpOutput2
def test_gdalwarp_lib_105():
# with proj 4.9.3 this will success. We limit the width and height
# otherwise a very big raster will be created with 4.9.3 which may cause
# hangups in Travis MacOSX
with gdaltest.error_handler():
gdal.Warp(
"",
["../gdrivers/data/small_world_pct.tif", "../gcore/data/byte.tif"],
format="MEM",
dstSRS="EPSG:32645",
width=100,
height=100,
)
###############################################################################
# Test failure in creation
def test_gdalwarp_lib_106():
with gdaltest.error_handler():
ds = gdal.Warp(
"/not_existing_dir/not_existing_file",
["../gdrivers/data/small_world_pct.tif", "../gcore/data/byte.tif"],
)
assert ds is None, "Did not expected dataset"
###############################################################################
# Test forced width only
def test_gdalwarp_lib_107():
ds = gdal.Warp("", "../gcore/data/byte.tif", format="MEM", width=20)
assert ds.GetRasterBand(1).Checksum() == 4672, "Bad checksum"
###############################################################################
# Test forced height only
def test_gdalwarp_lib_108():
ds = gdal.Warp("", "../gcore/data/byte.tif", format="MEM", height=20)
assert ds.GetRasterBand(1).Checksum() == 4672, "Bad checksum"
###############################################################################
# Test wrong cutline name
def test_gdalwarp_lib_109():
with gdaltest.error_handler():
ds = gdal.Warp(
"", "../gcore/data/byte.tif", format="MEM", cutlineDSName="/does/not/exist"
)
assert ds is None, "Did not expected dataset"
###############################################################################
# Test wrong cutline layer name
def test_gdalwarp_lib_110():
with gdaltest.error_handler():
ds = gdal.Warp(
"",
"../gcore/data/byte.tif",
format="MEM",
cutlineDSName="data/cutline.vrt",
cutlineLayer="wrong_name",
)
assert ds is None, "Did not expected dataset"
###############################################################################
# Test cutline SQL
def test_gdalwarp_lib_111():
ds = gdal.Warp(
"",
"../gcore/data/utmsmall.tif",
format="MEM",
cutlineDSName="data/cutline.vrt",
cutlineSQL="SELECT * FROM cutline",
cutlineWhere="1 = 1",
)
assert ds is not None
assert ds.GetRasterBand(1).Checksum() == 19139, "Bad checksum"
ds = None
###############################################################################
# Test cutline without geometry
def test_gdalwarp_lib_112():
ds = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource("/vsimem/cutline.shp")
lyr = ds.CreateLayer("cutline")
f = ogr.Feature(lyr.GetLayerDefn())
lyr.CreateFeature(f)
f = None
ds = None
with gdaltest.error_handler():
ds = gdal.Warp(
"",
"../gcore/data/utmsmall.tif",
format="MEM",
cutlineDSName="/vsimem/cutline.shp",
cutlineSQL="SELECT * FROM cutline",
)
assert ds is None, "Did not expected dataset"
ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("/vsimem/cutline.shp")
###############################################################################
# Test cutline with non polygon geometry
def test_gdalwarp_lib_113():
ds = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource("/vsimem/cutline.shp")
lyr = ds.CreateLayer("cutline")
f = ogr.Feature(lyr.GetLayerDefn())
f.SetGeometry(ogr.CreateGeometryFromWkt("POINT(0 0)"))
lyr.CreateFeature(f)
f = None
ds = None
with gdaltest.error_handler():
ds = gdal.Warp(
"",
"../gcore/data/utmsmall.tif",
format="MEM",
cutlineDSName="/vsimem/cutline.shp",
)
assert ds is None, "Did not expected dataset"
ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("/vsimem/cutline.shp")
###############################################################################
# Test cutline without feature
def test_gdalwarp_lib_114():
ds = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource("/vsimem/cutline.shp")
ds.CreateLayer("cutline")
ds = None
with gdaltest.error_handler():
ds = gdal.Warp(
"",
"../gcore/data/utmsmall.tif",
format="MEM",
cutlineDSName="/vsimem/cutline.shp",
)
assert ds is None, "Did not expected dataset"
ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("/vsimem/cutline.shp")