2017-07-20 24 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