Make raster stack with different extent












0















I am in trouble making raster stack which have slightly different extent. The answer (1st one) given here is useful but did not help in my case. For example, I want to make a raster stack using bio2 raster for Australia and this Australian raster. The second raster comes for Australia only and the first one is global. So I cropped the global bio2 raster to the same extent of Australian raster using crop() function, but the resultant raster extent (i.e., bio2.au) is slightly different (therefore, I cannot make raster using the cropped raster and the Australian raster, awc). Sample code is below:



library(raster)
awc <- raster("path to Australian raster")
bio2.g <- raster("path to Bio2 global raster")
# crop bio2.g to the same extent of awc
bio2.au <- crop(bio2.g, extent(awc))

# make a raster stack
st <- stack(awc, bio2.au)
Error in compareRaster(x) : different extent


I have also tried using quick=TRUE within the stack() function. But in this case the cell values in awc is lost. Note: the size of awc raster is 4gb.



# first make a list of rasters saved in the computer
li <- list.files("path to file", pattern = ".tif$", full.names = TRUE)
st <- stack(li, quick=TRUE)
st[[1]] # no cell values for awc


Your suggestions will be highly appreciated. My ultimate goal is to crop several bioclim rasters to the same extent of Australian raster awc and stack them together so that raster cell values are not lost.



Edit (after comment of @Cobin):



Below is the attribute of each raster



# global raster (bigger raster)
> r
class : RasterLayer
dimensions : 21600, 43200, 933120000 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : D:Worldclim2_Bioclimwc2.0_bio_30s_02.tif
names : wc2.0_bio_30s_02
values : 0, 37.06667 (min, max)


# Australian raster (smaller raster)
> r1
class : RasterLayer
dimensions : 43201, 49359, 2132358159 (nrow, ncol, ncell)
resolution : 0.0008333333, 0.0008333333 (x, y)
extent : 112.8921, 154.0246, -44.00042, -7.999583 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : D:SoilAWC5cm.EV1.tif
names : SoilAWC5cm.EV1
values : 2.997789, 27.86114 (min, max)

# new raster, after crop() function is applied
> r2 <- crop(r,extent(r1))
> r2
class : RasterLayer
dimensions : 4320, 4936, 21323520 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 112.8917, 154.025, -44, -8 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:UsersAnwarAppDataLocalTempRtmpmg9fyFrasterr_tmp_2018-11-23_164300_11308_65747.grd
names : wc2.0_bio_30s_02
values : 1.933333, 18.15833 (min, max)

# rebuild r2 to match r1
> r22 <- raster(vals=values(r2),ext=extent(r1), nrows=dim(r1)[1],ncols=dim(r1)[2])

Error in setValues(r, vals) :
length(values) is not equal to ncell(x), or to 1









share|improve this question




















  • 1





    global raster (bigger raster) resolution is different from Australian raster (smaller raster). If you want to stack r1 and r2, you should resample the raster insuring they have same resolution, extent, crs. Such as function resample(r1,r2, method='near')

    – Cobin
    Nov 23 '18 at 8:24













  • Thank you @Cobin. This trick worked successfully.

    – Crayfish
    Nov 24 '18 at 10:37
















0















I am in trouble making raster stack which have slightly different extent. The answer (1st one) given here is useful but did not help in my case. For example, I want to make a raster stack using bio2 raster for Australia and this Australian raster. The second raster comes for Australia only and the first one is global. So I cropped the global bio2 raster to the same extent of Australian raster using crop() function, but the resultant raster extent (i.e., bio2.au) is slightly different (therefore, I cannot make raster using the cropped raster and the Australian raster, awc). Sample code is below:



library(raster)
awc <- raster("path to Australian raster")
bio2.g <- raster("path to Bio2 global raster")
# crop bio2.g to the same extent of awc
bio2.au <- crop(bio2.g, extent(awc))

# make a raster stack
st <- stack(awc, bio2.au)
Error in compareRaster(x) : different extent


I have also tried using quick=TRUE within the stack() function. But in this case the cell values in awc is lost. Note: the size of awc raster is 4gb.



# first make a list of rasters saved in the computer
li <- list.files("path to file", pattern = ".tif$", full.names = TRUE)
st <- stack(li, quick=TRUE)
st[[1]] # no cell values for awc


Your suggestions will be highly appreciated. My ultimate goal is to crop several bioclim rasters to the same extent of Australian raster awc and stack them together so that raster cell values are not lost.



