## ---- echo=FALSE, include=FALSE----------------------------------------------- library(knitr) opts_chunk$set(fig.width = 5, fig.height = 5, fig.cap='', collapse = TRUE) library(dismo) ## ---- sdm1-------------------------------------------------------------------- # loads the dismo library library(dismo) file <- paste0(system.file(package="dismo"), "/ex/bradypus.csv") # this is the file we will use: file ## ---- sdm1b------------------------------------------------------------------- bradypus <- read.table(file, header=TRUE, sep=",") # or do: bradypus <- read.csv(file) # first rows head(bradypus) # we only need columns 2 and 3: bradypus <- bradypus[,2:3] head(bradypus) ## ---- sdm2-------------------------------------------------------------------- # load the saved S. acaule data data(acaule) # 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:4, c(1:5,7:10)] ## ---- sdm3-------------------------------------------------------------------- library(maptools) data(wrld_simpl) plot(wrld_simpl, xlim=c(-80,70), ylim=c(-60,10), axes=TRUE, col="light yellow") # restore the box around the map box() # add the points points(acgeo$lon, acgeo$lat, col='orange', pch=20, cex=0.75) # plot points again to add a border, for better visibility points(acgeo$lon, acgeo$lat, col='red', cex=0.75) ## ---- sdm4a------------------------------------------------------------------- acaule[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 < -50 & acg$lat > -50, ] ## ---- sdm6a------------------------------------------------------------------- library(sp) coordinates(acg) <- ~lon+lat crs(acg) <- crs(wrld_simpl) class(acg) ## ---- sdm6b------------------------------------------------------------------- class(wrld_simpl) ovr <- over(acg, wrld_simpl) ## ---- sdm6c------------------------------------------------------------------- head(ovr) cntr <- ovr$NAME ## ---- sdm6d------------------------------------------------------------------- i <- which(is.na(cntr)) i j <- which(cntr != acg$country) # for the mismatches, bind the country names of the polygons and points cbind(cntr, acg$country)[j,] ## ---- sdm6e------------------------------------------------------------------- plot(acg) plot(wrld_simpl, add=T, border='blue', lwd=2) points(acg[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-------------------------------------------------------------------- georef$cloc[4] #b <- geocode(georef$cloc[4], geo_key="abcdef" ) #b ## ---- sdm10------------------------------------------------------------------- # create a RasterLayer with the extent of acgeo r <- raster(acg) # set the resolution of the cells to (for example) 1 degree res(r) <- 1 # expand (extend) the extent of the RasterLayer a little r <- extend(r, extent(r)+1) # sample: acsel <- gridSample(acg, r, n=1) # to illustrate the method and show the result p <- rasterToPolygons(r) plot(p, border='gray') points(acg) # selected points in red points(acsel, cex=1, col='red', pch='x') ## ---- sdm12------------------------------------------------------------------- file <- paste(system.file(package="dismo"), '/ex/acaule.csv', sep='') acsel <- read.csv(file)