[R-sig-Geo] readGDAL and unz

boB Rudis bob at rudis.net
Tue Jul 21 23:40:04 CEST 2015


You can just do:

   unzip("x.zip", "x.tif")

and it will only extract that single file out to the current working
directory. readGDAL expects a filename vs a connection.

On Tue, Jul 21, 2015 at 3:17 PM, Sarah Goslee <sarah.goslee at gmail.com> wrote:
> Hi folks,
>
> I'm stuck trying to read spatial data from a zip archive using unz()
> rather than unzipping it. The latter option works, but the zip
> archives I'm working with have hundreds of files in them, and I'd
> rather avoid the mess of unzipping the whole thing.
>
> Using readGDAL(unz()) throws an error:
> rror in .local(.Object, ...) : `3' does not exist in the file system,
> and is not recognised as a supported dataset name.
>
> It appears that the name of the connection isn't being passed
> correctly. Do any of you know a way around this, or shall I just
> resign myself to unzipping everything?
>
> Here's both a working (csv) and nonworking (tif) example:
>
> ###
>
> library(rgdal)
>
> # create files to test with
> # NOTE: WRITES TO CURRENT DIRECTORY
> # creates x.tif and zip archive x.zip using rgdal built-in data
> x <- readGDAL(system.file("external/simple.ag", package="sp")[1])
> writeGDAL(x, "x.tif")
> zip("x.zip", "x.tif")
>
> # creates y.csv and zip archive y.zip
> y <- data.frame(A=1:3, B=letters[6:8])
> write.csv(y, "y.csv")
> zip("y.zip", "y.csv")
>
> ## works as expected
> # list files in y.zip
> y.files <- unzip("y.zip", list=TRUE)
> # read in first file
> y.in <- read.csv(unz("y.zip", y.files$Name[1]))
>
> ## does not work: fails with error
> ## Error in .local(.Object, ...) : `3' does not exist in the file system,
> ## and is not recognised as a supported dataset name.
> ## Note: the number increments.
> # list files in x.zip
> x.files <- unzip("x.zip", list=TRUE)
> # read in first file
> x.in <- readGDAL(unz("x.zip", x.files$Name[1]))
>
>
> ## There's no difference in the connection provided by unz():
> # > unz("y.zip", y.files$Name[1])
> #   description         class          mode          text        opened
> # "y.zip:y.csv"         "unz"           "r"        "text"      "closed"
> #      can read     can write
> #         "yes"         "yes"
> # > unz("x.zip", x.files$Name[1])
> #   description         class          mode          text        opened
> # "x.zip:x.tif"         "unz"           "r"        "text"      "closed"
> #      can read     can write
> #         "yes"         "yes"
>
> ## unzipping the file first works as expected
> unzip("x.zip")
> x.in2 <- readGDAL(x.files$Name[1])
>
> Thanks,
> Sarah
> --
> Sarah Goslee
> http://www.functionaldiversity.org
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list