-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathtest_gis.py
executable file
·667 lines (545 loc) · 21.1 KB
/
test_gis.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
#!/usr/bin/env python3
# SPDX-FileCopyrightText: Contributors to atlite <https://github.com/pypsa/atlite>
#
# SPDX-License-Identifier: MIT
"""
Created on Wed May 6 15:23:13 2020.
@author: fabian
"""
# IDEAS for tests
import functools
import warnings
import geopandas as gpd
import numpy as np
import pandas as pd
import pytest
import rasterio as rio
import rasterio.warp
import xarray as xr
from numpy import allclose, isclose
from shapely.geometry import box
from xarray.testing import assert_allclose, assert_equal
from atlite import Cutout
from atlite.gis import (
ExclusionContainer,
pad_extent,
padded_transform_and_shape,
regrid,
shape_availability,
)
TIME = "2013-01-01"
X0 = -4.0
Y0 = 56.0
X1 = 1.5
Y1 = 61.0
raster_clip = 0.25 # this rastio is excluded (True) in the raster
@pytest.fixture(scope="session")
def ref(cutouts_path):
tmp_path = cutouts_path / "creation_ref.nc"
return Cutout(path=tmp_path, module="era5", bounds=(X0, Y0, X1, Y1), time=TIME)
@pytest.fixture(scope="session")
def geometry(tmp_path_factory):
tmp_path = tmp_path_factory.mktemp("geometries")
geometry = gpd.GeoSeries(
[box(X0 / 2 + X1 / 2, Y0 / 2 + Y1 / 2, X1, Y1)],
crs="EPSG:4326",
index=[0],
name="boxes",
)
geometry = geometry.to_frame().set_geometry("boxes")
path = tmp_path / "geometry.gpkg"
geometry.to_file(path, driver="GPKG")
return path
@pytest.fixture(scope="session")
def raster(tmp_path_factory):
tmp_path = tmp_path_factory.mktemp("rasters")
bounds = (X0, Y0, X1, Y1) # same as in test_gis.py
res = 0.01
transform, shape = padded_transform_and_shape(bounds, res)
mask = np.random.rand(*shape) < raster_clip
mask = mask.astype(rio.int32)
path = tmp_path / "raster.tif"
with rio.open(
path,
"w",
driver="GTiff",
transform=transform,
crs=4326,
width=shape[1],
height=shape[0],
count=1,
dtype=mask.dtype,
) as dst:
dst.write(mask, indexes=1)
return path
@pytest.fixture(scope="session")
def raster_reproject(tmp_path_factory):
tmp_path = tmp_path_factory.mktemp("rasters")
bounds = rio.warp.transform_bounds(4326, 3035, X0, Y0, X1, Y1)
res = 1000
transform, shape = padded_transform_and_shape(bounds, res)
mask = np.random.rand(*shape) < raster_clip
mask = mask.astype(rio.int32)
path = tmp_path / "raster_reproject.tif"
with rio.open(
path,
"w",
driver="GTiff",
transform=transform,
crs=3035,
width=shape[1],
height=shape[0],
count=1,
dtype=mask.dtype,
) as dst:
dst.write(mask, indexes=1)
return path
@pytest.fixture(scope="session")
def raster_codes(tmp_path_factory):
tmp_path = tmp_path_factory.mktemp("rasters")
bounds = (X0, Y0, X1, Y1) # same as in test_gis.py
res = 0.01
transform, shape = padded_transform_and_shape(bounds, res)
mask = (np.random.rand(*shape) * 100).astype(int)
mask = mask.astype(rio.int32)
path = tmp_path / "raster_codes.tif"
with rio.open(
path,
"w",
driver="GTiff",
transform=transform,
crs=4326,
width=shape[1],
height=shape[0],
count=1,
dtype=mask.dtype,
) as dst:
dst.write(mask, indexes=1)
return path
def test_exclusioncontainer_repr(ref):
"""
Test ExclusionContainer.__repr__.
"""
excluder = ExclusionContainer(ref.crs, res=0.01)
assert "Exclusion Container" in repr(excluder)
def test_open_closed_checks(ref, geometry, raster):
"""
Test atlite.ExclusionContainer(...) file open/closed checks for
plausibility.
C.f. GH issue #225.
"""
res = 0.01
excluder = ExclusionContainer(ref.crs, res=res)
# Without raster/shapes, both should evaluate to True
assert excluder.all_closed and excluder.all_open
# First add geometries, than raster
excluder.add_geometry(geometry)
assert excluder.all_closed and not excluder.all_open
# Check if still works with 2nd geometry
excluder.add_geometry(geometry)
assert excluder.all_closed and not excluder.all_open
excluder.add_raster(raster)
assert excluder.all_closed and not excluder.all_open
# Check if still works with 2nd raster
excluder.add_raster(raster)
assert excluder.all_closed and not excluder.all_open
excluder.open_files()
assert not excluder.all_closed and excluder.all_open
# First add raster, then geometries
excluder = ExclusionContainer(ref.crs, res=res)
excluder.add_raster(raster)
assert excluder.all_closed and not excluder.all_open
# 2nd raster
excluder.add_raster(raster)
assert excluder.all_closed and not excluder.all_open
excluder.add_geometry(geometry)
assert excluder.all_closed and not excluder.all_open
# 2nd geometry
excluder.add_geometry(geometry)
assert excluder.all_closed and not excluder.all_open
excluder.open_files()
assert not excluder.all_closed and excluder.all_open
def test_area(ref):
"""
Test the area of the cutout.
"""
area = ref.area(crs=3035)
assert isinstance(area, xr.DataArray)
assert area.dims == ("y", "x")
# now test with a different crs
with pytest.warns(UserWarning):
assert ref.area().sum().item() == (X1 - X0 + ref.dx) * (Y1 - Y0 + ref.dy)
def test_transform():
"""
Test the affine transform.
It always has to point to cell origin.
"""
cutout = Cutout(
path="resolution",
module="era5",
time=slice("2013-01-01", "2013-01-01"),
x=slice(X0, X1),
y=slice(Y0, Y1),
)
assert cutout.transform * (0.5, 0.5) == (X0, Y0)
assert cutout.transform * cutout.shape[::-1] == (
X1 + cutout.dx / 2,
Y1 + cutout.dy / 2,
)
def test_grid_coords(ref):
gcoords = ref.grid[["x", "y"]]
spatial = ref.data.stack(spatial=["y", "x"])["spatial"].data
spatial = np.array([[s[1], s[0]] for s in spatial])
np.testing.assert_equal(gcoords, spatial)
def test_extent(ref):
pad = 0.25 / 2
np.testing.assert_array_equal(ref.extent, [X0 - pad, X1 + pad, Y0 - pad, Y1 + pad])
# Note that bounds is the same as extent but in different order.
def test_bounds(ref):
np.testing.assert_array_equal(ref.bounds, ref.grid.total_bounds)
def test_regrid():
"""
Test the atlite.gis.regrid function with average resampling.
"""
# define blocks
A = 0.25
B = 0.5
C = 0.3
D = 0.1
ones = np.ones((4, 4))
fine = np.block([[ones * A, ones * B], [ones * C, ones * D]])
# add coordinates
finecoords = np.arange(0.5, 8, 1)
fine = xr.DataArray(fine, coords=[("y", finecoords), ("x", finecoords)])
coarsecoords = np.arange(2, 8, 4)
coarse = xr.DataArray(np.nan, coords=[("y", coarsecoords), ("x", coarsecoords)])
# apply average resampling
res = regrid(fine, coarse.x, coarse.y, resampling=5)
target = np.array([[A, B], [C, D]])
assert allclose(res, target)
assert (coarse.x == res.x).all() and (coarse.y == res.y).all()
# now test multiple layers
fine = xr.concat([fine] * 10, pd.Index(range(10), name="z"))
res = regrid(fine, coarse.x, coarse.y, resampling=5)
target = np.stack([np.array([[A, B], [C, D]])] * 10)
assert allclose(res, target)
assert (coarse.x == res.x).all() and (coarse.y == res.y).all()
# now let the target grid cover a subarea of the original
fine = fine.sel(z=0, drop=True)
coarsecoords = np.arange(1, 6, 2)
coarse = xr.DataArray(np.nan, coords=[("y", coarsecoords), ("x", coarsecoords)])
# apply average resampling
res = regrid(fine, coarse.x, coarse.y, resampling=5)
target = np.array([[A, A, B], [A, A, B], [C, C, D]])
assert allclose(res, target)
assert (coarse.x == res.x).all() and (coarse.y == res.y).all()
def test_pad_extent():
"""
Test whether padding works with arrays of dimension > 2.
"""
src = np.ones((3, 2))
src_trans = rio.Affine(1, 0, 0, 0, 1, 0)
dst_trans = rio.Affine(2, 0, 0, 0, 2, 0)
crs = 4326
padded, trans = pad_extent(src, src_trans, dst_trans, crs, crs)
src = np.ones((1, 3, 2))
padded_ndim, trans_ndim = pad_extent(src, src_trans, dst_trans, crs, crs)
assert (padded_ndim[0] == padded).all()
assert trans == trans_ndim
# second check with large shape
src = np.ones((1, 2, 3, 2))
padded_ndim, trans_ndim = pad_extent(src, src_trans, dst_trans, crs, crs)
assert (padded_ndim[0, 0] == padded).all()
assert trans == trans_ndim
# other way round, here it should not pad, since target resolution is lower
padded_r, trans_r = pad_extent(src, dst_trans, src_trans, crs, crs)
assert (padded_r == src).all()
assert trans_r == dst_trans
def test_indicator_matrix(ref):
# This should be the grid cell at the lower left corner
cell = ref.grid.geometry[0]
indicator = ref.indicatormatrix([cell])
assert indicator[0, 0] == 1.0
assert indicator.sum() == 1
# This should be the grid cell at the lower left corner
cell = ref.grid.geometry.iloc[-2]
indicator = ref.indicatormatrix([cell])
assert indicator[0, -2] == 1.0
assert indicator.sum() == 1
def test_availability_matrix_flat(ref):
"""
Indicator matrix and availability matrix for an empty excluder must be the
same.
"""
shapes = gpd.GeoSeries(
[box(X0 + 1, Y0 + 1, X1 - 1, Y1 - 1)], crs=ref.crs
).rename_axis("shape")
I = ref.indicatormatrix(shapes).sum(0).reshape(ref.shape)
I = xr.DataArray(I, coords=[ref.coords["y"], ref.coords["x"]])
excluder = ExclusionContainer(ref.crs, res=0.01)
ds = ref.availabilitymatrix(shapes, excluder)
assert np.allclose(I, ds.sum("shape"))
def test_availability_matrix_flat_parallel(ref):
"""
Same as `test_availability_matrix_flat` but parallel and without
progressbar.
"""
shapes = gpd.GeoSeries(
[box(X0 + 1, Y0 + 1, X1 - 1, Y1 - 1)], crs=ref.crs
).rename_axis("shape")
I = ref.indicatormatrix(shapes).sum(0).reshape(ref.shape)
I = xr.DataArray(I, coords=[ref.coords["y"], ref.coords["x"]])
excluder = ExclusionContainer(ref.crs, res=0.01)
ds = ref.availabilitymatrix(shapes, excluder, nprocesses=2)
assert np.allclose(I, ds.sum("shape"))
def test_availability_matrix_flat_parallel_anonymous_function(ref, raster_codes):
"""
Test availability matrix in parallel mode with a non-anonymous filter
function.
"""
shapes = gpd.GeoSeries(
[box(X0 + 1, Y0 + 1, X1 - 1, Y1 - 1)], crs=ref.crs
).rename_axis("shape")
I = ref.indicatormatrix(shapes).sum(0).reshape(ref.shape)
I = xr.DataArray(I, coords=[ref.coords["y"], ref.coords["x"]])
excluder = ExclusionContainer(ref.crs, res=0.01)
func = functools.partial(np.greater_equal, 20)
excluder.add_raster(raster_codes, codes=func)
ref.availabilitymatrix(shapes, excluder, nprocesses=2)
def test_availability_matrix_flat_wo_progressbar(ref):
"""
Same as `test_availability_matrix_flat` but without progressbar.
"""
shapes = gpd.GeoSeries(
[box(X0 + 1, Y0 + 1, X1 - 1, Y1 - 1)], crs=ref.crs
).rename_axis("shape")
I = ref.indicatormatrix(shapes).sum(0).reshape(ref.shape)
I = xr.DataArray(I, coords=[ref.coords["y"], ref.coords["x"]])
excluder = ExclusionContainer(ref.crs, res=0.01)
ds = ref.availabilitymatrix(shapes, excluder, disable_progressbar=True)
assert np.allclose(I, ds.sum("shape"))
def test_availability_matrix_flat_parallel_wo_progressbar(ref):
"""
Same as `test_availability_matrix_flat` but parallel and without
progressbar.
"""
shapes = gpd.GeoSeries(
[box(X0 + 1, Y0 + 1, X1 - 1, Y1 - 1)], crs=ref.crs
).rename_axis("shape")
I = ref.indicatormatrix(shapes).sum(0).reshape(ref.shape)
I = xr.DataArray(I, coords=[ref.coords["y"], ref.coords["x"]])
excluder = ExclusionContainer(ref.crs, res=0.01)
ds = ref.availabilitymatrix(
shapes, excluder, nprocesses=2, disable_progressbar=True
)
assert np.allclose(I, ds.sum("shape"))
def test_shape_availability_area(ref):
"""
Area of the mask and the shape must be close.
"""
shapes = gpd.GeoSeries([box(X0 + 1, Y0 + 1, X1 - 1, Y1 - 1)], crs=ref.crs)
res = 100
excluder = ExclusionContainer(res=res)
with pytest.raises(AssertionError):
masked, transform = shape_availability(shapes, excluder)
shapes = shapes.to_crs(3035)
masked, transform = shape_availability(shapes, excluder)
assert np.isclose(shapes.area, masked.sum() * res**2)
masked2, transform2 = excluder.compute_shape_availability(shapes)
assert (masked == masked2).all()
assert transform == transform2
masked3, transform3 = excluder.compute_shape_availability(shapes.to_frame())
assert (masked == masked2).all()
assert transform == transform2
def test_exclusioncontainer_geometries():
crs = 3035
exclude = gpd.GeoSeries([box(X0 / 2 + X1 / 2, Y0 / 2 + Y1 / 2, X1, Y1)], crs=crs)
res = 0.01
excluder = ExclusionContainer(crs, res=res)
excluder.add_geometry(exclude, buffer=1)
excluder.open_files()
assert (excluder.geometries[0]["geometry"] != exclude).all()
excluder.open_files()
buffered = excluder.geometries[0]["geometry"]
# open again and check that the buffer remains the same
assert (excluder.geometries[0]["geometry"] == buffered).all()
# should take GeoDataFrames and the result is the same
excluder = ExclusionContainer(crs, res=res)
excluder.add_geometry(exclude.to_frame("geometry"), buffer=1)
excluder.open_files()
assert (excluder.geometries[0]["geometry"] == buffered).all()
def test_shape_availability_exclude_geometry(ref):
"""
When excluding the quarter of the geometry, the eligible area must be a
forth.
Test the inverted case too.
"""
shapes = gpd.GeoSeries([box(X0, Y0, X1, Y1)], crs=ref.crs)
exclude = gpd.GeoSeries(
[box(X0 / 2 + X1 / 2, Y0 / 2 + Y1 / 2, X1, Y1)], crs=ref.crs
)
res = 0.01
excluder = ExclusionContainer(ref.crs, res=res)
excluder.add_geometry(exclude)
masked, transform = shape_availability(shapes, excluder)
area = shapes.geometry[0].area # get area without warning
assert isclose(3 * area / 4, masked.sum() * res**2)
excluder = ExclusionContainer(ref.crs, res=res)
excluder.add_geometry(exclude, invert=True)
masked, transform = shape_availability(shapes, excluder)
area = shapes.geometry[0].area # get area without warning
assert isclose(area / 4, masked.sum() * res**2)
def test_shape_availability_exclude_raster(ref, raster):
"""
When excluding the half of the geometry, the eligible area must be half.
"""
shapes = gpd.GeoSeries([box(X0, Y0, X1, Y1)], crs=ref.crs)
res = 0.01
excluder = ExclusionContainer(ref.crs, res=res)
excluder.add_raster(raster)
masked, transform = shape_availability(shapes, excluder)
ratio = masked.sum() / masked.size
assert round(ratio, 2) == (1 - raster_clip)
excluder = ExclusionContainer(ref.crs, res=res)
excluder.add_raster(raster, invert=True)
masked, transform = shape_availability(shapes, excluder)
ratio = masked.sum() / masked.size
assert round(ratio, 2) == raster_clip
excluder = ExclusionContainer(ref.crs, res=res)
excluder.add_raster(raster, buffer=res)
masked, transform = shape_availability(shapes, excluder)
ratio = masked.sum() / masked.size
# should be close to zero
assert round(ratio, 2) < (1 - raster_clip)
excluder = ExclusionContainer(ref.crs, res=res)
excluder.add_raster(raster, buffer=res, invert=True)
masked, transform = shape_availability(shapes, excluder)
ratio2 = masked.sum() / masked.size
# for the case that we have more excluded area and this is buffered
if raster_clip < 0.5:
assert ratio >= ratio2
else:
assert ratio <= ratio2
def test_shape_availability_excluder_partial_overlap(ref, raster):
"""
Test behavior, when a raster only overlaps half of the geometry.
"""
bounds = X0 - 2, Y0, X0 + 2, Y1
area = abs((bounds[2] - bounds[0]) * (bounds[3] - bounds[1]))
shapes = gpd.GeoSeries([box(*bounds)], crs=ref.crs)
res = 0.01
excluder = ExclusionContainer(ref.crs, res=res)
excluder.add_raster(raster, codes=[0, 1])
masked, transform = shape_availability(shapes, excluder)
assert masked.sum() * (res**2) == area / 2
excluder = ExclusionContainer(ref.crs, res=res)
excluder.add_raster(raster, nodata=0)
masked, transform = shape_availability(shapes, excluder)
assert masked.sum() * (res**2) > area / 2
excluder = ExclusionContainer(ref.crs, res=res)
excluder.add_raster(raster, nodata=1)
masked, transform = shape_availability(shapes, excluder)
assert masked.sum() * (res**2) < area / 2
def test_shape_availability_excluder_raster_no_overlap(ref, raster):
"""
Check if the allow_no_overlap flag works.
"""
bounds = X0 - 10.0, Y0 - 10.0, X0 - 2.0, Y0 - 2.0
area = abs((bounds[2] - bounds[0]) * (bounds[3] - bounds[1]))
shapes = gpd.GeoSeries([box(*bounds)], crs=ref.crs)
res = 0.01
excluder = ExclusionContainer(ref.crs, res=res)
excluder.add_raster(raster)
with pytest.raises(ValueError):
masked, transform = shape_availability(shapes, excluder)
excluder = ExclusionContainer(ref.crs, res=res)
excluder.add_raster(raster, allow_no_overlap=True)
masked, transform = shape_availability(shapes, excluder)
assert (masked == 0).all()
excluder = ExclusionContainer(ref.crs, res=res)
excluder.add_raster(raster, allow_no_overlap=True, codes=[1, 255], invert=True)
masked, transform = shape_availability(shapes, excluder)
assert masked.sum() * (res**2) == area
excluder = ExclusionContainer(ref.crs, res=res)
excluder.add_raster(raster, allow_no_overlap=True, nodata=0)
masked, transform = shape_availability(shapes, excluder)
assert masked.sum() * (res**2) == area
def test_availability_matrix_rastered(ref, raster):
"""
Availability matrix with a non-zero raster must have less available area
than the Indicator matrix.
"""
shapes = gpd.GeoSeries(
[
box(X0 + 1, Y0 + 1, X1 - 1, Y0 / 2 + Y1 / 2),
box(X0 + 1, Y0 / 2 + Y1 / 2, X1 - 1, Y1 - 1),
],
crs=ref.crs,
).rename_axis("shape")
I = np.asarray(ref.indicatormatrix(shapes).todense())
I = I.reshape(shapes.shape + ref.shape)
I = xr.DataArray(I, coords=[shapes.index, ref.coords["y"], ref.coords["x"]])
excluder = ExclusionContainer(ref.crs, res=0.01)
excluder.add_raster(raster)
ds = ref.availabilitymatrix(shapes, excluder)
eligible_share = 1 - raster_clip
assert isclose(I.sum() * eligible_share, ds.sum(), atol=5)
assert_allclose(I.sum(["x", "y"]) * eligible_share, ds.sum(["x", "y"]), atol=5)
excluder = ExclusionContainer(ref.crs, res=0.01)
excluder.add_raster(raster)
assert_equal(ds, ref.availabilitymatrix(shapes, excluder, 2))
def test_availability_matrix_rastered_repro(ref, raster_reproject):
"""
Availability matrix with a non-zero raster must have less available area
than the Indicator matrix.
Test this with a raster of a different crs.
"""
shapes = gpd.GeoSeries(
[
box(X0 + 1, Y0 + 1, X1 - 1, Y0 / 2 + Y1 / 2),
box(X0 + 1, Y0 / 2 + Y1 / 2, X1 - 1, Y1 - 1),
],
crs=ref.crs,
).rename_axis("shape")
I = np.asarray(ref.indicatormatrix(shapes).todense())
I = I.reshape(shapes.shape + ref.shape)
I = xr.DataArray(I, coords=[shapes.index, ref.coords["y"], ref.coords["x"]])
excluder = ExclusionContainer()
excluder.add_raster(raster_reproject)
ds = ref.availabilitymatrix(shapes, excluder)
eligible_share = 1 - raster_clip
assert isclose(I.sum() * eligible_share, ds.sum(), atol=5)
assert_allclose(I.sum(["x", "y"]) * eligible_share, ds.sum(["x", "y"]), atol=5)
def test_shape_availability_exclude_raster_codes(ref, raster_codes):
"""
Test exclusion of multiple raster codes.
"""
shapes = gpd.GeoSeries([box(X0, Y0, X1, Y1)], crs=ref.crs)
res = 0.01
excluder = ExclusionContainer(ref.crs, res=res)
excluder.add_raster(raster_codes, codes=range(20))
masked, transform = shape_availability(shapes, excluder)
ratio = masked.sum() / masked.size
assert round(ratio, 1) == 0.8
excluder = ExclusionContainer(ref.crs, res=res)
excluder.add_raster(raster_codes, codes=range(20), invert=True)
masked, transform = shape_availability(shapes, excluder)
ratio = masked.sum() / masked.size
assert round(ratio, 1) == 0.2
# test with a function
excluder = ExclusionContainer(ref.crs, res=res)
excluder.add_raster(raster_codes, codes=lambda x: x < 20, invert=True)
masked, transform = shape_availability(shapes, excluder)
assert ratio == masked.sum() / masked.size
def test_plot_shape_availability(ref, raster):
"""
Test plotting of shape availability.
"""
shapes = gpd.GeoSeries([box(X0, Y0, X1, Y1)], crs=ref.crs)
res = 0.01
excluder = ExclusionContainer(ref.crs, res=res)
excluder.add_raster(raster)
# disable UserWarning
with warnings.catch_warnings():
warnings.simplefilter("ignore")
excluder.plot_shape_availability(shapes)