1

I have a satellite image with dimensions (11418, 13790, 1). The resolution is 10m. I have two other rasters, which fall inside of my satellite image which are the same resolution and crs.

How would I go about enhancing the extent of small rasters to the size of my satellite image, and then create NA values?

To be clear, I'm looking for a workflow which I think involves changing the extent of my small rasters to the large one using the extent argument and then using the mask function?

1
  • 1
    Note if you want to comment on an answer do it as a "Add a comment" instead of creating a new answer! The site should evolve to have one Question per Question, then a bunch of Answers (that answer the Question), then one Answer will get "Accepted" as correct, or the best!
    – Spacedman
    Commented Mar 10, 2022 at 20:02

1 Answer 1

3

Let's make an example with some sample rasters that represent your situation, hopefully. First a big raster with extent from (0,0) to (100,100):

big = raster(matrix(1:100,10,10))
extent(big)=c(0,100,0,100)

Now a small raster spanning part of that from (10,20) to (20,30):

small = raster(matrix(1:100,10,10))
extent(small)=c(10,20,20,30)

Now use extend to expand the bounds of small to the extent of big

smallextend = extend(small, big)

which is this:

class      : RasterLayer 
dimensions : 100, 100, 10000  (nrow, ncol, ncell)
resolution : 1, 1  (x, y)
extent     : 0, 100, 0, 100  (xmin, xmax, ymin, ymax)
crs        : NA 
source     : memory
names      : layer 
values     : 1, 100  (min, max)

Plotting shows smallextend stretching out to the extent of big, but padded with NA values:

enter image description here

Note the extent might not be exact if the resolution doesn't exactly fit into the big raster, for example:

> extent(small2) = c(11.23, 23.7, 22.1, 33.4)
> plot(small2, add=TRUE)
> extend(small2, big)
class      : RasterLayer 
dimensions : 89, 80, 7120  (nrow, ncol, ncell)
resolution : 1.247, 1.13  (x, y)
extent     : 0.007, 99.767, -0.5, 100.07  (xmin, xmax, ymin, ymax)
crs        : NA 
source     : memory
names      : layer 
values     : 1, 100  (min, max)

you can see the output extent isn't quite the same as big because the small2 is not made of 1x1 cells.

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