如何使用R从下载的gzip文件中提取KML文件?

时间:2018-09-05 23:17:35

标签: r gunzip

我正在尝试从Web下载压缩文件,然后提取其中的单个kml文件。我尝试了几种不同的utils函数来解压缩和解压缩,但不确定如何获取可以开始使用的kml(在sf包中)。

zipFileName <- "http://satepsanone.nesdis.noaa.gov/pub/volcano/FIRE/HMS_ARCHIVE/2010/KML/smoke20100101.kml.gz"
  smokeFileName <- "smoke20100101.kml"

  temp <- tempfile()
  download.file(url = zipFileName, destfile = temp)



  untar(tarfile = temp, files = smokeFileName)
  # Error in getOctD(x, offset, len) : invalid octal digit


  untar(tarfile = zipFileName, files = smokeFileName)
  # Error in gzfile(path.expand(tarfile), "rb") : cannot open the connection
  # In addition: Warning message:
  #   In gzfile(path.expand(tarfile), "rb") :
  #   cannot open compressed file 'http://satepsanone.nesdis.noaa.gov/pub/volcano/FIRE/HMS_ARCHIVE/2010/KML/smoke20100101.kml.gz', probable reason 'Invalid argument'

  unz(temp, smokeFileName)
  # A connection with                                                                                                     
  # description "C:\\Users\\jvargo\\AppData\\Local\\Temp\\RtmpemFaXC\\file33f82dd83714:smoke20100101.kml"
  # class       "unz"                                                                                    
  # mode        "r"                                                                                      
  # text        "text"                                                                                   
  # opened      "closed"                                                                                 
  # can read    "yes"                                                                                    
  # can write   "yes"

1 个答案:

答案 0 :(得分:0)

改编自https://community.rstudio.com/t/download-gz-file-and-extract-kml/13783

library(R.utils)

 gzFileURL <- "http://satepsanone.nesdis.noaa.gov/pub/volcano/FIRE/HMS_ARCHIVE/2010/KML/smoke20100101.kml.gz") 

  smokeZipName <-"smoke20100101.kml.gz"
  smokeFileName <- "smoke20100101.kml"

  directory <- tempdir()

  setwd(directory)

  temp <- tempfile(pattern = "", fileext = ".kml.gz")

  download.file(url = gzFileURL, destfile = temp)

  gunzip(temp)

  kmlFile <- list.files(tempdir(), pattern = ".kml")

  layers <- st_layers(kmlFile)$name