Skip to content

[GH-2409] Clip zonal statistics to the window of the geometry - #3597

Merged
jiayuasu merged 4 commits into
apache:masterfrom
jiayuasu:fix/zonal-stats-roi-window
Nov 30, 2026
Merged

jiayuasu merged 4 commits into
apache:masterfrom
jiayuasu:fix/zonal-stats-roi-window

Conversation

@jiayuasu

@jiayuasu jiayuasu commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Did you read the Contributor Guide?

Is this PR related to a ticket?

What changes were proposed in this PR?

RS_ZonalStats and RS_ZonalStatsAll rasterized the geometry into a band as large as the raster and went through every pixel of the raster, whatever the size of the zone.

The geometry is still burned by the code that burns that band, with the band's size and origin, but its pixels are kept as a list of horizontal spans instead of a band (Rasterization.select). The mask is as large as the spans reach, and the statistics go through that part of the raster's band only.

Nothing is assumed about where the pixels of a geometry are. A mask burned into a window computed from the geometry is not the same thing, because the polygon fill does not keep to that window:

These are defects of the fill, in RS_AsRaster as well, and they are not changed here. The statistics are those of the pixels the fill burns at full extent, as before.

Once the spans of a geometry would take more memory than a mask of the whole raster, such a mask is used instead.

The band is taken from the raster's image as before.

Results do not change

The pixels come from the same code, called with the same band size and origin, and they are read in the same order. Measured against master:

  • 17 rasters (seven grids, each as made and after the serializer, north-up and bottom-up, one arc second, and three GeoTIFFs) and 3,000 random points, lines, polygons and collections per raster, with and without allTouched and excludeNoData: 204,000 calls of RS_ZonalStatsAll, all nine statistics identical in every one.
  • 55 rasters: 24 grids, each as made and after the serializer, four GeoTools crops whose grid range starts at (2, 2), and three GeoTIFFs. The grids include pixel sizes of 0.1, 0.3 and 0.01, six grids with northings of ten million and more, five with pixels close to the spacing of doubles at their coordinates and two with pixels below it. 500 geometries per raster, with coordinates anywhere, on grid lines, on pixel centres, on half and quarter pixels and on whole numbers, with and without allTouched: 55,000 calls of RS_ZonalStatsAll and 110,000 outputs of the rasterizer, cropped and at full extent, all identical.

On the same 55 rasters, with 3,000 geometries each, the mask was compared pixel by pixel with the RS_AsRaster mask at full extent: 330,000 masks, and no pixel differs.

Time

Median of 15 runs of RS_ZonalStats(raster, zone, 1, 'sum') on a raster of 4096x4096 doubles in memory, in milliseconds:

zone master this PR
polygon of 20x20 pixels 38.8 0.08
polygon of 200x200 pixels 37.8 0.47
polygon of the whole raster 399 345

This replaces #3461, which rasterizes the geometry on a raster cropped to it and places that by the envelopes of the two rasters.

How was this patch tested?

New tests in RasterBandAccessorsTest:

  • A raster in memory whose band records every region its samples are taken from. For a zone of 11 by 9 pixels on a raster of 1200 by 900, only those 11 by 9 pixels are taken. On master the whole band is taken, and the test fails.
  • The statistics of 400 random geometries on each of eight rasters, among them a bottom-up raster, a one-arc-second raster, two rasters with pixels of 0.1, a raster at a northing of 11,000,000 and two rasters whose grid range starts at (2, 2), compared with the pixels of the RS_AsRaster mask at full extent.
  • Polygons for which the fill burns pixels outside the polygon, compared with the full-extent mask: one on test1.tiff with a pixel in the column after it, 100 triangles on a raster with pixels of 0.1 of which 33 have a pixel in the column before them, and the square at a northing of 10,999,960 that is burned over 10 rows, on a north-up and on a bottom-up raster.
  • A raster at 1e15 with rows of 0.15, where doubles are 0.125 apart: a point in each row, compared with the full-extent mask.
  • A geometry with more spans than a mask of the raster takes: its mask is the whole raster, and its statistics are those of the full-extent mask.

