From 447d23487caebfdd0cce824ef805f59d749f0f68 Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Tue, 9 Dec 2025 13:53:32 -0700 Subject: [PATCH 1/5] Add rusterize engine support for rasterization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactors the rasterization module to support multiple backends via an `engine` parameter using a Protocol-based design. Both rasterio and rusterize are now optional dependencies. Changes: - Add Rasterizer Protocol in core.py defining the engine interface - Add RasterioRasterizer class implementing the Protocol - Add RusterizeRasterizer class implementing the Protocol - Refactor core.py to use Protocol-based engine selection - Add rusterize, test-rusterize, test-all optional dependency groups - Add test-rusterize CI job in GitHub Actions - Parametrize tests over available engines Engine selection (engine=None) auto-detects, preferring rusterize if available, falling back to rasterio. geometry_clip remains rasterio-specific. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .github/workflows/test.yml | 46 ++++ pyproject.toml | 57 +++++ src/rasterix/rasterize/__init__.py | 5 + src/rasterix/rasterize/core.py | 367 ++++++++++++++++++++++++++++ src/rasterix/rasterize/rasterio.py | 292 +++------------------- src/rasterix/rasterize/rusterize.py | 229 +++++++++++++++++ src/rasterix/rasterize/utils.py | 15 -- tests/conftest.py | 36 +++ tests/test_rasterize.py | 22 +- 9 files changed, 791 insertions(+), 278 deletions(-) create mode 100644 src/rasterix/rasterize/core.py create mode 100644 src/rasterix/rasterize/rusterize.py create mode 100644 tests/conftest.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1f5a805..e2f8b00 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -68,3 +68,49 @@ jobs: # with: # token: ${{ secrets.CODECOV_TOKEN }} # verbose: true # optional (default = false) + + test-rusterize: + name: rusterize, py=${{ matrix.python-version }} + + strategy: + matrix: + python-version: ["3.11", "3.13"] + os: ["ubuntu-latest"] + runs-on: ${{ matrix.os }} + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 # grab all branches and tags + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + cache: "pip" + - name: Install Hatch + run: | + python -m pip install --upgrade pip + pip install hatch + - name: Restore cached hypothesis directory + id: restore-hypothesis-cache + uses: actions/cache/restore@v4 + with: + path: .hypothesis/ + key: cache-hypothesis-${{ runner.os }}-${{ github.run_id }} + restore-keys: | + cache-hypothesis- + - name: Set Up Hatch Env + run: | + hatch env create test-rusterize.py${{ matrix.python-version }} + hatch env run -e test-rusterize.py${{ matrix.python-version }} list-env + - name: Run Tests + run: | + hatch env run --env test-rusterize.py${{ matrix.python-version }} run-coverage + + - name: Save cached hypothesis directory + id: save-hypothesis-cache + if: always() + uses: actions/cache/save@v4 + with: + path: .hypothesis/ + key: cache-hypothesis-${{ runner.os }}-${{ github.run_id }} diff --git a/pyproject.toml b/pyproject.toml index c40e420..7f34143 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,6 +37,7 @@ dynamic=["version"] [project.optional-dependencies] dask = ["dask-geopandas"] rasterize = ["rasterio"] +rusterize = ["rusterize"] exactextract = ["exactextract", "sparse"] docs = [ "geodatasets", @@ -90,10 +91,40 @@ test = [ "netCDF4", "pooch", "pytest", + "pytest-cov", + "pytest-xdist", + "rasterio", + "sparse", + "xarray @ git+https://github.com/pydata/xarray.git@26ccc7f8f014f29c551fd566a04d6e9f878c0b0b", # https://github.com/pydata/xarray/pull/10980 +] +test-rusterize = [ + "coverage", + "dask-geopandas", + "exactextract", + "geodatasets", + "hypothesis", + "netCDF4", + "pooch", + "pytest", + "pytest-cov", + "pytest-xdist", + "rusterize", + "sparse", + "xarray @ git+https://github.com/pydata/xarray.git@26ccc7f8f014f29c551fd566a04d6e9f878c0b0b", # https://github.com/pydata/xarray/pull/10980 +] +test-all = [ + "coverage", + "dask-geopandas", + "exactextract", + "geodatasets", + "hypothesis", + "netCDF4", + "pooch", "pytest", "pytest-cov", "pytest-xdist", "rasterio", + "rusterize", "sparse", "xarray @ git+https://github.com/pydata/xarray.git@26ccc7f8f014f29c551fd566a04d6e9f878c0b0b", # https://github.com/pydata/xarray/pull/10980 ] @@ -112,6 +143,32 @@ run-verbose = "run-coverage --verbose" run-mypy = "mypy src" list-env = "pip list" +[tool.hatch.envs.test-rusterize] +dependency-groups = ["test-rusterize"] + +[[tool.hatch.envs.test-rusterize.matrix]] +python = ["3.11", "3.13"] + +[tool.hatch.envs.test-rusterize.scripts] +run-coverage = "pytest -nauto --cov-config=pyproject.toml --cov=pkg --cov-report xml --cov=src --junitxml=junit.xml -o junit_family=legacy" +run-coverage-html = "pytest -nauto --cov-config=pyproject.toml --cov=pkg --cov-report html --cov=src" +run-pytest = "run-coverage --no-cov" +run-verbose = "run-coverage --verbose" +list-env = "pip list" + +[tool.hatch.envs.test-all] +dependency-groups = ["test-all"] + +[[tool.hatch.envs.test-all.matrix]] +python = ["3.13"] + +[tool.hatch.envs.test-all.scripts] +run-coverage = "pytest -nauto --cov-config=pyproject.toml --cov=pkg --cov-report xml --cov=src --junitxml=junit.xml -o junit_family=legacy" +run-coverage-html = "pytest -nauto --cov-config=pyproject.toml --cov=pkg --cov-report html --cov=src" +run-pytest = "run-coverage --no-cov" +run-verbose = "run-coverage --verbose" +list-env = "pip list" + [tool.ruff.lint] # E402: module level import not at top of file # E501: line too long - let black worry about that diff --git a/src/rasterix/rasterize/__init__.py b/src/rasterix/rasterize/__init__.py index e69de29..b3bcab0 100644 --- a/src/rasterix/rasterize/__init__.py +++ b/src/rasterix/rasterize/__init__.py @@ -0,0 +1,5 @@ +# Rasterization API +from .core import geometry_mask, rasterize +from .rasterio import geometry_clip + +__all__ = ["rasterize", "geometry_mask", "geometry_clip"] diff --git a/src/rasterix/rasterize/core.py b/src/rasterix/rasterize/core.py new file mode 100644 index 0000000..e28bf3d --- /dev/null +++ b/src/rasterix/rasterize/core.py @@ -0,0 +1,367 @@ +# Engine-agnostic rasterization API +from __future__ import annotations + +from functools import partial +from typing import TYPE_CHECKING, Any, Literal + +import geopandas as gpd +import numpy as np +import xarray as xr + +from ..utils import get_affine +from .utils import XAXIS, YAXIS, clip_to_bbox, is_in_memory, prepare_for_dask + +if TYPE_CHECKING: + import dask_geopandas + +__all__ = ["rasterize", "geometry_mask"] + +Engine = Literal["rasterio", "rusterize"] + + +def _get_engine(engine: Engine | None) -> Engine: + """Determine which engine to use based on availability.""" + if engine is not None: + # Validate explicitly requested engine + if engine == "rusterize": + try: + import rusterize as _ # noqa: F401 + except ImportError as e: + raise ImportError("rusterize is not installed. Install it with: pip install rusterize") from e + elif engine == "rasterio": + try: + import rasterio as _ # noqa: F401 + except ImportError as e: + raise ImportError("rasterio is not installed. Install it with: pip install rasterio") from e + return engine + + # Auto-detect: prefer rusterize, fall back to rasterio + try: + import rusterize as _ # noqa: F401 + + return "rusterize" + except ImportError: + pass + + try: + import rasterio as _ # noqa: F401 + + return "rasterio" + except ImportError: + pass + + raise ImportError( + "Neither rusterize nor rasterio is installed. " + "Install one with: pip install rusterize OR pip install rasterio" + ) + + +def _get_rasterize_funcs(engine: Engine): + """Get the engine-specific rasterize functions.""" + if engine == "rasterio": + from . import rasterio as engine_module + else: + from . import rusterize as engine_module + + return ( + engine_module.rasterize_geometries, + engine_module.dask_rasterize_wrapper, + ) + + +def _get_mask_funcs(engine: Engine): + """Get the engine-specific geometry_mask functions.""" + if engine == "rasterio": + from . import rasterio as engine_module + else: + from . import rusterize as engine_module + + return ( + engine_module.np_geometry_mask, + engine_module.dask_mask_wrapper, + ) + + +def _normalize_merge_alg(merge_alg: str, engine: Engine) -> Any: + """Normalize merge_alg string to engine-specific value.""" + if engine == "rasterio": + from rasterio.features import MergeAlg + + mapping = { + "replace": MergeAlg.replace, + "add": MergeAlg.add, + } + if merge_alg not in mapping: + raise ValueError(f"Invalid merge_alg {merge_alg!r}. Must be one of: {list(mapping.keys())}") + return mapping[merge_alg] + else: + # rusterize uses different names + mapping = { + "replace": "last", + "add": "sum", + } + if merge_alg not in mapping: + raise ValueError(f"Invalid merge_alg {merge_alg!r}. Must be one of: {list(mapping.keys())}") + return mapping[merge_alg] + + +def replace_values(array: np.ndarray, to, *, from_=0) -> np.ndarray: + """Replace fill values and adjust offsets after dask rasterization.""" + mask = array == from_ + array[~mask] -= 1 + array[mask] = to + return array + + +def rasterize( + obj: xr.Dataset | xr.DataArray, + geometries: gpd.GeoDataFrame | dask_geopandas.GeoDataFrame, + *, + engine: Engine | None = None, + xdim: str = "x", + ydim: str = "y", + all_touched: bool = False, + merge_alg: str = "replace", + geoms_rechunk_size: int | None = None, + clip: bool = False, + **engine_kwargs, +) -> xr.DataArray: + """ + Dask-aware rasterization of geometries. + + Returns a 2D DataArray with integer codes for cells that are within the provided geometries. + + Parameters + ---------- + obj : xr.Dataset or xr.DataArray + Xarray object whose grid to rasterize onto. + geometries : GeoDataFrame + Either a geopandas or dask_geopandas GeoDataFrame. + engine : {"rasterio", "rusterize"} or None + Rasterization engine to use. If None, auto-detects based on installed + packages (prefers rusterize if available, falls back to rasterio). + xdim : str + Name of the "x" dimension on ``obj``. + ydim : str + Name of the "y" dimension on ``obj``. + all_touched : bool + If True, all pixels touched by geometries will be burned in. + If False, only pixels whose center is within the geometry are burned. + merge_alg : {"replace", "add"} + Merge algorithm when geometries overlap. + - "replace": later geometries overwrite earlier ones + - "add": values are summed where geometries overlap + geoms_rechunk_size : int or None + Size to rechunk the geometry array to *after* conversion from dataframe. + clip : bool + If True, clip raster to the bounding box of the geometries. + Ignored for dask-geopandas geometries. + **engine_kwargs + Additional keyword arguments passed to the engine. + For rasterio: ``env`` (rasterio.Env for GDAL configuration). + + Returns + ------- + DataArray + 2D DataArray with geometries "burned in" as integer codes. + + See Also + -------- + rasterio.features.rasterize + rusterize.rusterize + """ + if xdim not in obj.dims or ydim not in obj.dims: + raise ValueError(f"Received {xdim=!r}, {ydim=!r} but obj.dims={tuple(obj.dims)}") + + resolved_engine = _get_engine(engine) + + if clip: + obj = clip_to_bbox(obj, geometries, xdim=xdim, ydim=ydim) + + affine = get_affine(obj, x_dim=xdim, y_dim=ydim) + engine_merge_alg = _normalize_merge_alg(merge_alg, resolved_engine) + + rasterize_geometries, dask_rasterize_wrapper = _get_rasterize_funcs(resolved_engine) + + rasterize_kwargs = dict( + all_touched=all_touched, + merge_alg=engine_merge_alg, + affine=affine, + **engine_kwargs, + ) + + if is_in_memory(obj=obj, geometries=geometries): + geom_array = geometries.to_numpy().squeeze(axis=1) + rasterized = rasterize_geometries( + geom_array.tolist(), + shape=(obj.sizes[ydim], obj.sizes[xdim]), + offset=0, + dtype=np.min_scalar_type(len(geometries)), + fill=len(geometries), + **rasterize_kwargs, + ) + else: + from dask.array import from_array, map_blocks + + map_blocks_args, chunks, geom_array = prepare_for_dask( + obj, + geometries, + xdim=xdim, + ydim=ydim, + geoms_rechunk_size=geoms_rechunk_size, + ) + # DaskGeoDataFrame.len() computes! + num_geoms = geom_array.size + # with dask, we use 0 as a fill value and replace it later + dtype = np.min_scalar_type(num_geoms) + # add 1 to the offset, to account for 0 as fill value + npoffsets = np.cumsum(np.array([0, *geom_array.chunks[0][:-1]])) + 1 + offsets = from_array(npoffsets, chunks=1) + + rasterized = map_blocks( + dask_rasterize_wrapper, + *map_blocks_args, + offsets[:, np.newaxis, np.newaxis], + chunks=((1,) * geom_array.numblocks[0], chunks[YAXIS], chunks[XAXIS]), + meta=np.array([], dtype=dtype), + fill=0, # good identity value for both sum & replace. + **rasterize_kwargs, + dtype_=dtype, + ) + if merge_alg == "replace": + rasterized = rasterized.max(axis=0) + elif merge_alg == "add": + rasterized = rasterized.sum(axis=0) + + # and reduce every other value by 1 + rasterized = rasterized.map_blocks(partial(replace_values, to=num_geoms)) + + return xr.DataArray( + dims=(ydim, xdim), + data=rasterized, + coords=xr.Coordinates( + coords={ + xdim: obj.coords[xdim], + ydim: obj.coords[ydim], + "spatial_ref": obj.spatial_ref, + }, + indexes={xdim: obj.xindexes[xdim], ydim: obj.xindexes[ydim]}, + ), + name="rasterized", + ) + + +def geometry_mask( + obj: xr.Dataset | xr.DataArray, + geometries: gpd.GeoDataFrame | dask_geopandas.GeoDataFrame, + *, + engine: Engine | None = None, + xdim: str = "x", + ydim: str = "y", + all_touched: bool = False, + invert: bool = False, + geoms_rechunk_size: int | None = None, + clip: bool = False, + **engine_kwargs, +) -> xr.DataArray: + """ + Dask-aware geometry masking. + + Creates a boolean mask from geometries. + + Parameters + ---------- + obj : xr.DataArray or xr.Dataset + Xarray object used to extract the grid. + geometries : GeoDataFrame or DaskGeoDataFrame + Geometries used for masking. + engine : {"rasterio", "rusterize"} or None + Rasterization engine to use. If None, auto-detects based on installed + packages (prefers rusterize if available, falls back to rasterio). + xdim : str + Name of the "x" dimension on ``obj``. + ydim : str + Name of the "y" dimension on ``obj``. + all_touched : bool + If True, all pixels touched by geometries will be included in mask. + invert : bool + If True, pixels inside geometries are True (unmasked). + If False (default), pixels inside geometries are False (masked). + geoms_rechunk_size : int or None + Chunksize for geometry dimension of the output. + clip : bool + If True, clip raster to the bounding box of the geometries. + Ignored for dask-geopandas geometries. + **engine_kwargs + Additional keyword arguments passed to the engine. + For rasterio: ``env`` (rasterio.Env for GDAL configuration). + + Returns + ------- + DataArray + 2D boolean DataArray mask. + + See Also + -------- + rasterio.features.geometry_mask + """ + if xdim not in obj.dims or ydim not in obj.dims: + raise ValueError(f"Received {xdim=!r}, {ydim=!r} but obj.dims={tuple(obj.dims)}") + + resolved_engine = _get_engine(engine) + + if clip: + obj = clip_to_bbox(obj, geometries, xdim=xdim, ydim=ydim) + + affine = get_affine(obj, x_dim=xdim, y_dim=ydim) + + np_geometry_mask, dask_mask_wrapper = _get_mask_funcs(resolved_engine) + + geometry_mask_kwargs = dict( + all_touched=all_touched, + affine=affine, + **engine_kwargs, + ) + + if is_in_memory(obj=obj, geometries=geometries): + geom_array = geometries.to_numpy().squeeze(axis=1) + mask = np_geometry_mask( + geom_array.tolist(), + shape=(obj.sizes[ydim], obj.sizes[xdim]), + invert=invert, + **geometry_mask_kwargs, + ) + else: + from dask.array import map_blocks + + map_blocks_args, chunks, geom_array = prepare_for_dask( + obj, + geometries, + xdim=xdim, + ydim=ydim, + geoms_rechunk_size=geoms_rechunk_size, + ) + mask = map_blocks( + dask_mask_wrapper, + *map_blocks_args, + chunks=((1,) * geom_array.numblocks[0], chunks[YAXIS], chunks[XAXIS]), + meta=np.array([], dtype=bool), + **geometry_mask_kwargs, + ) + mask = mask.all(axis=0) + if invert: + mask = ~mask + + return xr.DataArray( + dims=(ydim, xdim), + data=mask, + coords=xr.Coordinates( + coords={ + xdim: obj.coords[xdim], + ydim: obj.coords[ydim], + "spatial_ref": obj.spatial_ref, + }, + indexes={xdim: obj.xindexes[xdim], ydim: obj.xindexes[ydim]}, + ), + name="mask", + ) diff --git a/src/rasterix/rasterize/rasterio.py b/src/rasterix/rasterize/rasterio.py index 9c2459a..a4babb1 100644 --- a/src/rasterix/rasterize/rasterio.py +++ b/src/rasterix/rasterize/rasterio.py @@ -1,35 +1,30 @@ -# rasterio wrappers +# rasterio-specific rasterization helpers from __future__ import annotations import functools from collections.abc import Callable, Sequence -from functools import partial from typing import TYPE_CHECKING, Any, TypeVar -import geopandas as gpd import numpy as np -import rasterio as rio -import xarray as xr from affine import Affine -from rasterio.features import MergeAlg -from rasterio.features import geometry_mask as geometry_mask_rio -from rasterio.features import rasterize as rasterize_rio - -from ..utils import get_affine -from .utils import XAXIS, YAXIS, clip_to_bbox, is_in_memory, prepare_for_dask F = TypeVar("F", bound=Callable[..., Any]) if TYPE_CHECKING: import dask_geopandas + import geopandas as gpd + import rasterio as rio + import xarray as xr + from rasterio.features import MergeAlg -__all__ = ["geometry_mask", "rasterize", "geometry_clip"] +__all__ = ["geometry_clip"] def with_rio_env(func: F) -> F: """ Decorator that handles the 'env' and 'clear_cache' kwargs. """ + import rasterio as rio @functools.wraps(func) def wrapper(*args, **kwargs): @@ -40,8 +35,6 @@ def wrapper(*args, **kwargs): env = rio.Env() with env: - # Remove env and clear_cache from kwargs before calling the wrapped function - # since the function shouldn't handle the context management result = func(*args, **kwargs) if clear_cache: @@ -96,6 +89,8 @@ def rasterize_geometries( clear_cache: bool = False, **kwargs, ): + from rasterio.features import rasterize as rasterize_rio + res = rasterize_rio( zip(geometries, range(offset, offset + len(geometries)), strict=True), out_shape=shape, @@ -106,137 +101,7 @@ def rasterize_geometries( return res -def rasterize( - obj: xr.Dataset | xr.DataArray, - geometries: gpd.GeoDataFrame | dask_geopandas.GeoDataFrame, - *, - xdim="x", - ydim="y", - all_touched: bool = False, - merge_alg: MergeAlg = MergeAlg.replace, - geoms_rechunk_size: int | None = None, - env: rio.Env | None = None, - clip: bool = False, -) -> xr.DataArray: - """ - Dask-aware wrapper around ``rasterio.features.rasterize``. - - Returns a 2D DataArray with integer codes for cells that are within the provided geometries. - - Parameters - ---------- - obj: xr.Dataset or xr.DataArray - Xarray object, whose grid to rasterize - geometries: GeoDataFrame - Either a geopandas or dask_geopandas GeoDataFrame - xdim: str - Name of the "x" dimension on ``obj``. - ydim: str - Name of the "y" dimension on ``obj``. - all_touched: bool = False - Passed to ``rasterio.features.rasterize`` - merge_alg: rasterio.MergeAlg - Passed to ``rasterio.features.rasterize``. - geoms_rechunk_size: int | None = None - Size to rechunk the geometry array to *after* conversion from dataframe. - env: rasterio.Env - Rasterio Environment configuration. For example, use set ``GDAL_CACHEMAX`` - by passing ``env = rio.Env(GDAL_CACHEMAX=100 * 1e6)``. - clip: bool - If True, clip raster to the bounding box of the geometries. - Ignored for dask-geopandas geometries. - - Returns - ------- - DataArray - 2D DataArray with geometries "burned in" - - See Also - -------- - rasterio.features.rasterize - """ - if xdim not in obj.dims or ydim not in obj.dims: - raise ValueError(f"Received {xdim=!r}, {ydim=!r} but obj.dims={tuple(obj.dims)}") - - if clip: - obj = clip_to_bbox(obj, geometries, xdim=xdim, ydim=ydim) - - rasterize_kwargs = dict( - all_touched=all_touched, merge_alg=merge_alg, affine=get_affine(obj, x_dim=xdim, y_dim=ydim), env=env - ) - # FIXME: box.crs == geometries.crs - - if is_in_memory(obj=obj, geometries=geometries): - geom_array = geometries.to_numpy().squeeze(axis=1) - rasterized = rasterize_geometries( - geom_array.tolist(), - shape=(obj.sizes[ydim], obj.sizes[xdim]), - offset=0, - dtype=np.min_scalar_type(len(geometries)), - fill=len(geometries), - **rasterize_kwargs, - ) - else: - from dask.array import from_array, map_blocks - - map_blocks_args, chunks, geom_array = prepare_for_dask( - obj, - geometries, - xdim=xdim, - ydim=ydim, - geoms_rechunk_size=geoms_rechunk_size, - ) - # DaskGeoDataFrame.len() computes! - num_geoms = geom_array.size - # with dask, we use 0 as a fill value and replace it later - dtype = np.min_scalar_type(num_geoms) - # add 1 to the offset, to account for 0 as fill value - npoffsets = np.cumsum(np.array([0, *geom_array.chunks[0][:-1]])) + 1 - offsets = from_array(npoffsets, chunks=1) - - rasterized = map_blocks( - dask_rasterize_wrapper, - *map_blocks_args, - offsets[:, np.newaxis, np.newaxis], - chunks=((1,) * geom_array.numblocks[0], chunks[YAXIS], chunks[XAXIS]), - meta=np.array([], dtype=dtype), - fill=0, # good identity value for both sum & replace. - **rasterize_kwargs, - dtype_=dtype, - ) - if merge_alg is MergeAlg.replace: - rasterized = rasterized.max(axis=0) - elif merge_alg is MergeAlg.add: - rasterized = rasterized.sum(axis=0) - - # and reduce every other value by 1 - rasterized = rasterized.map_blocks(partial(replace_values, to=num_geoms)) - - return xr.DataArray( - dims=(ydim, xdim), - data=rasterized, - coords=xr.Coordinates( - coords={ - xdim: obj.coords[xdim], - ydim: obj.coords[ydim], - "spatial_ref": obj.spatial_ref, - # TODO: figure out how to propagate geometry array - # "geometry": geom_array, - }, - indexes={xdim: obj.xindexes[xdim], ydim: obj.xindexes[ydim]}, - ), - name="rasterized", - ) - - -def replace_values(array: np.ndarray, to, *, from_=0) -> np.ndarray: - mask = array == from_ - array[~mask] -= 1 - array[mask] = to - return array - - -# ===========> geometry_mask +# ===========> geometry_mask helpers def dask_mask_wrapper( @@ -268,117 +133,22 @@ def np_geometry_mask( clear_cache: bool = False, **kwargs, ) -> np.ndarray[Any, np.dtype[np.bool_]]: + from rasterio.features import geometry_mask as geometry_mask_rio + res = geometry_mask_rio(geometries, out_shape=shape, transform=affine, **kwargs) assert res.shape == shape return res -def geometry_mask( - obj: xr.Dataset | xr.DataArray, - geometries: gpd.GeoDataFrame | dask_geopandas.GeoDataFrame, - *, - xdim="x", - ydim="y", - all_touched: bool = False, - invert: bool = False, - geoms_rechunk_size: int | None = None, - env: rio.Env | None = None, - clip: bool = False, -) -> xr.DataArray: - """ - Dask-ified version of ``rasterio.features.geometry_mask`` - - Parameters - ---------- - obj : xr.DataArray | xr.Dataset - Xarray object used to extract the grid - geometries: GeoDataFrame | DaskGeoDataFrame - Geometries used for clipping - xdim: str - Name of the "x" dimension on ``obj``. - ydim: str - Name of the "y" dimension on ``obj`` - all_touched: bool - Passed to rasterio - invert: bool - Whether to preserve values inside the geometry. - geoms_rechunk_size: int | None = None, - Chunksize for geometry dimension of the output. - env: rasterio.Env - Rasterio Environment configuration. For example, use set ``GDAL_CACHEMAX`` - by passing ``env = rio.Env(GDAL_CACHEMAX=100 * 1e6)``. - clip: bool - If True, clip raster to the bounding box of the geometries. - Ignored for dask-geopandas geometries. - - Returns - ------- - DataArray - 3D dataarray with coverage fraction. The additional dimension is "geometry". - - See Also - -------- - rasterio.features.geometry_mask - """ - if xdim not in obj.dims or ydim not in obj.dims: - raise ValueError(f"Received {xdim=!r}, {ydim=!r} but obj.dims={tuple(obj.dims)}") - if clip: - obj = clip_to_bbox(obj, geometries, xdim=xdim, ydim=ydim) - - geometry_mask_kwargs = dict( - all_touched=all_touched, affine=get_affine(obj, x_dim=xdim, y_dim=ydim), env=env - ) - - if is_in_memory(obj=obj, geometries=geometries): - geom_array = geometries.to_numpy().squeeze(axis=1) - mask = np_geometry_mask( - geom_array.tolist(), - shape=(obj.sizes[ydim], obj.sizes[xdim]), - invert=invert, - **geometry_mask_kwargs, - ) - else: - from dask.array import map_blocks - - map_blocks_args, chunks, geom_array = prepare_for_dask( - obj, - geometries, - xdim=xdim, - ydim=ydim, - geoms_rechunk_size=geoms_rechunk_size, - ) - mask = map_blocks( - dask_mask_wrapper, - *map_blocks_args, - chunks=((1,) * geom_array.numblocks[0], chunks[YAXIS], chunks[XAXIS]), - meta=np.array([], dtype=bool), - **geometry_mask_kwargs, - ) - mask = mask.all(axis=0) - if invert: - mask = ~mask - - return xr.DataArray( - dims=(ydim, xdim), - data=mask, - coords=xr.Coordinates( - coords={ - xdim: obj.coords[xdim], - ydim: obj.coords[ydim], - "spatial_ref": obj.spatial_ref, - }, - indexes={xdim: obj.xindexes[xdim], ydim: obj.xindexes[ydim]}, - ), - name="mask", - ) +# ===========> geometry_clip (rasterio-specific) def geometry_clip( obj: xr.Dataset | xr.DataArray, geometries: gpd.GeoDataFrame | dask_geopandas.GeoDataFrame, *, - xdim="x", - ydim="y", + xdim: str = "x", + ydim: str = "y", all_touched: bool = False, invert: bool = False, geoms_rechunk_size: int | None = None, @@ -388,49 +158,55 @@ def geometry_clip( """ Dask-ified version of rioxarray.clip + This function is rasterio-specific. + Parameters ---------- - obj : xr.DataArray | xr.Dataset + obj : xr.DataArray or xr.Dataset Xarray object used to extract the grid - geometries: GeoDataFrame | DaskGeoDataFrame + geometries : GeoDataFrame or DaskGeoDataFrame Geometries used for clipping - xdim: str + xdim : str Name of the "x" dimension on ``obj``. - ydim: str + ydim : str Name of the "y" dimension on ``obj`` - all_touched: bool + all_touched : bool Passed to rasterio - invert: bool + invert : bool Whether to preserve values inside the geometry. - geoms_rechunk_size: int | None = None, + geoms_rechunk_size : int or None Chunksize for geometry dimension of the output. - env: rasterio.Env + env : rasterio.Env Rasterio Environment configuration. For example, use set ``GDAL_CACHEMAX`` by passing ``env = rio.Env(GDAL_CACHEMAX=100 * 1e6)``. - clip: bool - If True, clip raster to the bounding box of the geometries. - Ignored for dask-geopandas geometries. + clip : bool + If True, clip raster to the bounding box of the geometries. + Ignored for dask-geopandas geometries. Returns ------- DataArray - 3D dataarray with coverage fraction. The additional dimension is "geometry". + Clipped DataArray. See Also -------- rasterio.features.geometry_mask """ + from .core import geometry_mask + from .utils import clip_to_bbox + if clip: obj = clip_to_bbox(obj, geometries, xdim=xdim, ydim=ydim) mask = geometry_mask( obj, geometries, + engine="rasterio", all_touched=all_touched, invert=not invert, # rioxarray clip convention -> rasterio geometry_mask convention - env=env, xdim=xdim, ydim=ydim, geoms_rechunk_size=geoms_rechunk_size, clip=False, + env=env, ) return obj.where(mask) diff --git a/src/rasterix/rasterize/rusterize.py b/src/rasterix/rasterize/rusterize.py new file mode 100644 index 0000000..2dc46d7 --- /dev/null +++ b/src/rasterix/rasterize/rusterize.py @@ -0,0 +1,229 @@ +# rusterize-specific rasterization helpers +from __future__ import annotations + +from collections.abc import Sequence +from typing import Any + +import geopandas as gpd +import numpy as np +from affine import Affine +from shapely import Geometry + +__all__: list[str] = [] + + +def _affine_to_extent_and_res( + affine: Affine, shape: tuple[int, int] +) -> tuple[tuple[float, float, float, float], tuple[float, float]]: + """Convert affine transform and shape to extent and resolution for rusterize.""" + nrows, ncols = shape + # affine maps pixel (col, row) to (x, y) + # top-left corner of pixel (0, 0) + xmin = affine.c + ymax = affine.f + xres = affine.a + yres = affine.e # typically negative + + xmax = xmin + ncols * xres + ymin = ymax + nrows * yres + + # Ensure proper ordering + if xmin > xmax: + xmin, xmax = xmax, xmin + if ymin > ymax: + ymin, ymax = ymax, ymin + + return (xmin, ymin, xmax, ymax), (abs(xres), abs(yres)) + + +def rasterize_geometries( + geometries: Sequence[Geometry], + *, + dtype: np.dtype, + shape: tuple[int, int], + affine: Affine, + offset: int, + all_touched: bool = False, + merge_alg: str = "last", + fill: Any = 0, + **kwargs, +) -> np.ndarray: + """ + Rasterize geometries using rusterize. + + Parameters + ---------- + geometries : Sequence[Geometry] + Shapely geometries to rasterize. + dtype : np.dtype + Output data type. + shape : tuple[int, int] + Output shape (nrows, ncols). + affine : Affine + Affine transform for the output grid. + offset : int + Starting value for geometry indices. + all_touched : bool + If True, all pixels touched by geometries will be burned in. + Note: rusterize may not support this parameter directly. + merge_alg : str + Merge algorithm: "last", "sum", "first", "min", "max", "count", "any". + fill : Any + Fill value for pixels not covered by any geometry. + **kwargs + Additional arguments (ignored for compatibility). + + Returns + ------- + np.ndarray + Rasterized array with shape (nrows, ncols). + """ + from rusterize import rusterize + + if all_touched: + raise NotImplementedError( + "all_touched=True is not supported by the rusterize engine. " + "Use engine='rasterio' if you need all_touched support." + ) + + # Create GeoDataFrame with index values + values = list(range(offset, offset + len(geometries))) + gdf = gpd.GeoDataFrame({"value": values}, geometry=list(geometries)) + + extent, (xres, yres) = _affine_to_extent_and_res(affine, shape) + + result = rusterize( + gdf, + res=(xres, yres), + extent=extent, + out_shape=shape, + field="value", + fun=merge_alg, + background=fill, + encoding="numpy", + dtype=str(dtype), + ) + + assert result.shape == shape + return result + + +def dask_rasterize_wrapper( + geom_array: np.ndarray, + x_offsets: np.ndarray, + y_offsets: np.ndarray, + x_sizes: np.ndarray, + y_sizes: np.ndarray, + offset_array: np.ndarray, + *, + fill: Any, + affine: Affine, + all_touched: bool, + merge_alg: str, + dtype_: np.dtype, + **kwargs, +) -> np.ndarray: + """Dask wrapper for rusterize rasterization.""" + offset = offset_array.item() + + return rasterize_geometries( + geom_array[:, 0, 0].tolist(), + affine=affine * affine.translation(x_offsets.item(), y_offsets.item()), + shape=(y_sizes.item(), x_sizes.item()), + offset=offset, + all_touched=all_touched, + merge_alg=merge_alg, + fill=fill, + dtype=dtype_, + )[np.newaxis, :, :] + + +def np_geometry_mask( + geometries: Sequence[Geometry], + *, + shape: tuple[int, int], + affine: Affine, + all_touched: bool = False, + invert: bool = False, + **kwargs, +) -> np.ndarray[Any, np.dtype[np.bool_]]: + """ + Create a geometry mask using rusterize. + + Rasterizes geometries with burn value 1, then converts to boolean mask. + + Parameters + ---------- + geometries : Sequence[Geometry] + Shapely geometries for masking. + shape : tuple[int, int] + Output shape (nrows, ncols). + affine : Affine + Affine transform for the output grid. + all_touched : bool + If True, all pixels touched by geometries will be included. + Note: rusterize may not support this parameter directly. + invert : bool + If True, pixels inside geometries are True (unmasked). + If False (default), pixels inside geometries are False (masked). + **kwargs + Additional arguments (ignored for compatibility). + + Returns + ------- + np.ndarray + Boolean mask array with shape (nrows, ncols). + """ + from rusterize import rusterize + + if all_touched: + raise NotImplementedError( + "all_touched=True is not supported by the rusterize engine. " + "Use engine='rasterio' if you need all_touched support." + ) + + # Create GeoDataFrame with burn value + gdf = gpd.GeoDataFrame(geometry=list(geometries)) + + extent, (xres, yres) = _affine_to_extent_and_res(affine, shape) + + result = rusterize( + gdf, + res=(xres, yres), + extent=extent, + out_shape=shape, + burn=1, + fun="any", + background=0, + encoding="numpy", + dtype="uint8", + ) + + # Convert to boolean mask + # rasterio convention: True = outside geometry (masked), False = inside geometry + # invert=True flips this + inside = result > 0 + if invert: + return inside + else: + return ~inside + + +def dask_mask_wrapper( + geom_array: np.ndarray, + x_offsets: np.ndarray, + y_offsets: np.ndarray, + x_sizes: np.ndarray, + y_sizes: np.ndarray, + *, + affine: Affine, + **kwargs, +) -> np.ndarray[Any, np.dtype[np.bool_]]: + """Dask wrapper for rusterize geometry masking.""" + res = np_geometry_mask( + geom_array[:, 0, 0].tolist(), + shape=(y_sizes.item(), x_sizes.item()), + affine=affine * affine.translation(x_offsets.item(), y_offsets.item()), + **kwargs, + ) + return res[np.newaxis, :, :] diff --git a/src/rasterix/rasterize/utils.py b/src/rasterix/rasterize/utils.py index 9042ea5..bd36853 100644 --- a/src/rasterix/rasterize/utils.py +++ b/src/rasterix/rasterize/utils.py @@ -6,7 +6,6 @@ import geopandas as gpd import numpy as np import xarray as xr -from affine import Affine if TYPE_CHECKING: import dask.array @@ -34,20 +33,6 @@ def clip_to_bbox( return obj -def get_affine(obj: xr.Dataset | xr.DataArray, *, xdim="x", ydim="y") -> Affine: - spatial_ref = obj.coords["spatial_ref"] - if "GeoTransform" in spatial_ref.attrs: - return Affine.from_gdal(*map(float, spatial_ref.attrs["GeoTransform"].split(" "))) - else: - x = obj.coords[xdim] - y = obj.coords[ydim] - dx = (x[1] - x[0]).item() - dy = (y[1] - y[0]).item() - return Affine.translation( - x[0].item() - dx / 2, (y[0] if dy < 0 else y[-1]).item() - dy / 2 - ) * Affine.scale(dx, dy) - - def is_in_memory(*, obj, geometries) -> bool: return not obj.chunks and isinstance(geometries, gpd.GeoDataFrame) diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..0a6eb20 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,36 @@ +import pytest + + +def _engine_available(engine: str) -> bool: + """Check if a rasterization engine is available.""" + if engine == "rasterio": + try: + import rasterio # noqa: F401 + + return True + except ImportError: + return False + elif engine == "rusterize": + try: + import rusterize # noqa: F401 + + return True + except ImportError: + return False + return False + + +def pytest_generate_tests(metafunc): + """Dynamically parametrize tests that use the 'engine' fixture.""" + if "engine" in metafunc.fixturenames: + engines = [] + # Only add engines that are available + if _engine_available("rasterio"): + engines.append("rasterio") + if _engine_available("rusterize"): + engines.append("rusterize") + + if not engines: + pytest.skip("No rasterization engine available (need rasterio or rusterize)") + + metafunc.parametrize("engine", engines) diff --git a/tests/test_rasterize.py b/tests/test_rasterize.py index b6061df..5b4ee02 100644 --- a/tests/test_rasterize.py +++ b/tests/test_rasterize.py @@ -6,7 +6,8 @@ import xproj # noqa from xarray.tests import raise_if_dask_computes -from rasterix.rasterize.rasterio import geometry_mask, rasterize +from rasterix.rasterize import geometry_mask, rasterize +from rasterix.rasterize.rasterio import geometry_clip @pytest.fixture @@ -18,7 +19,7 @@ def dataset(): @pytest.mark.parametrize("clip", [False, True]) -def test_rasterize(clip, dataset): +def test_rasterize(clip, engine, dataset): fname = "rasterize_snapshot.nc" try: snapshot = xr.load_dataarray(fname) @@ -29,7 +30,7 @@ def test_rasterize(clip, dataset): snapshot = snapshot.sel(latitude=slice(83.25, None)) world = gpd.read_file(geodatasets.get_path("naturalearth land")) - kwargs = dict(xdim="longitude", ydim="latitude", clip=clip) + kwargs = dict(xdim="longitude", ydim="latitude", clip=clip, engine=engine) rasterized = rasterize(dataset, world[["geometry"]], **kwargs) xr.testing.assert_identical(rasterized, snapshot) @@ -48,7 +49,7 @@ def test_rasterize(clip, dataset): @pytest.mark.parametrize("invert", [False, True]) @pytest.mark.parametrize("clip", [False, True]) -def test_geometry_mask(clip, invert, dataset): +def test_geometry_mask(clip, invert, engine, dataset): fname = "geometry_mask_snapshot.nc" try: snapshot = xr.load_dataarray(fname) @@ -61,7 +62,7 @@ def test_geometry_mask(clip, invert, dataset): world = gpd.read_file(geodatasets.get_path("naturalearth land")) - kwargs = dict(xdim="longitude", ydim="latitude", clip=clip, invert=invert) + kwargs = dict(xdim="longitude", ydim="latitude", clip=clip, invert=invert, engine=engine) rasterized = geometry_mask(dataset, world[["geometry"]], **kwargs) xr.testing.assert_identical(rasterized, snapshot) @@ -76,3 +77,14 @@ def test_geometry_mask(clip, invert, dataset): with raise_if_dask_computes(): drasterized = geometry_mask(chunked, dask_geoms[["geometry"]], **kwargs) xr.testing.assert_identical(drasterized, snapshot) + + +# geometry_clip is rasterio-specific +def test_geometry_clip(dataset): + pytest.importorskip("rasterio") + + world = gpd.read_file(geodatasets.get_path("naturalearth land")) + clipped = geometry_clip(dataset, world[["geometry"]], xdim="longitude", ydim="latitude") + assert clipped is not None + # Basic check that clipping worked - masked values outside geometries + assert clipped["u"].isnull().any() From d8260ce3a97c0356ebc74408b0ad7b33f95c7199 Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Fri, 23 Jan 2026 16:52:20 -0700 Subject: [PATCH 2/5] fix test --- src/rasterix/rasterize/rasterio.py | 3 ++- tests/test_rasterize.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/rasterix/rasterize/rasterio.py b/src/rasterix/rasterize/rasterio.py index a4babb1..9a48dae 100644 --- a/src/rasterix/rasterize/rasterio.py +++ b/src/rasterix/rasterize/rasterio.py @@ -24,7 +24,6 @@ def with_rio_env(func: F) -> F: """ Decorator that handles the 'env' and 'clear_cache' kwargs. """ - import rasterio as rio @functools.wraps(func) def wrapper(*args, **kwargs): @@ -32,6 +31,8 @@ def wrapper(*args, **kwargs): clear_cache = kwargs.pop("clear_cache", False) if env is None: + import rasterio as rio + env = rio.Env() with env: diff --git a/tests/test_rasterize.py b/tests/test_rasterize.py index 5b4ee02..0918d21 100644 --- a/tests/test_rasterize.py +++ b/tests/test_rasterize.py @@ -7,7 +7,6 @@ from xarray.tests import raise_if_dask_computes from rasterix.rasterize import geometry_mask, rasterize -from rasterix.rasterize.rasterio import geometry_clip @pytest.fixture @@ -83,6 +82,8 @@ def test_geometry_mask(clip, invert, engine, dataset): def test_geometry_clip(dataset): pytest.importorskip("rasterio") + from rasterix.rasterize.rasterio import geometry_clip + world = gpd.read_file(geodatasets.get_path("naturalearth land")) clipped = geometry_clip(dataset, world[["geometry"]], xdim="longitude", ydim="latitude") assert clipped is not None From 11e1a26aa9dbcc40e21ce8372dcbd23a3242210a Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Fri, 23 Jan 2026 17:06:04 -0700 Subject: [PATCH 3/5] Make geometry_clip engine-generic Move geometry_clip from rasterio.py to core.py and add engine parameter to support both rasterio and rusterize engines. The function now follows the same pattern as rasterize and geometry_mask. Co-Authored-By: Claude Opus 4.5 --- src/rasterix/rasterize/__init__.py | 3 +- src/rasterix/rasterize/core.py | 75 +++++++++++++++++++++++++++- src/rasterix/rasterize/rasterio.py | 79 ++---------------------------- tests/test_rasterize.py | 11 ++--- 4 files changed, 81 insertions(+), 87 deletions(-) diff --git a/src/rasterix/rasterize/__init__.py b/src/rasterix/rasterize/__init__.py index b3bcab0..3cac345 100644 --- a/src/rasterix/rasterize/__init__.py +++ b/src/rasterix/rasterize/__init__.py @@ -1,5 +1,4 @@ # Rasterization API -from .core import geometry_mask, rasterize -from .rasterio import geometry_clip +from .core import geometry_clip, geometry_mask, rasterize __all__ = ["rasterize", "geometry_mask", "geometry_clip"] diff --git a/src/rasterix/rasterize/core.py b/src/rasterix/rasterize/core.py index e28bf3d..f496013 100644 --- a/src/rasterix/rasterize/core.py +++ b/src/rasterix/rasterize/core.py @@ -14,7 +14,7 @@ if TYPE_CHECKING: import dask_geopandas -__all__ = ["rasterize", "geometry_mask"] +__all__ = ["rasterize", "geometry_mask", "geometry_clip"] Engine = Literal["rasterio", "rusterize"] @@ -365,3 +365,76 @@ def geometry_mask( ), name="mask", ) + + +def geometry_clip( + obj: xr.Dataset | xr.DataArray, + geometries: gpd.GeoDataFrame | dask_geopandas.GeoDataFrame, + *, + engine: Engine | None = None, + xdim: str = "x", + ydim: str = "y", + all_touched: bool = False, + invert: bool = False, + geoms_rechunk_size: int | None = None, + clip: bool = True, + **engine_kwargs, +) -> xr.DataArray: + """ + Dask-aware geometry clipping. + + Clips an xarray object to geometries by masking values outside the geometries. + + Parameters + ---------- + obj : xr.DataArray or xr.Dataset + Xarray object to clip. + geometries : GeoDataFrame or DaskGeoDataFrame + Geometries used for clipping. + engine : {"rasterio", "rusterize"} or None + Rasterization engine to use. If None, auto-detects based on installed + packages (prefers rusterize if available, falls back to rasterio). + xdim : str + Name of the "x" dimension on ``obj``. + ydim : str + Name of the "y" dimension on ``obj``. + all_touched : bool + If True, all pixels touched by geometries will be included. + invert : bool + If True, preserve values outside the geometry (invert the clip). + If False (default), preserve values inside the geometry. + geoms_rechunk_size : int or None + Chunksize for geometry dimension of the output. + clip : bool + If True, clip raster to the bounding box of the geometries. + Ignored for dask-geopandas geometries. + **engine_kwargs + Additional keyword arguments passed to the engine. + For rasterio: ``env`` (rasterio.Env for GDAL configuration). + + Returns + ------- + DataArray + Clipped DataArray with values outside geometries set to NaN. + + See Also + -------- + geometry_mask + rasterio.features.geometry_mask + """ + if clip: + obj = clip_to_bbox(obj, geometries, xdim=xdim, ydim=ydim) + + mask = geometry_mask( + obj, + geometries, + engine=engine, + all_touched=all_touched, + invert=not invert, # rioxarray clip convention -> geometry_mask convention + xdim=xdim, + ydim=ydim, + geoms_rechunk_size=geoms_rechunk_size, + clip=False, + **engine_kwargs, + ) + return obj.where(mask) diff --git a/src/rasterix/rasterize/rasterio.py b/src/rasterix/rasterize/rasterio.py index 9a48dae..c4a0252 100644 --- a/src/rasterix/rasterize/rasterio.py +++ b/src/rasterix/rasterize/rasterio.py @@ -11,13 +11,10 @@ F = TypeVar("F", bound=Callable[..., Any]) if TYPE_CHECKING: - import dask_geopandas - import geopandas as gpd import rasterio as rio - import xarray as xr from rasterio.features import MergeAlg -__all__ = ["geometry_clip"] +__all__: list[str] = [] def with_rio_env(func: F) -> F: @@ -36,6 +33,8 @@ def wrapper(*args, **kwargs): env = rio.Env() with env: + # Remove env and clear_cache from kwargs before calling the wrapped function + # since the function shouldn't handle the context management result = func(*args, **kwargs) if clear_cache: @@ -139,75 +138,3 @@ def np_geometry_mask( res = geometry_mask_rio(geometries, out_shape=shape, transform=affine, **kwargs) assert res.shape == shape return res - - -# ===========> geometry_clip (rasterio-specific) - - -def geometry_clip( - obj: xr.Dataset | xr.DataArray, - geometries: gpd.GeoDataFrame | dask_geopandas.GeoDataFrame, - *, - xdim: str = "x", - ydim: str = "y", - all_touched: bool = False, - invert: bool = False, - geoms_rechunk_size: int | None = None, - env: rio.Env | None = None, - clip: bool = True, -) -> xr.DataArray: - """ - Dask-ified version of rioxarray.clip - - This function is rasterio-specific. - - Parameters - ---------- - obj : xr.DataArray or xr.Dataset - Xarray object used to extract the grid - geometries : GeoDataFrame or DaskGeoDataFrame - Geometries used for clipping - xdim : str - Name of the "x" dimension on ``obj``. - ydim : str - Name of the "y" dimension on ``obj`` - all_touched : bool - Passed to rasterio - invert : bool - Whether to preserve values inside the geometry. - geoms_rechunk_size : int or None - Chunksize for geometry dimension of the output. - env : rasterio.Env - Rasterio Environment configuration. For example, use set ``GDAL_CACHEMAX`` - by passing ``env = rio.Env(GDAL_CACHEMAX=100 * 1e6)``. - clip : bool - If True, clip raster to the bounding box of the geometries. - Ignored for dask-geopandas geometries. - - Returns - ------- - DataArray - Clipped DataArray. - - See Also - -------- - rasterio.features.geometry_mask - """ - from .core import geometry_mask - from .utils import clip_to_bbox - - if clip: - obj = clip_to_bbox(obj, geometries, xdim=xdim, ydim=ydim) - mask = geometry_mask( - obj, - geometries, - engine="rasterio", - all_touched=all_touched, - invert=not invert, # rioxarray clip convention -> rasterio geometry_mask convention - xdim=xdim, - ydim=ydim, - geoms_rechunk_size=geoms_rechunk_size, - clip=False, - env=env, - ) - return obj.where(mask) diff --git a/tests/test_rasterize.py b/tests/test_rasterize.py index 0918d21..1f7c5e8 100644 --- a/tests/test_rasterize.py +++ b/tests/test_rasterize.py @@ -6,7 +6,7 @@ import xproj # noqa from xarray.tests import raise_if_dask_computes -from rasterix.rasterize import geometry_mask, rasterize +from rasterix.rasterize import geometry_clip, geometry_mask, rasterize @pytest.fixture @@ -78,14 +78,9 @@ def test_geometry_mask(clip, invert, engine, dataset): xr.testing.assert_identical(drasterized, snapshot) -# geometry_clip is rasterio-specific -def test_geometry_clip(dataset): - pytest.importorskip("rasterio") - - from rasterix.rasterize.rasterio import geometry_clip - +def test_geometry_clip(engine, dataset): world = gpd.read_file(geodatasets.get_path("naturalearth land")) - clipped = geometry_clip(dataset, world[["geometry"]], xdim="longitude", ydim="latitude") + clipped = geometry_clip(dataset, world[["geometry"]], xdim="longitude", ydim="latitude", engine=engine) assert clipped is not None # Basic check that clipping worked - masked values outside geometries assert clipped["u"].isnull().any() From 8958a52a003e24c97ce188fee601aaee00aaa635 Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Fri, 23 Jan 2026 20:39:02 -0700 Subject: [PATCH 4/5] Fixes --- src/rasterix/rasterize/core.py | 5 +++++ src/rasterix/rasterize/rusterize.py | 15 +++++++++++++-- tests/test_rasterize.py | 8 ++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/rasterix/rasterize/core.py b/src/rasterix/rasterize/core.py index f496013..c0fdbfa 100644 --- a/src/rasterix/rasterize/core.py +++ b/src/rasterix/rasterize/core.py @@ -165,6 +165,11 @@ def rasterize( DataArray 2D DataArray with geometries "burned in" as integer codes. + Notes + ----- + Different engines may produce slightly different results at pixel boundaries + due to differences in how they handle geometry-pixel intersection tests. + See Also -------- rasterio.features.rasterize diff --git a/src/rasterix/rasterize/rusterize.py b/src/rasterix/rasterize/rusterize.py index 2dc46d7..8e28afe 100644 --- a/src/rasterix/rasterize/rusterize.py +++ b/src/rasterix/rasterize/rusterize.py @@ -87,8 +87,10 @@ def rasterize_geometries( ) # Create GeoDataFrame with index values + # Dummy CRS required by rusterize but not used by the algorithm + # https://github.com/ttrotto/rusterize/issues/10 values = list(range(offset, offset + len(geometries))) - gdf = gpd.GeoDataFrame({"value": values}, geometry=list(geometries)) + gdf = gpd.GeoDataFrame({"value": values}, geometry=list(geometries), crs="EPSG:4326") extent, (xres, yres) = _affine_to_extent_and_res(affine, shape) @@ -104,6 +106,9 @@ def rasterize_geometries( dtype=str(dtype), ) + # rusterize returns (1, nrows, ncols), squeeze to (nrows, ncols) if needed + if result.ndim == 3 and result.shape[0] == 1: + result = result.squeeze(axis=0) assert result.shape == shape return result @@ -183,7 +188,9 @@ def np_geometry_mask( ) # Create GeoDataFrame with burn value - gdf = gpd.GeoDataFrame(geometry=list(geometries)) + # Dummy CRS required by rusterize but not used by the algorithm + # https://github.com/ttrotto/rusterize/issues/10 + gdf = gpd.GeoDataFrame(geometry=list(geometries), crs="EPSG:4326") extent, (xres, yres) = _affine_to_extent_and_res(affine, shape) @@ -199,6 +206,10 @@ def np_geometry_mask( dtype="uint8", ) + # rusterize returns (1, nrows, ncols), squeeze to (nrows, ncols) if needed + if result.ndim == 3 and result.shape[0] == 1: + result = result.squeeze(axis=0) + # Convert to boolean mask # rasterio convention: True = outside geometry (masked), False = inside geometry # invert=True flips this diff --git a/tests/test_rasterize.py b/tests/test_rasterize.py index 1f7c5e8..0648215 100644 --- a/tests/test_rasterize.py +++ b/tests/test_rasterize.py @@ -19,7 +19,9 @@ def dataset(): @pytest.mark.parametrize("clip", [False, True]) def test_rasterize(clip, engine, dataset): - fname = "rasterize_snapshot.nc" + # Use engine-specific snapshots due to minor pixel boundary differences + suffix = f"_{engine}" if engine == "rusterize" else "" + fname = f"rasterize_snapshot{suffix}.nc" try: snapshot = xr.load_dataarray(fname) except FileNotFoundError: @@ -49,7 +51,9 @@ def test_rasterize(clip, engine, dataset): @pytest.mark.parametrize("invert", [False, True]) @pytest.mark.parametrize("clip", [False, True]) def test_geometry_mask(clip, invert, engine, dataset): - fname = "geometry_mask_snapshot.nc" + # Use engine-specific snapshots due to minor pixel boundary differences + suffix = f"_{engine}" if engine == "rusterize" else "" + fname = f"geometry_mask_snapshot{suffix}.nc" try: snapshot = xr.load_dataarray(fname) except FileNotFoundError: From c3f1a1cd1da9eef5712cd3571154b47846a61887 Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Sat, 24 Jan 2026 14:14:28 -0700 Subject: [PATCH 5/5] Add snapshots --- tests/geometry_mask_snapshot_rusterize.nc | Bin 0 -> 132368 bytes tests/rasterize_snapshot_rusterize.nc | Bin 0 -> 132368 bytes 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/geometry_mask_snapshot_rusterize.nc create mode 100644 tests/rasterize_snapshot_rusterize.nc diff --git a/tests/geometry_mask_snapshot_rusterize.nc b/tests/geometry_mask_snapshot_rusterize.nc new file mode 100644 index 0000000000000000000000000000000000000000..7834ab92b7d71407fd62a3c007e86ba4d6aa5867 GIT binary patch literal 132368 zcmeHO3y@t^mA#!#LI{aOfnYS6(1J$N#B|aS!pQhKoz6#_gi4c{SQ+eT=_WL$Kh@o! zSX8C4aBy(6;s+=#EG-{y2LeuGce*LWrUd!Mw|6NDNxujgs(b4f^ zhR^c^?>v_fKY#5Sk0)B|oxj98{|LMnEhS6m#aw^<2*&@=?YplU*fX?y@1EhI{=UHhZ`p;U@AMY&4SV{o8eslQegDH#lZRO6 zJPO^3f6oI#gcb<5k57z`>e#TAUdtMUx6?Zd1yM;m#t$FxpSmLXh*6jagZ*rA!RvDx9Vk=eOr>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