<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Hi Michael,</p>
    <p>I can't think of a better way with the current API.</p>
    <p>There has been some work in progress in
      <a class="moz-txt-link-freetext" href="https://github.com/OSGeo/gdal/pull/8222">https://github.com/OSGeo/gdal/pull/8222</a> to make VSIFile accessible
      as a regular Python file, but this isn't merged</p>
    <p>Even<br>
    </p>
    <div class="moz-cite-prefix">Le 15/04/2024 à 01:48, Michael Sumner
      via gdal-dev a écrit :<br>
    </div>
    <blockquote type="cite"
cite="mid:CAAcGz99+LrjidB9xBzdjJedEwr-Cgps6wRQ8kR1ocGsr7Y8aqw@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <div dir="ltr">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. 
        <div><br>
        </div>
        <div>Is there anything especially wrong with the following? 
           It's essentially the same as the answer here: </div>
        <div><br>
        </div>
        <div><a
href="https://lists.osgeo.org/pipermail/gdal-dev/2016-August/045030.html"
            moz-do-not-send="true" class="moz-txt-link-freetext">https://lists.osgeo.org/pipermail/gdal-dev/2016-August/045030.html</a><br>
        </div>
        <div> </div>
        <div>   (I know it needs error checks, I just mean the bare
          style of how to get the task done). <br>
        </div>
        <div><br>
        </div>
        <div>temp_name = "/vsimem/some.tif"<br>
          from osgeo import gdal<br>
          gdal.UseExceptions()<br>
          <br>
          ## we do something with vrt<br>
          dsn = "vrt:///vsicurl/<a
href="https://github.com/cran/rgl/raw/master/inst/textures/worldsmall.png?a_ullr=-180,90,180,-90&expand=rgb"
            moz-do-not-send="true">https://github.com/cran/rgl/raw/master/inst/textures/worldsmall.png?a_ullr=-180,90,180,-90&expand=rgb</a>"<br>
          ds = gdal.Open(dsn)<br>
          ## write to tif format, but using MEM<br>
          gdal.Translate(temp_name, ds)<br>
          <br>
          ## now obtain those bytes from the GDAL virtual file<br>
          f = gdal.VSIFOpenL(temp_name, 'rb')<br>
          gdal.VSIFSeekL(f, 0, 2) # end<br>
          size = gdal.VSIFTellL(f)<br>
          gdal.VSIFSeekL(f, 0, 0) # begin</div>
        <div><br>
        </div>
        <div>## the desired result is this<br>
          data = gdal.VSIFReadL(1, size, f)<br>
          <br>
          ## and clean up<br>
          gdal.VSIFCloseL(f)<br>
          gdal.Unlink(temp_name)<br>
        </div>
        <div>
          <div><br>
          </div>
          <div>Cheers, Mike</div>
          <div><br>
          </div>
          <div><br clear="all">
            <div><br>
            </div>
            <span class="gmail_signature_prefix">-- </span><br>
            <div dir="ltr" class="gmail_signature"
              data-smartmail="gmail_signature">Michael Sumner<br>
              Software and Database Engineer<br>
              Australian Antarctic Division<br>
              Hobart, Australia<br>
              e-mail: <a href="mailto:mdsumner@gmail.com"
                target="_blank" moz-do-not-send="true"
                class="moz-txt-link-freetext">mdsumner@gmail.com</a></div>
          </div>
        </div>
      </div>
      <br>
      <fieldset class="moz-mime-attachment-header"></fieldset>
      <pre class="moz-quote-pre" wrap="">_______________________________________________
gdal-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:gdal-dev@lists.osgeo.org">gdal-dev@lists.osgeo.org</a>
<a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/gdal-dev">https://lists.osgeo.org/mailman/listinfo/gdal-dev</a>
</pre>
    </blockquote>
    <pre class="moz-signature" cols="72">-- 
<a class="moz-txt-link-freetext" href="http://www.spatialys.com">http://www.spatialys.com</a>
My software is free, but my time generally not.</pre>
  </body>
</html>