Skip to content

Instantly share code, notes, and snippets.

View PhillRob's full-sized avatar
🌐
At the office (for most of the time)

Philipp R PhillRob

🌐
At the office (for most of the time)
View GitHub Profile
@PhillRob
PhillRob / getGeoCode.R
Last active September 17, 2015 08:21
long/lat data from location description
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
@PhillRob
PhillRob / nat.year.nzpcn.R
Last active September 1, 2015 11:44
this runs through nzpcn.org.nz and gets native range and naturalised year. used to run: w<-matrix(rep(1:7700)) test <- sapply(w, function(x) nat.year.nzpcn(x))
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))
@PhillRob
PhillRob / stratifiedDT.R
Last active August 29, 2015 14:27 — forked from mrdwab/stratifiedDT.R
Attempt to rewrite stratified for `data.table`. The `data.frame` version can be found at https://gist.github.com/mrdwab/6424112
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 {
@PhillRob
PhillRob / point.2.poly
Last active August 29, 2015 14:24
match point data to country/state/region boundary polygon
# 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
@PhillRob
PhillRob / reverseGeo
Last active August 29, 2015 14:24
use google API to get match lon/lat data to country and state
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) {
@PhillRob
PhillRob / rbind.keep.columns
Last active August 29, 2015 14:24
rbind data frames with miss matching columns
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))
}