Skip to content

Commit

Permalink
Take into account per dataset mask
Browse files Browse the repository at this point in the history
  • Loading branch information
drnextgis committed Oct 15, 2019
1 parent 8a960e2 commit 07e780a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/rasterstats/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import rasterio
import warnings
from rasterio.transform import guard_transform
from rasterio.enums import MaskFlags
from affine import Affine
import numpy as np
try:
Expand Down Expand Up @@ -305,6 +306,11 @@ def read(self, bounds=None, window=None, masked=False):
self.array, window=win, nodata=nodata, masked=masked)
elif self.src:
# It's an open rasterio dataset
if all(MaskFlags.per_dataset in flags for flags in self.src.mask_flag_enums):
if not masked:
masked = True
warnings.warn("Setting masked to True because dataset mask has been detected")

new_array = self.src.read(
self.band, window=win, boundless=True, masked=masked)

Expand Down
Binary file added tests/data/dataset_mask.tif
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/test_zonal.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ def test_nodata():
assert '1.0' not in stats[0]


def test_dataset_mask():
polygons = os.path.join(DATA, 'polygons.shp')
raster = os.path.join(DATA, 'dataset_mask.tif')
stats = zonal_stats(polygons, raster, stats="*")
assert stats[0]['count'] == 75
assert stats[1]['count'] == 0


def test_partial_overlap():
polygons = os.path.join(DATA, 'polygons_partial_overlap.shp')
stats = zonal_stats(polygons, raster, stats="count")
Expand Down

0 comments on commit 07e780a

Please sign in to comment.