Script to grab land use data

https://pakillo.github.io/R-GIS-tutorial/#vector

load libraries

*tidyr and magrittr mess up extract function

library(foreign)
#library(dplyr)
#library(magrittr)
#library(tidyr)
library(gridExtra)
library(ggplot2)
#spatial
library(raster)
## Loading required package: sp
library(rasterVis)
## Warning: package 'rasterVis' was built under R version 3.3.2
## Loading required package: lattice
## Loading required package: latticeExtra
## Loading required package: RColorBrewer
## 
## Attaching package: 'latticeExtra'
## The following object is masked from 'package:ggplot2':
## 
##     layer
library(rgdal)
## rgdal: version: 1.1-10, (SVN revision 622)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 1.11.4, released 2016/01/25
##  Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rgdal/gdal
##  Loaded PROJ.4 runtime: Rel. 4.9.1, 04 March 2015, [PJ_VERSION: 491]
##  Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rgdal/proj
##  Linking to sp version: 1.2-3
library(dismo)

Test case for 1 species

#load image of land use into R
x<-raster("GLOBCOVER_L4_200901_200912_V2.3.tif")

#plot it to see if we did it right
plot(x,axes=T,xlim=c(-150,-30),ylim=c(-50,50))

##reading in Triatoma nitida
#Tnit<-read.csv("Globcover2009_V2.3_Global_/Triatoma_nitida.csv",header=TRUE)
#points(Tnit$Lon,Tnit$Lat,cex=1,pch=16,col="red") run this code with previous line to put points onto the map

#no clue waht this does, but following the code
#coordinates(Tnit)<-Tnit[,c("Lon","Lat")]
#it looks like it converted lon lat into spaace
#plot(Tnit)

#important code: projecting points from lat lon into space
crs.go<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")
#proj4string(Tnit)<-crs.go
#summary(Tnit)

#extract values of lat lon from the raster file
#Tnit$worked<-extract(x,cbind(Tnit$Lon,Tnit$Lat))
#Tnit$worked

For whole dataset presence

whole<-read.csv("20160328_species_distribution.csv",skip=0)#"metadata on first 2 lines"
coordinates(whole)<-whole[,c("Lon","Lat")]
plot(whole)

proj4string(whole)<-crs.go
summary(whole)
## Object of class SpatialPointsDataFrame
## Coordinates:
##           min       max
## Lon -121.9043 -35.58767
## Lat  -45.9382  50.84757
## Is projected: FALSE 
## proj4string :
## [+proj=longlat +ellps=WGS84 +datum=WGS84 +towgs84=0,0,0]
## Number of points: 889
## Data attributes:
##          Species         Locality        Lat               Lon         
##  T_dimidiata :122   Ponto 12 : 18   Min.   :-45.938   Min.   :-121.90  
##  T_infestans : 67   Ponto 13 : 17   1st Qu.:-20.772   1st Qu.: -84.81  
##  T_sordida   : 58   Ponto 14 : 17   Median : -4.653   Median : -66.18  
##  T_maculata  : 42   Ponto 10 : 15   Mean   : -1.736   Mean   : -70.68  
##  T_guasayana : 37   Ponto 11 : 15   3rd Qu.: 14.690   3rd Qu.: -57.17  
##  T_patagonica: 35   Ponto 8  : 15   Max.   : 50.848   Max.   : -35.59  
##  (Other)     :528   (Other)  :792                                      
##  Elevation.feet.       Binary      
##  Min.   :    0.1   Min.   :0.0000  
##  1st Qu.:  335.6   1st Qu.:0.0000  
##  Median :  903.4   Median :1.0000  
##  Mean   : 1651.8   Mean   :0.6063  
##  3rd Qu.: 2085.7   3rd Qu.:1.0000  
##  Max.   :17835.2   Max.   :1.0000  
##  NA's   :48
#plotting out points
plot(x,axes=T,main="Species distributions",xlim=c(-130,-30),ylim=c(-60,60))
points(whole$Lon,whole$Lat,cex=.5,pch=16,col="red")

