File tree Expand file tree Collapse file tree 2 files changed +18
-12
lines changed Expand file tree Collapse file tree 2 files changed +18
-12
lines changed Original file line number Diff line number Diff line change @@ -658,7 +658,8 @@ def mask(
658
658
xr.DataArray
659
659
A DataArray with the same shape as the original, where the elements matching the predicate are set to True.
660
660
"""
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 ()
662
663
tree = shapely .STRtree (cube_data )
663
664
indices = tree .query (geometry , predicate = predicate , distance = distance )
664
665
if indices .ndim == 1 :
Original file line number Diff line number Diff line change @@ -581,17 +581,22 @@ def _get_mean(
581
581
):
582
582
from rasterio import features
583
583
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 )))
595
600
596
601
if nodata is not None :
597
602
masked = masked .where (masked != nodata )
You can’t perform that action at this time.
0 commit comments