Edit (after comment of @Cobin):



Below is the attribute of each raster



# global raster (bigger raster)
> r
class : RasterLayer
dimensions : 21600, 43200, 933120000 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : D:Worldclim2_Bioclimwc2.0_bio_30s_02.tif
names : wc2.0_bio_30s_02
values : 0, 37.06667 (min, max)


# Australian raster (smaller raster)
> r1
class : RasterLayer
dimensions : 43201, 49359, 2132358159 (nrow, ncol, ncell)
resolution : 0.0008333333, 0.0008333333 (x, y)
extent : 112.8921, 154.0246, -44.00042, -7.999583 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : D:SoilAWC5cm.EV1.tif
names : SoilAWC5cm.EV1
values : 2.997789, 27.86114 (min, max)

# new raster, after crop() function is applied
> r2 <- crop(r,extent(r1))
> r2
class : RasterLayer
dimensions : 4320, 4936, 21323520 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 112.8917, 154.025, -44, -8 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:UsersAnwarAppDataLocalTempRtmpmg9fyFrasterr_tmp_2018-11-23_164300_11308_65747.grd
names : wc2.0_bio_30s_02
values : 1.933333, 18.15833 (min, max)

# rebuild r2 to match r1
> r22 <- raster(vals=values(r2),ext=extent(r1), nrows=dim(r1)[1],ncols=dim(r1)[2])

Error in setValues(r, vals) :
length(values) is not equal to ncell(x), or to 1









share|improve this question




















  • 1





    global raster (bigger raster) resolution is different from Australian raster (smaller raster). If you want to stack r1 and r2, you should resample the raster insuring they have same resolution, extent, crs. Such as function resample(r1,r2, method='near')

    – Cobin
    Nov 23 '18 at 8:24













  • Thank you @Cobin. This trick worked successfully.

    – Crayfish
    Nov 24 '18 at 10:37














0












0








0








I am in trouble making raster stack which have slightly different extent. The answer (1st one) given here is useful but did not help in my case. For example, I want to make a raster stack using bio2 raster for Australia and this Australian raster. The second raster comes for Australia only and the first one is global. So I cropped the global bio2 raster to the same extent of Australian raster using crop() function, but the resultant raster extent (i.e., bio2.au) is slightly different (therefore, I cannot make raster using the cropped raster and the Australian raster, awc). Sample code is below:



library(raster)
awc <- raster("path to Australian raster")
bio2.g <- raster("path to Bio2 global raster")
# crop bio2.g to the same extent of awc
bio2.au <- crop(bio2.g, extent(awc))

# make a raster stack
st <- stack(awc, bio2.au)
Error in compareRaster(x) : different extent


I have also tried using quick=TRUE within the stack() function. But in this case the cell values in awc is lost. Note: the size of awc raster is 4gb.



# first make a list of rasters saved in the computer
li <- list.files("path to file", pattern = ".tif$", full.names = TRUE)
st <- stack(li, quick=TRUE)
st[[1]] # no cell values for awc


Your suggestions will be highly appreciated. My ultimate goal is to crop several bioclim rasters to the same extent of Australian raster awc and stack them together so that raster cell values are not lost.



Edit (after comment of @Cobin):



Below is the attribute of each raster



# global raster (bigger raster)
> r
class : RasterLayer
dimensions : 21600, 43200, 933120000 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : D:Worldclim2_Bioclimwc2.0_bio_30s_02.tif
names : wc2.0_bio_30s_02
values : 0, 37.06667 (min, max)


# Australian raster (smaller raster)
> r1
class : RasterLayer
dimensions : 43201, 49359, 2132358159 (nrow, ncol, ncell)
resolution : 0.0008333333, 0.0008333333 (x, y)
extent : 112.8921, 154.0246, -44.00042, -7.999583 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : D:SoilAWC5cm.EV1.tif
names : SoilAWC5cm.EV1
values : 2.997789, 27.86114 (min, max)

# new raster, after crop() function is applied
> r2 <- crop(r,extent(r1))
> r2
class : RasterLayer
dimensions : 4320, 4936, 21323520 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 112.8917, 154.025, -44, -8 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:UsersAnwarAppDataLocalTempRtmpmg9fyFrasterr_tmp_2018-11-23_164300_11308_65747.grd
names : wc2.0_bio_30s_02
values : 1.933333, 18.15833 (min, max)

# rebuild r2 to match r1
> r22 <- raster(vals=values(r2),ext=extent(r1), nrows=dim(r1)[1],ncols=dim(r1)[2])

