如何用R中的线分割/剪裁多边形



我想把CO(一个多边形(分成不被道路(线串(分割的部分(也是多边形(。也就是说,我希望较小多边形的部分以道路或州边界为界,而不包含任何进出多边形的道路。

我能够使用lwgeom::st_split生成几何体集合,但我不确定这是否对我有帮助;我一直使用这个解决方案,因为我不知道如何提取集合中的几何图形,例如,如何为它们分配唯一的ID。

我的最终目标是确保我的点(单独的数据(不会被道路隔开。所以,如果你有一个更直接的解决方案,我也会洗耳恭听。

library(tidyverse)
library(tigris)
library(sf)
library(lwgeom)
co <- states(cb = T) %>% 
filter(NAME == "Colorado")
roads <- primary_secondary_roads(state = 'Colorado') 
cosplit <- st_split(co,roads) 

有人找到或看到解决方案了吗?

我想我想通了。。。但我肯定很想听听其他人的想法!!!

cosplitpoly <- cosplit %>% 
st_collection_extract(c("POLYGON"))

使用这个来解决我自己的问题的小提示。我发现"LINESTRING"不会像"MULTILINESTRING"那样分割多边形。

在我的测试用例中,当我第一次用有问题的多边形分割"LINESTRING"时,结果也干净多了。

rb <- data.frame('lon' = c(10,10,20,20, 10), 
'lat' = c(5,10,10,5,5)) %>% 
st_as_sf(coords = 1:2) %>% 
st_set_crs(4326) %>% 
concaveman()
sl <- data.frame('lon' = c(5,10,15,20,25), 
'lat' = c(7,7,7,7,7)) %>% 
st_as_sf(coords = 1:2) %>% 
st_set_crs(4326) %>% 
st_union() %>% 
st_cast('LINESTRING') %>% 
st_sf()
ggplot() + 
geom_sf(data = rb) + 
geom_sf(data = sl)
sls <- sl %>% lwgeom::st_split(., rb) %>% st_collection_extract('LINESTRING')
slc <- sls %>% st_sample(size = round(sum(st_length(.)) / as_units(10000, 'm'),0) %>% as.numeric())
bool_inside <- st_within(slc, rb, sparse = F) %>% rowSums() > 0
nl <- sls[bool_inside,]
rbi <- rb %>% lwgeom::st_split(., sls %>% st_cast('MULTILINESTRING') %>% 
st_union()) %>% st_collection_extract('POLYGON') %>% 
mutate(id = row_number())
ggplot() + 
geom_sf(data = rb) + 
geom_sf(data = nl)
ggplot(rbi)+geom_sf(aes(fill = id))

最新更新