菊花链通过单个最近点连接多个多边形
Daisychain connect multiple polygons by single nearest points
我想为多个多边形创建一个遮罩多边形。
对于单个多边形来说已经足够简单了:
但对于多个更棘手:
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")
编辑:
仅需一个环绕岛屿的面具:
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-04-04 创建
如果您对上面创建的 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 创建
我想为多个多边形创建一个遮罩多边形。
对于单个多边形来说已经足够简单了:
但对于多个更棘手:
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")
编辑: 仅需一个环绕岛屿的面具:
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-04-04 创建如果您对上面创建的 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 创建