-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #417 from GispoCoding/414-add-masking-parameter-to…
…-unify-raster-grids 414 add masking parameter to unify raster grids
- Loading branch information
Showing
6 changed files
with
121 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,27 @@ | ||
import tempfile | ||
from pathlib import Path | ||
|
||
import geopandas as gpd | ||
import numpy as np | ||
import rasterio | ||
|
||
from eis_toolkit.raster_processing.clipping import clip_raster | ||
from eis_toolkit.raster_processing.masking import mask_raster | ||
from tests.raster_processing.clip_test import polygon_path as SMALL_VECTOR_PATH | ||
from tests.raster_processing.clip_test import raster_path as SMALL_RASTER_PATH | ||
|
||
test_dir = Path(__file__).parent.parent | ||
small_raster_clipped_path = test_dir.joinpath("data/remote/small_raster_clipped.tif") | ||
|
||
|
||
def test_mask_raster(): | ||
"""Test that masking raster works as intended.""" | ||
with rasterio.open(SMALL_RASTER_PATH) as raster: | ||
geodataframe = gpd.read_file(SMALL_VECTOR_PATH) | ||
out_image, out_meta = clip_raster(raster, geodataframe) | ||
with tempfile.NamedTemporaryFile() as tmpfile: | ||
with rasterio.open(tmpfile.name, "w", **out_meta) as dest: | ||
dest.write(out_image) | ||
with rasterio.open(tmpfile.name) as base_raster: | ||
old_nodata_count = np.count_nonzero(raster.read(1) == raster.nodata) | ||
out_image, out_profile = mask_raster(raster, base_raster) | ||
with rasterio.open(small_raster_clipped_path) as base_raster: | ||
old_nodata_count = np.count_nonzero(raster.read(1) == raster.nodata) | ||
out_image, out_profile = mask_raster(raster, base_raster) | ||
|
||
new_nodata_count = np.count_nonzero(out_image == out_profile["nodata"]) | ||
new_nodata_count = np.count_nonzero(out_image == out_profile["nodata"]) | ||
|
||
# Check nodata count has increased | ||
assert new_nodata_count > old_nodata_count | ||
# Check that nodata exists now in identical locations in input raster and base raster | ||
np.testing.assert_array_equal( | ||
base_raster.read(1) == base_raster.nodata, out_image[0] == out_profile["nodata"] | ||
) | ||
# Check nodata count has increased | ||
assert new_nodata_count > old_nodata_count | ||
# Check that nodata exists now in identical locations in input raster and base raster | ||
np.testing.assert_array_equal( | ||
base_raster.read(1) == base_raster.nodata, out_image[0] == out_profile["nodata"] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters