Fastest cartesian distance (R) from each point in SpatialPointsDataFrame to closest points/lines in 2nd shapefile
Asked Answered
S

1

1

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.

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

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.

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.)

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!

Scrubby answered 11/6, 2020 at 23:45 Comment(0)
S
0

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, ],]))

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

Scrubby answered 12/6, 2020 at 16:54 Comment(4)
This answer shows how you can find the distance to the nearest polygon in a SpatialPolygonsDataFrame, and the same approach should work equally well with a SpatialLinesDataFrame.Mendacious
Josh thank you! gDistance requires a projection that is not lat-lon, right? So if I'm working w/ lat-lon projected data, I need to first project it to UTM (or otherwise), just sort of randomly pick any appropriate projection, and then implement this?Scrubby
Exactly. It just needs to be projected into a locally appropriate projection with x and y coordinates both in units of, e.g. meters or kilometers.Mendacious
Thank you!! This worked perfectly, and I really, really appreciate you taking the time to give me the link. One thing: I noted below your answer that lUTM needs to be replaced with pUTM for the example to work. There is no previously defined lUTM object in your example.Scrubby

© 2022 - 2024 — McMap. All rights reserved.