我如何找到一个方向,即polygon_sf2的最接近点来自polygon_sf1,分为八个方向之一(北,东北,东,东南,东南,南,西南,西,西,西北,西北)。 ..

问题描述 投票:0回答:1

这可能会让您入门。 Initialise
r gis polygon
1个回答
0
投票
-Objects。可以简化此步骤。

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)

从这个this this this

答案

借来

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

为心内方向创建参考载体

cardinal_direction = setNames(seq(0, 360, 45), c('north', 'north-east', 'east',' south-east', 'south',' south-west', 'west', 'north-west'))

苹果
 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)

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.