【问题标题】:Error in downloading multiple raster files in GeoTIFF format through downloadHandler of R Shiny通过 R Shiny 的 downloadHandler 下载 GeoTIFF 格式的多个光栅文件时出错
【发布时间】:2021-06-24 07:10:34
【问题描述】:

我正在尝试制作一个基于 Shiny 的 Web 应用程序,它可以接受多个二进制文件作为用户的输入,并为每个输入文件创建一个 GeoTiff 光栅文件供用户下载。但是,我无法使用 R Shiny 的 DownloadHandler 将多个输出 GeoTiff 文件下载为 zip 文件夹。我尝试过的代码如下。

#Import required packages
library(shiny)
library(raster)
library(rgdal)
library(zip)
library(stringr)

#User Interface
ui = fluidPage(
titlePanel(title = "Convert Binary raster data to GeoTiff"),
sidebarLayout(position = "right",
sidebarPanel(
fileInput(inputId = "layer", label = "Upload your binary raster 
data", multiple = TRUE)
),
mainPanel(tableOutput("files"),
tableOutput("path"),
downloadButton(outputId = "downloadTiff", label = "Download GoeTiff 
file")
)
)
)
#Function to convert binary file to GeoTIFF raster data
bin_raster = function(x){
rf.file = file(x, 'rb')
bin_data = readBin(rf.file, what = 'double', n = 120705, size = 4, 
endian = "big")
rf_matrix = matrix(data = bin_data, nrow = 301, ncol = 401, byrow = 
T)
rf_rast1 = flip(raster(rf_matrix, xmn = 70, xmx = 110, ymn = 5, ymx = 
35,
crs = "+proj=longlat +datum=WGS84"), direction = 2)
return(rf_rast1)
}

#Server
server = function(input, output){

inFile = reactive({
if(is.null(input$layer$datapath)){return()}
else{
 for (i in input$layer$datapath) {
    wb = bin_raster(i)
    return(wb)
}
}

})

output$files = renderTable(input$layer)
output$path = renderTable(input$layer$datapath)

output$downloadTiff = downloadHandler(
filename = "GeoTiff.zip",
content = function(file){
#go to a temp dir to avoid permission issues
owd <- setwd(tempdir())
on.exit(setwd(owd))
files <- NULL;

for (i in 1:input$layer$datapath){

  fileName <- paste(input$layer$name,i,".tif",sep = "")
  writeRaster(inFile()$wb[i],fileName, format = "GTiff", overwrite = 
   TRUE)
  files <- c(fileName,files)
  }
#create the zip file
 zip(file,files)
 }
 )

 }

 shinyApp(ui = ui, server = server)

它向我显示以下错误: 1:input$layer$datapath 中的警告: 数值表达式有 2 个元素:仅使用第一个元素 download$func(tmpdata) 中的警告:强制引入的 NA 警告: :: NA/NaN 参数中的错误 [没有可用的堆栈跟踪]

请帮帮我。

【问题讨论】:

    标签: r shiny download zip raster


    【解决方案1】:

    这是一些运行的代码。

    library(shiny)
    library(raster)
    library(rgdal)
    library(zip)
    library(stringr)
    
    ui = fluidPage(
        titlePanel(title = "Convert Binary raster data to GeoTiff"),
        sidebarLayout(
            position = "right",
            sidebarPanel(
                fileInput(
                    inputId = "layer",
                    label = "Upload your binary raster data",
                    multiple = TRUE
                )
            ),
            mainPanel(
                tableOutput("files"),
                tableOutput("path"),
                downloadButton(outputId = "downloadTiff", label = "Download GoeTiff file")
            )
        )
    )
    
    bin_raster = function(x) {
        rf.file = file(x, 'rb')
        bin_data = readBin(
            rf.file,
            what = 'double',
            n = 120705,
            size = 4,
            endian = "big"
        )
        rf_matrix = matrix(
            data = bin_data,
            nrow = 301,
            ncol = 401,
            byrow = T
        )
        rf_rast1 = flip(
            raster(
                rf_matrix,
                xmn = 70,
                xmx = 110,
                ymn = 5,
                ymx = 35,
                crs = "+proj=longlat +datum=WGS84"
            ),
            direction = 2
        )
        return(rf_rast1)
    }
    
    server = function(input, output) {
        inFile = reactive({
            if (is.null(input$layer$datapath)) {
                return()
            }
            else{
                for (i in input$layer$datapath) {
                    wb = bin_raster(i)
                    return(wb)
                }
            }
      })
    
        output$files = renderTable(input$layer)
        output$path = renderTable(input$layer$datapath)
        
        output$downloadTiff = downloadHandler(
            filename = "GeoTiff.zip",
            content = function(file) {
                #go to a temp dir to avoid permission issues
                owd <- setwd(tempdir())
                on.exit(setwd(owd))
                files <- NULL
                for (i in 1:length(input$layer$datapath)) {
                    fileName <- paste(input$layer$name[i], ".tif", sep = "")
                    rr <- bin_raster(input$layer$datapath[i])
                    writeRaster(rr,
                                            fileName,
                                            format = "GTiff",
                                            overwrite = TRUE)
                    files <- c(fileName, files)
                }
                #create the zip file
                zip::zip(file, files)
            }
        )
    }
    
    shinyApp(ui = ui, server = server)
    

    我更改了以下内容

    • for 循环必须使用输入的长度
    • 必须读取每个文件并将其转换为光栅,因此我在写入gtiff 光栅之前调用了bin_rasterinFile() 函数不起作用,因为它返回输入列表中的第一项,而不是所有项的列表。

    读取栅格的代码会产生奇怪的结果,但我认为这是一个单独的问题。通常你会使用raster(),但如果栅格包含许多文件,这将不起作用。

    【讨论】:

      猜你喜欢
      • 2020-12-08
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 2020-04-30
      • 2019-10-03
      • 2021-01-04
      • 1970-01-01
      • 2023-03-17
      相关资源
      最近更新 更多