我有一个经度和纬度点的数据框,绘制时如下所示: 我想定义一个单独的数据框,其中包含所有外部左边缘。我在这里强调了几点: 。
我尝试使用 st_convex_hull() 函数以及 getBorders() (制图包的一部分),但这给了我整个外边缘,而不仅仅是我想要的左边缘。
这个解决方案使用最北点和最南点来用
lwgeom
分割边界。需要手动选择所需的“边缘”(三行中的两行)。我用 meuse
作为例子。使用 st_concave_hull(ratio=0.1)
获得边界,并在其上测试点。根据凹度,可能需要进行调整。
library(tidyverse)
library(sf)
#> Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
library(lwgeom) # for lwgeom::st_split
#> Warning: le package 'lwgeom' a été compilé avec la version R 4.3.3
#> Linking to liblwgeom 3.0.0beta1 r16016, GEOS 3.11.2, PROJ 9.3.1
#> Warning in fun(libname, pkgname): PROJ versions differ: lwgeom has 9.3.1 sf has
#> 9.3.0
#>
#> Attachement du package : 'lwgeom'
#> L'objet suivant est masqué depuis 'package:sf':
#>
#> st_perimeter
library(sp) # for meuse
data(meuse)
df_sf = meuse %>% st_as_sf(coords= c("x","y"),remove= FALSE) %>%
mutate(ID= 1:n())
ggplot(data=df_sf)+geom_sf()
# Boundary
st_simplify(df_sf %>% summarise() %>% st_concave_hull(ratio=0.1) %>% st_cast(to ="LINESTRING")) %>%
st_as_sfc() %>% ggplot(data=.)+geom_sf()
# Getting the points
border_point = df_sf %>% summarise() %>% st_concave_hull(ratio=0.1) %>% st_cast(to ="POINT") %>%
st_join(df_sf, .predicate = st_equals)
# Choosing start/end point.
MinMax = bind_rows(border_point %>% slice_max(y),
border_point %>% slice_min(y))
# Splitting along those points
boundary_split =
lwgeom::st_split(df_sf %>% summarise() %>% st_concave_hull(ratio=0.1) %>% st_cast(to ="LINESTRING"),
MinMax ) %>%
st_collection_extract(., "LINESTRING") %>%
mutate(id_line = 1:n())
# Three lines are generated, since the start/end of the linestring acts as a splitting point as well
# Plotting to identify the releveants id_line (left shore),
boundary_split %>%
ggplot(data=.)+geom_sf( aes(col = id_line %>% as.factor()))
# manually selecting
left_boundary = boundary_split %>% filter( id_line %in% c(1,2))
left_points = st_join(df_sf,left_boundary ) %>%
# Removing unjoinned point (NA), there is certainly a better way
filter( id_line %in% c(1,2))
# Ploting elements
ggplot()+
geom_sf(data=df_sf %>% summarise() %>% st_concave_hull(ratio=0.1),fill=NA)+
geom_sf(data=df_sf,col ="grey")+
geom_sf(data =MinMax, col ="green", size =5)+
geom_sf(data = boundary_split,aes(col = id_line %>% as.factor()) )+
geom_sf(data = left_points, col ="red")
创建于 2024-04-16,使用 reprex v2.1.0