[gdal-dev] Zip files and VISFileFromMemBuffer()
Sean Gillies
sean at mapbox.com
Fri Feb 3 11:10:14 PST 2017
Yes (as I said off-list), that works too. I've written a post about
Rasterio's MemoryFile and ZipMemoryFile here:
https://github.com/mapbox/rasterio/issues/977. I like GDAL's virtual files
a lot and I think that these classes could make them very accessible to
Python programmers.
On Wed, Feb 1, 2017 at 12:56 AM, Kurt Schwehr <schwehr at gmail.com> wrote:
> Sean,
>
> Very cool. One follow up question, do you really need the "with" for
> open? Does this not work (and MemoryFile can drop the file descriptor as
> soon as it wants to)?
>
> with MemoryFile(open(path_zip_file, 'rb')) as memfile:
> with memfile.open('white-gemini-iv.vrt') as dataset:
> assert dataset.count == 3
>
> On Tue, Jan 31, 2017 at 3:35 PM, Sean Gillies <sean at mapbox.com> wrote:
>
>> Thanks for confirming that I was on the right trail, Even! I got it
>> figured out: I was naively trying to open the /vsizip/vsimem/ file in
>> update mode.
>>
>> In case you're interested, my new Python usage is like this:
>>
>> with open(path_zip_file, 'rb') as f:
>> with MemoryFile(f) as memfile:
>> with memfile.open('white-gemini-iv.vrt') as dataset:
>> assert dataset.count == 3
>>
>> https://github.com/mapbox/rasterio/pull/972/files#diff-d3388
>> b510bd97235e4741b8417cc41e9R148
>>
>>
>> On Tue, Jan 31, 2017 at 5:42 PM, Even Rouault <even.rouault at spatialys.com
>> > wrote:
>>
>>> On mardi 31 janvier 2017 17:33:23 CET Sean Gillies wrote:
>>>
>>> > Hey all,
>>>
>>> >
>>>
>>> > The tests in
>>>
>>> > https://svn.osgeo.org/gdal/branches/1.9/autotest/gcore/vsizip.py show
>>> how
>>>
>>> > to create a zip archive in memory and create directories and files
>>> within
>>>
>>> > it. I'm looking for a shortcut.
>>>
>>> >
>>>
>>> > Given a sequence of bytes representing an existing zip file that
>>> contains a
>>>
>>> > TIFF and a VRT (referencing the TIFF), is it possible to make a VSI
>>> file
>>>
>>> > from this buffer (using VISFileFromMemBuffer()) and then access the VRT
>>>
>>> > using a path like /vsizip/vsimem/archive.zip/example.vrt protocol?
>>>
>>>
>>>
>>> Hi Sean,
>>>
>>>
>>>
>>> That sounds like you tested it and something went wrong ? Perhaps check
>>> the path to the source in the VRT and the value of the relativeToVRT
>>> attribute.
>>>
>>>
>>>
>>> Your above strategy does work for me:
>>>
>>>
>>>
>>> $ gdal_translate byte.tif byte.vrt -of VRT
>>>
>>> Input file size is 20, 20
>>>
>>>
>>>
>>> $ zip byte.tif.zip byte.tif byte.vrt
>>>
>>> adding: byte.tif (deflated 96%)
>>>
>>> adding: byte.vrt (deflated 53%)
>>>
>>>
>>>
>>> $ python
>>>
>>> >>> from osgeo import gdal
>>>
>>> >>> data = open('byte.tif.zip', 'rb').read()
>>>
>>> >>> gdal.FileFromMemBuffer('/vsimem/archive.zip', data)
>>>
>>> >>> ds = gdal.Open('/vsizip/vsimem/archive.zip/byte.vrt')
>>>
>>> >>> ds.GetRasterBand(1).Checksum()
>>>
>>> 4672
>>>
>>>
>>>
>>>
>>>
>>> Even
>>>
>>>
>>>
>>>
>>>
>>> --
>>>
>>> Spatialys - Geospatial professional services
>>>
>>> http://www.spatialys.com
>>>
>>
>>
>>
>> --
>> Sean Gillies
>>
>> _______________________________________________
>> gdal-dev mailing list
>> gdal-dev at lists.osgeo.org
>> https://lists.osgeo.org/mailman/listinfo/gdal-dev
>>
>
>
>
> --
> --
> http://schwehr.org
>
--
Sean Gillies
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20170203/8718dc2d/attachment.html>
More information about the gdal-dev
mailing list