Error in setValues(r, vals) :
length(values) is not equal to ncell(x), or to 1









share|improve this question
















I am in trouble making raster stack which have slightly different extent. The answer (1st one) given here is useful but did not help in my case. For example, I want to make a raster stack using bio2 raster for Australia and this Australian raster. The second raster comes for Australia only and the first one is global. So I cropped the global bio2 raster to the same extent of Australian raster using crop() function, but the resultant raster extent (i.e., bio2.au) is slightly different (therefore, I cannot make raster using the cropped raster and the Australian raster, awc). Sample code is below:



library(raster)
awc <- raster("path to Australian raster")
bio2.g <- raster("path to Bio2 global raster")
# crop bio2.g to the same extent of awc
bio2.au <- crop(bio2.g, extent(awc))

# make a raster stack
st <- stack(awc, bio2.au)
Error in compareRaster(x) : different extent


I have also tried using quick=TRUE within the stack() function. But in this case the cell values in awc is lost. Note: the size of awc raster is 4gb.



# first make a list of rasters saved in the computer
li <- list.files("path to file", pattern = ".tif$", full.names = TRUE)
st <- stack(li, quick=TRUE)
st[[1]] # no cell values for awc


Your suggestions will be highly appreciated. My ultimate goal is to crop several bioclim rasters to the same extent of Australian raster awc and stack them together so that raster cell values are not lost.



Edit (after comment of @Cobin):



Below is the attribute of each raster



# global raster (bigger raster)
> r
class : RasterLayer
dimensions : 21600, 43200, 933120000 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : D:Worldclim2_Bioclimwc2.0_bio_30s_02.tif
names : wc2.0_bio_30s_02
values : 0, 37.06667 (min, max)


# Australian raster (smaller raster)
> r1
class : RasterLayer
dimensions : 43201, 49359, 2132358159 (nrow, ncol, ncell)
resolution : 0.0008333333, 0.0008333333 (x, y)
extent : 112.8921, 154.0246, -44.00042, -7.999583 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : D:SoilAWC5cm.EV1.tif
names : SoilAWC5cm.EV1
values : 2.997789, 27.86114 (min, max)

# new raster, after crop() function is applied
> r2 <- crop(r,extent(r1))
> r2
class : RasterLayer
dimensions : 4320, 4936, 21323520 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 112.8917, 154.025, -44, -8 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:UsersAnwarAppDataLocalTempRtmpmg9fyFrasterr_tmp_2018-11-23_164300_11308_65747.grd
names : wc2.0_bio_30s_02
values : 1.933333, 18.15833 (min, max)

# rebuild r2 to match r1
> r22 <- raster(vals=values(r2),ext=extent(r1), nrows=dim(r1)[1],ncols=dim(r1)[2])

Error in setValues(r, vals) :
length(values) is not equal to ncell(x), or to 1






r stack spatial raster extent






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 7:36







Crayfish

















asked Nov 23 '18 at 3:11









CrayfishCrayfish

6510




6510








  • 1





    global raster (bigger raster) resolution is different from Australian raster (smaller raster). If you want to stack r1 and r2, you should resample the raster insuring they have same resolution, extent, crs. Such as function resample(r1,r2, method='near')

    – Cobin
    Nov 23 '18 at 8:24













  • Thank you @Cobin. This trick worked successfully.

    – Crayfish
    Nov 24 '18 at 10:37














  • 1





    global raster (bigger raster) resolution is different from Australian raster (smaller raster). If you want to stack r1 and r2, you should resample the raster insuring they have same resolution, extent, crs. Such as function resample(r1,r2, method='near')

    – Cobin
    Nov 23 '18 at 8:24













  • Thank you @Cobin. This trick worked successfully.

    – Crayfish
    Nov 24 '18 at 10:37








1




1





global raster (bigger raster) resolution is different from Australian raster (smaller raster). If you want to stack r1 and r2, you should resample the raster insuring they have same resolution, extent, crs. Such as function resample(r1,r2, method='near')

– Cobin
Nov 23 '18 at 8:24







global raster (bigger raster) resolution is different from Australian raster (smaller raster). If you want to stack r1 and r2, you should resample the raster insuring they have same resolution, extent, crs. Such as function resample(r1,r2, method='near')

– Cobin
Nov 23 '18 at 8:24















Thank you @Cobin. This trick worked successfully.

– Crayfish
Nov 24 '18 at 10:37





