【发布时间】:2014-07-17 23:36:25
【问题描述】:
我有一个代表街道的 SpatialLinesDataFrame。我需要找到两条街道的交叉点。
我将从How to get the intersection point of two vector?复制一个玩具示例
library(rgdal)
library(rgeos)
a = c(1,5,2,6,3,6,3,5,7)
b = c(5,3,5,7,2,6,9,3,6)
SL1 <- SpatialLines(list(Lines(Line(cbind(seq_along(a),a)), "A")))
SL2 <- SpatialLines(list(Lines(Line(cbind(seq_along(b),b)), "B")))
# Build my own SpatialLines with both objects, couldn't find a way
# to build it from SL1 and SL2
SL <- SpatialLines(list(Lines(Line(cbind(seq_along(a),a)), "A"), Lines(Line(cbind(seq_along(b),b)), "B")))
SL.df <- SpatialLinesDataFrame(SL, data=data.frame(OBJECTID=paste(1:2), NAME=c("st 1", "av B"), row.names=row.names(SL)))
从我复制的问题的答案中,我知道我可以得到交集
# Find intersections
coords <- coordinates(gIntersection(SL1, SL2))
但假设我已经使用 readOGR 阅读了 SL.df 并且无权访问原始组件。我应该如何提取个别街道?
经过一段时间的徘徊,我想出了
st.1 <- SpatialLines(SL.df@lines[which(row.names(SL.df) == row.names(SL.df)[which(SL.df@data$NAME == 'st 1')])])
av.B <- SpatialLines(SL.df@lines[which(row.names(SL.df) == row.names(SL.df)[which(SL.df@data$NAME == 'av B')])])
coords <- coordinates(gIntersection(st.1, av.B))
它有效,但个别街道的提取似乎不是很优雅。有没有更好的方法?
谢谢!
【问题讨论】: