[R-sig-Geo] readGDAL and unz
Michael Sumner
mdsumner at gmail.com
Wed Jul 22 00:03:20 CEST 2015
On Wed, 22 Jul 2015 at 05:18 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.
>
>
You might be able to get it working with the "readinzip" wrapper of GDAL.
https://trac.osgeo.org/gdal/wiki/UserDocs/ReadInZip
I get this "info" summary on Ubuntu -
R version 3.2.1 (2015-06-18) -- "World-Famous Astronaut"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
...
> library(rgdal)
Loading required package: sp
rgdal: version: 1.0-4, (SVN revision (unknown))
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 2.1.0dev, released 2015/99/99
Path to GDAL shared files: /usr/local/share/gdal
Loaded PROJ.4 runtime: Rel. 4.9.1, 04 March 2015, [PJ_VERSION: 491]
Path to PROJ.4 shared files: (autodetected)
Linking to sp version: 1.1-1
> x <- readGDAL("/vsizip/x.zip/x.tif")
/vsizip/x.zip/x.tif has GDAL driver GTiff
and has 4 rows and 3 columns
(I get the same with rgdal 0.9-3 / GDAL 1.11.2,)
It might be that readGDAL itself is modifying the path string here
(I'm not familliar with the syntax in detail, and I am further confused as
to how to use it in Windows. )
GDAL's "connections" have their own zoo of syntax specializations, so
there's quite a lot of variety to explore here - this probably can't work
with a zipped NetCDF or HDF (but then more recent versions of those formats
have either internal compression, or .gz compression that works over a URL
handled by the driver).
Cheers, Mike.
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
>
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list