【问题标题】:Write KML File in R在 R 中编写 KML 文件
【发布时间】:2021-01-26 22:41:59
【问题描述】:

我正在创建一个闪亮的应用程序,允许用户将数据写入 csv 或 kml。但是,我下面的代码不会将功能写入 kml 文件,因此当我在 google earth 中打开 KML 时,它会显示黑点,单击时会显示原始数据的行索引,而不是该特定数据的所有列值观点。我正在使用 writeOGR 函数,但它没有写入文件,所以我切换到使用 plotKML 包。我希望用户选择文件的保存位置(使用我指定的带有日期和时间戳的文件名)并在 Google 地球中显示任何给定数据点的所有功能。

output$downloadData <- downloadHandler(
filename = function() {
  paste0("data_",Sys.Date(), input$download_type)
},
content = function(file) {
  if (input$download_type == ".csv"){
    write.csv(data, file, row.names = FALSE)
  } else if (input$download_type == ".KML") {

    features <- c("COLUMN_1", "COLUMN_2", "COLUMN_3") #These are the features I want displayed in Google Earth
    data[features] <- as.character(data[features])

    coordinates(data) <- ~X + Y
    proj4string(data) <- CRS("+proj=longlat +datum=WGS84")
    
    kml_description(data, caption = "Data",
                    delim.sign = "_", asText = F)
    kml(data, file = file) #Not sure why this produces points but doesn't display features in Google Earth

    #writeOGR(data, dsn = file, layer="Data", driver = "KML")
  }
})

enter image description here

【问题讨论】:

    标签: r shiny plotkml


    【解决方案1】:

    以 Google 地图可以充分读取的方式获取 KML 还没有想象中那么容易。

    您可能想尝试通过sf 包和libkml 导出。

    sf::st_write(obj = an_sf_object, dsn = kml_file_path, 驱动程序 = "libkml")

    (您可能需要在服务器上安装 libkml)

    另请参阅this function from the latlon2map 包以获取有效(尽管不太理想)的实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      • 2020-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多