Skip to content

Add 'gdal raster neighbors' to compute focal statistics#13088

Merged
rouault merged 2 commits intoOSGeo:masterfrom
rouault:gdal_raster_neighbor
Sep 21, 2025
Merged

Add 'gdal raster neighbors' to compute focal statistics#13088
rouault merged 2 commits intoOSGeo:masterfrom
rouault:gdal_raster_neighbor

Conversation

@rouault
Copy link
Copy Markdown
Member

@rouault rouault commented Sep 16, 2025

Fixes #12768

@rouault rouault added this to the 3.12.0 milestone Sep 16, 2025
@rouault rouault added enhancement funded through GSP Work funded through the GDAL Sponsorship Program gdal_cli Anything related to the new 3.11 "gdal" CLI frontend labels Sep 16, 2025
@rouault rouault force-pushed the gdal_raster_neighbor branch 4 times, most recently from 7c24fd8 to 5455d2e Compare September 17, 2025 00:37
@coveralls
Copy link
Copy Markdown
Collaborator

coveralls commented Sep 17, 2025

Coverage Status

coverage: 71.228% (+0.008%) from 71.22%
when pulling a3843fe on rouault:gdal_raster_neighbor
into f028914 on OSGeo:master.

@rouault rouault force-pushed the gdal_raster_neighbor branch from 5455d2e to 0b63f85 Compare September 17, 2025 02:59
Copy link
Copy Markdown
Member

@dbaston dbaston left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a great addition and makes the VRTKernelFilteredSource so much more useful!

src_ds.WriteRaster(0, 0, 3, 3, array.array("B", [1, 2, 3, 4, 5, 6, 7, 8, 9]))

neighbors["input"] = src_ds
neighbors["kernel"] = "one_3x3"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about specifying the neighborhood size and weighting separately?

neighbors["size"] = 3 # (or [3, 3]) ?
neighbors["weighting"] = "equal" # (or "sharpen", "gaussian_blur") etc.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was considering that, but I don't think (or at least I don't know how) all kernels can be set to an arbitrary size. For example, u and v likely only make sense for 3x3. edge and sharpen likely too. gaussian_blur there's likely a formula somewhere, but that's pretty much the only one we can size arbitrarily. I let that for further improvements

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how many can be arbitrary but "equal" (here, "one") certainly can. GRASS has formulas for "gaussian" and "exponential", https://grass.osgeo.org/grass-stable/manuals/r.neighbors.html#:~:text=For%20the%20gaussian%20weighting%20function,*x+y*y))

These wouldn't need to be implemented now, since we can error out for an unsupported combination of kernel and size, but I think the API should support an arbitrary size even if it's only implemented for an equal-weight kernel.

We could also support non-square kernels by padding with zeros to allow e.g. --weighting equal --size 3 5

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've adding a commit adding --size (single value), and combining 'size' and 'kernel' values (with only 'equal' being supported for any size value)

@rouault rouault force-pushed the gdal_raster_neighbor branch from 0b63f85 to ff330e7 Compare September 17, 2025 17:56
@rouault rouault force-pushed the gdal_raster_neighbor branch 2 times, most recently from 9a70ff6 to 7f94de7 Compare September 17, 2025 20:32
@rouault rouault force-pushed the gdal_raster_neighbor branch from 7f94de7 to cec1e75 Compare September 17, 2025 21:49
@rouault rouault force-pushed the gdal_raster_neighbor branch from cec1e75 to a3843fe Compare September 17, 2025 21:50
@rouault rouault merged commit a8f0531 into OSGeo:master Sep 21, 2025
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement funded through GSP Work funded through the GDAL Sponsorship Program gdal_cli Anything related to the new 3.11 "gdal" CLI frontend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calculate the derivative of a raster band

4 participants