r-使用 sf 将线串拆分为多线串
r-Split linestring into multilinestring with sf
我正在尝试在数据框中以 "LINESTRING" sf 格式拆分 2 行,在另一个数据框中以 "MULTIPOLYGON" sf 格式拆分 2 个圆圈。
# make data frame with sf points
lndf <- data.frame(
x = c(40, 55, 60, 70),
y = c(5, 20, 30, 35),
attr_data = c(10,10,10,10),
var = c("abc", "abc", "bac", "bac")
) %>% # convert longitude and latitude into sf points with crs
st_as_sf(coords = c("x","y"), dim = "XY") %>%
st_set_crs(4326)
# convert sf points to sf lines -> first dataframe
lndf <- lndf %>%
group_by(var) %>%
summarize(a = sum(attr_data)) %>%
st_cast("LINESTRING")
# make data frame with sf points
cidf <- data.frame(
x = c(40, 55, 60, 70),
y = c(5, 20, 30, 35),
attr_data = c(10,10,10,10),
gr = c("abc", "abc", "bac", "bac")
) %>% # convert longitude and latitude into sf points with crs
st_as_sf(coords = c("x","y"), dim = "XY") %>%
st_set_crs(4326)
# convert sf points to sf circle polygons
cidf <- st_buffer(cidf, 1)
# convert sf circle polygons to sf multilinestring -> second dataframe
mls_cidf <- cidf %>%
group_by(gr) %>%
summarize(a = sum(attr_data)) %>%
st_cast("MULTILINESTRING", group_or_split = FALSE) %>%
st_set_crs(4326)
# Calculating intersection points between lines and circle polygons
inters <- st_intersection(lndf$geometry, mls_cidf)
# Calculating small circles around intersection points
buffer <- st_buffer(inters, dist = 1e-12)
# split linestring into multilinestring
difference <- st_difference(lndf, buffer)
在这里,我期待有两行的 sf 数据框,其中有两个多线串,但我得到的是 4 行(有 4 个几何图形)的 sf 数据框。我只对 mls_cidf 的第 n 行的小圆形多边形如何从 lndf 的第 n 行切割线串感兴趣,而不是两个数据帧中所有行之间的所有组合。得到两个多线串后,我想用以下线串将它们分开:
lnstrngs <- st_cast(difference, "LINESTRING")
非常感谢任何意见。期望的输出:
似乎答案包括使用 purrr::map2
# split linestring into multilinestring
mls_to_ls <- function(ls, pl) {
st_difference(ls, st_buffer(st_intersection(ls, pl), dist = 1e-12))
}
# iterate over rows of lndf and mls_cidf
map2(lndf$geometry, mls_cidf$geometry, mls_to_ls)
但答案是一个多线串列表,我无法将其转换为 sf 数据框。欢迎任何意见
我正在尝试在数据框中以 "LINESTRING" sf 格式拆分 2 行,在另一个数据框中以 "MULTIPOLYGON" sf 格式拆分 2 个圆圈。
# make data frame with sf points
lndf <- data.frame(
x = c(40, 55, 60, 70),
y = c(5, 20, 30, 35),
attr_data = c(10,10,10,10),
var = c("abc", "abc", "bac", "bac")
) %>% # convert longitude and latitude into sf points with crs
st_as_sf(coords = c("x","y"), dim = "XY") %>%
st_set_crs(4326)
# convert sf points to sf lines -> first dataframe
lndf <- lndf %>%
group_by(var) %>%
summarize(a = sum(attr_data)) %>%
st_cast("LINESTRING")
# make data frame with sf points
cidf <- data.frame(
x = c(40, 55, 60, 70),
y = c(5, 20, 30, 35),
attr_data = c(10,10,10,10),
gr = c("abc", "abc", "bac", "bac")
) %>% # convert longitude and latitude into sf points with crs
st_as_sf(coords = c("x","y"), dim = "XY") %>%
st_set_crs(4326)
# convert sf points to sf circle polygons
cidf <- st_buffer(cidf, 1)
# convert sf circle polygons to sf multilinestring -> second dataframe
mls_cidf <- cidf %>%
group_by(gr) %>%
summarize(a = sum(attr_data)) %>%
st_cast("MULTILINESTRING", group_or_split = FALSE) %>%
st_set_crs(4326)
# Calculating intersection points between lines and circle polygons
inters <- st_intersection(lndf$geometry, mls_cidf)
# Calculating small circles around intersection points
buffer <- st_buffer(inters, dist = 1e-12)
# split linestring into multilinestring
difference <- st_difference(lndf, buffer)
在这里,我期待有两行的 sf 数据框,其中有两个多线串,但我得到的是 4 行(有 4 个几何图形)的 sf 数据框。我只对 mls_cidf 的第 n 行的小圆形多边形如何从 lndf 的第 n 行切割线串感兴趣,而不是两个数据帧中所有行之间的所有组合。得到两个多线串后,我想用以下线串将它们分开:
lnstrngs <- st_cast(difference, "LINESTRING")
非常感谢任何意见。期望的输出:
似乎答案包括使用 purrr::map2
# split linestring into multilinestring
mls_to_ls <- function(ls, pl) {
st_difference(ls, st_buffer(st_intersection(ls, pl), dist = 1e-12))
}
# iterate over rows of lndf and mls_cidf
map2(lndf$geometry, mls_cidf$geometry, mls_to_ls)
但答案是一个多线串列表,我无法将其转换为 sf 数据框。欢迎任何意见