![](/img/trans.png)
[英]How to control for the amount of noise added to x, y spatial points in R?
[英]Retain start and end IDs when creating spatial lines from X Y points
以下代码将创建连接数据集中所有x / y点的SpatialLines。 但是,每个x / y点都有唯一的ID。 我需要将x / y的起点和终点ID都保留为连接它们的每条SpatialLines的“属性”(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)创建的最终SpatialLines不包含对起点和终点的引用。 我希望这些SpatialLines包含两个“属性”列,这些列引用起点和终点的ID。 我认为这是将newds1(数据帧)绑定到l.spatial(空间行)的问题,但是我正在运行的代码似乎没有做到这一点。
"Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘spCbind’ for signature ‘"SpatialLines", "factor"’"
我懂了
"Error in (function (classes, fdef, mtable) : unable to find an inherited method for function 'spCbind' for signature '"SpatialLines", "factor"'"
简而言之:
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 <-c(6,4,8,2,9,6,1)id <-c(“ a”,“ b” ,“ c”,“ d”,“ e”,“ f”,“ g”)##首先不要强迫字符cbind(x,y,id) 必须是字符##,然后是数据。默认情况下,frame将字符转换为因子dataset <-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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.