2

我试图在一个数据帧中以“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") 

我非常感谢任何意见。期望的输出: 输出

4

1 回答 1

1

似乎答案包括使用 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 数据帧的多线字符串列表。欢迎任何意见

于 2018-03-06T15:47:35.663 回答