如何使用 sf 按因子从点构造/绘制多边形的凸包? [英] How to construct/plot convex hulls of polygons from points by factor using sf?

查看:60
本文介绍了如何使用 sf 按因子从点构造/绘制多边形的凸包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个物种出现的数据集,我试图通过制作凸包将其转换为出现的区域.我可以手动执行此操作(即一次一个物种),但我真的很希望能够通过物种名称自动处理它.

可以在此处找到精简的示例数据集:

#group 并按物种汇总,并绘制船体船体<-df.sf%>%group_by(物种)%>%总结(几何= st_combine(几何))%>%st_convex_hull()#结果地图视图::地图视图(列表(df.sf,船体))

I've got a dataset of species occurrences which I'm trying to convert into areas of occurrence by making convex hulls. I'm able to do this manually (ie. one species at a time) but I'd really love to be able to just have it handled automatically by the species name.

A pared-down example dataset can be found here: https://pastebin.com/dWxEvyUB

Here's how I'm currently doing it manually:

library(tidyverse)
library(sf)
library(rgeos)
library(maps)
library(mapview)
library(mapdata)
library(ggplot2)


fd <- read_csv("occurrence.csv")

spA.dist <- fd %>%
  filter(species == "sp.A") %>%
  dplyr::select(lon,lat) %>%
  as.matrix() %>%
  coords2Polygons(ID="distribution") %>%
  gConvexHull() %>%
  gBuffer()

spB.dist <- fd %>%
  filter(species == "sp.B") %>%
  dplyr::select(lon,lat) %>%
  as.matrix() %>%
  coords2Polygons(ID="distribution") %>%
  gConvexHull() %>%
  gBuffer() 

wrld2 = st_as_sf(map('world2', plot=F, fill=T))
ggplot() + 
  geom_sf(data=wrld2, fill='gray20',color="lightgrey",size=0.07) +
  geom_polygon(aes(x=long,y=lat,group=group),color="red",data=spA.dist,fill=NA) +
  geom_polygon(aes(x=long,y=lat,group=group),color="blue",data=spB.dist,fill=NA) + 
  coord_sf(xlim=c(100,300), ylim=c(-60,60))

That displays a map with the two species occurrence areas based on the convex hull of their observations. I realize I'm mixing different spatial libraries here so it would be nice to do it all in sf if possible. In my real data I have more than two species and I can copy and paste the code I've got for each one but it seems like it should be possible to simplify this so the polygons (and subsequent convex hulls) are constructed by factor level automatically. Something more like this:

polys <- st_as_sf(fd) %>%
  group_by(species) %>%
  magically_make_polygons(lon,lat) %>%
  st_convex_hull() %>%
  st_buffer()

I've been searching for days as well as digging through reams of documentation. A lot of this spatial stuff is non-intuitive to me so I expect there's a lot of basic understanding I'm missing. Can this be done?

解决方案

Here is a possible solution using the tidyverse (in fact only dplyr) and the sf-package (and the mapview package for some quick viewing).

You were very close with your own sulution (kudo's). The trick is to summarise the grouped data, and then create the hulls..

library( tidyverse )
library( sf )

#create simple feature
df.sf <- df %>%
  st_as_sf( coords = c( "lon", "lat" ), crs = 4326 )
#what are we working with? 
# perform fast visual check using mapview-package
mapview::mapview( df.sf )

#group and summarise by species, and draw hulls
hulls <- df.sf %>%
  group_by( species ) %>%
  summarise( geometry = st_combine( geometry ) ) %>%
  st_convex_hull()

#result
mapview::mapview( list( df.sf, hulls ) )

这篇关于如何使用 sf 按因子从点构造/绘制多边形的凸包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