[GIS] How to save python ogr2ogr shapefile to memory, and stream to web client as zip

ogr2ogrpython

I'm trying to accomplish something similar to the site http://ogre.adc4gis.com/

I want to be able to manipulate a geojson file, and send it to the client as a shapefile, in a single zip file.

I'm writing the program in python, and I see that the site mentioned above is using Javascript.

I've already been able to save the json as a shapefile with the following command:

ogr2ogr.main(["", "-f", "ESRI Shapefile", "fishnet", "fishnet.json"])

This saves our geojson file as 4 separate files in the folder fishnet. I'm wondering how I can avoid saving files to the harddisk and just serve the zip file to the client in memory.

Update

So far I've zipped it. Still yet to stream to the client, but I think it should be easy enough to find some resources on StringIO.

Basically, what I've done so far is to go ahead and save the files to disk, get the data from them, convert them, and then delete them (after they've been streamed):

import ogr2ogr, shutil, uuid
name_of_files = 'custom_name_here'
# random directory name
# http://stackoverflow.com/questions/10501247/best-way-to-generate-random-file-names-in-python
directory_for_files = str(uuid.uuid4())
# -nln gives a custom name to files in folder
# string without .shp gets saved to directory as four files
ogr2ogr.main(['','-f','ESRI Shapefile','-nln', name_of_files, directory_for_files, 'test.json'])
# create zip from directory with same name
shutil.make_archive(directory_for_files, "zip", directory_for_files)
# remove directory
shutil.rmtree(directory_for_files)

Best Answer

OGR's vsizip/vismem features are poorly documented, but look at http://svn.osgeo.org/gdal/trunk/autotest/gcore/vsizip.py for starters. I think applications scale better without trying to do read-process-write in a single thread, but maybe vsizip/vsimem is just right for yours.