R - Calculate distance between two points along a

2019-05-19 22:58发布

In R - I have imported a polyline shapefile that is not straight (represents a winding interstate highway) into the environment. I have read a .csv file that contains points (latitude/longitude in decimal degrees) into the environment. All of those points lie along the polyline.

If I take two random points I can calculate the "as the crow flies" distance between them using great circle distance calculation. But what is needed is distance traveled along the polyline. See reference image:

https://i.stack.imgur.com/zhorb.png

Is anyone aware of an R package that can calculate the distance between two lat/lon points along a polyline?

1条回答
趁早两清
2楼-- · 2019-05-19 23:58

Package PBSmapping has a calcLength function which directly calculates the length of a polyline or polygon (in what it terms a PolySet). The package can import ESRI shapefiles as well, if your shapefile is in that format.

Example of a simple polyline:

line = read.table(text = "X, Y, POS, PID
37.772, -122.214, 1, 1
21.291, -157.821, 2, 1
-18.142, 178.431, 3, 1
-27.467, 153.027, 4, 1", header = TRUE, sep = ",")

library(PBSmapping)
pline = as.PolySet(line, projection = 1)

calcLength(pline)
  PID   length
1   1 404.8539 

plotLines(pline)

enter image description here

查看更多
登录 后发表回答