R 使用sf将线串拆分为多线串
我试图在一个数据帧中以“LINESTRING”sf格式拆分两行,在另一个数据帧中以“MULTIPOLYGON”sf格式拆分两个圆圈R 使用sf将线串拆分为多线串,r,dataframe,gis,sf,R,Dataframe,Gis,Sf,我试图在一个数据帧中以“LINESTRING”sf格式拆分两行,在另一个数据帧中以“MULTIPOLYGON”sf格式拆分两个圆圈 # 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") ) %>% #
# 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点制作数据帧
lndf%#使用crs将经度和纬度转换为sf点
st_as_sf(coords=c(“x”,“y”),dim=“XY”)%>%
st_集_crs(4326)
#将sf点转换为sf线->第一个数据帧
lndf%
分组依据(var)%>%
汇总(a=总和(属性数据))%>%
st_铸造(“线串”)
#使用sf点制作数据帧
cidf%#使用crs将经度和纬度转换为sf点
st_as_sf(coords=c(“x”,“y”),dim=“XY”)%>%
st_集_crs(4326)
#将sf点转换为sf圆多边形
第二数据帧
mls_cidf%
分组依据(gr)%>%
汇总(a=总和(属性数据))%>%
st_cast(“多重约束”,组或分割=假)%>%
st_集_crs(4326)
#计算直线和圆多边形之间的交点
inters似乎答案包括使用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)
#将行字符串拆分为多行字符串
mls_to_ls要转换为sf
data.frame
您需要几个额外的步骤:sf::st_sf(geometry=sf::st_sfc(purr::map2(lndf$geometry,mls_cidf$geometry,mls_to_ls))
# 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)