我有一些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
我有一个数据框(坐标),在每行中包含线的起点s,中心c和终点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所需的尺寸缩放矢量,但是不知何故,新的起点和终点不再位于回归线上:
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")
不确定我要哪种格式的输出,但这可能会让您入门...
产生两个新列;new_s
和new_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" )
旧点是红色,新点是蓝色..中心点保持不变..
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句