【问题标题】:R find distance between line and point at fixed bearing angles [duplicate]R以固定的方位角找到线和点之间的距离[重复]
【发布时间】:2021-02-19 04:53:13
【问题描述】:

这是我的可重现示例

########################################

library(sf)

# matrix of lon lat for the definition of the linestring
m<-rbind(
  c(12.09136, 45.86471),
  c(12.09120, 45.86495),
  c(12.09136, 45.86531),
  c(12.09137, 45.86540),
  c(12.09188, 45.86585),
  c(12.09200, 45.86592),
  c(12.09264, 45.86622),
  c(12.09329, 45.86624),
  c(12.09393, 45.86597),
  c(12.09410, 45.86585),
  c(12.09423, 45.86540),
  c(12.09411, 45.86495),
  c(12.09393, 45.86471),
  c(12.09383, 45.86451),
  c(12.09329, 45.86414),
  c(12.09264, 45.86413),
  c(12.09200, 45.86425),
  c(12.09151, 45.86451),
  c(12.09136, 45.86471)
)

# define a linestring
ls<-st_linestring(m)

# create a simple feature with appropriate crs
ls<-st_sfc(ls, crs=4326)

# and now again going through the very same 
# definition process for a point

# define a point 
pt <- st_point(c(12.09286,45.86557))

# crate simple feature with appropriate crs
pt<-st_sfc(pt, crs = 4326)

plot(ls)
plot(pt, add=TRUE)

# this is computing the minimum distance from the point to the line
st_distance(ls, pt)


###############

鉴于上面提到的玩具数据集,我需要找到一个合适的方法来计算:

1 - 线的每个顶点到给定点的距离:这可能很容易通过勾股定理的简单应用计算每对点(线顶点与点)之间的距离来完成,即使我对此很怀疑,因为正在使用 crs(即 epsg 4326,以度为单位),所以我可能需要首先将整个数据集转换为另一个参考系统(使用公制单位)...

2 - 以固定方位角(10°、20°、30°、....、360° 从北方)的点和线之间的距离:这就是我真正迷路的地方.. ..

请给我一些帮助,以便正确进行计算,可能使用我现在正在尝试熟悉的“sf”标准

谢谢

【问题讨论】:

    标签: r sf


    【解决方案1】:

    感谢你为我指明了正确的方向

    为了完整起见,我制定了我在此处发布的最终解决方案

    # my reproducible example
    
    library(sf)
    
    # matrix of lon lat for the definition of the linestring
    m<-rbind(
      c(12.09136, 45.86471),
      c(12.09120, 45.86495),
      c(12.09136, 45.86531),
      c(12.09137, 45.86540),
      c(12.09188, 45.86585),
      c(12.09200, 45.86592),
      c(12.09264, 45.86622),
      c(12.09329, 45.86624),
      c(12.09393, 45.86597),
      c(12.09410, 45.86585),
      c(12.09423, 45.86540),
      c(12.09411, 45.86495),
      c(12.09393, 45.86471),
      c(12.09383, 45.86451),
      c(12.09329, 45.86414),
      c(12.09264, 45.86413),
      c(12.09200, 45.86425),
      c(12.09151, 45.86451),
      c(12.09136, 45.86471)
    )
    
    # define the linestring
    ls<-st_linestring(m)
    
    # create a simple feature linestring with appropriate crs
    ls<-st_sfc(ls, crs=4326)
    
    # and now again going through the very same 
    # definition process for a point
    
    # define the origin point 
    pt <- st_point(c(12.09286,45.86557))
    
    # create simple feature point with appropriate crs
    pt<-st_sfc(pt, crs = 4326)
    
    plot(ls)
    plot(pt, add=TRUE)
    
    # get minimum distance from the origin point to the line
    dist_min<-st_distance(ls, pt)
    
    # get cordinates of the origin point
    pt_orig<-st_coordinates(pt)
    
    # load library for later use of the function destPoint()
    library(geosphere)
    
    # create vector of bearing angles of 10 degress amplitude
    b_angles<-seq(0, 350, 10) 
    
    # create empty container for final result as data frame
    result<-data.frame(bearing=NULL, distance=NULL)
    
    for(i in 1:length(b_angles)){
      
      result[i,"bearing"]<-b_angles[i]
      
      # calculate destination point coordinates with bearing angle i
      # at fixed safe distance (i.e. 100 times the minimum distance)
      # so that to avoid null intersection in next step calculation
      pt_dest<-destPoint(p=pt_orig, b=b_angles[i],d=dist_min*100)
      
      # define linestring from origin to destination
      b_ls<-st_sfc(st_linestring(rbind(pt_orig, pt_dest)), crs=4326)
      
      # get the intersection point between two features
      pt_int<-st_intersection(ls, b_ls)
      
      # get the distance
      d<-st_distance(pt, pt_int)
      
      result[i,"distance"]<-d
    }
    

    我尽可能坚持使用“sf”方法,该方法在执行 st_intersection() 时在 for 循环内给出以下警告:“虽然坐标是经度/纬度,但 st_intersection 假定它们是平面的”

    但考虑到我正在使用的短距离,在我看来这是一个可以接受的近似值

    顺便说一下,据我了解,它在“sf”包中不存在与 geosphere::destPoint 对应的函数

    谢谢

    【讨论】:

      猜你喜欢
      • 2022-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-25
      • 2022-06-23
      • 2018-05-21
      • 2018-07-04
      • 2012-10-05
      相关资源
      最近更新 更多