0
votes

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.

"DEM"

My cropped (according to extent) state shapefile looks like this: enter image description here

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!). enter image description here

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. enter image description here

NOTE: the white elevations within the watershed boundaries are masked out on purpose.

1

1 Answers

0
votes

If I understand you well, you are looking for raster::crop

library(raster)
b <- as(extent(6, 6.4, 49.75, 50), 'SpatialPolygons')
crs(b) <- "+proj=longlat +datum=WGS84"    
p <- shapefile(system.file("external/lux.shp", package="raster"))

pb <- crop(p, b)

If I did not understand you well, then please clarify your answer using simple example data like I do. There is little value in referring to objects that others do not have.