Download gz file and extract KML


#1

trying to download a zipped file from the web, then extract the single kml file within. I have tried several different utils functions to unzip and extract but not sure how to get the kml that I can begin to work with (in sf package).

  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"

#2

Here you are friend. I think that you were having issues because this is a .gz file rather than a .tar.gz. I used the gunzip function available with the R.utils package and st_read from sf to read the KML at the end.

library(R.utils)
#> Loading required package: R.oo
#> Loading required package: R.methodsS3
#> R.methodsS3 v1.7.1 (2016-02-15) successfully loaded. See ?R.methodsS3 for help.
#> R.oo v1.22.0 (2018-04-21) successfully loaded. See ?R.oo for help.
#> 
#> Attaching package: 'R.oo'
#> The following objects are masked from 'package:methods':
#> 
#>     getClasses, getMethods
#> The following objects are masked from 'package:base':
#> 
#>     attach, detach, gc, load, save
#> R.utils v2.7.0 successfully loaded. See ?R.utils for help.
#> 
#> Attaching package: 'R.utils'
#> The following object is masked from 'package:utils':
#> 
#>     timestamp
#> The following objects are masked from 'package:base':
#> 
#>     cat, commandArgs, getOption, inherits, isOpen, parse, warnings
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, proj.4 4.9.3

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

smokeKZName <- "smoke20100101.kml"

download.file(url = zipFileName, dest = smokeFileName)

gunzip(smokeFileName)

st_read(smokeKZName)
#> Reading layer `Layer #0' from data source `C:\Users\whaleyry\AppData\Local\Temp\Rtmpcj3gyI\smoke20100101.kml' using driver `KML'
#> Simple feature collection with 24 features and 2 fields
#> geometry type:  GEOMETRY
#> dimension:      XY
#> bbox:           xmin: -156.126 ymin: 19.301 xmax: -78.302 ymax: 34.738
#> epsg (SRID):    4326
#> proj4string:    +proj=longlat +datum=WGS84 +no_defs