I have raster files with the same resolution (30x30m), but different dimensions, and I would like to have all rasters with the same dimension and resolution.

So, here is the first raster:


enter image description here

and here is the second:


enter image description here

Together, they look like this:

enter image description here

I want them to have the same shape. Using R, I managed to crop A according to B, by simply using the raster::crop() function, and created a new raster (let's call it A1). However, it doesn't work if I try to crop B according to A or A1, but I don't get any kind of error. I am even able to plot it, and it shows the raster with the same rectangular shape, instead of considering the limits of A/A1...

I have also tried with QGIS and I have exactly the same problem: I can crop A according to B but not the opposite.

Does anyone know how can I crop this raster?

closed as off-topic by PolyGeo Oct 4 at 20:18

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Hi, you must share your code to see if a code issue. But for what I see, both rasters have different CRS. R requires datasets in the same CRS to work as expected – aldo_tapia Oct 4 at 14:16

The result is correct. Since the extent of A is larger, the function is correctly cropping the extent of B. To get the same "shape", try something along the lines of:

mask(B, crop(B, extent(A)), A) 

However, this will still show a smaller extent in B. If you really need the two rasters to show the same extent of data then you will likely have to coerce your NA values in B to a real background value (eg., 0) before cropping/masking the data.

  • The formula, and variants, doesn't seem to be working... I changed the extent, as a first error was related to that, and now I get this: " Error in filename != "" : comparison (2) is possible only for atomic and list types " The problem of attributing a value is that that will influence the next steps, as the value "0" as a meaning in other pixels (each pixel represents a distance to a given feature) – mtao Oct 4 at 18:35

In QGIS you could use the "Extract layer extent" processing tool to get a bounding box for Raster B. Then use that as the mask layer in the "Clip Raster by Mask Layer" tool to clip Raster A

  • This works, but I get the same result I obtained in R as well: that is, I can easily clip raster A according to B. The problem is to crop B according to A... – mtao Oct 4 at 19:15

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