从SpatialPointsDataFrame中的每个点到第二个shapefile中最近的点/线的最快笛卡尔距离(R) [英] Fastest cartesian distance (R) from each point in SpatialPointsDataFrame to closest points/lines in 2nd shapefile

查看:134
本文介绍了从SpatialPointsDataFrame中的每个点到第二个shapefile中最近的点/线的最快笛卡尔距离(R)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道最快的算法,用于获取SpatialPointsDataFrame(X)中的每个点与(a)第二个SpatialPointsDataFrame(Y)中最接近的点,或(b)最接近的点之间的笛卡尔距离SpatialLinesDataFrame(Y)中的线段.因此,这基本上是2个问题,答案可能相同.

I want to know the fastest algorithms for obtaining the cartesian distances between each point in a SpatialPointsDataFrame (X) and either (a) the closest point in a second SpatialPointsDataFrame (Y), or (b) the closest line segment in a SpatialLinesDataFrame (Y). So this is basically 2 questions, with perhaps the same answer.

对于这些行,我知道我可以使用dist2Line(X,Y, distfun=distGeo),但这太慢了.在将XY都转换为ppp对象之后,我也尝试使用nncross,如下所示.这是行不通的;热测新距离测量结果表明它没有从Y辐射出去.

For the lines, I know I can use dist2Line(X,Y, distfun=distGeo) but this is insanely slow. I also tried using nncross, after converting both X and Y to ppp objects, as below. This is did NOT work; heat mapping the new distance measure showed that it does not radiate from Y.

    X_ppp <- as(X, "ppp")
    Y_psp <- as(Y, "psp")
    distR <- nncross(X_ppp,Y_ppp,what="dist",k=1)
    X$dist2road <- distR

对于行,我也尝试使用gDistance(X,Y),但遇到错误,因为i = 1,2:Spatial object i is not projected; GEOS expects planar coordinates.我认为这是因为我使用的是lat-lon,它需要一个真实的投影.但是我正在使用的所有文件都是lat-lon,而且我不确定如何选择和指定(对于坦桑尼亚)不使用其他文件中的投影的投影.

For lines, I also tried using gDistance(X,Y) but was met with the error, for i=1,2: Spatial object i is not projected; GEOS expects planar coordinates. I think this is because I'm using lat-lon, and it needs a true projection. But all the files i'm working with are lat-lon, and I'm not sure how to choose and specify a projection (for tanzania) w/out coping it from another file.

对于点,再次使用nncross方法会导致绝对错误的距离. (在每个点和线的情况下,这是因为输出向量的排序方式与X内的点不同吗?如果是这样,我现在看到的是为X内的点输出ID的方法.)

For points, again using the nncross approach resulted in definitely wrong distances. (In each the point and line case, is this because the output vector is not ordered in the same way that the points within X are? If so, I see now way of outputting an ID for the point within X.)

同样,下面的knn代码确实有效.但这显然不在笛卡尔距离内,因此我想将其转换或找到其他提供笛卡尔距离的算法.

Also for points, this knn code below did work. But it's clearly not in cartesian distance, and so I'd like to convert it or find some other algorithm that provides cartesian distance.

    knn.results = knn(data=coordinates(market.shp), 
              query=coordinates(tzprice.shp), k=1)
    knn.results <- data.frame(knn.results)
    tzprice.shp$dist2market <- knn.results[,2]

基本上,我希望为每种目的找到最快的算法(到最近点的距离,到最近线的距离),并以笛卡尔距离或可转换为笛卡尔距离的形式输出.谢谢!

Basically, my hope is to find the fastest algorithm for each purpose (distance to nearest point, distance to nearest line), with output either in cartesian distance or convertible to cartesian distance. Thanks!

推荐答案

有人将我引向一个可能的答案,即找到SpatialPointsDataFrame(X)中每个点与第二个SpatialPointsDataFrame中最接近点之间的笛卡尔距离.称为Y).所以这是我的问题的上半部分……也许那里有一个更快的方法,但是这种方式非常快,并且至少以proj = longlat的形式,它的返回结果是以Km为单位的.

Somebody pointed me towards one possible answer for finding the cartesian distance between each point in a SpatialPointsDataFrame (X) and the closest point in a second SpatialPointsDataFrame (let's call it Y). So that's the first half of my question... perhaps there's a faster method out there, but this way is quite fast, and it DOES return answers in Km, at least if proj=longlat.

    tree <- createTree(coordinates(Y))
    inds <- knnLookup(tree, newdat=coordinates(X), k=1)
    distkm <- sapply(seq_len(nrow(inds)), function(i) spDists(X[i, ], Y[inds[i, ],]))

仍在寻找一种算法,可以快速地找到从X中的每个点到SpatialLinesDataFrame中最近的线的米/公里.

Still looking for an algorithm that (quickly) finds meters/km from each point in X to the nearest line in a SpatialLinesDataFrame.

这篇关于从SpatialPointsDataFrame中的每个点到第二个shapefile中最近的点/线的最快笛卡尔距离(R)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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