我可以在R(sf包)上为不同类别(最小凸多边形)创建一系列多边形吗

Álv*_*ack 2 r polygon geospatial r-sf

我正在 R 中处理空间信息(sf),并且我有一个物种列表及其各自的出现点。

我想使用st_convex_hull函数获得每个物种的多边形。我已设法为所有点创建一个多边形,但无法按物种变量对它们进行分组。

这是我到目前为止所拥有的脚本:

#Library
library(sf)
library(tidiverse)
library(mapview)

#create data frame
lat <- c(15.53033,15.53033,15.5219,15.1739,19.08333,17.13333,18.09861,21.27667,25.54863,18.80907,25.54147,
        20.0605,23.299,25.5501,25.55005,25.54997)
lon <- c(-92.802,-92.802,-92.7997,-92.3361,-96.9667,-91.9333,-94.4297,-99.2117,-100.271,-99.2168,-100.272,-97.4713,
         -106.443,-100.27,-100.27,-100.27)
species <- c("Sp1", "Sp1","Sp1", "Sp1","Sp1", "Sp1","Sp1", "Sp2","Sp2","Sp2","Sp2","Sp2","Sp2","Sp2","Sp2","Sp2")
df <- data.frame(lat, lon, species)

#create spatial matrix
df.1<- df %>%
sf::st_as_sf(coords = c("lon", "lat"),crs="EPSG: 4326")
mapview::mapview(df.1) #View map
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

#create polygon for the points
sp1_mcp <- st_convex_hull(st_union(df.1))
Run Code Online (Sandbox Code Playgroud)

到目前为止,一切进展顺利,但我希望它根据数据框的“物种”值生成不同的多边形。真正的问题是我的数据库包含 200 个物种,这就是我想要自动化该过程的原因。

最后,我还希望能够将 shapefile (.shp) 保存为文件。

mar*_*usl 6

您可以应用与聚合非空间数据集时相同的逻辑dplyrgroup_by后跟summarise()每组返回一条记录。对于没有任何额外参数的sf对象summarise(),仅总结几何图形,因此这里它将返回点的并集 a MULTIPOINT,您可以将其传递给st_convex_hull()

\n
library(sf)\n#> Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE\nlibrary(dplyr, warn.conflicts = FALSE)\nlibrary(mapview)\n\n# create data frame\nlat <- c(15.53033,15.53033,15.5219,15.1739,19.08333,17.13333,18.09861,21.27667,25.54863,18.80907,25.54147,\n        20.0605,23.299,25.5501,25.55005,25.54997)\nlon <- c(-92.802,-92.802,-92.7997,-92.3361,-96.9667,-91.9333,-94.4297,-99.2117,-100.271,-99.2168,-100.272,-97.4713,\n         -106.443,-100.27,-100.27,-100.27)\nspecies <- c("Sp1", "Sp1","Sp1", "Sp1","Sp1", "Sp1","Sp1", "Sp2","Sp2","Sp2","Sp2","Sp2","Sp2","Sp2","Sp2","Sp2")\ndf <- data.frame(lat, lon, species)\n\ndf.1 <- df %>%\n  st_as_sf(coords = c("lon", "lat"),crs="EPSG:4326") %>% \n  group_by(species) %>% \n  summarise() %>% \n  st_convex_hull()\n\ndf.1\n#> Simple feature collection with 2 features and 1 field\n#> Geometry type: POLYGON\n#> Dimension:     XY\n#> Bounding box:  xmin: -106.443 ymin: 15.1739 xmax: -91.9333 ymax: 25.5501\n#> Geodetic CRS:  WGS 84\n#> # A tibble: 2 \xc3\x97 2\n#>   species                                                               geometry\n#> * <chr>                                                            <POLYGON [\xc2\xb0]>\n#> 1 Sp1     ((-92.3361 15.1739, -92.7997 15.5219, -96.9667 19.08333, -91.9333 17.\xe2\x80\xa6\n#> 2 Sp2     ((-99.2168 18.80907, -106.443 23.299, -100.27 25.5501, -97.4713 20.06\xe2\x80\xa6\n\n# visualise\nmapview(df.1)\n
Run Code Online (Sandbox Code Playgroud)\n

\n
# save shp\ndir.create("species")\nst_write(df.1, "species/species.shp")\n#> Writing layer `species\' to data source \n#>   `species/species.shp\' using driver `ESRI Shapefile\'\n#> Writing 2 features with 1 fields and geometry type Polygon.\n\n# check resulting shp\nfs::dir_tree("species/")\n#> species/\n#> \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 species.dbf\n#> \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 species.prj\n#> \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 species.shp\n#> \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 species.shx\nst_layers("species/")\n#> Driver: ESRI Shapefile \n#> Available layers:\n#>   layer_name geometry_type features fields crs_name\n#> 1    species       Polygon        2      1   WGS 84\nread_sf("species/")\n#> Simple feature collection with 2 features and 1 field\n#> Geometry type: POLYGON\n#> Dimension:     XY\n#> Bounding box:  xmin: -106.443 ymin: 15.1739 xmax: -91.9333 ymax: 25.5501\n#> Geodetic CRS:  WGS 84\n#> # A tibble: 2 \xc3\x97 2\n#>   species                                                               geometry\n#>   <chr>                                                            <POLYGON [\xc2\xb0]>\n#> 1 Sp1     ((-92.3361 15.1739, -92.7997 15.5219, -96.9667 19.08333, -91.9333 17.\xe2\x80\xa6\n#> 2 Sp2     ((-99.2168 18.80907, -106.443 23.299, -100.27 25.5501, -97.4713 20.06\xe2\x80\xa6\n
Run Code Online (Sandbox Code Playgroud)\n

创建于 2023-10-04,使用reprex v2.0.2

\n

  • 如果需要,`df %&gt;% sfheaders::sf_multipoint(x = "lon", y = "lat", multipoint_id = "species") %&gt;% sf::st_convex_hull()` 可以保存 vew 步骤。 (2认同)