在 sf 文件 R 中按类别创建多边形

发布于 2025-01-11 06:12:23 字数 1890 浏览 2 评论 0原文

我有来自关键和濒危习惯联邦登记处的大量坐标。我正在尝试将这些地图数字化以进行分析。以下是数据样本作为示例。

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") 

mapview 坐标图 将其

绘制成图形我的主要目标是根据 ID(A 或 B)创建两个不同的多边形,但我很挣扎。

我可以用以下命令将它们全部变成一个多边形:

polygon <- plot_local1 %>% 
# dplyr::group_by(ID) %>% 
  dplyr::summarise() %>%
  st_cast("POLYGON")

mapview(polygon, map.types = "Esri.NatGeoWorldMap") 

Single Polygon

但是,如果我按照建议取消注释 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") 

multiple Polygon attempts

当然,我可以为每个多边形创建单独的数据框,然后稍后将它们组合在一起,但考虑到我的数据量我正在处理这确实不切实际。我还更新了我正在使用的所有软件包,所以我知道这不是问题。感谢任何和所有的帮助!

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") 

mapview coordinates plot

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") 

Single polygon

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") 

multiple polygon attempt

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗳卜坏 2025-01-18 06:12:23

作为您评论的后续行动,我准备了一个表示,以便您可以测试代码。它应该可以工作...

如果它不起作用,这里有一些建议:

  1. 确保所有库都是最新的,尤其是 tidyversemapviewSF。在我这边,我使用以下版本运行代码:tidyverse 1.3.1mapview 2.10.0sf 1.0.6
  2. 关闭所有打开 R 会话中的文档并关闭 R。重新打开新的 R 会话并仅打开包含要测试的代码的文件。
  3. 仅加载运行代码所需的库。

希望这有帮助。我祈祷这些建议能让你摆脱困境。

Reprex

  • 您的数据
library(tidyverse)
library(mapview)
library(sf)

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)


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") 

  • 应该有效的代码...
polygon <- plot_local1 %>% 
  dplyr::group_by(ID) %>% 
  dplyr::summarize() %>% 
  sf::st_cast("POLYGON")

mapview(polygon, map.types = "Esri.NatGeoWorldMap") 

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:

  1. Make sure all your libraries are up to date, especially tidyverse, mapview and sf. On my side, I run the code with the following versions: tidyverse 1.3.1, mapview 2.10.0 and sf 1.0.6
  2. Close all open documents in your R session and close R. Reopen a new R session and open only the file that contains the code to test.
  3. Load only the libraries needed to run the code.

Hope this helps. I'm crossing my fingers that these suggestions will unstuck you.

Reprex

  • Your data
library(tidyverse)
library(mapview)
library(sf)

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)


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") 

  • The code that should work...
polygon <- plot_local1 %>% 
  dplyr::group_by(ID) %>% 
  dplyr::summarize() %>% 
  sf::st_cast("POLYGON")

mapview(polygon, map.types = "Esri.NatGeoWorldMap") 

Created on 2022-03-05 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文