如何通过 R 中的不同 X、Y 值移动空间线

How to shift spatial lines by different X, Y values in R

我有一个由 57 条线组成的空间线数据框。我想将每一行移动不同的 x 和 y 值。 也就是说,我希望构成第一条线的所有点都在 x 方向移动 100,在 y 方向移动 50。然后第二条线的所有点在x方向移动20,在y方向移动150。

我在光栅包中使用了 shift 函数,但它会将构成每一行的坐标移动不同的 x 和 y 值

也许我需要提取每一行然后移动它,但我不知道该怎么做。

下面的示例带有两行 DF。我希望第一条空间线移动 (100,50),下一条空间线移动 (20,150)

    #Make example spatial line DF  
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")))

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

#list of x, y co-ords to shift each line by  
x<-c(100,20)
y<-c(50,150)

#Shift lines
shifted_lines<-shift(SL.df,x,y)

SL.df@lines
shifted_lines@lines

第一行的第一个坐标移动了 (100,50)。第一行的第二个坐标移动了 (20,150)。这也发生在构成第二行的坐标

我想你可以这样做:

library(raster)
a <- c(1,5,2,6,3,6,3,5,7)
b <- c(5,3,5,7,2,6,9,3,6)
SL.df <- spLines(cbind(1:9 ,a), cbind(1:9, b), attr=data.frame(OBJECTID=paste(1:2), NAME=c("st 1", "av B")))
x <- c(100,20)
y <- c(50,150)

s <- list()
for (i in 1:length(SL.df)) {
   s[i] <- shift(SL.df[i, ], x[i], y[i])
}
ss <- do.call(bind, s)