Thank you @Cobin. This trick worked successfully.

– Crayfish
Nov 24 '18 at 10:37












1 Answer
1






active

oldest

votes


















1














I suppose that the extent of two raster are differet though the raster masked by crop function.You
should check the both of awc and bio.au extent base on same reolution, rows and columns. Because I couldn't download data from
hyperlink, I give an example of my own data.



r <- raster('/big_raster')
r1 <- raster('/small_raster')
r2 <- crop(r,extent(r1))

r1
class : RasterLayer
dimensions : 74, 157, 11618 (nrow, ncol, ncell)
resolution : 0.0833333, 0.0833333 (x, y)
extent : 89.2185, 102.3018, 30.96238, 37.12905 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : D:DtempRtestmodis8km.tif
names : modis8km
values : -32768, 32767 (min, max)

r2
class : RasterLayer
dimensions : 74, 157, 11618 (nrow, ncol, ncell)
resolution : 0.08333333, 0.08333333 (x, y)
extent : 89.25, 102.3333, 31, 37.16667 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : g201401a
values : -32768, 7789 (min, max)


Though r1 and r1 with same resolution and dimension, the extent have tiny offset. It cause stack error.



 stack(r1,r2)
Error in compareRaster(x) : different extent


So, you should rebuid the r2 to match r1:



r22 <- raster(vals=values(r2),ext=extent(r1),crs=crs(r1),
nrows=dim(r1)[1],ncols=dim(r1)[2])


Now stack(r22,r1) will be successful.






share|improve this answer


























  • Thank you. My r1 extent is: 112.8921, 154.0246, -44.00042, -7.999583, and my r2 extent after cropping is: 112.8917, 154.025, -44, -8. But I have got the following error: r22 <- raster(vals=values(r2),ext=extent(r1), nrows=dim(r1)[1], ncols=dim(r1)[2]). Error in setValues(r, vals) : length(values) is not equal to ncell(x), or to 1 @Cobin

    – Crayfish
    Nov 23 '18 at 5:51











  • The main problem is that the small rasster is not snapping big raster.The rasters have different rows and collumns (width and height) after crop.You should show bio2.au and awc atrribute in question. Maybe cellFrom function to crop raster is preciser. @Crayfish

    – Cobin
    Nov 23 '18 at 6:27













  • Thank you. I have now added raster attributes in my main question. @Cobin

    – Crayfish
    Nov 23 '18 at 7:38











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440229%2fmake-raster-stack-with-different-extent%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I suppose that the extent of two raster are differet though the raster masked by crop function.You
should check the both of awc and bio.au extent base on same reolution, rows and columns. Because I couldn't download data from
hyperlink, I give an example of my own data.



r <- raster('/big_raster')
r1 <- raster('/small_raster')
r2 <- crop(r,extent(r1))

r1
class : RasterLayer
dimensions : 74, 157, 11618 (nrow, ncol, ncell)
resolution : 0.0833333, 0.0833333 (x, y)
extent : 89.2185, 102.3018, 30.96238, 37.12905 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : D:DtempRtestmodis8km.tif
names : modis8km
values : -32768, 32767 (min, max)

r2
class : RasterLayer
dimensions : 74, 157, 11618 (nrow, ncol, ncell)
resolution : 0.08333333, 0.08333333 (x, y)
extent : 89.25, 102.3333, 31, 37.16667 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : g201401a
values : -32768, 7789 (min, max)


Though r1 and r1 with same resolution and dimension, the extent have tiny offset. It cause stack error.



 stack(r1,r2)
Error in compareRaster(x) : different extent


So, you should rebuid the r2 to match r1:



r22 <- raster(vals=values(r2),ext=extent(r1),crs=crs(r1),
nrows=dim(r1)[1],ncols=dim(r1)[2])


Now stack(r22,r1) will be successful.






share|improve this answer


























  • Thank you. My r1 extent is: 112.8921, 154.0246, -44.00042, -7.999583, and my r2 extent after cropping is: 112.8917, 154.025, -44, -8. But I have got the following error: r22 <- raster(vals=values(r2),ext=extent(r1), nrows=dim(r1)[1], ncols=dim(r1)[2]). Error in setValues(r, vals) : length(values) is not equal to ncell(x), or to 1 @Cobin

    – Crayfish
    Nov 23 '18 at 5:51











  • The main problem is that the small rasster is not snapping big raster.The rasters have different rows and collumns (width and height) after crop.You should show bio2.au and awc atrribute in question. Maybe cellFrom function to crop raster is preciser. @Crayfish

    – Cobin
    Nov 23 '18 at 6:27













  • Thank you. I have now added raster attributes in my main question. @Cobin

    – Crayfish
    Nov 23 '18 at 7:38
















