您可以使用rgeos::gDistance()
和byid=TRUE
来获得从每个点到每条线的距离矩阵。从那里,它是比较容易提取10行的ID最接近各点:
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"
搜索:http://stackoverflow.com/questions/15294343/calculating-the-distance-between-polygon-and -point-in-r –
链接的答案很有用,但仅显示如何计算一点与一个多边形之间的距离。 –