在 sf 文件 R 中按类别创建多边形
我有来自关键和濒危习惯联邦登记处的大量坐标。我正在尝试将这些地图数字化以进行分析。以下是数据样本作为示例。
library(tidyverse)
library(mapview)
library(sf)
library(ggplot2)
lat <- c(300786, 301348, 301467, 302384, 304644, 304747, 304863, 304882)
lon <- c(4215918, 4215650, 4215784, 4216077, 4211303, 4211336, 4211395, 4211457)
geodeticDA <- c("NAD83", "NAD83", "NAD83", "NAD83", "NAD83", "NAD83", "NAD83", "NAD83")
utmZone <- c("11N", "11N", "11N", "11N", "11N", "11N", "11N", "11N")
ID <- c("A", "A", "A", "A", "B", "B", "B", "B")
df <- data.frame(lat, lon, geodeticDA, utmZone, ID)
我已经成功地将 df 转换为 sf 并使用以下
plot_local1 <- st_as_sf(df,
coords = c("lat", "lon"),
crs = "+proj=utm +zone=11 +ellps=GRS80 +datum=NAD83 +units=m +no_defs ")
mapview(plot_local1, map.types = "Esri.NatGeoWorldMap")
将其
绘制成图形我的主要目标是根据 ID(A 或 B)创建两个不同的多边形,但我很挣扎。
我可以用以下命令将它们全部变成一个多边形:
polygon <- plot_local1 %>%
# dplyr::group_by(ID) %>%
dplyr::summarise() %>%
st_cast("POLYGON")
mapview(polygon, map.types = "Esri.NatGeoWorldMap")
但是,如果我按照建议取消注释 group_by()通过其他网站/问题,我收到此错误
错误: ! tible 中的所有列都必须是向量。 x 列geometry
是一个sfc_POINT/sfc
对象。
我已经设法使用以下代码创建多个多边形,但是当我绘制它时,它不正确。
polys <- st_sf(
aggregate(
plot_local1$geometry,
list(plot_local1$ID),
function(g){
st_cast(st_combine(g),"POLYGON")
}
))
mapview(polys, map.types = "Esri.NatGeoWorldMap")
当然,我可以为每个多边形创建单独的数据框,然后稍后将它们组合在一起,但考虑到我的数据量我正在处理这确实不切实际。我还更新了我正在使用的所有软件包,所以我知道这不是问题。感谢任何和所有的帮助!
I have a large set of coordinates from the critical and endangered habit federal registry. I'm trying to digitize these maps for analysis. Here's a sample of the data as an example.
library(tidyverse)
library(mapview)
library(sf)
library(ggplot2)
lat <- c(300786, 301348, 301467, 302384, 304644, 304747, 304863, 304882)
lon <- c(4215918, 4215650, 4215784, 4216077, 4211303, 4211336, 4211395, 4211457)
geodeticDA <- c("NAD83", "NAD83", "NAD83", "NAD83", "NAD83", "NAD83", "NAD83", "NAD83")
utmZone <- c("11N", "11N", "11N", "11N", "11N", "11N", "11N", "11N")
ID <- c("A", "A", "A", "A", "B", "B", "B", "B")
df <- data.frame(lat, lon, geodeticDA, utmZone, ID)
I've successfully been able to turn the df into an sf and graph it using the following
plot_local1 <- st_as_sf(df,
coords = c("lat", "lon"),
crs = "+proj=utm +zone=11 +ellps=GRS80 +datum=NAD83 +units=m +no_defs ")
mapview(plot_local1, map.types = "Esri.NatGeoWorldMap")
My main goal is to create two different polygons based on their ID (A or B) but I'm struggling.
I can turn them all into one polygon with this:
polygon <- plot_local1 %>%
# dplyr::group_by(ID) %>%
dplyr::summarise() %>%
st_cast("POLYGON")
mapview(polygon, map.types = "Esri.NatGeoWorldMap")
However, if I uncomment the group_by(), as is suggested by other sites/questions, I get this error
Error:
! All columns in a tibble must be vectors.
x Column geometry
is a sfc_POINT/sfc
object.
I've managed to create multiple polygons with the following code but when I graph it it's not correct.
polys <- st_sf(
aggregate(
plot_local1$geometry,
list(plot_local1$ID),
function(g){
st_cast(st_combine(g),"POLYGON")
}
))
mapview(polys, map.types = "Esri.NatGeoWorldMap")
Certainly I could create separate data frames for each polygon and then combine them together later but given the amount of data I'm working with that really isn't practical. I've also updated all of the packages I'm using so I know that's not the issue. Any and all help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为您评论的后续行动,我准备了一个表示,以便您可以测试代码。它应该可以工作...
如果它不起作用,这里有一些建议:
tidyverse
、mapview
和SF
。在我这边,我使用以下版本运行代码:tidyverse 1.3.1
、mapview 2.10.0
和sf 1.0.6
希望这有帮助。我祈祷这些建议能让你摆脱困境。
Reprex
由 reprex 包 (v2.0.1)
As a follow-up to your comment, I have prepared a reprex so that you can test the code. It should work...
If it doesn't work, here are some suggestions:
tidyverse
,mapview
andsf
. On my side, I run the code with the following versions:tidyverse 1.3.1
,mapview 2.10.0
andsf 1.0.6
Hope this helps. I'm crossing my fingers that these suggestions will unstuck you.
Reprex
Created on 2022-03-05 by the reprex package (v2.0.1)