R/GIS: How to subset a shapefile by a lat-long bounding box?
You could try the following:
library(rgeos)
rivers <- readWKT("MULTILINESTRING((15 5, 1 20, 200 25), (-5 -8,-10 -8,-15 -4), (0 10,100 5,20 230))")
bbx <- readWKT("POLYGON((0 40, 20 40, 20 0, 0 0, 0 40))")
rivers.cut <- gIntersection(rivers, bbx)
plot(rivers, col="grey")
plot(bbx, add=T, lty=2)
plot(rivers.cut, add=T, col="blue")
Subset Spatial Features by Bounding Box or Polygon
Looks like you're pretty close. I added the missing step of turning the two diagonal points from xlim & ylim into an sf
object, and using their bounding box to subset the rivers.
library(sf)
# Download river shapefile here: https://www.weather.gov/gis/Rivers
# Import river data as SF
my_rivers <- st_read(dsn = '/home/x/Downloads/rivers/', layer = 'rv16my07')
#> Reading layer `rv16my07' from data source `/home/x/Downloads/rivers' using driver `ESRI Shapefile'
#> Simple feature collection with 61122 features and 17 fields
#> Geometry type: MULTILINESTRING
#> Dimension: XY
#> Bounding box: xmin: -124.7068 ymin: 25.83636 xmax: -67.11324 ymax: 52.80121
#> Geodetic CRS: NAD83
# The rivers data comes with a crs, so this step wasn't needed.
# Add a common CRS to the river dataset
#st_crs(my_rivers) <- CRS('+proj=longlat')
# Set x and y limits for the plot, then make the points an sf object,
# set the crs as the same for my_rivers
ylims <- c(30.2, 31.4)
xlims <- c(-88.3, -87)
box_coords <- tibble(x = xlims, y = ylims) %>%
st_as_sf(coords = c("x", "y")) %>%
st_set_crs(st_crs(my_rivers))
#get the bounding box of the two x & y coordintates, make sfc
bounding_box <- st_bbox(box_coords) %>% st_as_sfc()
river_subset <- st_intersection(my_rivers, bounding_box)
head(river_subset)
#> Simple feature collection with 6 features and 17 fields
#> Geometry type: LINESTRING
#> Dimension: XY
#> Bounding box: xmin: -87.07318 ymin: 30.60249 xmax: -87 ymax: 30.83638
#> Geodetic CRS: NAD83
#> IHABBSRF_I RR HUC TYPE PMILE PNAME OWNAME
#> 8616 8616 03140104001 3140104 T 0.0 BLACKWATER R 0
#> 8617 8617 03140104002 3140104 R 0.5 BLACKWATER R 0
#> 8618 8618 03140104003 3140104 R 4.2 BLACKWATER R 0
#> 8630 8630 03140104015 3140104 R 7.8 BIG COLDWATER CR 0
#> 8631 8631 03140104016 3140104 R 17.3 BIG COLDWATER CR E FK 0
#> 8634 8634 03140104019 3140104 R 17.3 BIG COLDWATER CR W FK 0
#> PNMCD OWNMCD DSRR DSHUC USDIR LEV J TERMID TRMBLV K
#> 8616 3140104001 <NA> 3140105007 3140105 R 1 0 205 1 0
#> 8617 3140104001 <NA> 3140104001 3140104 R 1 1 205 1 0
#> 8618 3140104001 <NA> 3140104002 3140104 R 1 1 205 1 0
#> 8630 3140104007 <NA> 3140104003 3140104 R 2 1 205 1 0
#> 8631 3140104008 <NA> 3140104015 3140104 R 2 2 205 1 0
#> 8634 3140104010 <NA> 3140104015 3140104 L 3 2 205 1 0
#> geometry
#> 8616 LINESTRING (-87.02298 30.60...
#> 8617 LINESTRING (-87.02928 30.60...
#> 8618 LINESTRING (-87.00626 30.64...
#> 8630 LINESTRING (-87 30.76254, -...
#> 8631 LINESTRING (-87.02458 30.78...
#> 8634 LINESTRING (-87.02458 30.78...
#Plot
ggplot() +
geom_sf(data = river_subset) +
geom_sf(data = bounding_box, fill = NA, color = 'red')
Created on 2022-03-30 by the reprex package (v2.0.1)
Subset class sfc_LINESTRING & sfc objects within a bbox
Here's an example using an sf
object from tigris
, just for reproducibility. I'm using towns in New Haven County, Connecticut, plotting it the way it comes in. Then I crop it to a bounding box I made up, using st_crop
, which I believe was added fairly recently to sf
. If I had the bbox as a shape, instead of a vector of coordinates, I could have used st_intersection
.
I don't have a linestring object handy, but I'd assume it works the same way.
library(tidyverse)
library(sf)
# selecting just to limit the amount of data in my sf
ct_sf <- tigris::county_subdivisions(state = "09", county = "09", cb = T, class = "sf") %>%
select(NAME, geometry)
plot(ct_sf)
crop_bbox <- c(xmin = -73, ymin = 41.2, xmax = -72.7, ymax = 41.5)
ct_cropped <- st_crop(ct_sf, crop_bbox)
plot(ct_cropped)
Related Topics
Locator Equivalent in Ggplot2 (For Maps)
R 'Inf' When It Has Class 'Date' Is Printing 'Na'
Find Elements Not in Smaller Character Vector List But in Big List
How to Extract Multiples of a Number from a Vector
Using R - Delete Rows When a Value Repeated Less Than 3 Times
R Ggplot2 Boxplots - Ggpubr Stat_Compare_Means Not Working Properly
Vectorised Rcpp Random Binomial Draws
Generating Non-Duplicate Combination Pairs in R
How to Rename All Columns of a Data Frame Based on Another Data Frame in R
How to Reverse Legend (Labels and Color) So High Value Starts at Bottom
Understanding Ddply Error Message - Argument "By" Is Missing, with No Default
How to Install R-Packages Not in the Conda Repositories
Solving a System of Nonlinear Equations in R
Text Color Based on Contrast Against Background