rgdal / readOGR - unable to read shapefile from .zip

Alex W picture Alex W · Aug 23, 2012 · Viewed 8.7k times · Source

I'm trying to load a zip level shapefile to do some plotting, per: https://github.com/hadley/ggplot2/wiki/plotting-polygon-shapefiles http://www.nceas.ucsb.edu/scicomp/usecases/ReadWriteESRIShapeFiles etc

My code:

library(rgdal)
library(RColorBrewer)
library(ggplot2)
zipmap = readOGR(dsn="file.zip/", layer="myZIPmap")

I'm getting this error:

Error in ogrInfo(dsn = dsn, layer = layer, input_field_name_encoding = input_field_name_encoding) : 
  Cannot open file

I checked the drivers but to be honest am not able to interpret the output:

ogrDrivers()
             name write
1      AeronavFAA FALSE
2          ARCGEN FALSE
3          AVCBin FALSE
4          AVCE00 FALSE
5             BNA  TRUE
6             CSV  TRUE
7             DGN  TRUE
8             DXF  TRUE
9          EDIGEO FALSE
10 ESRI Shapefile  TRUE
11     Geoconcept  TRUE
12        GeoJSON  TRUE
13       Geomedia FALSE
14         GeoRSS  TRUE
15            GML  TRUE
16            GMT  TRUE
17       GPSBabel  TRUE
18  GPSTrackMaker  TRUE
19            GPX  TRUE
20            HTF FALSE
21         Idrisi FALSE
22            KML  TRUE
23   MapInfo File  TRUE
24         Memory  TRUE
25   MSSQLSpatial  TRUE
26           ODBC  TRUE
27        OpenAir FALSE
28         PCIDSK  TRUE
29            PDS FALSE
30         PGDump  TRUE
31           PGeo FALSE
32            REC FALSE
33            S57  TRUE
34           SDTS FALSE
35       SEGUKOOA FALSE
36           SEGY FALSE
37            SUA FALSE
38            SVG FALSE
39          TIGER  TRUE
40        UK .NTF FALSE
41            VFK FALSE
42            VRT FALSE
43         XPlane FALSE

file.info gives:

 file.info(path="K:/2012 - IPD - Policy Maps/fe_2007_us_zcta500.zip/")
                                                             size isdir mode               mtime               ctime
    K:/2012 - IPD - Policy Maps/fe_2007_us_zcta500.zip/ 661131516 FALSE  666 2012-08-22 14:54:53 2012-08-22 14:50:43
                                                                      atime exe
    K:/2012 - IPD - Policy Maps/fe_2007_us_zcta500.zip/ 2012-08-22 14:58:38  no

Given the internet searches I've tried, it looks like I'm not the only one having this issue but I've been unable to find an answer. I'm not sure if the problem is related to the shapefile being inside a .zip folder or not. As it's a work computer, I have to wait for IT to install WinZip so that I can extract the shapefile and try outside a .zip folder. Hopefully that will be tomorrow.

Also, the package help states "Note that stray files in data source directories (such as *.dbf) may lead to suprious errors that accompanying *.shp are missing." The .zip folder has the following: a.dbf, b.prj, c.shp, d.shp.xml, e.shx.

Any help you can provide would be wonderful! -Alex

Answer

user666993 picture user666993 · Aug 23, 2012

You should be able to unzip the files without the need of an external program with unzip. To read a shapefile with readOGR, the dsn is the directory name of where your 'unzipped' files are located, and layer is the name of your shape file without the extension. In the example below, replace myZIPmap with the appropriate shapefile name.

library(utils)
library(rgdal)

unzip("K:/2012 - IPD - Policy Maps/fe_2007_us_zcta500.zip")
zipmap <- readOGR(dsn = "K:/2012 - IPD - Policy Maps/fe_2007_us_zcta500", layer = "myZIPmap" )