The tests that compare with the full-extent mask pass on master as well. The raster tests of the common module: 584 run, 0 failures. Spark rasteralgebraTest and rasterIOTest on Spark 3.5: 194 succeeded; the three RS_MapAlgebra tests fail on my machine with and without this change, and pass in CI.

Did this PR include necessary documentation updates?

  • No, this PR does not affect any public API so no need to change the documentation.

RS_ZonalStats and RS_ZonalStatsAll rasterized the geometry at the full extent of
the raster, read the whole band and the whole mask into two arrays of doubles, and
went through every pixel of the raster. The cost followed the size of the raster,
whatever the size of the zone.

The rasterizer now returns the pixels a geometry selects as a mask over the window
of the raster that holds them, with the window's place in the raster in whole
pixels (Rasterization.select). The statistics read that window of the band and
nothing else, and an image that does not hold its pixels in memory is asked for
the window only.

The pixels selected are those of the full extent, in the same order, so results
do not change. Two things in the polygon fill depend on the extent it is computed
for, and the window keeps both as they are at full extent:

- A pixel whose centre lies exactly on a polygon edge is decided by an x-intercept
  computed in floating point from the origin of the output. For an output cropped
  to the geometry it can come out on the other side. The fill of the window is
  computed from the raster's origin.
- For a vertical edge on the last column line of the polygon's window, the fill
  can burn one pixel in the column after the window, outside the polygon
  (apacheGH-3595). The window has that column.
…efore

The mask and the loop cover the window of the geometry, which is what the
issue asks for. The band is taken from the raster's image the way it was,
so RasterUtils.getRaster(image, region) is not needed.

The test of the window now uses a raster in memory whose band records the
regions its samples are taken from.
@jiayuasu jiayuasu changed the title [GH-2409] Read only the window of the geometry in zonal statistics [GH-2409] Clip zonal statistics to the window of the geometry Nov 27, 2026
…ith a column either side

The mask over a geometry's window could miss a pixel that the mask at full
extent holds. The polygon fill burns an unpaired x-intercept of a scanline as
the pixel it falls in, by the floor of an x computed in floating point. For a
vertex on the window's first column line that x can come out below the line's
index, 0.3 / 0.1 = 2.9999999999999996, and the pixel is in the column before
the window. The window had a spare column after it only. It has one on either
side now.

The band of the mask was sized and placed from the window's envelope. Where
the pixel size is close to the spacing of doubles, an edge of that envelope is
a grid line rounded by a large part of a pixel, the band could come out a row
too high, and in the raster's last row it was read from beyond the raster. The
window is now kept in whole cells, from the grid lines that enclose the
geometry, and the band is sized and placed by them. The envelope that the
polygon is clipped to is made from the same cells and is the one it was.
…ent burn

The mask of a geometry's window was burned into a band of the window's size,
and the polygon fill does not keep to that window. A band at full extent can
hold pixels of a polygon outside it: the pixel of an unpaired x-intercept, and
whole rows beyond a polygon whose northings are ten million or more, which the
fill rounds to tens of rows. A square of 4 by 4 pixels at a northing of
10,999,960 is burned over 10 rows at full extent, and its statistics had 16
pixels where they have 40 on master.

The pixels are now burned by the code that burns a band at full extent, with
the size and the origin of that band, and kept as a list of spans instead of a
band as large as the raster. The mask is as large as the spans reach. Nothing
is assumed about where the fill burns, so the statistics are those of the
full-extent mask for every geometry and grid. Spans that would take more
memory than a mask of the whole raster are given up for such a mask.

The window in whole cells and its spare columns are not needed any more. The
rasterizer is back to its code on master apart from where the pixels go.
@jiayuasu jiayuasu added this to the sedona-2.0.0 milestone Nov 30, 2026
@jiayuasu
jiayuasu merged commit a6c6b9c into apache:master Nov 30, 2026
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Sedona Core] Sedona doesn't clip raster to geometry extent in zonal statistics which can lead to inefficient queries

1 participant