Does anyone have an idea how I could aggregate my raster so that in the output the most frequent value is used, as shown on the picture?
2 Answers
Here's an approach in R
using the raster
package:
x <- c(
1, 1, 1, 1, 1, 2, 4, 6, 7,
1, 3, 3, 2, 5, 6, 6, 7, 8,
1, 1, 3, 2, 2, 2, 4, 5, 6,
1, 2, 2, 2, 2, 4, 4, 6, 6,
1,NA, 1, 2, 2, 2, 4, 5, 6,
1,NA, 1, 2, 2, 3, 4, 5, 6,
1, 1, 1, 1, 1, 2, 3, 4, 5,
0, 0, 1, 1, 1, 2, 4, 4, 5,
0, 1, 1, 1, 1, 2, 3, 4, 4
)
## build the raster object
library(raster)
r <- raster(matrix(x, ncol = 9, byrow = TRUE))
## aggregate by a factor of 3, with "modal"
m <- aggregate(r, fact = 3, fun = modal, na.rm = TRUE)
plot(m, addfun = function() text(m))
-
thank you for your comment, it works perfectly! I guess it is very good option for me, I just wanted to find similar function/tool in ArcGIS.– TinaFeb 9, 2016 at 12:20
-
@mdsumner Would you know how you can define ties when using the fun= modal within aggregate? By default, does it pick one at random? Jul 2, 2017 at 15:55
-
By default it is random, see ?raster::modal - arguments get "passed along" to "fun", i.e. aggregate(r, fact = 3, fun = modal, na.rm = TRUE, ties = "random")– mdsumnerJul 2, 2017 at 21:56
You're probably looking for something called "Majority Filter".
ArcGIS
You can use majority filtering also in the context of resampling a raster. In ArcGIS you will find the tool RESAMPLE in the Data Management Toolbox (check documentation).
Use this tool with "resampling_type" MAJORITY. Using this you can alter cell size for the target raster. The code in arcpy reads as follows:
arcpy.Resample_management("image.tif", "resample.tif", "10", "MAJORITY")
QGIS
For QGIS there are other options provided by the processing toolbox and SAGA/GRASS functions. Follow this link for further details.
-
Riccardo, thank you for your reply I've seen these ArcGIS tools but my problem is how to aggregate those pixels to the coarse resolution? so, I have a land use map with 250m resolution and would like to get the land use map with 3000m cell size and each cell should represent the dominant land use class. I am quite new in the ArcGIS, and unfortunatelly I cannot figure it out how those tools could be helpful for me. Thank you.– TinaFeb 9, 2016 at 11:13
-
Hi @Tina. I improved my answer. Check the function "RESAMPLE" in ArcGIS– RiccardoFeb 9, 2016 at 12:29