1
votes

I am trying to use the 'grainchanger' package in R to aggregate finer resolution raster to a coarser one.

I have 10km grid outlines of the UK, which I have converted from shapefile to raster.

I also have a land cover map of the UK in raster form.

I need to look at the land cover %s in each 10km square of the land cover map.

When I try aggregating this using the grainchanger package, I get the following error:

Error in .local(x, y, ...) : extents do not overlap

I think this is because my 10km grid raster is squares which the land cover map does not fill.

How can I change the extent of the land cover map so it matches the 10km grid raster?

This is my code:

library(raster)

# continuous landscape
show_landscape(cont_ls)

# load the coarse resolution raster
g_raster <- raster("raster.tif", package = "grainchanger")

show_landscape(g_raster)

# direct aggregation using range
dda <- nomove_agg(coarse_dat = g_raster,
              fine_dat = cont_ls, 
              agg_fun = var_range)
#> aggregation assumes all cells are rectangular
#> ● set `is_grid = FALSE` if coarse_dat is not a grid
1

1 Answers

0
votes

This error message suggests that you could have an issue with projection, but it's hard to know without having more information.

You could check the extents do overlap with

library(raster)
ext.ras <- extent(6e+05, 709800, 5690220, 5800020)
ext.pol <- extent(499386.6, 517068.2, 6840730, 6857271)


plot(ext.ras, xlim = c( 499386.6,709800), ylim= c(5690220,6857271), col="red")
plot(ext.pol, add=T, col="blue")

taken from this answer.

If they do not overlap and you expect them to you can use spTransform() to change the projection.