本文介绍了R/GIS:查找位置和最近的线之间的正交距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一组位置坐标和一组线(道路或河流)之间的正交距离.点集以纬度/经度对的形式出现,而线在shapefile(.shp)中.使用maptoolsPBSmapping在地图上绘制它们都不是问题.但是我的基本问题是找到一个人必须要从某个位置行驶到道路或河流的最小距离.有什么办法可以在R中做到这一点?

I am trying to find the orthogonal distance between a set of location coordinates and a set of lines (roads or rivers). The set of points are in the form of latitude/longitude pairs, and the lines are in a shapefile (.shp). Plotting them on a map is not a problem, using either maptools or PBSmapping. But my basic problem is to find the minimum distance one has to travel from a location to reach a road or a river. Is there any way to do this in R?

推荐答案

如果我理解正确,则可以使用rgeos包中的gDistance来完成此操作.

If I understand correctly, you can do this simply enough with gDistance in the rgeos package.

SpatialLines/DataFrame读入行,以SpatialPoints/DataFrame读入点,然后遍历每个点,每次都计算距离:

Read in the lines as SpatialLines/DataFrame and points as SpatialPoints/DataFrame and then loop over each point calculating the distance each time:

require(rgeos)
## untested code
shortest.dists <- numeric(nrow(sp.pts))
for (i in seq_len(nrow(sp.pts)) {
    shortest.dists[i] <- gDistance(sp.pts[i,], sp.lns)
}

此处sp.pts是空间点"对象,而sp.lns是空间线"对象.

Here sp.pts is the Spatial points object, and sp.lns is the Spatial lines object.

必须循环,以便仅将sp.pts中的单个坐标与sp.lns中所有线的几何形状进行比较,否则,您将获得所有点上的合计值的距离.

You must loop so that you only compare a single coordinate in sp.pts with the entirety of all lines geometries in sp.lns, otherwise you get the distance from an aggregate value across all points.

由于您的数据以经度/纬度为单位,因此您应该将线和点都转换为合适的投影,因为gDistance函数采用笛卡尔距离.

Since your data are in latitude/longitude you should transform both the lines and points to a suitable projection since the gDistance function assumes Cartesian distance.

更多讨论和示例(编辑)

MORE DISCUSSION AND EXAMPLE (edit)

获得线上最接近的点而不只是距离很整齐,但这打开了另一个选择,即您是否需要沿直线的最接近的坐标或实际的 intersection 的线段比任何现有顶点都近.如果您的顶点足够密集,以至于差异无关紧要,请在sp包中使用spDistsN1.您必须从集合中的每条线中提取所有坐标(不难,但有点难看),然后循环遍历每个兴趣点,计算到线顶点的距离-然后您可以找到最短的线并选择从顶点集中的坐标,因此您可以轻松获得距离和坐标.由于函数可以在longlat = TRUE参数中使用椭圆距离,因此也无需进行投影.

It would be neat to get the nearest point on the line/s rather than just the distance, but this opens another option which is whether you need the nearest coordinate along a line, or an actual intersection with a line segment that is closer than any existing vertex. If your vertices are dense enough that the difference doesn't matter, then use spDistsN1 in the sp package. You'd have to extract all the coordinates from every line in the set (not hard, but a bit ugly) and then loop over each point of interest calculating the distance to the line vertices - then you can find which is the shortest and select that coordinate from the set of vertices, so you can have the distance and the coordinate easily. There's no need to project either since the function can use ellipsoidal distances with longlat = TRUE argument.

library(maptools)

## simple global data set, which we coerce to Lines
data(wrld_simpl)

wrld_lines <- as(wrld_simpl, "SpatialLinesDataFrame")

## get every coordinate as a simple matrix (scary but quick)
wrld_coords <- do.call("rbind", lapply(wrld_lines@lines, function(x1) do.call("rbind", lapply(x1@Lines, function(x2) x2@coords[-nrow(x2@coords), ]))))

以交互方式签出,您必须对其进行修改以保存坐标或最小距离.这将绘制线条并等待您单击绘图中的任意位置,然后将单击后的线条绘制到最接近的 vertex 线上.

Check it out interactively, you'll have to modify this to save the coords or minimum distances. This will plot up the lines and wait for you to click anywhere in the plot, then it will draw a line from your click to the nearest vertex on a line.

## no out of bounds clicking . . .
par(mar = c(0, 0, 0, 0), xaxs = "i", yaxs = "i")

plot(wrld_lines, asp = "")

n <- 5

for (i in seq_len(n)) {
xy <- matrix(unlist(locator(1)), ncol = 2)
    all.dists <- spDistsN1(wrld_coords, xy, longlat = TRUE)
    min.index <- which.min(all.dists)
    points(xy, pch = "X")
lines(rbind(xy, wrld_coords[min.index, , drop = FALSE]), col = "green", lwd = 2)
}

这篇关于R/GIS:查找位置和最近的线之间的正交距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:06