Aggregate raster using a non-integer factor with an arbitrary function

I would like to aggregate the population raster coefficient by 1.5 times, summing the cell values.

While aggregate() allows me to summarize values ​​during aggregation, the factor parameter accepts only integer values. projectRaster() and resample() allow me to fine- resample() resolution, but (as far as I know) I am limited to the methods of calculating bilinear interpolation and the nearest neighbor.

Is there a way to aggregate a raster using a non-integer coefficient and specify the function that will be used when aggregating?

 library(raster) set.seed(10) proj <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs" r <- raster(resolution = 1, nrow = 100, crs = proj) r[] <- round(rnorm(ncell(r), 100, 10)) # Doesn't accept non-integer factors aggregate(r, fact = 1.5, fun = sum) template <- raster(extent(r), crs = crs(r), resolution = 1.5) # Correct resolution, but incorrect / impossible values for population projectRaster(r, to = template, method = "ngb") projectRaster(r, to = template, method = "bilinear") 

Possible workaround

So far, the only method I could come up with is to force the template to the SpatialPoints object; Retrieve values ​​from a higher-resolution source raster and rasterize() result:

 pts <- as(template, "SpatialPoints") vals <- extract(r, pts) pts2 <- SpatialPointsDataFrame(pts, data.frame(vals)) rasterize(pts2, template, field = "vals", fun = sum) 

However, if the points are created in the centroids of the raster cells, I’m not sure how they are processed when retrieving with a resolution of 1.5x the original raster. My preferred method would be to create a SpatialPolygonsDataFrame and rasterize using fun = mean , but (in my experience) extracting raster values ​​using polygons is very inefficient.

+5
source share
1 answer

Here's a workaround:

 #first resample to higher resolution template <- raster(extent(r), crs = crs(r), resolution = .5) detailedRas <- projectRaster(r, to = template, method = "ngb") #then use an integer as a factor (in this case 3) aggRas <- aggregate(detailedRas, fact=3, fun=sum) 

Please note, however, that the amount in this case will not refund the amount of people living in a particular aggregated area.

Ie: Let them say that we have four cells, and these values ​​with a resolution of 1 m:

 10 15 12 18 

after oversampling to 0.5 using NN:

 10 10 15 15 10 10 15 15 12 12 18 18 12 12 18 18 

Then, summing up to 1.5 m, we get for the first pixel:

10 + 10 + 15 + 10 + 10 + 15 + 12 + 12 + 18 = 112

Although in fact it should be something like: 10 + 15/2 + 12/2 + 18/4 = 28 (if we assume an equal distribution of the population for each pixel.)

I would recommend using the focal raster function with a user / user function to summarize population values ​​as you wish.

Or you divide the re-selected raster by 4, and then take the amount:

 2.5 2.5 3.75 3.75 2.5 2.5 3.75 3.75 3 3 4.5 4.5 3 3 4.5 4.5 

2.5 + 2.5 + 3.75 + 2.5 + 2.5 + 3.75 + 3 + 3 + 4.5 = 28

+1
source

Source: https://habr.com/ru/post/1265352/


All Articles