R-计算沿折线的两点之间的距离 [英] R - Calculate distance between two points along a polyline

查看:207
本文介绍了R-计算沿折线的两点之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中-我已将不直的多段线shapefile(表示蜿蜒的州际公路)导入到环境中.我已经阅读了一个.csv文件,其中包含指向环境的点(以十进制表示的纬度/经度).所有这些点都位于多义线上.

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

有人知道R包可以计算沿一条折线的两个纬度/经度点之间的距离吗?

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

推荐答案

数据包PBSmapping具有calcLength函数,该函数直接计算折线或多边形的长度(即PolySet).如果您的shapefile采用该格式,则该包也可以导入ESRI shapefile.

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.

简单折线的示例:

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)

这篇关于R-计算沿折线的两点之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