1














I suppose that the extent of two raster are differet though the raster masked by crop function.You
should check the both of awc and bio.au extent base on same reolution, rows and columns. Because I couldn't download data from
hyperlink, I give an example of my own data.



r <- raster('/big_raster')
r1 <- raster('/small_raster')
r2 <- crop(r,extent(r1))

r1
class : RasterLayer
dimensions : 74, 157, 11618 (nrow, ncol, ncell)
resolution : 0.0833333, 0.0833333 (x, y)
extent : 89.2185, 102.3018, 30.96238, 37.12905 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : D:DtempRtestmodis8km.tif
names : modis8km
values : -32768, 32767 (min, max)

r2
class : RasterLayer
dimensions : 74, 157, 11618 (nrow, ncol, ncell)
resolution : 0.08333333, 0.08333333 (x, y)
extent : 89.25, 102.3333, 31, 37.16667 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : g201401a
values : -32768, 7789 (min, max)


Though r1 and r1 with same resolution and dimension, the extent have tiny offset. It cause stack error.



 stack(r1,r2)
Error in compareRaster(x) : different extent


So, you should rebuid the r2 to match r1:



r22 <- raster(vals=values(r2),ext=extent(r1),crs=crs(r1),
nrows=dim(r1)[1],ncols=dim(r1)[2])


Now stack(r22,r1) will be successful.






share|improve this answer


























  • Thank you. My r1 extent is: 112.8921, 154.0246, -44.00042, -7.999583, and my r2 extent after cropping is: 112.8917, 154.025, -44, -8. But I have got the following error: r22 <- raster(vals=values(r2),ext=extent(r1), nrows=dim(r1)[1], ncols=dim(r1)[2]). Error in setValues(r, vals) : length(values) is not equal to ncell(x), or to 1 @Cobin

    – Crayfish
    Nov 23 '18 at 5:51











  • The main problem is that the small rasster is not snapping big raster.The rasters have different rows and collumns (width and height) after crop.You should show bio2.au and awc atrribute in question. Maybe cellFrom function to crop raster is preciser. @Crayfish

    – Cobin
    Nov 23 '18 at 6:27













  • Thank you. I have now added raster attributes in my main question. @Cobin

    – Crayfish
    Nov 23 '18 at 7:38














1












1








1







I suppose that the extent of two raster are differet though the raster masked by crop function.You
should check the both of awc and bio.au extent base on same reolution, rows and columns. Because I couldn't download data from
hyperlink, I give an example of my own data.



r <- raster('/big_raster')
r1 <- raster('/small_raster')
r2 <- crop(r,extent(r1))

r1
class : RasterLayer
dimensions : 74, 157, 11618 (nrow, ncol, ncell)
resolution : 0.0833333, 0.0833333 (x, y)
extent : 89.2185, 102.3018, 30.96238, 37.12905 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : D:DtempRtestmodis8km.tif
names : modis8km
values : -32768, 32767 (min, max)

r2
class : RasterLayer
dimensions : 74, 157, 11618 (nrow, ncol, ncell)
resolution : 0.08333333, 0.08333333 (x, y)
extent : 89.25, 102.3333, 31, 37.16667 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : g201401a
values : -32768, 7789 (min, max)


Though r1 and r1 with same resolution and dimension, the extent have tiny offset. It cause stack error.



 stack(r1,r2)
Error in compareRaster(x) : different extent


So, you should rebuid the r2 to match r1:



r22 <- raster(vals=values(r2),ext=extent(r1),crs=crs(r1),
nrows=dim(r1)[1],ncols=dim(r1)[2])


Now stack(r22,r1) will be successful.






share|improve this answer















I suppose that the extent of two raster are differet though the raster masked by crop function.You
should check the both of awc and bio.au extent base on same reolution, rows and columns. Because I couldn't download data from
hyperlink, I give an example of my own data.



r <- raster('/big_raster')
r1 <- raster('/small_raster')
r2 <- crop(r,extent(r1))

r1
class : RasterLayer
dimensions : 74, 157, 11618 (nrow, ncol, ncell)
resolution : 0.0833333, 0.0833333 (x, y)
extent : 89.2185, 102.3018, 30.96238, 37.12905 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : D:DtempRtestmodis8km.tif
names : modis8km
values : -32768, 32767 (min, max)

