-
Notifications
You must be signed in to change notification settings - Fork 103
/
test_reader.py
861 lines (756 loc) · 26.5 KB
/
test_reader.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
"""tests rio_tiler.reader"""
import os
import numpy
import pytest
import rasterio
from numpy.testing import assert_array_almost_equal
from rasterio.warp import transform_bounds
from rio_tiler import constants, reader
from rio_tiler.constants import WGS84_CRS
from rio_tiler.errors import PointOutsideBounds, TileOutsideBounds
S3_KEY = "hro_sources/colorado/201404_13SED190110_201404_0x1500m_CL_1.tif"
S3_KEY_ALPHA = "hro_sources/colorado/201404_13SED190110_201404_0x1500m_CL_1_alpha.tif"
S3_KEY_NODATA = "hro_sources/colorado/201404_13SED190110_201404_0x1500m_CL_1_nodata.tif"
S3_KEY_MASK = "hro_sources/colorado/201404_13SED190110_201404_0x1500m_CL_1_mask.tif"
S3_KEY_EXTMASK = (
"hro_sources/colorado/201404_13SED190110_201404_0x1500m_CL_1_extmask.tif"
)
S3_LOCAL = PREFIX = os.path.join(os.path.dirname(__file__), "fixtures", "my-bucket")
S3_PATH = os.path.join(S3_LOCAL, S3_KEY)
S3_ALPHA_PATH = os.path.join(S3_LOCAL, S3_KEY_ALPHA)
S3_NODATA_PATH = os.path.join(S3_LOCAL, S3_KEY_NODATA)
S3_MASK_PATH = os.path.join(S3_LOCAL, S3_KEY_MASK)
S3_EXTMASK_PATH = os.path.join(S3_LOCAL, S3_KEY_EXTMASK)
KEY_PIX4D = "pix4d/pix4d_alpha_nodata.tif"
PIX4D_PATH = os.path.join(S3_LOCAL, KEY_PIX4D)
COG = os.path.join(os.path.dirname(__file__), "fixtures", "cog.tif")
COG_SCALE = os.path.join(os.path.dirname(__file__), "fixtures", "cog_scale.tif")
COG_CMAP = os.path.join(os.path.dirname(__file__), "fixtures", "cog_cmap.tif")
COG_NODATA = os.path.join(os.path.dirname(__file__), "fixtures", "cog_nodata.tif")
COG_NODATA_FLOAT_NAN = os.path.join(
os.path.dirname(__file__), "fixtures", "cog_nodata_float_nan.tif"
)
@pytest.fixture(autouse=True)
def testing_env_var(monkeypatch):
"""Set fake env to make sure we don't hit AWS services."""
monkeypatch.setenv("AWS_ACCESS_KEY_ID", "jqt")
monkeypatch.setenv("AWS_SECRET_ACCESS_KEY", "rde")
monkeypatch.delenv("AWS_PROFILE", raising=False)
monkeypatch.setenv("AWS_CONFIG_FILE", "/tmp/noconfigheere")
monkeypatch.setenv("AWS_SHARED_CREDENTIALS_FILE", "/tmp/noconfighereeither")
monkeypatch.setenv("GDAL_DISABLE_READDIR_ON_OPEN", "EMPTY_DIR")
def test_tile_read_valid():
"""Should work as expected (read landsat band)."""
# Tile 7-43-24 - Full tile
bounds = [
-6574807.42497772,
12210356.646387195,
-6261721.357121638,
12523442.714243278,
]
with rasterio.open(COG) as src_dst:
arr, mask = reader.part(
src_dst, bounds, 16, 16, dst_crs=constants.WEB_MERCATOR_CRS
)
assert arr.shape == (1, 16, 16)
assert mask.shape == (16, 16)
# Read bounds at full resolution
with rasterio.open(COG) as src_dst:
arr, mask = reader.part(src_dst, bounds, dst_crs=constants.WEB_MERCATOR_CRS)
assert arr.shape == (1, 893, 893)
assert mask.shape == (893, 893)
# set max_size for the returned array
with rasterio.open(COG) as src_dst:
arr, mask = reader.part(
src_dst, bounds, max_size=50, dst_crs=constants.WEB_MERCATOR_CRS
)
assert arr.shape == (1, 50, 50)
assert mask.shape == (50, 50)
# If max_size is bigger than actual size, there is no effect
with rasterio.open(COG) as src_dst:
arr, mask = reader.part(
src_dst, bounds, max_size=1000, dst_crs=constants.WEB_MERCATOR_CRS
)
assert arr.shape == (1, 893, 893)
assert mask.shape == (893, 893)
# Incompatible max_size with height and width
with pytest.warns(UserWarning):
with rasterio.open(COG) as src_dst:
arr, mask = reader.part(
src_dst,
bounds,
max_size=50,
width=25,
height=25,
dst_crs=constants.WEB_MERCATOR_CRS,
)
assert arr.shape == (1, 25, 25)
assert mask.shape == (25, 25)
def test_resampling_returns_different_results():
"""Make sure resampling works."""
bounds = [
-6574807.42497772,
12210356.646387195,
-6261721.357121638,
12523442.714243278,
]
with rasterio.open(COG) as src_dst:
arr, _ = reader.part(
src_dst, bounds, 16, 16, dst_crs=constants.WEB_MERCATOR_CRS
)
arr2, _ = reader.part(
src_dst,
bounds,
16,
16,
dst_crs=constants.WEB_MERCATOR_CRS,
resampling_method="bilinear",
)
assert not numpy.array_equal(arr, arr2)
arr, _ = reader.part(
src_dst, bounds, 16, 16, dst_crs=constants.WEB_MERCATOR_CRS
)
arr2, _ = reader.part(
src_dst,
bounds,
16,
16,
dst_crs=constants.WEB_MERCATOR_CRS,
reproject_method="bilinear",
)
assert not numpy.array_equal(arr, arr2)
def test_resampling_with_diff_padding_returns_different_results():
"""Test result is different with different padding."""
bounds = [
-6574807.42497772,
12210356.646387195,
-6261721.357121638,
12523442.714243278,
]
with rasterio.open(COG) as src_dst:
arr, _ = reader.part(
src_dst,
bounds,
32,
32,
nodata=0,
dst_crs=constants.WEB_MERCATOR_CRS,
resampling_method="bilinear",
)
arr2, _ = reader.part(
src_dst,
bounds,
32,
32,
nodata=0,
padding=10,
dst_crs=constants.WEB_MERCATOR_CRS,
resampling_method="bilinear",
)
assert not numpy.array_equal(arr, arr2)
# # This is NOT TRUE, padding affects the whole array not just the border.
# def test_tile_padding_only_effects_edge_pixels():
# """Adding tile padding should effect edge pixels only."""
# bounds = [
# -6574807.42497772,
# 12210356.646387195,
# -6261721.357121638,
# 12523442.714243278
# ]
# with rasterio.open(COG) as src_dst:
# arr, _ = reader.part(src_dst, bounds, 32, 32, nodata=0)
# arr2, _ = reader.part(src_dst, bounds, 32, 32, nodata=0, padding=2)
# assert not np.array_equal(arr[0][0], arr2[0][0])
# assert np.array_equal(arr[0][5:-5][5:-5], arr2[0][5:-5][5:-5])
def test_tile_read_invalidResampling():
"""Should raise an error on invalid resampling method name."""
bounds = [
-6574807.42497772,
12210356.646387195,
-6261721.357121638,
12523442.714243278,
]
with pytest.raises(KeyError):
with rasterio.open(COG) as src_dst:
reader.part(src_dst, bounds, 16, 16, resampling_method="jacques")
def test_tile_read_tuple_index():
"""Should work as expected"""
bounds = (
-11663507.036777973,
4715018.0897710975,
-11663487.927520901,
4715037.199028169,
)
with rasterio.open(S3_PATH) as src_dst:
arr, mask = reader.part(src_dst, bounds, 16, 16, indexes=(1,))
assert arr.shape == (1, 16, 16)
assert mask.shape == (16, 16)
def test_tile_read_int_index():
"""Should work as expected."""
bounds = (
-11663507.036777973,
4715018.0897710975,
-11663487.927520901,
4715037.199028169,
)
with rasterio.open(S3_PATH) as src_dst:
arr, mask = reader.part(src_dst, bounds, 16, 16, indexes=1)
assert arr.shape == (1, 16, 16)
assert mask.shape == (16, 16)
def test_tile_read_bgr():
"""Should work as expected (read rgb)"""
bounds = (
-11663507.036777973,
4715018.0897710975,
-11663487.927520901,
4715037.199028169,
)
with rasterio.open(S3_PATH) as src_dst:
arr, mask = reader.part(src_dst, bounds, 16, 16, indexes=(3, 2, 1))
assert arr.shape == (3, 16, 16)
assert mask.shape == (16, 16)
def test_tile_read_nodata():
"""Should work as expected when forcing nodata value."""
# Partial Tile 7-42-24
bounds = [
-6887893.4928338025,
12210356.646387195,
-6574807.424977721,
12523442.714243278,
]
tilesize = 16
with rasterio.open(COG) as src_dst:
arr, mask = reader.part(src_dst, bounds, tilesize, tilesize, nodata=1)
assert arr.shape == (1, 16, 16)
assert mask.shape == (16, 16)
assert not mask.all()
def test_tile_read_nodata_and_alpha():
"""Should work as expected when forcing nodata value"""
bounds = (
13604568.04230881,
-333876.9395496497,
13605791.034761373,
-332653.9470970885,
)
tilesize = 16
with rasterio.open(PIX4D_PATH) as src_dst:
arr, mask = reader.part(src_dst, bounds, tilesize, tilesize, indexes=[1, 2, 3])
assert arr.shape == (3, 16, 16)
assert mask.shape == (16, 16)
assert not mask.all()
def test_tile_read_dataset_nodata():
"""Should work as expected (read rgb)"""
# non-boundless tile covering the nodata part 22-876431-1603670
bounds = (
-11663535.70066358,
4715027.644399633,
-11663526.146035044,
4715037.199028169,
)
tilesize = 16
with rasterio.open(S3_NODATA_PATH) as src_dst:
arr, mask = reader.part(src_dst, bounds, tilesize, tilesize)
assert arr.shape == (3, 16, 16)
assert not mask.all()
def test_tile_read_not_covering_the_whole_tile():
"""Should raise an error when dataset doesn't cover more than 50% of the tile."""
bounds = (
-11271098.442818949,
12210356.646387195,
-10958012.374962866,
12523442.714243278,
)
tilesize = 16
with pytest.raises(TileOutsideBounds):
with rasterio.open(COG) as src_dst:
reader.part(src_dst, bounds, tilesize, tilesize, minimum_overlap=0.6)
# See https://github.com/cogeotiff/rio-tiler/issues/105#issuecomment-492268836
def test_tile_read_validMask():
"""Dataset mask should be the same as the actual mask."""
# bounds fully outside dataset
bounds = [
-6887893.4928338025,
12210356.646387195,
-6574807.424977721,
12523442.714243278,
]
tilesize = 128
with rasterio.open(COG) as src_dst:
arr, mask = reader.part(src_dst, bounds, tilesize, tilesize, nodata=1)
masknodata = (arr[0] != 1).astype(numpy.uint8) * 255
numpy.testing.assert_array_equal(mask, masknodata)
def test_read_nodata():
"""Dataset mask should be the same as the actual mask."""
bounds = [
316470,
8094354,
415375,
8148789,
]
with rasterio.open(COG) as src_dst:
arr, mask = reader.part(src_dst, bounds, nodata=1)
masknodata = (arr[0] != 1).astype(numpy.uint8) * 255
numpy.testing.assert_array_equal(mask, masknodata)
with rasterio.open(COG) as src_dst:
arr, mask = reader.read(src_dst, nodata=1)
masknodata = (arr[0] != 1).astype(numpy.uint8) * 255
numpy.testing.assert_array_equal(mask, masknodata)
with rasterio.open(COG) as src_dst:
arr, mask = reader.read(src_dst, dst_crs="epsg:3857", nodata=1)
masknodata = (arr[0] != 1).astype(numpy.uint8) * 255
numpy.testing.assert_array_equal(mask, masknodata)
def test_tile_read_crs():
"""Read tile using different target CRS and bounds CRS."""
bounds = (
-11663507.036777973,
4715018.0897710975,
-11663487.927520901,
4715037.199028169,
)
tilesize = 16
with rasterio.open(S3_PATH) as src_dst:
# Test target CRS with input bounds in bounds_crs
arr, mask = reader.part(
src_dst,
bounds,
tilesize,
tilesize,
indexes=(3, 2, 1),
dst_crs=constants.WGS84_CRS,
bounds_crs=constants.WEB_MERCATOR_CRS,
)
assert arr.shape == (3, 16, 16)
assert mask.shape == (16, 16)
# Test target CRS with input bounds in target CRS
bounds = (
-104.7750663757324,
38.95353532141203,
-104.77489471435543,
38.95366881479646,
)
arr_crs, _ = reader.part(
src_dst,
bounds,
tilesize,
tilesize,
indexes=(3, 2, 1),
dst_crs=constants.WGS84_CRS,
)
assert numpy.array_equal(arr, arr_crs)
def test_tile_read_vrt_option():
"""Should work as expected (read landsat band)."""
bounds = [
-6887893.4928338025,
12210356.646387195,
-6574807.424977721,
12523442.714243278,
]
tilesize = 16
with rasterio.open(COG) as src_dst:
arr, mask = reader.part(
src_dst,
bounds,
tilesize,
tilesize,
vrt_options={"source_extra": 10, "num_threads": 10},
)
assert arr.shape == (1, 16, 16)
assert mask.shape == (16, 16)
def test_point():
"""Read point values"""
with rasterio.open(COG) as src_dst:
pt = reader.point(
src_dst,
[-53.54620193828792, 73.28439084323475],
coord_crs="epsg:4326",
indexes=1,
nodata=1,
)
assert pt.data == numpy.array([1])
assert pt.mask == numpy.array([0])
assert pt.band_names == ["b1"]
with rasterio.open(COG_SCALE) as src_dst:
pt = reader.point(src_dst, [310000, 4100000], coord_crs=src_dst.crs, indexes=1)
assert pt.data == numpy.array([8917])
assert pt.mask == numpy.array([255])
assert pt.band_names == ["b1"]
pt = reader.point(src_dst, [310000, 4100000], coord_crs=src_dst.crs)
assert pt.data == numpy.array([8917])
assert pt.band_names == ["b1"]
with pytest.raises(PointOutsideBounds):
reader.point(src_dst, [810000, 4100000], coord_crs=src_dst.crs)
with rasterio.open(S3_ALPHA_PATH) as src_dst:
# Test with COG + Alpha Band
assert reader.point(src_dst, [-104.77519499, 38.95367054]).data[0]
assert (
reader.point(src_dst, [-104.77519499, 38.95367054]).mask[0] == 0
) # Masked
def test_part_with_buffer():
"""Make sure buffer works as expected."""
bounds = [
-6574807.42497772,
12210356.646387195,
-6261721.357121638,
12523442.714243278,
]
# Read part at full resolution
with rasterio.open(COG) as src_dst:
img_no_buffer = reader.part(src_dst, bounds, dst_crs=constants.WEB_MERCATOR_CRS)
x_size = img_no_buffer.width
y_size = img_no_buffer.height
x_res = (bounds[2] - bounds[0]) / x_size
y_res = (bounds[3] - bounds[1]) / y_size
nx = x_size + 4
ny = y_size + 4
# apply a 2 pixel buffer
bounds_with_buffer = (
bounds[0] - x_res * 2,
bounds[1] - y_res * 2,
bounds[2] + x_res * 2,
bounds[3] + y_res * 2,
)
with rasterio.open(COG) as src_dst:
img = reader.part(
src_dst,
bounds_with_buffer,
height=ny,
width=nx,
dst_crs=constants.WEB_MERCATOR_CRS,
)
assert img.width == nx
assert img.height == ny
with rasterio.open(COG) as src_dst:
imgb = reader.part(
src_dst, bounds, buffer=2, dst_crs=constants.WEB_MERCATOR_CRS
)
assert imgb.width == nx
assert imgb.height == ny
assert numpy.array_equal(img.data, imgb.data)
assert img.bounds == imgb.bounds
# No resampling is involved. Because we read the full resolution data
# all arrays should be equal
numpy.array_equal(img_no_buffer.data, imgb.data[:, 2:-2, 2:-2])
numpy.array_equal(img_no_buffer.data, img.data[:, 2:-2, 2:-2])
def test_read():
"""Test reader.read function."""
with rasterio.open(COG) as src:
img = reader.read(src)
assert img.width == src.width
assert img.height == src.height
assert img.count == src.count
assert img.bounds == src.bounds
assert img.crs == src.crs
with rasterio.open(COG) as src:
with pytest.warns(UserWarning):
img = reader.read(src, max_size=1000, width=100, height=100)
assert img.width == 100
assert img.height == 100
assert img.count == src.count
assert img.bounds == src.bounds
assert img.crs == src.crs
with rasterio.open(COG) as src:
img = reader.read(src, width=100, height=100)
assert img.width == 100
assert img.height == 100
assert img.count == src.count
assert img.bounds == src.bounds
assert img.crs == src.crs
with rasterio.open(COG) as src:
img = reader.read(src, max_size=100)
assert max(img.width, img.height) == 100
assert img.count == src.count
assert img.bounds == src.bounds
assert img.crs == src.crs
with rasterio.open(COG) as src:
img = reader.read(src, dst_crs="epsg:3857")
assert not img.width == src.width
assert not img.height == src.height
assert img.count == src.count
assert not img.bounds == src.bounds
assert not img.crs == src.crs
with rasterio.open(COG) as src:
img = reader.read(src)
assert img.mask.all()
with rasterio.open(COG) as src:
img = reader.read(src, nodata=1)
assert not img.mask.all()
with rasterio.open(COG) as src:
img = reader.read(src, window=((0, 100), (0, 100)))
assert img.width == 100
assert img.height == 100
assert img.count == src.count
assert not img.bounds == src.bounds
assert img.crs == src.crs
# Boundless Read
with rasterio.open(COG) as src:
img = reader.read(src, window=((-10, 100), (-10, 100)))
assert img.width == 110
assert img.height == 110
assert img.count == src.count
assert not img.bounds == src.bounds
assert img.crs == src.crs
with rasterio.open(COG) as src:
img = reader.read(src, window=((0, 4000), (0, 4000)))
assert img.width == 4000
assert img.height == 4000
assert img.count == src.count
assert not img.bounds == src.bounds
assert img.crs == src.crs
# Can't use boundless window with WarpedVRT
with rasterio.open(COG) as src:
with pytest.raises(ValueError):
reader.read(src, window=((0, 4000), (0, 4000)), dst_crs="epsg:3857")
# Unscale Dataset
with rasterio.open(COG_SCALE) as src:
assert not src.dtypes[0] == numpy.float32
img = reader.read(src, unscale=True)
assert img.data.dtype == numpy.float32
# Dataset with Alpha using WarpedVRT
with rasterio.open(S3_ALPHA_PATH) as src:
img = reader.read(src, dst_crs="epsg:3857")
assert not img.mask.all()
def test_part_no_VRT():
"""Test reader.part function without VRT."""
bounds = [
-56.6015625,
73.0001215118412,
-51.67968749999999,
74.23886253330774,
] # boundless part
# Read part at full resolution
with rasterio.open(COG) as src_dst:
bounds_dst_crs = transform_bounds(
"epsg:4326", src_dst.crs, *bounds, densify_pts=21
)
img = reader.part(src_dst, bounds, bounds_crs="epsg:4326")
assert img.height == 1453
assert img.width == 1613
assert img.mask[0, 0] == 255
assert img.mask[-1, -1] == 0 # boundless
assert img.bounds == bounds_dst_crs
# Use bbox in Image CRS
img_crs = reader.part(src_dst, bounds_dst_crs)
assert img.height == 1453
assert img.width == 1613
assert img_crs.mask[0, 0] == 255
assert img_crs.mask[-1, -1] == 0 # boundless
assert img.bounds == bounds_dst_crs
# MaxSize
img = reader.part(src_dst, bounds, bounds_crs="epsg:4326", max_size=1024)
assert img.height < 1024
assert img.width == 1024
assert img.mask[0, 0] == 255
assert img.mask[-1, -1] == 0 # boundless
assert img.bounds == bounds_dst_crs
# Width/Height
img = reader.part(
src_dst,
bounds,
bounds_crs="epsg:4326",
width=100,
height=100,
)
assert img.height == 100
assert img.width == 100
assert img.mask[0, 0] == 255
assert img.mask[-1, -1] == 0 # boundless
assert img.bounds == bounds_dst_crs
# Buffer
img = reader.part(src_dst, bounds, bounds_crs="epsg:4326", buffer=1)
assert img.height == 1455
assert img.width == 1615
assert img.mask[0, 0] == 255
assert img.mask[-1, -1] == 0 # boundless
assert not img.bounds == bounds_dst_crs
# Padding
img = reader.part(src_dst, bounds, bounds_crs="epsg:4326")
img_pad = reader.part(src_dst, bounds, bounds_crs="epsg:4326", padding=1)
assert img_pad.height == 1453
assert img_pad.width == 1613
assert img_pad.mask[0, 0] == 255
assert img_pad.mask[-1, -1] == 0 # boundless
assert img_pad.bounds == bounds_dst_crs
# Padding should not have any influence when not doing any rescaling/reprojection
numpy.array_equal(img_pad.data, img.data)
# Read bbox smaller than one pixel
bounds_small = [bounds[0], bounds[1], bounds[0] + 1e-6, bounds[1] + 1e-6]
bounds_small_dst_crs = transform_bounds("epsg:4326", src_dst.crs, *bounds_small)
img_small = reader.part(src_dst, bounds_small, bounds_crs="epsg:4326")
assert img_small.height == 1
assert img_small.width == 1
assert_array_almost_equal(img_small.bounds, bounds_small_dst_crs)
@pytest.mark.parametrize(
"bounds,crs",
[
(
(
-56.624124590533825,
73.50183615350426,
-56.530950796449005,
73.52687881825946,
),
"epsg:32621",
), # Case 1 - square bounds within dataset
(
(
-62.841631140841685,
73.15163488990189,
-60.36648908847309,
73.97773652099218,
),
"epsg:32621",
), # Case 2 - boundless (left)
(
(
-52.927554190740736,
73.3960640725901,
-51.96837664926392,
73.77350422465656,
),
"epsg:32621",
), # Case 3 - boundless (right)
(
(
-57.15027188947926,
74.56177365126999,
-56.37556339673152,
74.75029925196495,
),
"epsg:32621",
), # Case 4 - boundless (top)
(
(
-55.86202533996874,
71.8988448629112,
-54.6335972683694,
72.28789003457715,
),
"epsg:32621",
), # Case 5 - boundless (bottom)
(
(
-62.968685159182414,
71.95907543637196,
-51.60091205568341,
74.78461407516858,
),
"epsg:32621",
), # Case 6 - boundless whole raster
(
(
-66.79529480522785,
74.22513769476188,
-65.89488418613195,
74.48258818252089,
),
"epsg:32621",
), # Case 7 - outside bounds
# With Reprojection
(
(
-56.624124590533825,
73.50183615350426,
-56.530950796449005,
73.52687881825946,
),
"epsg:4326",
), # Case 1 - square bounds within dataset
(
(
-62.841631140841685,
73.15163488990189,
-60.36648908847309,
73.97773652099218,
),
"epsg:4326",
), # Case 2 - boundless (left)
(
(
-52.927554190740736,
73.3960640725901,
-51.96837664926392,
73.77350422465656,
),
"epsg:4326",
), # Case 3 - boundless (right)
(
(
-57.15027188947926,
74.56177365126999,
-56.37556339673152,
74.75029925196495,
),
"epsg:4326",
), # Case 4 - boundless (top)
(
(
-55.86202533996874,
71.8988448629112,
-54.6335972683694,
72.28789003457715,
),
"epsg:4326",
), # Case 5 - boundless (bottom)
(
(
-62.968685159182414,
71.95907543637196,
-51.60091205568341,
74.78461407516858,
),
"epsg:4326",
), # Case 6 - boundless whole raster
(
(
-66.79529480522785,
74.22513769476188,
-65.89488418613195,
74.48258818252089,
),
"epsg:4326",
), # Case 7 - outside bounds
],
)
def test_part_align_transform(bounds, crs):
"""test `align_bounds_with_dataset` option."""
with rasterio.open(COG) as src_dst:
img = reader.part(
src_dst,
bounds,
dst_crs=crs,
bounds_crs="epsg:4326",
align_bounds_with_dataset=True,
)
img_default = reader.part(
src_dst,
bounds,
dst_crs=crs,
bounds_crs="epsg:4326",
align_bounds_with_dataset=False,
)
assert not img.array.shape == img_default.array.shape
assert not img.bounds == img_default.bounds
# output image aligned with bounds should have the origin
# with the bounds UL
if crs != WGS84_CRS:
bounds = transform_bounds(WGS84_CRS, crs, *bounds, densify_pts=21)
assert round(img_default.transform.c, 5) == round(bounds[0], 5)
assert round(img_default.transform.f, 5) == round(bounds[3], 5)
# output image bounds aligned to the dataset transform should have the origin
# with the bounds greater than UL
assert img.transform.c < bounds[0]
assert img.transform.f > bounds[3]
def test_nodata_orverride():
"""Make sure notata override."""
with rasterio.open(COG_NODATA) as src_dst:
prev = reader.read(src_dst, max_size=100)
assert prev.mask[0, 0] == 0
prev = reader.read(src_dst, max_size=100, nodata=2720)
assert prev.mask[0, 0] == 255
assert not numpy.all(prev.mask)
def test_tile_read_nodata_float():
"""Should work as expected when using NaN as nodata value."""
with rasterio.open(COG_NODATA_FLOAT_NAN) as src_dst:
prev = reader.read(src_dst, max_size=100)
assert prev.mask[0, 0] == 0
assert not numpy.all(prev.mask)