#grabbing land use
whole$land.use<-extract(x,cbind(whole$Lon,whole$Lat))
whole$land.use
##   [1]  50  50 190 130 130 130 140  40  40  20  40  30  50  50 130  20  30
##  [18]  30  14  20  40  20  14 130  20  20 130  20  20  14 130  40  30  50
##  [35]  40 180  40  40  20  40  20 130 130  40  20  14  14  20  30  30  20
##  [52]  30 130 110  40 110 140 180 190 190  40  40  40  40  40  40  40 130
##  [69]  40  40 130  30  30  20  30 130  30  30  30  40 180  14  14  30  30
##  [86]  30  30  14  40  20  40  40  40  40  40  40  20  30 130  30  14  40
## [103]  40  40  40  40  40  40  40  40  40  40  14 120 130  14 120 190 120
## [120] 190 190  30 110 110 190  40  50  30 130  40  40  40  40  20  30 130
## [137] 130  40  40  40  40  40  40 210 140  30 140  20 140  40  30 140  14
## [154]  30  40 210  40  30  40  30 130 150  30 110  40 110  40  40  40  40
## [171] 140 140 140  30  40 110 140  40  40  20  30  20  20  30  30  30  40
## [188]  20  20  40 140  30 110 130 140 140  50 190  50 130 140  70 140 190
## [205]  40  40 140 190  40  30 130 140 140 120 140 110  30  20 100  30  40
## [222] 110  30  30 140 140  30  30  40 130 130  30 140  40 210  40  40 190
## [239]  40 120  40 130 120 140  50 100  70  50 140  70  30  40  40  40  40
## [256]  40  40  40  40  40  40  40  40 130  40 140  40  30  40  40  40  40
## [273]  40  40  40  40 140  40 120 130 190 120  30 130 120 130 190 200 120
## [290] 190  40  14  30  40  14  40  40  30  20  20 190  50 110 190 190 210
## [307]  50 130 120 120 200 130 120 110  30 120 160  40  40  40  40  40  40
## [324]  40  40  40 130 120 130 110 130 190 130 130 140  30 130  20 140 140
## [341] 140  50  40  20  40  40  30  14 120 130  14  20  30 110 190 120 130
## [358] 190 200 120 130 190 210 190  40 120 190  40  30  50  20  50  30  40
## [375]  40  40  40  40  40  40  40  40  40  40 190 110 120  30 130  14  30
## [392] 110 110 190 120 130 130 120 130 190 200 200 110 190 130 130  14  14
## [409] 210  20  14  14  30 110 200 120  20 190  50  40 200 190 200 200 130
## [426] 130  20  14  40  20  40  40  40  20  20  50  30 120 130 130 130  40
## [443]  14 160  20  30  30 140 140 110  40  20  14 140 160  40  40  40  40
## [460]  40 130  20 130  30  40 140 110  30 210  30 120 140 210  40  20 140
## [477] 140  30  30 210  30 140  40  30 110  20  30 140  40  30 160  40  40
## [494]  40  30  30  20  50  20  40 130  20  30  40  20  40 130 130  20 130
## [511]  30  40  40  40  40  40  40 130  20  30  40  20 130  20  20  14  40
## [528]  40  40  40  40  50  40  20  40  40 130  70 120 140 130  30 130  50
## [545] 130  50  30 140  40 140  40  40  40  30  30 210 140 110  20 140  30
## [562] 160  40  40 190  40 130  40 130  14  40  40  40  40 180  40  40  50
## [579]  40 140  30  50  70 100  70  30 140  14 140 130  30 110 110 130 120
## [596] 120 130 190  14 120  14 130 130 200 120 190 120 190 130 110  14  30
## [613]  14  30 180  40  40  40  40  40  40  40  40  40 130  20  30 130 160
## [630]  40  40  40  40  40 130 130  40  30 120 130  70  70 140 130  30 130
## [647]  50 130  30 190 130 130 130 130 110  40  40  20 140  30  14  50  50
## [664]  50  30 130  20 190  30  14  20 130  20  20  20  40  14 130 130  40
## [681] 130  40  40  40  40  40  40  40  20  30 130  70  50  50 130 130  50
## [698]  70  50  50  30 120  20  40  30  40 130  70 120 140 130 130 130  50
## [715]  50  30 120 140 140  30  70  50  50 140  40  20  20  70 130  14  30
## [732] 130  40  30  30  20  20  30  14  40  40  40  40  40  30 130  40  40
## [749]  20  50  70  30  30  70  50  70  70  14  20  50  20 190 170 140  70
## [766]  50  70 140 140  50  30  70 120  50 130 130  70 130 130  30  70 140
## [783] 140  70  50 130  30  50  50  20 130  70 140 110 140  50 130  14 180
## [800]  30 130 210 120  20  50  50  14  40 110  40 130  30  40  40  14  20
## [817]  50  20  14  40 130  14  40  20 120 190  40  20  20 130  40 190 130
## [834] 130  20  40  30  50  50  14  20  20  20  30 210  40  40  40 160  40
## [851] 180  40  40  40 130  20  40  20  40  20 190  40 190  14 190 130  40
## [868]  40  40  40  40  40  40  40  40  40  40  40 110  40  40  40  40  40
## [885]  30  40  40 140  40
#write.csv(whole,"20160328_species_distribution_with_LANDUSE.csv")

For absence

none<-read.csv("20160328_species_distribution.csv")
str(none)
none<-na.omit(none);dim(none)
coordinates(none)<-none[,c("Lon","Lat")]
plot(none)

proj4string(none)<-crs.go
summary(none)


#plotting out points
plot(x,axes=T,main="Species distributions")
points(none$Lon,none$Lat,cex=.5,pch=16,col="red")

#grabbing land use
none$land.use<-extract(x,cbind(none$Lon,none$Lat))
none$land.use
write.csv(none,"20160227_absence_species_with_land_use.csv")

Session info

sessionInfo()
## R version 3.3.1 (2016-06-21)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: OS X 10.12.1 (Sierra)
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] dismo_1.1-1         rgdal_1.1-10        rasterVis_0.41     
##  [4] latticeExtra_0.6-28 RColorBrewer_1.1-2  lattice_0.20-33    
##  [7] raster_2.5-8        sp_1.2-3            ggplot2_2.1.0      
## [10] gridExtra_2.2.1     foreign_0.8-66     
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.7       knitr_1.14        magrittr_1.5     
##  [4] munsell_0.4.3     viridisLite_0.1.3 colorspace_1.2-6 
##  [7] stringr_1.1.0     plyr_1.8.4        tools_3.3.1      
## [10] parallel_3.3.1    grid_3.3.1        gtable_0.2.0     
## [13] htmltools_0.3.5   yaml_2.1.13       digest_0.6.10    
## [16] formatR_1.4       evaluate_0.9      rmarkdown_1.0    
## [19] stringi_1.1.2     scales_0.4.0      hexbin_1.27.1    
## [22] zoo_1.7-13