簡體   English   中英

基於@data信息的SpatialLinesDataFrame子集

[英]Subset SpatialLinesDataFrame based on @data info

我有一個SpatialLinesDataFrame代表街道。 我需要找到2條街道的交叉點。

我將從“ 如何獲取兩個向量的交點”中復制一個玩具示例

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))

它可以工作,但是提取單個街道似乎並不十分優雅。 有更好的方法嗎?
謝謝!

有兩個選擇:

coordinates(gIntersection(subset(SL.df, NAME=="st 1"), 
                          subset(SL.df, NAME=="av B")))
#          x        y
# 1 1.666667 3.666667
# 1 2.400000 3.800000
# 1 4.500000 4.500000
# 1 6.000000 6.000000
# 1 7.750000 4.500000


coordinates(gIntersection(SL.df[SL.df$NAME=="st 1",], 
                          SL.df[SL.df$NAME=="av B",]))
#          x        y
# 1 1.666667 3.666667
# 1 2.400000 3.800000
# 1 4.500000 4.500000
# 1 6.000000 6.000000
# 1 7.750000 4.500000

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM