Skip to content
Prev 19846 / 29559 Next

Problem with cell numbers when using a raster extent object

Robert,

Here are examples of the two versions.

### This one does not select points within the extent ###

library(raster)
crs_predImage <- CRS("+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs 
+ellps=WGS84 +towgs84=0,0,0")
predImage <- raster(nrows=7561, ncols=8521, xmn=240585, xmx=496215, 
ymn=5297985, ymx=5524815)
projection(predImage) <- crs_predImage
predImage[] <- 1

commonExt <- extent(366760.6 , 425233.5, 5396052, 5450158)

sampleCells <- sampleRandom(predImage, size=20, sp=TRUE, extent=commonExt)

plot(predImage)
plot(commonExt, add=T)
plot(sampleCells, add=T)


### This one does work ###

library(raster)
crs_predImage <- CRS("+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs 
+ellps=WGS84 +towgs84=0,0,0")
predImage <- raster(nrows=7561, ncols=8521, xmn=240585, xmx=496215, 
ymn=5297985, ymx=5524815)
projection(predImage) <- crs_predImage
predImage[] <- 1

commonExt <- extent(366760.6 , 425233.5, 5396052, 5450158)

sampleCells <- sampleRandom(crop(predImage,commonExt), size=20, sp=TRUE, 
ext=commonExt, na.rm=TRUE)

plot(predImage)
plot(commonExt, add=T)
plot(sampleCells, add=T)
On 11/25/2013 01:04 PM, Robert J. Hijmans wrote: