以下代码将创建连接数据集中所有x / y点的SpatialLines。但是,这些x / y点中的每一个都具有唯一的ID。我需要保留x / y的起点和终点ID作为"属性" (连接它们的每个SpatialLine的ArcGIS术语)。
帮助将不胜感激。
x<-c(2,4,6,3,7,9,1)
y<-c(6,4,8,2,9,6,1)
id<-c("a","b","c","d","e","f","g")
dataset<-data.frame(cbind(x,y,id))
dataset$x<-as.numeric(as.character(dataset$x)) #converting from factor to numeric
dataset$y<-as.numeric(as.character(dataset$y))
plot(dataset$x,dataset$y)
dataset<-do.call(rbind, replicate(7, dataset, simplify=FALSE))
nm=matrix(ncol=3)
for (i in 1:7){
nm<-rbind(nm,do.call(rbind,replicate(7,as.matrix(dataset[i,]),simplify=FALSE)))
}
nm<-nm[-1,]
colnames(nm)<-c("x2","y2","id.dest")
newds<-cbind(dataset,as.data.frame(nm))
newds1<-newds[-which(newds$id==newds$id.dest),]
newds1$x2<-as.numeric(as.character(newds1$x2)) #converting from factor to numeric
newds1$y2<-as.numeric(as.character(newds1$y2))
plot(newds1$x, newds1$y)
plot(newds1$x2, newds1$y2, col="red")
#### *
l <- vector("list", nrow(newds1)) #
origins<-data.frame(cbind(newds1$x, newds1$y))
destinations<-data.frame(cbind(newds1$x2, newds1$y2))
library(sp)
for (i in seq_along(l)) {
l[[i]] <- Lines(list(Line(rbind(origins[i, ], destinations[i,]))), as.character(i))
}
l.spatial<-SpatialLines(l)
plot(l.spatial, add=T)
对象newds1包含起点和终点。但是,从这些起点和终点(l.spatial)创建的最终SpatialLine不包含对起点和终点的引用。我希望那些SpatialLines包含两个&#34;属性&#34;引用起点和终点ID的列。我认为这是将newds1(数据帧)绑定到l.spatial(空间线)上的问题,但运行的代码似乎并没有这样做。
row.names(newds1)<-1:length(newds1$id) #renaming rows in the dataframe so they match the spatial object
id<-newds1$id
newds2<-spCbind(l.spatial, id)
我得到了
"Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘spCbind’ for signature ‘"SpatialLines", "factor"’"
答案 0 :(得分:1)
简而言之:
newds2 <- SpatialLinesDataFrame(l.spatial, newds1, match.ID = FALSE)
## or you can use the rownames of newds1 in the lines loop)
有42条不同的线,通过将每个坐标彼此匹配一次而制成。
FWIW,您不需要从数字因子转换:
x <- c(2,4,6,3,7,9,1)
y <- c(6,4,8,2,9,6,1)
id <- c("a","b","c","d","e","f","g")
## don't coerce to character in the
##first place cbind(x, y, id) *must* be
## character and then data.frame
## converts characters to factors
dataset <- data.frame(x = x, y = y, id = id)
还有其他方法可以简化您的任务,但这是一种相当直接的方式(我认为这就是您所追求的):
x < - c(2,4,6,3,7,9,1) y&lt; - c(6,4,8,2,9,6,1) ID&LT; -C(&#34;&#34;&#34; B&#34;&#34; C&#34;&#34; d&#34;&#34; E&#34 ;, &#34; F&#34;&#34; G&#34) ##不要在第一时间强制转换为字符cbind(x,y,id)必须为字符 ##然后data.frame默认将字符转换为因子 数据集&lt; -data.frame(x = x,y = y,id = id)
l <- vector("list", nrow(dataset) * (nrow(dataset) - 1))
origID <- destID <- character(length(l))
##xy <- as.matrix(dataset[, c("x", "y")])
cnt <- 0
for (i in seq(nrow(dataset))) {
pt0 <- as.matrix(dataset[i, c("x", "y") ])
pts <- dataset[-i, ]
for (j in seq(nrow(pts))) {
cnt <- cnt + 1
l[[cnt]] <- Lines(list(Line(rbind(pt0, as.matrix(pts[j, c("x", "y")])))), as.character(cnt))
destID[cnt] <- pts$id[j]
origID[cnt] <- dataset$id[i]
}
}
x <- SpatialLinesDataFrame(SpatialLines(l), data.frame(dest = destID, orig = origID, row.names = as.character(1:cnt)))
选择一行并进行调查:
itest <- 10
## so for example
as.data.frame(x[itest, ])
index <- c(x$orig[itest], x$dest[itest])
plot(x)
plot(x[itest, ], lwd = 4, add = TRUE)
lines(dataset[index, c("x", "y")], col = "firebrick", lwd = 2)
text(dataset[index, c("x", "y")], label = dataset$id[index], col = "dodgerblue", cex = 4)