简体   繁体   English

R - 计算沿折线的两点之间的距离

[英]R - Calculate distance between two points along a polyline

In R - I have imported a polyline shapefile that is not straight (represents a winding interstate highway) into the environment.在 R - 我已经将一个不直的多段线形状文件(代表一条蜿蜒的州际公路)导入到环境中。 I have read a .csv file that contains points (latitude/longitude in decimal degrees) into the environment.我已将包含点(十进制度数的纬度/经度)的 .csv 文件读取到环境中。 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 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?有没有人知道可以计算沿折线的两个纬度/经度点之间的距离的 R 包?

Package PBSmapping has a calcLength function which directly calculates the length of a polyline or polygon (in what it terms a PolySet ).PBSmapping有一个calcLength函数,它直接计算折线或多边形的长度(用它的术语来说是PolySet )。 The package can import ESRI shapefiles as well, if your shapefile is in that format.如果您的 shapefile 是该格式,该包也可以导入 ESRI shapefile。

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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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