I'm trying to clip (not crop according to extent) U.S. state and county shapefiles to match the boundaries of this DEM (digital elevation model) clipped raster.
My cropped (according to extent) state shapefile looks like this:
How do I clip the state shapefile to match the complex outline of the DEM? I have tried intersecting the two, which works, but that does not distinguish between the state and watershed boundaries, making the map very difficult to read (especially with counties!).
huc6_cty_intersect <- raster::intersect(huc6s, counties)
I need to be able to overlay the states and counties separately, so I can change their border width, color, etc so the maps are readable.
I have tried rasterizing the state and county shapefiles, and then masking by the dem, but that seems to take a very, very long time (overnight was not enough time).
counties_rast <- rasterize(counties, dem)
r4 <- raster::mask(counties_rast, huc6_cty_intersect)
I am trying to get to the following image, but with all state boundaries outside the watersheds chopped off. Any help in how to do this would be wonderful. Thank you.
NOTE: the white elevations within the watershed boundaries are masked out on purpose.