【发布时间】:2016-01-22 15:16:38
【问题描述】:
在 R 中工作我有 2 个对象,一个 SpatialLinesDataFrame(表示道路网络)和一个 SpatialPointsDataFrame(表示对象位置)。我需要输出最接近每个对象位置的点在道路上的坐标。
我可以从搜索中找到的所有方法都是在其他语言中执行此操作(例如 Python - How to find the closest point on a line segment to an arbitrary point?)或用于查找点和线之间的最小距离(例如使用 geosphere::dist2Line() 或 rgeos::gDistance())。我只对返回直线上最近点的坐标感兴趣。
编辑: 这是我的道路网络的一小部分:
new("SpatialLinesDataFrame"
, data = structure(list(ID = c(0, 0, 0, 0, 0, 0, 0),
ET_ID = c("4", "4", "4", "4", "4", "4", "4"),
length = c(0.280848, 0.812133, 0.0402004, 0.209611, 0.0433089, 0.501865, 0.363501)),
.Names = c("ID", "ET_ID", "length"),
row.names = c(980L, 982L, 983L, 984L, 987L, 988L, 989L),
class = "data.frame")
, lines = list(<S4 object of class structure("Lines", package = "sp")>,
<S4 object of class structure("Lines", package = "sp")>,
<S4 object of class structure("Lines", package = "sp")>,
<S4 object of class structure("Lines", package = "sp")>,
<S4 object of class structure("Lines", package = "sp")>,
<S4 object of class structure("Lines", package = "sp")>,
<S4 object of class structure("Lines", package = "sp")>)
, bbox = structure(c(433266.568837884, 547825.73420664, 437050.511867258, 548168.921069476),
.Dim = c(2L, 2L),
.Dimnames = list(c("x", "y"), c("min", "max")))
, proj4string = new("CRS", projargs = "+proj=utm +zone=37 +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
)
以及我的对象位置:
new("SpatialPointsDataFrame"
, data = structure(list(x = c(38.4129, 38.41697, 38.41501), y = c(4.95659, 4.95809, 4.96122)),
.Names = c("x", "y"), row.names = c(105L, 166L, 185L), class = "data.frame")
, coords.nrs = numeric(0)
, coords = structure(c(434912.0166297, 435363.392542353, 435146.398500838, 547894.637850701, 548060.055746692, 548406.25007762),
.Dim = c(3L, 2L), .Dimnames = list(c("105", "166", "185"), c("x", "y")))
, bbox = structure(c(434912.0166297, 547894.637850701, 435363.392542353, 548406.25007762),
.Dim = c(2L, 2L), .Dimnames = list(c("x", "y"), c("min", "max")))
, proj4string = new("CRS", projargs = "+proj=utm +zone=37 +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
)
提前致谢!
【问题讨论】:
-
也许您愿意分享您的数据示例?
-
好的,我试试输出
-
回答this SO question 有帮助吗?或者
geosphere包中的dist2line函数。 -
您是否在
maptools包中寻找nearestPointOnLine?不过,您必须稍微调整一下您的SpatialLinesDF。此外,由于未打印出 S4 对象,因此您粘贴的数据将不起作用。 -
啊,
snapPointsToLines与Spatial*对象一起使用。您需要几何(投影)坐标,因为它不适用于经纬度。只有在处理全球范围内的数据集时才会出现问题...
标签: r coordinates gis spatial