[gdal-dev] advice on python style - get file/dataset as bytes

Even Rouault even.rouault at spatialys.com
Mon Apr 15 00:32:49 PDT 2024


Hi Michael,

I can't think of a better way with the current API.

There has been some work in progress in 
https://github.com/OSGeo/gdal/pull/8222 to make VSIFile accessible as a 
regular Python file, but this isn't merged

Even

Le 15/04/2024 à 01:48, Michael Sumner via gdal-dev a écrit :
> Hi, I'm getting some pushback on my code style. I just want the raw 
> bytes in-memory of a file in Python, for a manageable tiny dataset.
>
> Is there anything especially wrong with the following?  It's 
> essentially the same as the answer here:
>
> https://lists.osgeo.org/pipermail/gdal-dev/2016-August/045030.html
>    (I know it needs error checks, I just mean the bare style of how to 
> get the task done).
>
> temp_name = "/vsimem/some.tif"
> from osgeo import gdal
> gdal.UseExceptions()
>
> ## we do something with vrt
> dsn = 
> "vrt:///vsicurl/https://github.com/cran/rgl/raw/master/inst/textures/worldsmall.png?a_ullr=-180,90,180,-90&expand=rgb 
> <https://github.com/cran/rgl/raw/master/inst/textures/worldsmall.png?a_ullr=-180,90,180,-90&expand=rgb>"
> ds = gdal.Open(dsn)
> ## write to tif format, but using MEM
> gdal.Translate(temp_name, ds)
>
> ## now obtain those bytes from the GDAL virtual file
> f = gdal.VSIFOpenL(temp_name, 'rb')
> gdal.VSIFSeekL(f, 0, 2) # end
> size = gdal.VSIFTellL(f)
> gdal.VSIFSeekL(f, 0, 0) # begin
>
> ## the desired result is this
> data = gdal.VSIFReadL(1, size, f)
>
> ## and clean up
> gdal.VSIFCloseL(f)
> gdal.Unlink(temp_name)
>
> Cheers, Mike
>
>
>
> -- 
> Michael Sumner
> Software and Database Engineer
> Australian Antarctic Division
> Hobart, Australia
> e-mail: mdsumner at gmail.com
>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/gdal-dev

-- 
http://www.spatialys.com
My software is free, but my time generally not.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20240415/b2ab3e24/attachment.htm>


More information about the gdal-dev mailing list