You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@sedona.apache.org by "jiayuasu (via GitHub)" <gi...@apache.org> on 2026/09/29 21:53:26 UTC
[PR] [GH-3425] Align rasterization boundary handling with GDAL's half-open cells [sedona]
jiayuasu opened a new pull request, #3428:
URL: https://github.com/apache/sedona/pull/3428
## Did you read the Contributor Guide?
- Yes, I have read the [Contributor Rules](https://sedona.apache.org/latest/community/rule/) and [Contributor Development Guide](https://sedona.apache.org/latest/community/develop/)
## Is this PR related to a ticket?
- Yes, and the PR name follows the format `[GH-XXX] my subject`. Closes #3425
## What changes were proposed in this PR?
GDAL treats every cell as half-open, with or without `all_touched`: a cell contains its first edges in pixel order but not its last, so a geometry never burns a cell it touches only on its boundary. Sedona already follows GDAL for rasterization (#3120, `test_rasterize_parity.py`), but still used a closed rule in a few places. This aligns them, in `Rasterization.java`:
- **Points** burn the one half-open cell that contains them, whatever `allTouched` is. A point on a grid line or corner used to burn up to four cells; a point on the raster's right or bottom edge used to burn the edge cell and now burns nothing. The cell is computed exactly in the raster's own pixel order, so on a bottom-up raster a point on a horizontal grid line falls in the cell above it, as in GDAL. A point's extent (and cropped output) is that cell.
- **Grid-aligned polygons with `allTouched`** no longer burn the column to their right and the row below them. `rasterizeGeomExtent` widened the window by a pixel on any side lying on a grid line, and the outline traversal burned the half-open cell after an outline edge on a grid line. The widening now applies only to lines (where it sizes the window for the cells the line traversal burns), and an outline edge lying on a grid line burns the cell on the polygon's side, found from the ring orientation.
- **A polygon that only touches the raster** burns nothing: the points and lines its clip leaves behind are boundary contact and are dropped, with or without `allTouched`.
A geometry can therefore cover no cell of the raster. Behaviour in that case:
| | Before | After |
|---|---|---|
| `RS_AsRaster(..., useGeometryExtent = true)` | `NullPointerException` (polygons) or a burned edge cell | `null`: there is no extent to crop to |
| `RS_AsRaster(..., useGeometryExtent = false)`, `RS_ZonalStats*` | edge cell burned / counted with `allTouched` | nothing burned, count 0 |
| `RS_SetValues` | edge cell set | raster unchanged |
| `RS_Clip` | edge cell kept | all nodata; with `crop = true`, `null` if `lenient`, else an error (as for a geometry that does not intersect) |
A mixed `GeometryCollection`'s window is now the union of its members' windows, so a cropped output keeps every member's cells.
### What users will see
- Points on grid lines, corners or the raster's far edges burn one cell (or none) instead of up to four.
- With `allTouched = true`, zones whose edges lie on grid lines no longer include the pixels just beyond their right and bottom edges. In `rasteralgebraTest`, `RS_ZonalStats(..., 'count', true, true)` for a zone with grid-aligned right and bottom edges drops from 14655 to 14366 (190 + 100 - 1 cells); GDAL counts 14357, and the remaining nine cells are a separate difference along a steep edge (below).
- Per-tile zonal counts of a zone that ends on a tile boundary now add up to the count on one raster covering both tiles; before, the neighbouring tile also counted the edge column.
- Cropped outputs lose the empty row or column the widening used to add.
## How was this patch tested?
- **GDAL comparison, grid-aligned.** A corpus of 526 cases, generated with rasterio 1.5.1 / GDAL 3.12.4 on unit, bottom-up, decimal, anisotropic and 1/3600-degree grids (points on lines, corners and every edge; grid-aligned boxes, L shapes, holes; polygons past or touching each edge and corner; lines; and the inputs of the tests changed below), replayed through `RS_AsRaster` at full extent and cropped:
| GDAL mismatches (full / cropped) | master | this PR |
|---|---|---|
| points | 91 / 95 | 18 / 18 |
| polygons | 72 / 134 | 35 / 34 |
| lines (unchanged path) | 34 / 36 | 34 / 36 |
| cases that matched on master and no longer match | | **0** |
The remaining point mismatches are all on the 1/3600-degree grid, where GDAL rounds a coordinate that lies on a grid line in floating point while Sedona resolves it with the same exact decimal arithmetic it already uses to snap extents; both burn exactly one cell. The remaining polygon and line mismatches are pre-existing and unchanged: pixel centres exactly on an edge with `allTouched = false`, GDAL's scan-order burn of some cells touched only at a vertex (which #3120 deliberately does not follow), and line endpoints on grid lines.
- **Existing expectations** that encoded the closed rule (#3414's corner and grazing cases, point extents, cropped arrays, `RS_SetValues` points, the zonal count above) were updated only after checking GDAL's answer for the same inputs.
- **New tests** in `RasterizationTest`: points on lines, corners and edges on north-up and bottom-up rasters (full and cropped); grid-aligned box, L shape and polygon with a hole; polygons touching each edge and corner through `RS_AsRaster`, zonal stats, `RS_SetValues` and `RS_Clip`; a zone on the boundary between two tiles; a cropped mixed collection.
- **`test_rasterize_parity.py`**: new grid-aligned test (21 geometries x 3 grids x 2 modes, compared with rasterio). I could not run PySpark locally, so I replayed the same 126 inputs through the Java code; all match GDAL at full extent and cropped.
- All tests under `org.apache.sedona.common.raster`: 506 run, 0 failures. Spark `rasteralgebraTest` and `rasterIOTest`: 192 succeeded, 0 failed. The existing 211-case random parity corpus: 0 mismatches.
## Did this PR include necessary documentation updates?
- Yes, I have updated the documentation: the `allTouched` description of `RS_AsRaster`, `RS_Clip`, `RS_SetValues`, `RS_ZonalStats` and `RS_ZonalStatsAll`, the `RS_AsRaster` note on points (which said a point on a shared corner burns every adjacent pixel), and the `null` result of `RS_AsRaster` when cropping to a geometry that covers no pixel.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [GH-3425] Align rasterization boundary handling with GDAL's half-open cells [sedona]
Posted by "jiayuasu (via GitHub)" <gi...@apache.org>.
jiayuasu merged PR #3428:
URL: https://github.com/apache/sedona/pull/3428
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org