【问题标题】:Daisychain connect multiple polygons by single nearest points菊花链通过单个最近点连接多个多边形
【发布时间】:2021-04-03 21:47:47
【问题描述】:

我希望为多个多边形创建一个遮罩多边形。

单个多边形很容易:

How to apply a polygon mask layer in ggplot

但对于多个来说要复杂得多:

https://www.stat.auckland.ac.nz/~paul/Reports/GraphicsEngine/definitions/definitions.html

我觉得我已经很接近了,但我需要一种在最近多边形的点之间画一条线的方法,即我想尝试用一条线连接最近的岛屿。

同时将这些多边形连接到外部多边形框,但仅使用一条线:

library(tidyverse)
library(sf)
library(albersusa)

usa <- usa_sf()

HI <- st_coordinates(usa %>%
                             filter(name %in% c("Hawaii"))) %>%
        as.data.frame() %>%
        select(X, Y)


rec_box <-
        data.frame(
                X = c(-108,-108,-101,-101,-108),
                Y = c(24, 28, 28, 24, 24)
        )

mask <- rbind(HI, rec_box)

eg <- st_as_sf(data.frame(mask), coords = c("X", "Y"))
poly <- st_convex_hull(eg)

ggplot() +
        geom_sf(data = poly) +
        geom_density2d_filled(data = HI, aes(x = X, y = Y)) +
        geom_polygon(data = mask,
                     aes(x = X, y = Y),
                     color = "black",
                     fill = "white")

【问题讨论】:

    标签: r ggplot2 gis sf


    【解决方案1】:

    编辑: 仅用于岛屿周围的面具:

    library(tidyverse)
    library(sf)
    #> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
    library(albersusa)
    #library(concaveman)
    #library(nngeo)
    
    usa <- usa_sf()
    
    # keep only the geometry from Hawaii (not the data)
    HI <- usa %>%
           filter(name %in% c("Hawaii")) %>%
           st_cast('POLYGON') %>% 
           st_geometry() %>% 
           st_as_sf()
    
    
    # HI as dataframe as your example
    HI2 <- st_coordinates(usa %>%
            filter(name %in% c("Hawaii"))) %>%
            as.data.frame() %>%
            select(X, Y)
    # you may need a larger box. This uses sf:st_bbox to get exact
    #  rectangle of the polygons. adjust accordingly
    hi_box <- st_bbox(HI) %>% st_as_sfc() %>% st_as_sf()
    
    # find the difference betwee HI & hi_box
    hi_mask <- st_difference(hi_box, st_union(HI))
    
    
    ggplot() +
      geom_density_2d_filled(data = HI2, aes(x = X, y = Y)) +
      geom_sf(data = hi_mask, fill = 'black') 
    

    reprex package (v0.3.0) 于 2021 年 4 月 4 日创建

    如果您对上面创建的 HI 掩码感到满意,可以使用带有 nngeo::st_connect() 的单行将其连接到您定义的边界框。

    这应该找到两者之间最短的连接线。如果您希望连接线位于特定点(上例中的西南角),请将其作为 sf 对象提供给 st_connect() 函数,而不是整个框。

    library(tidyverse)
    library(sf)
    #> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
    library(albersusa)
    library(concaveman)
    library(nngeo)
    
    usa <- usa_sf()
    
    # Hawaii, cast to POLYGON rather than the original MULTIPOLYGON
    HI <- (usa %>%
           filter(name %in% c("Hawaii"))) %>%
           st_cast('POLYGON')
    
    connected_HI <- concaveman(HI)
    
    # rec box as an sf object with same crs as HI
    rec_box <-
      data.frame(X = c(-108, -108, -101, -101, -108),
                 Y = c(24, 28, 28, 24, 24)) %>%
      st_as_sf(coords = c('X', 'Y'
      )) %>%
      st_set_crs(st_crs(HI))
    
    # connecting HI mask you made to your rec_box
    connected_to_rec_box <- nngeo::st_connect(st_zm(connected_HI), rec_box)
    
    
    ggplot() +
      geom_sf(data = connected_HI, fill = 'turquoise', alpha = .4) +
      geom_sf(data = connected_to_rec_box, color = 'red')
    

    reprex package (v0.3.0) 于 2021-04-04 创建

    【讨论】:

    • 有什么方法可以将多边形与两点之间的线与目前的三点楔形连接起来吗?
    • @SCDCE 可能使用了类似的方法。有机会我会去看看。他们需要连接吗?未连接的 sf 对象可能会起作用。
    • 我稍微编辑了代码以添加另一个图层。最终,我试图为geom_density() 之类的东西创建一个面具,所以它只在陆地上显示。在我之前的链接帖子中,我通过将两个多边形连接起来打一个洞与美国实现了这一点,我不确定另一种方法,但如果它可以完成这项工作,当然可以接受任何方法。
    • @SCDCE 查看仅在岛屿陆地上的掩码(反掩码?)的编辑。
    • 非常好!谢谢你。不要以为您想为此发布答案:stackoverflow.com/questions/66911086/… 以包含来自usa_sf() 的插图?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 2014-12-06
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多