2015-12-05 46 views
0

我希望有一個簡單的方法來計算座標的最小值和最大值爲SpatialLinesDataFrame對象每行從SpatialLinesDataFrame對象座標的最大值和計算的最小

代碼:

coordinates(contour) 

提取SpatialLinesDataFrame對象:

[[9]] 
[[9]][[1]] 
       [,1]  [,2] 
    [1,] -4.44583300 45.87010 
    [2,] -4.24583300 45.87874 
    [3,] -4.04583300 45.90037 
    [4,] -4.02830912 45.90306 

[20,] -1.6458330 42.98340 
[21,] -1.8458330 43.07336 


[[12]] 
[[12]][[1]] 
      [,1]  [,2] 
[1,] -1.845833 43.48721 
[2,] -1.849027 43.50306 
[3,] -1.845833 43.50926 
[4,] -1.710073 43.70306 
[5,] -1.645833 43.74554 
[6,] -1.445833 43.73724 
[7,] -1.373848 43.70306 
[8,] -1.261626 43.50306 
[9,] -1.308085 43.30306 
[10,] -1.445833 43.17663 
[11,] -1.645833 43.16952 
[12,] -1.808587 43.30306 
[13,] -1.845833 43.48721 


[[13]] 
[[13]][[1]] 
      [,1]  [,2] 
[1,] -1.645833 43.34325 
[2,] -1.712682 43.50306 
[3,] -1.645833 43.58276 
[4,] -1.445833 43.58877 
[5,] -1.376018 43.50306 
[6,] -1.445833 43.33714 
[7,] -1.645833 43.34325 

有沒有更簡單的方法呢?

編輯:

我給通過@EDi採取展示我想résulat一個例子:

[[1]] 
[[1]][[1]] 
    [,1] [,2] 
[1,] 1 3 
[2,] 2 2 
[3,] 3 2 

min(1,2,3)=1 & min(3,2,2)=2 
max(1,2,3)=3 & max(3,2,2)=3 

[[1]][[2]] 
    [,1] [,2] 
[1,] 1.05 3.05 
[2,] 2.05 2.05 
[3,] 3.05 2.05 

min(1.05,2.05,3.05)= 1.05 & min(3.05,2.05,2.05)= 2.05 
max(1.05,2.05,3.05)= 3 .05 & max(3.05,2.05,2.05)= 3.05 


[[2]] 
[[2]][[1]] 
    [,1] [,2] 
[1,] 1 1.0 
[2,] 2 1.5 
[3,] 3 1.0 


min(1,2,3)= 1& min(1.0,1.5,1.0)= 1.0 
max(1,2,3)= 3 & max(1.0,1.5,1.0)= 1.5 

回答

1

注知道如果我明白你的正確的......像這樣的事情?

# Some Lines -------------------------------------------------------------- 
require(sp) 
l1 = cbind(c(1,2,3), c(3,2,2)) 
l1a = cbind(l1[,1]+.05,l1[,2]+.05) 
l2 = cbind(c(1,2,3),c(1,1.5,1)) 
sl1 = Lines(list(Line(l1), Line(l1a)), ID = 'a') 
sl2 = Lines(Line(l2), ID = 'b') 
sl = SpatialLines(list(sl1, sl2)) 
plot(sl, col = c("red", "blue")) 
abline(v = 1:3, lty = 'dotted') 
abline(h = 1:3, lty = 'dotted') 



# Extract min/max of coordinates for each line -------------------------- 
cc <- coordinates(sl) 
foo <- function(y) { 
    # combine coordinates lines with same ID 
    ccl <- do.call(rbind, y) 
    # return min/max 
    return(c(range(ccl[,1]), range(ccl[,2]))) 
} 
out <- t(sapply(cc, foo)) 
out 
# for each line one row 
# from left to right (min(x), max(x), min(y), max(y)) 

更新

根據您的修改(這是我不清楚,你要爲每個線段的程度),我建議:

foo <- function(y) { 
    return(c(range(y[,1]), range(y[,2]))) 
} 
rapply(cc, foo, how = 'unlist') 
matrix(rapply(cc, foo, how = 'unlist'), nrow = 4) 

rapply()應用功能也列入子列表中,matrix()僅用於格式化。

+0

我還沒有得到我想要的,我編輯我的文章是舉一個例子,展示你已經採取的例子,你能看到嗎? – user26480

+0

非常感謝它的工作 – user26480