5

I am currently running a process that involves the creation of many "Focal Statistics" rasters in ArcGIS. These rasters use varying-radius circular search areas and calculate three different statistics: Mean, Sum, and Standard Deviation (depending on the input data source). They also must be created in a programmatic manner - either from Python, R, or Bash/Command Line.

I would like to move this to an open source alternative but am having trouble finding an open source equivalent to ArcGIS's "Focal Statistics". The raster::focal() function in R is terribly slow, and for the size of my data it will not work. SAGA GIS has the 'simple filter' tool, but that will only calculate the Mean, not the other two statistics.

Does anyone have an alternative that replicates the functionality of "Focal Statistics" in a reasonably fast, free, and programmatic manner with a circular search window?

3 Answers 3

3

Within GRASS GIS you can use r.neighbors which looks at each cell in a raster input file, and examines the values assigned to the cells in some user-defined "neighborhood" around it. It outputs a new raster map layer in which each cell is assigned a value that is some (user-specified) function of the values in that cell's neighborhood. Using the -c flag you can run a circular filter.

To call the functionality from outside, the grass-session Python package is interesting (usage example).

2
  • 1
    This answer led me to the rgrass7 package in R, which seems appropriate for my needs. Thank you!
    – Jackson
    Commented Feb 3, 2019 at 0:17
  • Is there really no way to do this in gdal? Since gdal is THE library underlying all else (including grass!), this surprises me greatly.
    – Ratnanil
    Commented Aug 12, 2021 at 12:43
2

For staying in R, take a look at the velox package. For performance, it has moved processing into C+, using Rcpp and the Boost Geometry libraries. Operations such as extract, focal, aggregation and rasterization benchmark notably faster than comparable raster package functions.

Unfortunately, it does not look like there is a focal standard deviation per se but, you could submit a development request through the GitHub page or, write a Rcpp function yourself and add it to the package.

2
  • 1
    Thank you, velox is the answer to some of my other unasked questions! I'll reach out to the devs and see if they plan on updating with other statistics. After some more investigation, I may just run this in GRASS from the command line with the --exec flag and r.neighbors.
    – Jackson
    Commented Oct 17, 2018 at 19:44
  • 1
    Hi Jackson- I have the same question (running focal stats using open source). Were you able to run GRASS? If so- how did you get it working? I want to run focal stats by calling grass from a stand along python script but haven't had luck calling it. There are a lot of resources online and I've followed all of them and keep dead ending. Any advice would be great, thanks!
    – Sam
    Commented Dec 7, 2018 at 15:32
1

terra::focal() is much faster than raster::focal(). But in some cases, still too slow. Another great tool for moving window calculations: pkfilter from pktools. From the docs:

This utility implements spatial and spectral filtering for raster data. In the spatial domain (X, Y), the filter typically involves a rectangular convolution kernel (moving window). To avoid image shifting, the size of the window should be odd (3, 5, 7, ...). You can set the window sizes in X and Y directions separately with the options -dx and -dy. A circular kernel (disc) is applied if option -circ is set.

For example, this creates a circular moving window of 11 x 11 cells calculating the mean per cell:

pkfilter -i inputfile.tif -o output.tif -dx 11 -dy 11 -f mean -circ

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.