首页 > 解决方案 > bbox 中的子集类 sfc_LINESTRING 和 sfc 对象

问题描述

例子:

bbox <- c(-0.1178, 51.4232, -0.0185, 51.5147) # I know it needs to be sf df object
# we have 
df
#> Geometry set for 300 features 
#> geometry type:  LINESTRING
#> dimension:      XY
#> bbox:           xmin: -0.113894 ymin: 51.49739 xmax: -0.0764779 ymax: 51.59839
#> epsg (SRID):    4326
#> proj4string:    +proj=longlat +datum=WGS84 +no_defs
#> LINESTRING (-0.113894 51.50631, -0.1135137 51.5...
#> LINESTRING (-0.0767875 51.59837, -0.0764779 51....
#> ....

我该怎么做

df[bbox] 

并保留 bbox 内的线串。谢谢。

标签: rdataframegeospatialsf

解决方案


这是一个使用来自 的sf对象的示例tigris,只是为了重现性。我正在使用康涅狄格州纽黑文县的城镇,以它进入的方式绘制它。然后我将它裁剪到我制作的边界框,使用st_crop,我相信它是最近添加到sf. 如果我将 bbox 作为形状,而不是坐标向量,我可以使用st_intersection.

我没有方便的线串对象,但我认为它的工作方式相同。

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)


推荐阅读