从R中的航向和距离求坐标

jgovgodb  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(160)

我想得到新点的坐标,最好使用sf包,当初始位置和距离和行进方向已知时。考虑一下这个;我们有三个点,附有航向和以公里为单位的距离。如何找到新位置的坐标?

library(data.table, dplyr, sf)
dat <- data.table(lon = c(10,10.1,10.4), lat = c(58.4,57.4,57.8), 
                      heading = c(45,10,235), distance_km = c(1,5.3,3))
pts <- dat %>%
      sf::st_as_sf(coords = c("lon","lat")) %>%
      sf::st_set_crs(4326)

Simple feature collection with 3 features and 2 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 10 ymin: 57.4 xmax: 10.4 ymax: 58.4
Geodetic CRS:  WGS 84
  heading distance_km          geometry
1      45         1.0   POINT (10 58.4)
2      10         5.3 POINT (10.1 57.4)
3     235         3.0 POINT (10.4 57.8)

正在考虑在点周围做圆圈,但不知道如何用正确的航向将点连接到圆圈。

buf <- st_buffer(pts, dist = pts$distance_km*1000)
circ <- st_cast(buf, "LINESTRING")
xienkqul

xienkqul1#

在这里找到了答案:Calculate coordinate from starting point, having distance and an angle for all quadrants和这里:Convert radians to degree / degree to radians
我将发布我的R解决方案以确保完整性。如果有人有更好或更流畅的解决方案,请随时发布。

library(data.table, sf, mapview)
dat <- data.table(lon = c(10,10.1,10.4), lat = c(58.4,57.4,57.8), 
                  heading = c(45,10,235), distance_km = c(1,5.3,3))

pts <- dat %>%
  sf::st_as_sf(coords = c("lon","lat")) %>%
  sf::st_set_crs(4326)

pts <- st_transform(pts, 32632)
pts$utm_n <- st_coordinates(pts)[,1]
pts$utm_e <- st_coordinates(pts)[,2]

buf <- st_buffer(pts, dist = pts$distance_km*1000)

circ <- st_cast(buf, "LINESTRING")

rad2deg <- function(rad) {(rad * 180) / (pi)}
deg2rad <- function(deg) {(deg * pi) / (180)}

pts$newp_e <- pts$utm_e + (pts$distance_km*1000* cos(deg2rad(pts$heading)))
pts$newp_n <- pts$utm_n + (pts$distance_km*1000* sin(deg2rad(pts$heading)))

dt <- data.table(pts)

pts2 <- dt %>%
  sf::st_as_sf(coords = c("newp_n", "newp_e")) %>%
  sf::st_set_crs(32632)

mapview(pts2) + mapview(pts, zcol = "heading") + mapview(circ)

相关问题