This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rbind.keep.columns <- function(x, y) { | |
x.dif <- setdiff(colnames(x), colnames(y)) | |
y.dif <- setdiff(colnames(y), colnames(x)) | |
x[, c(as.character(y.dif))] <- NA | |
y[, c(as.character(x.dif))] <- NA | |
return(rbind(x, y)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
reverseGeo <- function(latlng) { | |
require("XML") | |
require("httr") | |
latlng <- as.numeric(latlng) | |
latlngStr <- gsub(" ", "%20", paste(round(latlng, 2), collapse = ",")) | |
url <- "http://maps.google.com" | |
path <- "/maps/api/geocode/xml" | |
query <- list(sensor = "false", latlng = latlngStr) | |
response <- GET(url, path = path, query = query) | |
if (response$status != 200) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Matching point records to polygones boundaries of a country, state or | |
# region polygones for regions of NZ | |
require(maptools) | |
library(maps) | |
library(SDMTools) | |
# obtain lon/lat boundaries for north and south island of NZ | |
nz.n.poly <- cbind(map("nz", regions = "North.Island")$x, map("nz", regions = "North.Island")$y) | |
nz.s.poly <- cbind(map("nz", regions = "South.Island")$x, map("nz", regions = "South.Island")$y) | |
# match to points where d[(6:5)] are the columns of a dataframe containing lon/lat data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stratifiedDT <- function(indt, group, size, select = NULL, | |
replace = FALSE, keep.rownames = FALSE, | |
bothSets = FALSE) { | |
require(data.table) | |
if (is.numeric(group)) group <- names(indt)[group] | |
if (!is.data.table(indt)) indt <- as.data.table( | |
indt, keep.rownames = keep.rownames) | |
if (is.null(select)) { | |
indt <- indt | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nat.year.nzpcn <- function(x) { | |
sp <- getURL(paste0("http://nzpcn.org.nz/flora_details.aspx?ID=", x)) | |
if (length(grep("Error: Cannot find species with ID =", sp))) { | |
return("Not_found") | |
} else { | |
flush.console() | |
sp.st <- unlist(gregexpr(pattern = "<title>", sp, ignore.case = F, | |
fixed = T)) | |
sp.end <- unlist(gregexpr(pattern = " | New Zealand Plant Conservation Network", | |
sp, ignore.case = F, fixed = T)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getGeoCode <- function(gcStr, countryID) { | |
#library("RJSONIO") #Load Library | |
print(gcStr) | |
apik<-"AIzaSyCAaXX1jrJoD19uHBUnL97xpegOUxsDeSw" | |
apik1<-"AIzaSyA1rQIGrUfizL6E4mE22RhUh_X5saYPMU0" | |
gcStr <- gsub(' ','%20',gcStr) #Encode URL Parameters | |
#Open Connection | |
data.json <- fromJSON(paste(getURL(paste('https://maps.google.com/maps/api/geocode/json?sensor=false&address=',gcStr, '&components=country:',countryID,sep="")))) | |
#'&key=',apik1 | |
#Flatten the received JSON |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get.eol.trait <- function(sp,trait) | |
{ | |
if (length(trait)==0) | |
{ | |
print("need trait") | |
stop() | |
} | |
library("reol"); library("traits"); library("dplyr") | |
pages<-DownloadSearchedTaxa(sp, to.file=F, MyKey=NULL, verbose=TRUE) | |
pages1<-as.numeric(gsub("eol","",colnames(as.data.frame(pages)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
isgg<- function(x) { | |
species<-gsub(" ","+",x) | |
sp <- getURL(paste0("http://www.issg.org/database/species/search.asp?sts=sss&st=sss&fr=1&x=16&y=13&sn=", species,"&rn=&hci=-1&ei=-1&lang=EN")) | |
if (length(grep("No invasive species currently recorded",sp))) { | |
return("Not_found") | |
} else {return("Found")} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
disp.sp <- function(x) { | |
print(x) | |
x.genus <- unlist( strsplit(x," "))[1] | |
x.species <- unlist( strsplit(x," "))[2] | |
sp.test <- getURL(paste0("http://data.kew.org/sid/SidServlet?Clade=&Order=&Family=&APG=off&Genus=", x.genus,"&Species=",x.species,"&StorBehav=0&DsFlag=on")) | |
if (grepl("0 records found.", sp.test)) | |
{ | |
#return("Not_found") | |
results<-c(x,"Not_found") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ha <- function(x){ | |
x$Lat<-(as.numeric(paste(x$Lat))) | |
x$Lon<-(as.numeric(paste(x$Lon))) | |
pre<-x[(x[,17]==0),] | |
post<-x[(x[,17]==1),] | |
if(nrow(pre)<2| nrow(post)<2){results<-paste("not enought records per phase")}else{ | |
early <- x[(which.min(x$Year)),] | |
e1 <- early[1,] | |
predist<-apply(pre, 1, function(x) distHaversine(as.numeric(e1[4:3]),as.numeric(x[4:3]), r=6378137)) | |
postdist<-apply(post, 1, function(x) distHaversine(as.numeric(e1[4:3]),as.numeric(x[4:3]), r=6378137)) |
OlderNewer