Skip to content

rasterize: resolution= argument not honored when bounds don't divide evenly #2573

@brendancol

Description

@brendancol

Describe the bug

When resolution= is passed to rasterize(), width and height come from ceil((xmax - xmin) / x_res), but output coordinates are then rebuilt over the original bounds. If the bounds don't divide evenly by the resolution, the actual cell size ends up smaller than what was asked for.

Example:

from shapely.geometry import box
from xrspatial.rasterize import rasterize

r = rasterize([(box(0,0,1,1), 1.0)], resolution=0.3, bounds=(0,0,1,1), fill=0)
# shape (4, 4); actual cell size = (1 - 0) / 4 = 0.25, not 0.3
print(r.x.values[1] - r.x.values[0])  # 0.25

Caller asked for 0.3-unit pixels, got 0.25-unit pixels.

Expected behavior

Either honor resolution= exactly (expand the output bounds so each cell is genuinely 0.3 units; matches rasterio / GDAL from_bounds / from_origin semantics), or document the parameter as a maximum cell size with a note that the actual size may shrink to fit the input bounds.

Code location

  • xrspatial/rasterize.py:3223 -- width/height computed with ceil(bounds / resolution)
  • xrspatial/rasterize.py:3352 -- output coords rebuilt over original xmin..xmax

Proposed fix

Take the first path. When resolution= drives width/height, set xmax_out = xmin + final_width * x_res and ymax_out = ymin + final_height * y_res before building coords. Existing tests with bounds that divide evenly pass unchanged.

Metadata

Metadata

Assignees

No one assigned

    Labels

    apiAPI design and consistencybugSomething 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