Skip to content

Instantly share code, notes, and snippets.

@wuhland
Last active August 1, 2016 21:14
Show Gist options
  • Save wuhland/30445b2a125b5fdb6c7b69ae6a0a9f62 to your computer and use it in GitHub Desktop.
Save wuhland/30445b2a125b5fdb6c7b69ae6a0a9f62 to your computer and use it in GitHub Desktop.

Return vector of areas from SpatialPolygon object

sapply(slot(SP, "polygons"), slot, "area")

Function to clip SpatialPolygon to bbox (requires package 'raster')

gClip <- function(shp, bb){
  if(class(bb) == "matrix") b_poly <- as(extent(as.vector(t(bb))), "SpatialPolygons")
  else b_poly <- as(extent(bb), "SpatialPolygons")
  gIntersection(shp, b_poly, byid = T)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment