0

I've created a new raster derived from a first one in which I set a specific extent.

e <- extent(-122, -18, -56, 14) 
1st.raster <- crop(raw.raster, e)

This first raster is used as a mask for other analysis I've made. So the coords are the same through my modelling process.

My model result is a vector (Z values) with the same nrow of my 1st.raster. To produce a new raster with this vector I've extracted the coords from the 1st.raster.

coords <- na.omit(cbind(xyFromCell(current, 1:ncell(current)), values(current)))[,1:2]

2nd.raster <- rasterFromXYZ(cbind(coords, vec.Z))

Is expected that my e extent should be the same in both rasters, but that is not the case.

Here are some details of my real rasters.

> current
class       : RasterStack 
dimensions  : **1680**, 2496, 4193280, 5  (nrow, ncol, ncell, nlayers)
resolution  : 0.04166667, 0.04166667  (x, y)
extent      : -122, -18, -56, 14  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
names       : bio02, bio03, bio10, bio14, bio16 
min values  :     9,    15,   -68,     0,     0 
max values  :   211,    95,   303,   697,  3450 



    > suit_c
class       : RasterBrick 
dimensions  : **1680**, 1924, 3232320, 7  (nrow, ncol, ncell, nlayers)
resolution  : 0.04166667, 0.04166667  (x, y)
**extent      : -109.4583, -29.29167**, -56, 14  (xmin, xmax, ymin, ymax)
coord. ref. : NA 
data source : in memory
names       :        SP1,        SP2,        SP3,        SP4,        SP5,        SP6,        SP7 
min values  : 0.07558069, 0.07239924, 0.03424645, 0.07757321, 0.09786071, 0.09050093, 0.08286063 
max values  :  0.8428620,  0.8791401,  0.8592408,  0.8562305,  0.8667494,  0.8393829,  0.8629798 

I do need to create a new raster by combining the layers of this two rasters into one. As expected the different extents are preventing addLayer to work accordingly.

> FinalRaster <- addLayer(current, PA_c)
Error in compareRaster(c(x, rasters)) : different extent
> 

How to create a new raster keeping the original extent?

  • Are there NA values in the raster stack current? If so that could be your problem since you are using na.omit. What is the length of vec.Z? I think there is a simpler solution to what you are trying to do using setValues. – Jacob F Sep 7 '18 at 14:02
  • No, there is no NAs values there. As a test, I just tried na.omit in current, coords and suit_c. This process did not have any effect on the final extents. This may have occured because I used na.omit while creating this objects in the first place. Could you kindely elaborate on implementing setValues? – Milton Alves Sep 7 '18 at 14:23
  • If your vector of results really is the same length as the number of cells in your raster then you can just fill the values using 2nd.raster <- setValues(current[[1]], vec.Z) and skip trying to create a new raster from coordinates. – Jacob F Sep 7 '18 at 15:02
  • The ncell does not match in both objects. They have a different number of layers between each one, thus having a different ncol. Nevertheless, they both have the same nrow and were made with the same coords. So this is why my initial assumption was that they logically should have the same extents. Thank you for your help so far. – Milton Alves Sep 7 '18 at 15:27
  • The number of layers has nothing to do with the number of columns, the number of cells, or the extent. The extent is determined by the number of rows, number of columns, and resolution. Somewhere in your process you are removing columns from your data which is why the extent changes. – Jacob F Sep 7 '18 at 15:38
0

I also had this problem with rasterfromxyz for my research. I'm not sure why it didn't work, but instead I imported the excel table of the lat, long, and value for each cell in the raster I created into arcmap. From there I displayed XY data and exported as a shapefile, which I then converted into a raster using point to raster. I'm not sure if you have that csv file available but it fixed the issue for me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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