【问题标题】:Save features created with leaflet.draw to comptuer将使用 leaflet.draw 创建的特征保存到计算机
【发布时间】:2015-05-21 13:37:02
【问题描述】:

我正在制作一张传单地图,我想使用 leaflet.draw 插件为用户提供绘制矢量和标记的选项。接下来,我想为用户提供将向量/标记保存到他们的计算机的选项(例如,作为 geojson)。我正在寻找如何实现这一点的指针和示例。

【问题讨论】:

    标签: draw leaflet geojson


    【解决方案1】:

    如果您可以将所有功能放入LayerGroup,那么您可以在其上调用toGeoJSON() - 请参阅http://leafletjs.com/reference.html#layergroup-togeojson。然后可以将结果提供给FileSaver.js 以供客户端下载,例如

    var blob = new Blob([JSON.stringify(result)], {type: "application/json;charset=utf-8"});
    saveAs(blob, "features.json");
    

    【讨论】:

      【解决方案2】:
      editCoord <- function(){
        pgn <- NULL
        if(length(input$map_draw_all_features$features) != 0) {
          if(unique(unlist(lapply(input$map_draw_all_features$features, function(x){x$geometry$type}))) == "Polygon") {
            for(j in 1:length(input$map_draw_all_features$features)){
              geo <- unlist(lapply(input$map_draw_all_features$features[j], function(x){x$geometry$coordinates}))
              v <- seq(1, length(geo), 2)
              n <- length(geo)/2 
              for (i in 1:n) {  
                xy <- c(geo[v[i]], geo[i*2])
                if (i == n) break()
                pgn <- c(pgn, paste0(xy[1]," ",xy[2],","))
              }
              pgn <- c(pgn, paste0(xy[1]," ",xy[2]))
            }
            cat(pgn, "\n", file=paste0("f", input$usertext))
            readLines(paste0("f", input$usertext))
          }
        } 
      }
      
      editPlot <- function(){
        db <- dbConnect(MySQL(), dbname = "watsan", host = options()$rds$host, 
                        port = options()$rds$port, user = options()$rds$user, 
                        password = options()$rds$password)
        # Construct the update query
        query <- paste0('UPDATE plots SET `geom_plot`= ST_GeomFromText("POLYGON((',"",Coord(),"",'))") WHERE `parcel_id`=',"'",  ID() ,"'",';')
        query <- gsub('\"', "'", query)
        # Submit the fetch query and disconnect
        dbGetQuery(db, query)
        dbDisconnect(db)
      }
      

      【讨论】:

      • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-06
      • 1970-01-01
      • 2012-12-05
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      相关资源
      最近更新 更多