Worldclim database is not working for random samples

36 views Asked by At

I have a raster with a forest cover of one area

    class       : RasterLayer 
dimensions  : 5436, 2633, 14312988  (nrow, ncol, ncell)
resolution  : 100, 100  (x, y)
extent      : -109346.5, 153953.5, -290837.1, 252762.9  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=tmerc +lat_0=39.66825833333333 +lon_0=-8.133108333333334 +k=1 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs 
data source : c://Total_Forest_cove
names       : Total_Forest_cove

with values from 0 to 3 I want to create a dataset with random samples from the pixels with values higher than 0.

For that I do

library(dismo)
Forestcover[Forestcover < 1] <- NA
randompoints <- sampleRandom(Forestcover, size = 10, sp=TRUE, na.rm=TRUE)
plot(Forestcover, axes=FALSE, legend=FALSE)
randompoints <- coordinates(randompoints)

when I try to pull out the enviromental variables form worldclim

randpoints_wc <- extract(worldclim, randompoints)
randpoints_wc <- cbind(randompoints, randpoints_wc) 

My dataset is always empty

Not sure if there is other method to extract random that can be apply here.

Regards

1

There are 1 answers

0
Robert Hijmans On BEST ANSWER

Your forest cover data set has a Mercator coordinate reference system

coord. ref. : +proj=tmerc 

You do not show your WorldClim data, but presumably it has

coord. ref. : +proj=longlat

So, it is no surprise (and a good thing) that you get an empty result set. You can use rgadl::spTransform to make the coordinates match the raster.

For example:

fc <- reclassify(ForestCover, cbind(1, Inf, NA))     
randompoints <- sampleRandom(fc, size = 10, sp=TRUE, na.rm=TRUE)

library(rgdal)
rp <- spTransform(randompoints, CRS("+proj=longlat +datum=WGS84"))


rp_wc <- extract(worldclim, rp)
rp_wc <- cbind(coordinates(randompoints), coordinates(rp), rp_wc)