Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion xvec/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,8 @@ def mask(
xr.DataArray
A DataArray with the same shape as the original, where the elements matching the predicate are set to True.
"""
cube_data = self._obj.data.ravel()
# need to replace nan with None
cube_data = self._obj.where(~self._obj.isnull(), None).data.ravel()
tree = shapely.STRtree(cube_data)
indices = tree.query(geometry, predicate=predicate, distance=distance)
if indices.ndim == 1:
Expand Down
27 changes: 16 additions & 11 deletions xvec/zonal.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,17 +581,22 @@ def _get_mean(
):
from rasterio import features

mask = features.geometry_mask(
[geom_arr.item()],
out_shape=(
obj[y_coords].shape[0],
obj[x_coords].shape[0],
),
transform=transform,
invert=True,
all_touched=all_touched,
)
masked = obj.where(xr.DataArray(mask, dims=(y_coords, x_coords)))
if pd.isna(geom_arr.item()):
masked = obj.where(
xr.DataArray(np.full_like(obj.data, False), dims=(y_coords, x_coords))
)
else:
mask = features.geometry_mask(
[geom_arr.item()],
out_shape=(
obj[y_coords].shape[0],
obj[x_coords].shape[0],
),
transform=transform,
invert=True,
all_touched=all_touched,
)
masked = obj.where(xr.DataArray(mask, dims=(y_coords, x_coords)))

if nodata is not None:
masked = masked.where(masked != nodata)
Expand Down