Skip to content

Commit 5107646

Browse files
authored
better support for nan-filled variable geometry (#127)
1 parent ae81b1d commit 5107646

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

xvec/accessor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,8 @@ def mask(
658658
xr.DataArray
659659
A DataArray with the same shape as the original, where the elements matching the predicate are set to True.
660660
"""
661-
cube_data = self._obj.data.ravel()
661+
# need to replace nan with None
662+
cube_data = self._obj.where(~self._obj.isnull(), None).data.ravel()
662663
tree = shapely.STRtree(cube_data)
663664
indices = tree.query(geometry, predicate=predicate, distance=distance)
664665
if indices.ndim == 1:

xvec/zonal.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -581,17 +581,22 @@ def _get_mean(
581581
):
582582
from rasterio import features
583583

584-
mask = features.geometry_mask(
585-
[geom_arr.item()],
586-
out_shape=(
587-
obj[y_coords].shape[0],
588-
obj[x_coords].shape[0],
589-
),
590-
transform=transform,
591-
invert=True,
592-
all_touched=all_touched,
593-
)
594-
masked = obj.where(xr.DataArray(mask, dims=(y_coords, x_coords)))
584+
if pd.isna(geom_arr.item()):
585+
masked = obj.where(
586+
xr.DataArray(np.full_like(obj.data, False), dims=(y_coords, x_coords))
587+
)
588+
else:
589+
mask = features.geometry_mask(
590+
[geom_arr.item()],
591+
out_shape=(
592+
obj[y_coords].shape[0],
593+
obj[x_coords].shape[0],
594+
),
595+
transform=transform,
596+
invert=True,
597+
all_touched=all_touched,
598+
)
599+
masked = obj.where(xr.DataArray(mask, dims=(y_coords, x_coords)))
595600

596601
if nodata is not None:
597602
masked = masked.where(masked != nodata)

0 commit comments

Comments
 (0)