Skip to content

Instantly share code, notes, and snippets.

@jebyrnes
Last active January 12, 2018 20:16
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 jebyrnes/c2e8d2b6c82849dad3a813d952ab8bb0 to your computer and use it in GitHub Desktop.
Save jebyrnes/c2e8d2b6c82849dad3a813d952ab8bb0 to your computer and use it in GitHub Desktop.
crop out coasts with R
###########
# Libraries
###########
library(sp)
library(rgdal)
library(rgeos)
library(raster)
library(tidyverse)
library(spdplyr)
#### Load polygons
#data from https://www.nceas.ucsb.edu/~byrnes/floating_forests/ff_polys_proj.rds
classifications <- readRDS("../../data/output/consensus_shapefiles/ff_polys_proj.rds")
rownames(classifications@data) <- row.names(classifications) #this was throwing some oddness
#### Load coastline
#data from https://github.com/Irosenthal/Floating_Forests with this path
ca_coastline <- readOGR("../../data/coastlines/California", layer="California")
#Clip out anything on land in California
clip <- gDifference(classifications, ca_coastline, byid=TRUE)
row.names(clip) <- gsub(" .*", "", row.names(clip))
all_spdfs_together_cropped <- SpatialPolygonsDataFrame(clip,
data=classifications@data)
#check to make sure it worked
plot(all_spdfs_together_cropped %>% filter(threshold==1), border="red")
plot(ca_coastline, add=TRUE)
@jebyrnes
Copy link
Author

Hrm - after more feedback, is raster::erase just the solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment