Skip to content

rasterize(like=...) silently flips spatial meaning when y coordinates ascend #2170

@brendancol

Description

@brendancol

Describe the bug

rasterize(like=...) ignores the y-axis orientation of the template. The rasterizer always burns with row 0 = ymax (standard image convention), then the output reuses like.y verbatim. When like.y is ascending, the y coords no longer match where the geometry got burned, so anything that selects by world coordinate downstream (zonal stats, spatial joins, plotting) silently lines up against the wrong rows.

_extract_grid_from_like() at xrspatial/rasterize.py:1957 takes min/max of the coords, so the bounding box is fine. The orientation just never makes it through the pipeline, and the coord assignment at xrspatial/rasterize.py:2276 hands back the original coords without flipping the array to match.

Reproduce

import numpy as np
import xarray as xr
from shapely.geometry import box
from xrspatial.rasterize import rasterize

x = np.array([0.5, 1.5])
y_desc = np.array([1.5, 0.5])  # standard top-down y
y_asc  = np.array([0.5, 1.5])  # ascending y (bottom-up)

like_desc = xr.DataArray(np.zeros((2, 2)), dims=['y', 'x'], coords={'y': y_desc, 'x': x})
like_asc  = xr.DataArray(np.zeros((2, 2)), dims=['y', 'x'], coords={'y': y_asc,  'x': x})

geom = [(box(0, 0, 1, 1), 1.0)]  # world y in [0, 1] -> lower row

print(rasterize(geom, like=like_desc, fill=0).sel(y=0.5, x=0.5).item())  # 1.0  ok
print(rasterize(geom, like=like_asc,  fill=0).sel(y=0.5, x=0.5).item())  # 0.0  wrong

Expected behavior

result.sel(y=0.5) should return the burned value whether like.y is ascending or descending. output.y should still equal like.y so xr.align keeps working.

Scope

Ascending-y only. There's a separate sibling issue for irregular like grids (non-uniform spacing) that should stay out of this fix.

Backends

All four (numpy, cupy, dask+numpy, dask+cupy) hit the same coord assignment path, so all four are affected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions