【问题标题】:Scale a lon/lat vector to a certain length in meter将 lon/lat 矢量缩放到一定长度(以米为单位)
【发布时间】:2020-09-14 03:07:12
【问题描述】:

我有一些 GPS 测量线,每条线有 3 个点,由于测量的不确定性而弯曲,我想用长度正好 50m 以中心点为中心的线代替测量线和测量点的大致方向。

为此,我想从中心点(由 lon/lat 坐标给出)向某个方向移动 25m,该方向由 lon/lat 矢量给出。现在的问题是,在不同的纬度 25m 的度数并不相同。我做了一些计算来得到 1m 在该位置的长度:

# Calculate the length of a 1 degree difference
earth_circumference <- 40075016.7
for(i in 1:nrow(coords)){
  coords$d_m_lon[i] <- earth_circumference/360*cos(abs(coords$c_lat[1])*pi/180)
  coords$d_m_lat[i] <- earth_circumference/360
}

# Claculate the difference in degree that 1m at the surface makes
coords$m_d_lon <- 1/coords$d_m_lon
coords$m_d_lat <- 1/coords$d_m_lat

我有一个 dataframe (coords),它在每一行中包含起始 s、中心 c 的坐标em> 和结束 e 点。我做了一个回归以获得最接近点的一般方向的线(在这里,我使用 [1] 作为数据的第一行;如果它有效,它显然会循环重复所有行的处理):

# Calculate average transect directions
points <- data.frame(
  x = c(coords$s_lon[1], coords$c_lon[1], coords$e_lon[1]),
  y = c(coords$s_lat[1], coords$c_lat[1], coords$e_lat[1])
)
mod <- lm(points$y ~ points$x)
a <- coefficients(mod)[1]
b <- coefficients(mod)[2]

现在我尝试按照模型给定的方向前进,并在给定的纬度上将矢量缩放到 25m 长,但不知何故新的开始 em> 和 end 点不再在回归线上:

center <- c(coords$c_lon[1], coords$c_lat[1])
direction <- c(b-a, 1)/sqrt((b-a)^2 + 1)

start <- center + 25*c(coords$m_d_lon[1], coords$m_d_lat[1])*direction
end <- center - 25*c(coords$m_d_lon[1], coords$m_d_lat[1])*direction

是否有人看到错误出现在哪里或知道如何解决问题?我想我没有正确缩放矢量...

如果您希望数据的第一行对此进行测试:

coords <- structure(list(s_lat = -29.6032, s_lon = 29.3376, c_lat = -29.6032, 
    c_lon = 29.3379, e_lat = -29.6032, e_lon = 29.3381, d_m_lon = 96788.6617220582, 
    d_m_lat = 111319.490833333, m_d_lon = 1.03317886848321e-05, 
    m_d_lat = 8.98315283796251e-06), row.names = 1L, class = "data.frame")

【问题讨论】:

标签: r vector geospatial latitude-longitude


【解决方案1】:

不确定我想要输出什么格式,但这可能会让你开始......

产生两个新列; new_snew_e,在 lon,lat - 格式..

library( tidyverse )  
library( geosphere )

coords <- coords %>%
  #calculate bearing from center ppint to s-point
  mutate( bearing_c_to_s = pmap( list ( a = c_lon,
                                        b = c_lat,
                                        x = s_lon,
                                        y = s_lat ),
                                 ~ geosphere::bearing( c(..1, ..2), c(..3, ..4) ) 
                                 )
          ) %>%
  #calculate bearing from center ppint to s-point
  mutate( bearing_c_to_e = pmap( list ( a = c_lon,
                                        b = c_lat,
                                        x = e_lon,
                                        y = e_lat ),
                                 ~ geosphere::bearing( c(..1, ..2), c(..3, ..4) )  
                                 )
          ) %>%
  #calculate new point s coordinates, 25 meters from c using bearing c_to_s
  mutate( new_s = pmap( list ( a = c_lon,
                               b = c_lat,
                               x = bearing_c_to_s,
                               y = 25 ),
                        ~ geosphere::destPoint( c(..1, ..2), ..3, ..4 ) 
                        )
          ) %>%
  #calculate new point e coordinates, 25 meters from c using bearing c_to_e
  mutate( new_e = pmap( list ( a = c_lon,
                               b = c_lat,
                               x = bearing_c_to_e,
                               y = 25 ),
                        ~ geosphere::destPoint( c(..1, ..2), ..3, ..4 ) 
                        )
  )

在地图上输出

library( sf )
library( data.table )
setDT(coords)
coords_old <- data.table::melt(coords, measure.vars = patterns( lat = "^[a-z]_lat", lon = "^[a-z]_lon" ) )[, c("lon","lat")]
coords_old <- sf::st_as_sf( coords_old, coords = c("lon", "lat"), crs = 4326 )
coords[, c("new_s_lon", "new_s_lat") := ( as.list( unlist( new_s ) ) ) ]
coords[, c("new_e_lon", "new_e_lat") := ( as.list( unlist( new_e ) ) ) ]
coords_new <- data.table::melt(coords, measure.vars = patterns( lat = "^(c|new_[se])_lat", lon = "^(c|new_[se])_lon" ) )[, c("lon","lat")]
coords_new <- sf::st_as_sf( coords_new, coords = c("lon", "lat"), crs = 4326 )

library( leaflet )
leaflet() %>% 
  addTiles() %>% 
  addCircleMarkers( data = coords_old, color = "red" ) %>%
  addCircleMarkers( data = coords_new, color = "blue" )

旧点为红色,新点为蓝色.. 中心点保持不变....

【讨论】:

  • 已更新.. 一些小错误已移除(我正在回收旧代码...)
  • 再次更新....计算方位的方式错误...现在可以正常工作...将很快添加示例输出..
  • 添加了输出图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-06
  • 2020-09-07
  • 2018-09-05
  • 2019-05-30
  • 2015-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多