简体   繁体   中英

How to calculate road network distances between a reference line(or point) and a dataframe of long/lat points with R?

I want to calculate road network distances between a reference line (or a reference point if a single point facilitates the possible solution) and a dataframe of long/lat points. I have the following data frame:

   Latitude Longitude
1  40.66858 22.88713
2  40.66858 22.88713
3  40.66858 22.88713
4  40.66858 22.88713
5  40.66858 22.88714
6  40.66857 22.88715
7  40.66858 22.88716
8  40.66858 22.88717
9  40.66859 22.88718
10 40.66861 22.88719 

and the following reference line with start/end coordinates:

22.88600 40.66885
22.88609 40.66880 

(If we want a single reference point in the middle of the line (instead of the whole line) its coordinates are: 22.88602844465866,40.66883357487465 ) Here is a screenshot from google earth after plotting the points and the line: 在此处输入图片说明

I have tried to compute the distances of each point with the reference line with the following way:

dist2Line(points, line, distfun=distHaversine) #from geosphere package

The distance which is computed (eg for the first point) is the one with the yellow line in the following screenshot. The desired one is the one with the red line (road network distance). How can I solve this? I want to compute the road network distances for all points! Thank you in advance! 在此处输入图片说明

library(sp)
library(rgeos)
library(geosphere)

Let's join the midpoint of your line to the other line:

pt1 <- matrix(c(22.88600, 40.66885), ncol=2)
pt2 <- matrix(c(22.88609, 40.66880), ncol=2)

midpt <- as.data.frame(midPoint(pt1, pt2))

NOTE: The first 4 line points are the same in your supplied data

read.csv(text="lat,lon
40.66858,22.88713
40.66858,22.88713
40.66858,22.88713
40.66858,22.88713
40.66858,22.88714
40.66857,22.88715
40.66858,22.88716
40.66858,22.88717
40.66859,22.88718
40.66861,22.88719", stringsAsFactors = FALSE) -> l

l <- rbind.data.frame(midpt, l)

Using the midpoint on the line isn't perfect so you could use the spatial intersection operations as well to find the correct intersecting point.

Now, make it a spatial object and give it the boring longlat "projection".

l <- SpatialLines(list(Lines(Line(l[,2:1]), "1")), proj4string = CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"))

Convert said "projection" to something meaningful (I picked EPSG:3265 , but choose whatever you want so you can get real distance):

l <- spTransform(l, CRS("+init=epsg:3265"))

Get the points from the line:

pts <- as(l, "SpatialPoints")

Follow How to calculate geographic distance between two points along a line in R? to get the distance between points which you can do the rest from there:

diff(sort(gProject(l, pts, normalized = FALSE)))
##  [1] 372.553928   0.000000   0.000000   0.000000   3.360954   4.581859
##  [7]   4.581860   3.360956   4.581862   7.077129

It'd be 👍🏼 if someone who knows how to do this with sf could do that as well since I couldn't find a gProject equivalent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM