在 R 中,我有两个对象,一个 SpatialLinesDataFrame(表示道路网络)和一个 SpatialPointsDataFrame(表示对象位置)。我需要输出距离每个对象位置最近的道路上的坐标。
我所能找到的搜索结果都是其他语言(例如 Python - 如何找到线段上到任意点最近的点?)或者用于查找点与线之间最小距离的方法(例如使用
编辑: 这是我的道路网络的一个小子集:
我的物体位置:
提前感谢!
我所能找到的搜索结果都是其他语言(例如 Python - 如何找到线段上到任意点最近的点?)或者用于查找点与线之间最小距离的方法(例如使用
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")
)
提前感谢!
@coords
来表示点的坐标,但我不确定如何输出线的坐标。 - AndrewsnapPointsToLines
适用于Spatial*
对象。由于它不适用于纬度和经度坐标,所以需要使用几何(投影)坐标。只有在处理全球范围很大的数据集时才会出现问题... - Spacedmanmaptools::snapPointsToLines
可以完成任务。我的研究区域很小,所以没有范围的问题。 - Andrew