2

After reading a bit of GDAL Virtual File Systems docs, I am trying to write a zipped Shapefile using this gdal command involving the use of /vsizip/:

ogr2ogr -f "ESRI Shapefile" /vsizip/test.zip\test.shp WFS:"https://www.wondermap.it/cgi-bin/qgis_mapserv.fcgi?&map=/home/ubuntu/qgis/projects/Demo_sci_WMS/demo_sci.qgs&SERVICE=WFS&VERSION=1.1.0&REQUEST=GetFeature&typename=domini_sciabili&bbox=544138,5098446,564138,5108446"

, and I get this error:

ERROR 1: Read-write random access not supported for /vsizip
ERROR 1: Cannot create test.shx while another file is being written in the .zip
ERROR 1: Failed to create file /vsizip/test.zip/test.shx: No such file or directory
ERROR 1: Terminating translation prematurely after failed
translation of layer domini_sciabili (use -skipfailures to skip errors)

The code is only writing test.shp (0KB though) inside test.zip.

I get what is happening (the error is self-explanatory), but sincerely, I don't know if I can go farther than this without probably using the GDAL API. Am I wrong?

3 Answers 3

4

In GDAL 3.1+ you can create zipped shapefiles, although not using vsizip (as far as I've tested). See docs page

Starting with GDAL 3.1, the driver can also support reading, creating and editing .shz files (ZIP files containing the .shp, .shx, .dbf and other side-car files of a single layer) and .shp.zip files (ZIP files contains one or several layers). Creation and editing involves the creation of temporary files.

You simply need to change the output filename extension:

ogr2ogr -f "ESRI Shapefile" C:\Temp\test.shp.zip WFS:"https://www.wondermap.it/cgi-bin/qgis_mapserv.fcgi?&map=/home/ubuntu/qgis/projects/Demo_sci_WMS/demo_sci.qgs&SERVICE=WFS&VERSION=1.1.0&REQUEST=GetFeature&typename=domini_sciabili&bbox=544138,5098446,564138,5108446"
1

Answering myself thanks to this answer on the GDAL mailing list.

I can't, because Shapefile is a multi file format, and OGR updates the files in a non sequential way. I have to create a temporary shapefile and zip it afterwards.

1

You can use either the .shp.zip or the .shz extension to create a compressed shapefile. Example using the .shz extension:

ogr2ogr -f "ESRI Shapefile" -skipfailures /tmp/output.shz "https://irig-madeira.pt/postgresql/drote/public/baram_2020/cgi-bin/qgis_mapserv.fcgi?service=WFS&version=2.0.0&request=GetFeature&typename=ponto_cotado&EXCEPTIONS=application/json&srsname=EPSG:5016"

The contents can be checked with:

unzip -v /tmp/output.shz
Archive:  /tmp/output.shz
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
     156  Defl:N       68  56% 2023-05-30 23:25 93672253  output.shp
     708  Defl:N      184  74% 2023-05-30 23:25 06613a49  output.dbf
     116  Defl:N       61  47% 2023-05-30 23:25 e4d276c6  output.shx
--------          -------  ---                            -------
     980              313  68%                            3 files

Source: GDAL documentation

Your Answer

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

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