<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Tim,</p>
    <p>good catch. Fix queued in
      <a class="moz-txt-link-freetext" href="https://github.com/OSGeo/gdal/pull/11966">https://github.com/OSGeo/gdal/pull/11966</a>. The FlushCache()
      workaround is a safe one.</p>
    <p>Even<br>
    </p>
    <div class="moz-cite-prefix">Le 14/03/2025 à 19:37, Tim Harris via
      gdal-dev a écrit :<br>
    </div>
    <blockquote type="cite"
cite="mid:CAK3fJgXe6YuizgsrkHMn9nbUcTtH1pQR8sG=dmPcPDc-TXuSbQ@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <div dir="ltr">
        <div dir="ltr">
          <div>I ran into sort of an odd problem with some rasterization
            code that changed between 3.10.0 and 3.10.1. The problem
            also exists with 3.10.2. The core of the problem is flushing
            data to disk but it seems that threading and compression
            also have something to do with it.</div>
          <div><br>
          </div>
          <div>Here's a way to reproduce it... simple made up geometry
            first, call it box.geojson:</div>
          <div><span style="font-family:monospace"><br>
            </span></div>
          <div>---</div>
          <div><span style="font-family:monospace">{<br>
                "type": "FeatureCollection",<br>
                "features": [<br>
                  {<br>
                    "type": "Feature",<br>
                    "geometry": {<br>
                      "type": "Polygon",<br>
                      "coordinates": [<br>
                        [<br>
                          [ 0.25, 0.25 ],<br>
                          [ 0.75, 0.25 ],<br>
                          [ 0.75, 0.75 ],<br>
                          [ 0.25, 0.75 ],<br>
                          [ 0.25, 0.25 ]<br>
                        ]<br>
                      ]<br>
                    }<br>
                  }<br>
                ]<br>
              }</span><br>
          </div>
          <div>---</div>
          <div><br>
          </div>
          <div>Here's the script:</div>
          <div>---</div>
          <div><span style="font-family:monospace">from osgeo import
              gdal, osr<br>
              <br>
              gdal.UseExceptions()<br>
              gdal.SetConfigOption("GDAL_NUM_THREADS", "8")<br>
              <br>
              ds_aoi = gdal.OpenEx("box.geojson")<br>
              <br>
              drv = gdal.GetDriverByName("GTiff")<br>
              ds_out = drv.Create("test.tif",<br>
                                  xsize=100,<br>
                                  ysize=100,<br>
                                  bands=1,<br>
                                  eType=gdal.GDT_Byte,<br>
                                  options=[<br>
                                      "COMPRESS=DEFLATE"<br>
                                  ])<br>
              ds_out.SetGeoTransform((0.0, 0.01, 0.0, 1.0, 0.0, -0.01))<br>
              srs = osr.SpatialReference()<br>
              srs.ImportFromEPSG(4326)<br>
              ds_out.SetSpatialRef(srs)<br>
              srs = None<br>
              <br>
              # Uncomment this and it works...<br>
              # ds_out.FlushCache()<br>
              <br>
              result = gdal.Rasterize(ds_out,<br>
                                      ds_aoi,<br>
                                      burnValues=[1],<br>
                                      bands=[1],<br>
                                      allTouched=True,<br>
                                      callback=gdal.TermProgress)<br>
              print(f"result = {result}")<br>
              <br>
              ds_aoi = None<br>
              ds_out = None</span><br>
            ---</div>
          <div><br>
          </div>
          <div>Run that script, and it will generate a blank TIF and
            print "result = 0" if using GDAL 3.10.1 or 3.10.2. But if
            using 3.10.0, it prints "result = 1" and the output looks as
            expected, a TIF with a box of 1s in the middle of it and 0s
            around the outside.</div>
          <div><br>
          </div>
          <div>Side note... it's also weird that the gdal.Rasterize
            doesn't seem to throw any exception, but I'm not even sure
            what the problem is. I tried CPL_DEBUG=ON and don't see
            anything obvious:</div>
          <div>---</div>
          <div><span style="font-family:monospace">GeoJSON: First pass:
              100.00 %<br>
              GDAL: GDALOpen(box.geojson, this=0x356de0d0) succeeds as
              GeoJSON.<br>
              GDAL:
              GDALDriver::Create(GTiff,test.tif,100,100,1,Byte,0x357529a0)<br>
              GTiff: Using up to 8 threads for compression/decompression<br>
              GDAL: GDAL_CACHEMAX = 799 MB<br>
              GDAL: Rasterizer operating on 1 swaths of 100 scanlines.<br>
              0result = 0<br>
              GDAL: GDALClose(box.geojson, this=0x356de0d0)<br>
              GTIFF: Waiting for worker job to finish handling block 0<br>
              GDAL: GDALClose(test.tif, this=0x35ab1d80)</span></div>
          <div>---</div>
          <div><br>
          </div>
          <div>It seems that this combination is required to produce the
            error:</div>
          <div>- Set GDAL_NUM_THREADS</div>
          <div>- Specify COMPRESS in the GTiff creation options</div>
          <div>- Do NOT flush the cache or close the file between
            creation and rasterization</div>
          <div><br>
          </div>
          <div>If any of those conditions are changed, it appears to
            work ok.</div>
          <div><br>
          </div>
          <div>Is this expected? Is that FlushCache call supposed to be
            necessary? It's odd that it wasn't required before, but it
            is now.</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>