1

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? enter image description here

2 Answers 2

8

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))

enter image description here

3
  • 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.
    – Tina
    Feb 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")
    – mdsumner
    Jul 2, 2017 at 21:56
1

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.

2
  • 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.
    – Tina
    Feb 9, 2016 at 11:13
  • Hi @Tina. I improved my answer. Check the function "RESAMPLE" in ArcGIS
    – Riccardo
    Feb 9, 2016 at 12:29

Your Answer

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

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