## ---- echo=FALSE, include=FALSE----------------------------------------------- library(knitr) opts_chunk$set(fig.width = 5, fig.height = 5, fig.cap='', collapse = TRUE) library(dismo) ## ---- sdm15a------------------------------------------------------------------ library(dismo) # get the file names files <- list.files(path=paste(system.file(package="dismo"), '/ex', sep=''), pattern='grd', full.names=TRUE ) # we use the first file to create a RasterLayer mask <- raster(files[1]) # select 500 random points # set seed to assure that the examples will always # have the same random sample. set.seed(1963) bg <- randomPoints(mask, 500 ) ## ---- sdm15, fig.width=9, fig.height=6---------------------------------------- # set up the plotting area for two maps par(mfrow=c(1,2)) plot(!is.na(mask), legend=FALSE) points(bg, cex=0.5) # now we repeat the sampling, but limit # the area of sampling using a spatial extent e <- extent(-80, -53, -39, -22) bg2 <- randomPoints(mask, 50, ext=e) plot(!is.na(mask), legend=FALSE) plot(e, add=TRUE, col='red') points(bg2, cex=0.5) ## ---- sdm16a------------------------------------------------------------------ file <- paste(system.file(package="dismo"), '/ex/acaule.csv', sep='') ac <- read.csv(file) ## ---- sdm16b------------------------------------------------------------------ coordinates(ac) <- ~lon+lat projection(ac) <- CRS('+proj=longlat +datum=WGS84') ## ---- sdm17------------------------------------------------------------------- # circles with a radius of 50 km x <- circles(ac, d=50000, lonlat=TRUE) pol <- polygons(x) ## ---- sdm19------------------------------------------------------------------- # sample randomly from all circles samp1 <- spsample(pol, 250, type='random', iter=25) # get unique cells cells <- cellFromXY(mask, samp1) length(cells) cells <- unique(cells) length(cells) xy <- xyFromCell(mask, cells) ## ---- sdm20, fig.width=9, fig.height=6---------------------------------------- plot(pol, axes=TRUE) points(xy, cex=0.75, pch=20, col='blue') ## ---- sdm21a------------------------------------------------------------------ spxy <- SpatialPoints(xy, proj4string=CRS('+proj=longlat +datum=WGS84')) o <- over(spxy, geometry(x)) xyInside <- xy[!is.na(o), ] ## ---- sdm21b, fig.width=9, fig.height=6--------------------------------------- # extract cell numbers for the circles v <- extract(mask, x@polygons, cellnumbers=T) # use rbind to combine the elements in list v v <- do.call(rbind, v) # get unique cell numbers from which you could sample v <- unique(v[,1]) head(v) # to display the results m <- mask m[] <- NA m[v] <- 1 plot(m, ext=extent(x@polygons)+1) plot(x@polygons, add=T)