R - Calculate distance between two points along a polyline

狂风中的少年 提交于 2019-12-05 21:27:02

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!