Repository navigation
[GH-2409] Clip zonal statistics to the window of the geometry - #3597
Merged
Merged
Conversation
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.
…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.
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.
Did you read the Contributor Guide?
Is this PR related to a ticket?
[GH-XXX] my subject. Closes [Sedona Core] Sedona doesn't clip raster to geometry extent in zonal statistics which can lead to inefficient queries #2409What changes were proposed in this PR?
RS_ZonalStatsandRS_ZonalStatsAllrasterized 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:
POLYGON ((0.3 -0.55, 0.3 -0.5, 0.4 -0.5, 0.3 -0.55))on a raster at (0, 0) with pixels of 0.1 covers no pixel centre, and pixel (2, 5), in the column before it, is burned.POLYGON ((40 10999960, 80 10999960, 80 10999920, 40 10999920, 40 10999960))covers 4 by 4 pixels and is burned over 10 rows, 40 pixels.These are defects of the fill, in
RS_AsRasteras 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:
allTouchedandexcludeNoData: 204,000 calls ofRS_ZonalStatsAll, all nine statistics identical in every one.allTouched: 55,000 calls ofRS_ZonalStatsAlland 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_AsRastermask 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: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:RS_AsRastermask at full extent.test1.tiffwith 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.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
rasteralgebraTestandrasterIOTeston Spark 3.5: 194 succeeded; the threeRS_MapAlgebratests fail on my machine with and without this change, and pass in CI.Did this PR include necessary documentation updates?