Skip to content

Commit e6a15bb

Browse files
committed
switch default strategy, preserve attrs
1 parent 5107646 commit e6a15bb

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

xvec/accessor.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import json
44
import warnings
55
from collections.abc import Callable, Hashable, Iterable, Mapping, Sequence
6-
from typing import TYPE_CHECKING, Any, cast
6+
from typing import TYPE_CHECKING, Any, Literal, cast
77

88
import numpy as np
99
import pandas as pd
@@ -987,6 +987,9 @@ def zonal_stats(
987987
all_touched: bool = False,
988988
n_jobs: int = -1,
989989
nodata: Any = None,
990+
strategy: Literal[
991+
"feature-sequential", "raster-sequential"
992+
] = "raster-sequential",
990993
**kwargs: dict[str, Any],
991994
) -> xr.DataArray | xr.Dataset:
992995
"""Extract the values from a dataset indexed by a set of geometries
@@ -1067,6 +1070,10 @@ def zonal_stats(
10671070
nodata : Any
10681071
Value representing missing data. If not specified, the value is included in
10691072
the aggregation.
1073+
strategy : str, optional
1074+
The strategy for ``exactextract`` method to use for the extraction, by
1075+
default "raster-sequential". Use either "feature-sequential" and
1076+
"raster-sequential". See :func:`exactextract.exact_extract` for details.
10701077
**kwargs : optional
10711078
Keyword arguments to be passed to the aggregation function
10721079
(e.g., ``Dataset.quantile(**kwargs)``).
@@ -1194,6 +1201,7 @@ def zonal_stats(
11941201
stats=stats,
11951202
name=name,
11961203
nodata=nodata,
1204+
strategy=strategy,
11971205
**kwargs,
11981206
)
11991207
else:

xvec/zonal.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import gc
44
from collections.abc import Callable, Hashable, Iterable, Sequence
5-
from typing import Any
5+
from typing import Any, Literal
66

77
import numpy as np
88
import pandas as pd
@@ -454,6 +454,10 @@ def _zonal_stats_exactextract(
454454

455455
vec_cube.attrs = acc._obj.attrs
456456

457+
if isinstance(acc._obj, xr.Dataset):
458+
for var in vec_cube.variables:
459+
vec_cube[var].attrs = acc._obj[var].attrs
460+
457461
if original_is_ds:
458462
acc._obj = original_ds
459463

@@ -469,7 +473,7 @@ def _agg_exactextract(
469473
stats: str | Callable | Iterable[str | Callable | tuple] = "mean",
470474
name: str = "geometry",
471475
original_is_ds: bool = False,
472-
strategy: str = "feature-sequential",
476+
strategy: Literal["feature-sequential", "raster-sequential"] = "raster-sequential",
473477
nodata: Any = None,
474478
):
475479
"""Extract the values from a dataset indexed by a set of geometries
@@ -504,7 +508,7 @@ def _agg_exactextract(
504508
Value representing missing data. If not specified, the value is included in
505509
the aggregation.
506510
strategy : str, optional
507-
The strategy to use for the extraction, by default "feature-sequential"
511+
The strategy to use for the extraction, by default "raster-sequential"
508512
Use either "feature-sequential" and "raster-sequential".
509513
510514
Returns
@@ -654,4 +658,6 @@ def _variable_zonal(
654658

655659
combined = xr.combine_by_coords(r)
656660

661+
combined.attrs = acc._obj.attrs
662+
657663
return combined

0 commit comments

Comments
 (0)