r2
class : RasterLayer
dimensions : 74, 157, 11618 (nrow, ncol, ncell)
resolution : 0.08333333, 0.08333333 (x, y)
extent : 89.25, 102.3333, 31, 37.16667 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : g201401a
values : -32768, 7789 (min, max)


Though r1 and r1 with same resolution and dimension, the extent have tiny offset. It cause stack error.



 stack(r1,r2)
Error in compareRaster(x) : different extent


So, you should rebuid the r2 to match r1:



r22 <- raster(vals=values(r2),ext=extent(r1),crs=crs(r1),
nrows=dim(r1)[1],ncols=dim(r1)[2])


Now stack(r22,r1) will be successful.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 6:34

























answered Nov 23 '18 at 5:05









CobinCobin

338212




338212













  • Thank you. My r1 extent is: 112.8921, 154.0246, -44.00042, -7.999583, and my r2 extent after cropping is: 112.8917, 154.025, -44, -8. But I have got the following error: r22 <- raster(vals=values(r2),ext=extent(r1), nrows=dim(r1)[1], ncols=dim(r1)[2]). Error in setValues(r, vals) : length(values) is not equal to ncell(x), or to 1 @Cobin

    – Crayfish
    Nov 23 '18 at 5:51











  • The main problem is that the small rasster is not snapping big raster.The rasters have different rows and collumns (width and height) after crop.You should show bio2.au and awc atrribute in question. Maybe cellFrom function to crop raster is preciser. @Crayfish

    – Cobin
    Nov 23 '18 at 6:27













  • Thank you. I have now added raster attributes in my main question. @Cobin

    – Crayfish
    Nov 23 '18 at 7:38



















  • Thank you. My r1 extent is: 112.8921, 154.0246, -44.00042, -7.999583, and my r2 extent after cropping is: 112.8917, 154.025, -44, -8. But I have got the following error: r22 <- raster(vals=values(r2),ext=extent(r1), nrows=dim(r1)[1], ncols=dim(r1)[2]). Error in setValues(r, vals) : length(values) is not equal to ncell(x), or to 1 @Cobin

    – Crayfish
    Nov 23 '18 at 5:51











  • The main problem is that the small rasster is not snapping big raster.The rasters have different rows and collumns (width and height) after crop.You should show bio2.au and awc atrribute in question. Maybe cellFrom function to crop raster is preciser. @Crayfish

    – Cobin
    Nov 23 '18 at 6:27













  • Thank you. I have now added raster attributes in my main question. @Cobin

    – Crayfish
    Nov 23 '18 at 7:38

















Thank you. My r1 extent is: 112.8921, 154.0246, -44.00042, -7.999583, and my r2 extent after cropping is: 112.8917, 154.025, -44, -8. But I have got the following error: r22 <- raster(vals=values(r2),ext=extent(r1), nrows=dim(r1)[1], ncols=dim(r1)[2]). Error in setValues(r, vals) : length(values) is not equal to ncell(x), or to 1 @Cobin

– Crayfish
Nov 23 '18 at 5:51





Thank you. My r1 extent is: 112.8921, 154.0246, -44.00042, -7.999583, and my r2 extent after cropping is: 112.8917, 154.025, -44, -8. But I have got the following error: r22 <- raster(vals=values(r2),ext=extent(r1), nrows=dim(r1)[1], ncols=dim(r1)[2]). Error in setValues(r, vals) : length(values) is not equal to ncell(x), or to 1 @Cobin

– Crayfish
Nov 23 '18 at 5:51













The main problem is that the small rasster is not snapping big raster.The rasters have different rows and collumns (width and height) after crop.You should show bio2.au and awc atrribute in question. Maybe cellFrom function to crop raster is preciser. @Crayfish

– Cobin
Nov 23 '18 at 6:27







The main problem is that the small rasster is not snapping big raster.The rasters have different rows and collumns (width and height) after crop.You should show bio2.au and awc atrribute in question. Maybe cellFrom function to crop raster is preciser. @Crayfish

– Cobin
Nov 23 '18 at 6:27















Thank you. I have now added raster attributes in my main question. @Cobin

– Crayfish
Nov 23 '18 at 7:38





Thank you. I have now added raster attributes in my main question. @Cobin

– Crayfish
Nov 23 '18 at 7:38




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440229%2fmake-raster-stack-with-different-extent%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()