Honor requested resolution exactly in from_template() (#3494)#3495
Merged
Conversation
Nudge the far edges of the study-area box to an exact multiple of the cell size (anchor lower-left) so attrs['res'] matches the requested resolution instead of drifting when the bbox isn't a whole number of cells. Mirrors the reproject grid path in _compute_output_grid.
brendancol
commented
Jun 25, 2026
brendancol
left a comment
Contributor
Author
There was a problem hiding this comment.
PR Review: Honor requested resolution exactly in from_template() (#3494)
Blockers (must fix before merge)
None.
Suggestions (should fix, not blocking)
- The new exact-resolution tests only cover projected-CRS regions (nyc, conus) and the preserve path. Country codes come back in EPSG:4326 and run through the same nudge math, but nothing pins exact
resfor a 4326 template. A one-liner likefrom_template('FRA', resolution=0.25).attrs['res'] == (0.25, 0.25)would close that gap. (xrspatial/tests/test_templates.py)
Nits (optional improvements)
- The dask backend's exact
resis covered indirectly bytest_dask_numpy_backend(it assertsattrs == numpy ref). An explicit assertion would document the intent, but it's optional. (xrspatial/tests/test_templates.py:139)
What looks good
- The fix mirrors the existing reproject grid path (
_compute_output_gridinreproject/_grid.pylines 457-459), so the two paths now agree on how resolution is honored. - The change sits in the shared grid math before
_make_data, so all four backends get identical coords andreswith no per-backend code. - Pixel centers stay inside the original registry bbox: the center sits res/2 inward, which absorbs the half-cell nudge. Checked both axes.
- I checked the EPSG:4326 country path for latitude/longitude overflow from the nudge. Latitude never gets near 90 (the northernmost bbox top is ~83.6 for GRL), and the lon>180 values come from pre-existing antimeridian-wrapped bboxes, not from the nudge. No new invalid-coordinate risk.
- Docstring updated to describe the nudge and the lower-left anchor; autosummary picks it up.
Checklist
- Algorithm matches the reproject grid convention
- All four backends produce consistent results (shared pre-allocation math)
- NaN handling unchanged (fill path untouched)
- Edge cases covered (tuple res, centers within bbox)
- No dask chunk concern (change is before array allocation)
- No premature materialization
- No benchmark needed (constant-time arithmetic)
- No README change needed (no new function, no backend change)
- Docstring present and accurate
brendancol
commented
Jun 25, 2026
brendancol
left a comment
Contributor
Author
There was a problem hiding this comment.
Follow-up review (after 0dc8a4e)
Both findings from the first pass are addressed:
- Suggestion (4326 exact-res test): fixed. Added
test_country_resolution_honored_exactly, assertingfrom_template('FRA', resolution=0.25).attrs['res'] == (0.25, 0.25). - Nit (explicit dask exact-res): fixed.
test_dask_numpy_backendnow asserts the dask+numpy path returnsres == (10.0, 10.0).
Full test_templates.py suite passes (45 tests). No new findings.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
from_template(name, resolution=r)now honors the requested resolution exactly. The previous code held the template's bbox fixed and recomputed the realized cell size from it, soresdrifted when the extent was not a whole number of cells (e.g.from_template('nyc', resolution=10)returnedres=(10.0, 10.000757)).attrs['res']matches the requested resolution._compute_output_gridinreproject/_grid.py).Closes #3494
Backends
Backend-agnostic: the change is in the shared grid math before the array is allocated, so numpy, cupy, dask+numpy, and dask+cupy all get the same exact
res.Test plan
from_template('nyc', resolution=10).attrs['res'] == (10.0, 10.0)