SpatialLinesDataFrame:如何计算最小值.点与线之间的距离 [英] SpatialLinesDataFrame: how to calculate the min. distance between a point and a line

查看:118
本文介绍了SpatialLinesDataFrame:如何计算最小值.点与线之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有街道的SpatialLinesDataFrame,并且有一个GPS坐标列表.我需要做的是找出每个GPS坐标的10个最接近的街道名称.

I have a SpatialLinesDataFrame with streets and I have a list of GPS coordinates. What I need to do is to get out the 10 closest street names for each individual GPS coordinate.

R中是否有一个函数/程序包可以为SpatialLinesDataFrame计算线与点之间的距离?我看不到任何有助于'sp'的东西.

Is there a function/package in R that would calculate the distance between a line and a point for a SpatialLinesDataFrame? I can't see anything that would help in 'sp'.

有一个相关的问题:计算多边形之间的距离和R中的点,但是我想找到线对象和点之间的距离,而不是多边形/点,点/点.

There is a related question: Calculating the distance between polygon and point in R, but I want to find the distance between a line object and a point, not polygon/point, point/point.

推荐答案

您可以将rgeos::gDistance()byid=TRUE结合使用,以获取从每个点到每条线的距离矩阵.从那里,提取最接近每个点的10条线的ID相对容易:

You could use rgeos::gDistance() with byid=TRUE to get a matrix of distances from each point to each line. From there, it's relatively easy to extract the ids of the 10 lines nearest to each point:

library(sp)
library(rgeos)

## Create a SpatialPoints and a SpatialLines object
example("SpatialPoints-class", ask=FALSE, echo=FALSE)
example("SpatialLines-class", ask=FALSE, echo=FALSE)

## Compute the matrix of distances between them.
(m <- gDistance(S, Sl, byid=TRUE))
#          1   2        3        4        5
# a 0.000000 0.0 2.757716 1.414214 2.757716
# b 1.788854 0.5 3.640055 1.000000 3.605551

## And then use it to extract the ids of the 10 (or in this case 1) lines
## closest to each point.
## apply(m, 2, function(X) rownames(m)[order(X)][1:10]) ## Finds 10 closest
apply(m, 2, function(X) rownames(m)[order(X)][1])       ## Finds single closest
#   1   2   3   4   5 
# "a" "a" "a" "b" "a" 

这篇关于SpatialLinesDataFrame:如何计算最小值.点与线之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