Skip to content

Instantly share code, notes, and snippets.

@jhollist
Created March 31, 2015 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhollist/b73440a1d46de3e28461 to your computer and use it in GitHub Desktop.
Save jhollist/b73440a1d46de3e28461 to your computer and use it in GitHub Desktop.
Pass complex sp polygon to spocc
devtools::install_github("jhollist/quickmapr")
devtools::install_github("ropensci/spocc")
install.packages("rgeos")
library("quickmapr")
library("spocc")
library("rgeos")
library("magrittr")
data(lake)
## sp bbox to poly
bbox_to_sp <- function(sp) {
bbox <- bbox(sp)
x <- c(bbox[1, 1], bbox[1, 1], bbox[1, 2], bbox[1, 2], bbox[1, 1])
y <- c(bbox[2, 1], bbox[2, 2], bbox[2, 2], bbox[2, 1], bbox[2, 1])
p <- Polygon(cbind(x, y))
ps <- Polygons(list(p), "p1")
sp <- SpatialPolygons(list(ps), 1L, proj4string = CRS(proj4string(sp)))
return(sp)
}
## extract spatial from poly
extract_spatial <- function(in_sp, over_poly) {
idx <- over(in_sp, over_poly)
idx <- !is.na(idx)
return(in_sp[idx, ])
}
lake_buffer <- lake %>%
gBuffer(width = 1000) %>%
spTransform(CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))
spocc_sunapee <- lake_buffer %>%
bbox_to_sp() %>%
occ(geometry = .) %>%
occ_to_sp(coord_string = proj4string(lake_buffer)) %>%
extract_spatial(lake_buffer)
qmap(lake_buffer, spocc_sunapee)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment