7

I have thousands of shapefiles in .zip archives to work with. How can I use and other GDAL/OGR tools to work with these data without having to extract the archives first?

From gdal cheatsheet and Reading a GDAL dataset in a .gz file or a .zip archive I know that I can use the visizip driver, but for formats that consist of multiple files per data layer like shapefile you need to explicitly name the file inside the archive:

ogrinfo /vsizip/archive.zip/105A_wetland_a.shp

How might I efficiently work with archives, that could contain multiple shapefiles per zip, without having to know the and pass the name of each layer ahead of time? e.g.

ogrinfo /vsizip/archive.zip

Example archive file: bndt_105a_shp_en.zip from Geogratis.

2
  • That should be correct assuming archive.zip is in your current directory, and you have gdal version 1.6.0+ for zip or 1.8.0+ for gzip.
    – Andy C
    May 15, 2017 at 19:37
  • @AndyC ogrinfo /vsizip/archive.zip enumerates the names of the .shp inside the archive but follows with series of ERROR 4: Unable to open file /vsizip/archive.zip/shapefilename.shp or .... See pastebin.com/AJtgdths. OGR version "GDAL 2.1.3, released 2017/20/01" May 15, 2017 at 20:11

1 Answer 1

7

Your command is basically OK but the error message looks worse than it actually is. The important message is

It may be corrupt or read-only file accessed in update mode.

You can open files from zip but you can't update them. If you read the zip as read-only you can get rid of the error messages.

ogrinfo /vsizip/bndt_105a_shp_en.zip -ro
INFO: Open of `/vsizip/bndt_105a_shp_en.zip'
      using driver `ESRI Shapefile' successful.
1: 105A_buildin_p (Point)
2: 105A_builtup_a (Polygon)
3: 105A_builtup_p (Point)
4: 105A_campgro_p (Point)
5: 105A_contour_l (Line String)
6: 105A_dis_str_p (Point)
7: 105A_elev_pt_p (Point)
8: 105A_esker_l (Line String)
9: 105A_haz_air_p (Point)
10: 105A_haz_nav_p (Point)
11: 105A_li_road_l (Line String)
12: 105A_mininga_a (Polygon)
13: 105A_nav_aid_p (Point)
14: 105A_nts_lim_l (Line String)
15: 105A_picnic_p (Point)
16: 105A_road_l (Line String)
17: 105A_runway_p (Point)
18: 105A_sand_a (Polygon)
19: 105A_seapl_b_p (Point)
20: 105A_ski_cen_p (Point)
21: 105A_toponym_p (Point)
22: 105A_tower_p (Point)
23: 105A_trail_l (Line String)
24: 105A_vegetat_a (Polygon)
25: 105A_wat_dis_a (Polygon)
26: 105A_wat_dis_l (Line String)
27: 105A_wat_dis_p (Point)
28: 105A_water_b_a (Polygon)
29: 105A_water_c_l (Line String)
30: 105A_wetland_a (Polygon)

It is possible to convert all the layers into any format that supports multiple layers with a one-line command. As an example a conversion into GeoPackage:

ogr2ogr -f gpkg test.gpkg /vsizip/bndt_105a_shp_en.zip

Adding more data to existing layers and adding new layers into existing GeoPackage should go this way:

ogr2ogr -f gpkg -update -append test.gpkg /vsizip/another.zip
4
  • Oh! Thank you. I didn't investigate the read-only part of the message much because to my mind an info command would already be read-only. Do you have any suggestions for the other part of the question, using ogr2ogr on zips without having to name the layers? May 15, 2017 at 22:25
  • What is your aim with ogr2ogr? Probably not just to write everything out to shapefiles because then you could simply unzip.
    – user30184
    May 16, 2017 at 6:05
  • ultimately the aim is to merge thousands of shapefiles into a few in some other format (file-gdb, postgres, ...) without having to unzip first May 17, 2017 at 19:50
  • I edited the answer and added two ogr2ogr commands which should work but I did not test them.
    – user30184
    May 17, 2017 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.