2017-07-20 23 views
0

我想從Leaflet & Shiny中的繪製事件構建SpatialLine對象(對照柵格進行評估)。我正在使用leaflet.extras中的addDrawToolbar。從Leaflet/Shiny中繪製的特徵構建SpatialLine

I have done this with a polygon和思想的過渡將是簡單,但顯然不,我使用已經嘗試(和變化):

# get the coordinates of the drawn line 
line_coordinates <- input$mymap_draw_new_feature$geometry$coordinates[[1]] 

# transform them to an sp line 
drawn_line <- Line(do.call(rbind,lapply(line_coordinates,function(x){c(x[[1]][1],x[[2]][1])}))) 

但與NA錯誤或下標出界錯誤的。

下的應用程序不會產生錯誤:

# remove the [[1]] subscript 
line_coordinates <- input$rasmap_draw_new_feature$geometry$coordinates 

# list to matrix of coordinates for Line 
raw <- as.numeric(as.character(do.call(rbind,line_coordinates))) 
raw <- do.call(rbind,lapply(line_coordinates,function(x){c(x[1],x[2])})) 

但是當我陸侃到:

# make Line object 
drawn_line <- Line(raw) 
Warning: Error in .local: cannot derive coordinates from non-numeric matrix 

# or 
drawn_line <- Line(as.numeric(raw)) 
Warning: Error in <Anonymous>: unable to find an inherited method for function ‘coordinates’ for signature ‘"numeric"’ 

但我每次形成這種哪種方式,我有「不能從非派生座標 - 「數字矩陣」或「無法找到函數的繼承方法'座標'簽名'」數字「'」

回答

0

好吧,我有一點成功;

從leaflet.extras內addDrawToolbar繪製的功能如下:

# line feature of 3 vertices 
line_coords <- input$rasmap_draw_new_feature$geometry$coordinates 
print(line_coords) 
[[1]] 
[[1]][[1]] 
[1] -3.214188 

[[1]][[2]] 
[1] 54.55634 

[[2]] 
[[2]][[1]] 
[1] -3.213501 

[[2]][[2]] 
[1] 54.53383 

[[3]] 
[[3]][[1]] 
[1] -3.185349 

[[3]][[2]] 
[1] 54.53323 

class(line_coords) 
"list" 

# then rbind the list into a matrix, all fine 
raw <- do.call(rbind,line_coords) 

print(raw) 
    [,1]  [,2]  
[1,] -3.214188 54.55634 
[2,] -3.213501 54.53383 
[3,] -3.185349 54.53323 

class(raw) 
[1] "matrix" 

所有似乎不錯,但錯誤「的。本地錯誤:無法獲得來自非數字矩陣座標」仍然存在,如果你在上述矩陣上執行Line(raw)。

一看STR(原始)顯示:

str(raw) 
List of 6 
$ : num -3.21 
$ : num -3.21 
$ : num -3.19 
$ : num 54.6 
$ : num 54.5 
$ : num 54.5 
- attr(*, "dim")= int [1:2] 3 2 
NULL 

現在,我真的不知道這是爲什麼發生,列表的矩陣。當上述行爲在列表的標準列表上執行時,它不會發生;

g <- list(list(322000,512000),list(323000,512000),list(325000,514000)) 
co <- do.call(rbind,g) 
str(co) 
num [1:3, 1:2] 322000 323000 325000 512000 512000 514000 

將其更改回數字(按列),將修復它;

raw <- apply(raw, 2,as.numeric) 
str(raw) 
num [1:3, 1:2] -3.19 -3.2 -3.17 54.54 54.52 54.51 

,我可以去,形成線路(原始)對象,並最終空間對象和潛在的柵格表面等

反正進行提取,我希望這是幫助他人的未來。

3

我認爲mapedit結合sf可以幫助你在這裏。這裏有一個小例子(顯然不是嚴格重複的,因爲你可以繪製任何你想要的)。 editMap返回類sf的簡單要素對象,然後可以將其轉換爲sp對象。

library(mapedit) 
library(sf) 

drawn = editMap() # zoom to where you wanna draw and draw a line 
head(drawn) # a sf LINESTRING object 

Simple feature collection with 1 feature and 2 fields 
    geometry type: LINESTRING 
    dimension:  XY 
    bbox:   xmin: 3.8232 ymin: 46.4076 xmax: 9.0088 ymax: 48.8936 
    epsg (SRID): 4326 
    proj4string: +proj=longlat +datum=WGS84 +no_defs 
     X_leaflet_id feature_type      geometry 
    1   81  polyline LINESTRING(4.5483 46.9803, ... 

drawn_sp = as(drawn, "Spatial") # to convert the LINESTRING to a SpatialLinesDataFrame object. 

如果你想利用自己的閃亮的內部應用程序這個功能,看看http://r-spatial.org/r/2017/06/09/mapedit_0-2-0.html#shiny-modules其中@timelyportfolio提供內部閃亮的editMap使用的例子。

+0

歡呼蒂姆,我需要看看你使用繪圖工具開發什麼 – Sam

1

leaflet.extras作者在這裏。返回到Shiny事件的對象是JavaScript端的GeoJSON,我認爲它以R列表的形式出現在R端。如果你想從它的空間對象考慮geojson/geojsonio pkgs將列表轉換爲sp/sf格式。 Tim的使用mapedit的建議也是一個不錯的選擇。 mapedit專門開發用於幫助交互式GIS操作,例如您正在嘗試執行的操作。 leaflet.extras爲它提供了低級基礎,但mapedit提供了更豐富的用戶體驗。

+1

在那個geojson筆記上,'sf :: st_read'也可以讀取這些geojson列表。這是我們在mapedit中使用的。 – TimSalabim

+1

感謝您的回答,我將開始研究geojson軟件包以轉換爲sp格式。我很高興我用原始R代碼管理它,但值得嘗試geojson和mapedit。 TA! – Sam