## ----sdm10A------------------------------------------------------------------- library(terra) library(predicts) filename <- file.path(system.file(package="predicts"), "ex/bradypus.csv") # this is the file we will use: basename(filename) ## ----sdm11A------------------------------------------------------------------- bradypus <- read.csv(filename) # first rows head(bradypus) # we only need columns 2 and 3: bradypus <- bradypus[,2:3] head(bradypus) ## ----sdm11B, eval=FALSE------------------------------------------------------- ## acaule <- geodata::sp_occurrence("solanum", "acaule*", geo=FALSE) ## ## Loading required namespace: jsonlite ## ## 7238 records found ## ## 0-300-600-900-1200-1500-1800-2100-2400-2700-3000-3300-3600-3900-4200 ## ----sdm2--------------------------------------------------------------------- # load the saved S. acaule data acfile <- file.path(system.file(package="predicts"), "ex/acaule.csv") acaule <- read.csv(acfile) # how many rows and colums? dim(acaule) #select the records that have longitude and latitude data colnames(acaule) acgeo <- subset(acaule, !is.na(lon) & !is.na(lat)) dim(acgeo) # show some values acgeo[1:5, c(1:5,7:10)] ## ----sdm3--------------------------------------------------------------------- library(geodata) wrld <- world(path=".") plot(wrld, xlim=c(-110,60), ylim=c(-80,40), col="light yellow", border="light gray") # add the points points(acgeo$lon, acgeo$lat, col='red', pch=20) ## ----sdm4a-------------------------------------------------------------------- acgeo[c(303,885),1:10] ## ----sdm4b-------------------------------------------------------------------- lonzero <- subset(acgeo, lon==0) # show all records, only the first 13 columns lonzero[, 1:13] ## ----sdm5a-------------------------------------------------------------------- # which records are duplicates (only for the first 10 columns)? dups <- duplicated(lonzero[, 1:10]) # remove duplicates lonzero <- lonzero[dups, ] lonzero[,1:13] ## ----sdm5b-------------------------------------------------------------------- # differentiating by (sub) species # dups2 <- duplicated(acgeo[, c('species', 'lon', 'lat')]) # ignoring (sub) species and other naming variation dups2 <- duplicated(acgeo[, c('lon', 'lat')]) # number of duplicates sum(dups2) # keep the records that are _not_ duplicated acg <- acgeo[!dups2, ] ## ----sdm5c-------------------------------------------------------------------- i <- acg$lon > 0 & acg$lat > 0 acg$lon[i] <- -1 * acg$lon[i] acg$lat[i] <- -1 * acg$lat[i] acg <- acg[acg$lon < -60 & acg$lat > -50, ] ## ----sdm6a-------------------------------------------------------------------- library(terra) acv <- vect(acg, geom=c("lon", "lat"), crs="+proj=longlat +datum=WGS84") class(acv) ## ----sdm6b-------------------------------------------------------------------- ovr <- extract(acv, wrld) ## ----sdm6c-------------------------------------------------------------------- head(ovr) cntr <- ovr$NAME_0 ## ----sdm6d-------------------------------------------------------------------- i <- which(is.na(cntr)) i j <- which(cntr != acv$country) # for the mismatches, bind the country names of the polygons and points m <- cbind(cntr[j], acg$country[j]) colnames(m) <- c("polygons", "acaule") m ## ----sdm6e-------------------------------------------------------------------- plot(acv) lines(wrld, col='blue', lwd=2) points(acv[j, ], col='red', pch=20, cex=2) ## ----sdm8--------------------------------------------------------------------- georef <- subset(acaule, (is.na(lon) | is.na(lat)) & ! is.na(locality) ) dim(georef) georef[1:3,1:13] ## ----sdm9, eval=FALSE--------------------------------------------------------- ## georef$cloc[4] ## #b <- geocode(georef$cloc[4], geo_key="abcdef" ) ## #b ## ----sdm100A------------------------------------------------------------------ # create a SpatRaster with the extent of acgeo r <- rast(acv) # set the resolution of the cells to (for example) 1 degree res(r) <- 1 # extend (expand) the extent of the SpatRaster a little r <- extend(r, ext(r)+1) # sample: set.seed(13) acsel <- spatSample(acv, size=1, "random", strata=r) # to illustrate the method and show the result p <- as.polygons(r) plot(p, border='gray') points(acv) # selected points in red points(acsel, cex=1, col='red', pch='x') ## ----sdm12-------------------------------------------------------------------- file <- paste(system.file(package="predicts"), '/ex/acaule.rds', sep='') acsel <- readRDS(file)