这可能会让您入门。 Initialise
library(sf)
p1 = st_as_sf(as.data.frame(coords1), coords = c("V1", "V2"), crs = 4269)
polygon_sf1 = st_sf(st_sfc(st_polygon(list(coords1)), crs = 4326))
p2 = st_as_sf(as.data.frame(coords2), coords = c("V1", "V2"), crs = 4269)
polygon_sf2 = st_sf(st_sfc(st_polygon(list(coords2)), crs = 4326))
o = st_nearest_points(polygon_sf1, polygon_sf2)
借来
first_last_dir = \(xy) geosphere::bearing(xy[1, 1:2], xy[nrow(xy), 1:2])
orient = \(lines) {
lines |>
sf::st_coordinates() |>
data.frame() |>
split(~L1) |>
sapply(first_last_dir)
}
为心内方向创建参考载体苹果
names(cardinal_direction)[findInterval(orient(o), cardinal_direction)]
#> [1] "north-east"
关于索引的其他选择还有很多其他选择。解释留给您。 plot
library(ggplot2)
ggplot() +
geom_sf(data = polygon_sf2) +
geom_sf(data = polygon_sf1) +
geom_sf(data = o)
data
coords1 = matrix(c(
-1.6522, 55.571,
-1.6487, 55.568,
-1.6522, 55.565,
-1.6550, 55.568,
-1.6522, 55.571
), ncol = 2, byrow = TRUE)
coords2 = matrix(c(
-1.645, 55.575,
-1.6449, 55.571,
-1.6472, 55.569,
-1.649, 55.571,
-1.645, 55.575
), ncol = 2, byrow = TRUE)