[gdal-dev] Add to PythonGotchas: saving and closing raster datasets
Even Rouault
even.rouault at mines-paris.org
Sun May 4 01:40:50 PDT 2014
Le dimanche 04 mai 2014 06:03:28, Mike Toews a écrit :
> Hi,
>
> This seems to be a common gotcha: http://gis.stackexchange.com/q/93212/1872
>
> I've been caught by it before, and I'm certain many others have too.
> This is a gotcha since "WriteArray" doesn't write the array to disk,
> but rather it is written when the dataset object is dereferenced.
That depends on the size of the GDAL block cache and your dataset. The real
answer is that if your dataset uncompressed size is below the GDAL block cache
size (40 MB by default), nothing will hit the disk just after WriteArray(). If
your dataset is larger, then blocks will be partially written to disk. But in
any case, you are right, the only way to be sure that a dataset is reliably
written to disk (such as being in a state to be readable by another process)
is to close it properly. FlushCache() might not even be enough in some cases
(it depends on drivers)
> I've
> drafted up an entry for the wiki, which could be placed before
> "Certain objects contain a Destroy() method, but you should never use
> it".
>
> Furthermore, I'm unsure of the best way to demonstrate how to
> dereference Python objects. The wiki has two different styles:
> obj = None
> or
> del obj
>
> I'm unsure of which is regarded best practice, but encourage
> consistency. Please edit as necessary or reply with suggestions.
Python purists will say that both are not really ideal from a clearness point
of view ( http://sgillies.net/blog/2013/12/17/teaching-python-gis-users-to-be-
more-rational.html ), but there's no really other alternative in the API,
so...
>
> -Mike
>
>
> === Saving and closing datasets ===
>
> To save and close raster datasets, the object needs to be
> dereferenced, such as setting it to `None`. It is not written with
> {{{WriteArray}}}. For example:
> {{{
> from osgeo import gdal
> driver = gdal.GetDriverByName('GTiff')
> dst_ds = driver.Create('new.tif', 10, 15)
> band = dst_ds.GetRasterBand(1)
> arr = band.ReadAsArray() # raster values are all zero
> arr[2,4:] = 50 # modify some data
> band.WriteArray(arr) # raster file still unmodified
> band = None # dereference band to avoid gotcha described previously
> dst_ds = None # save, close
> }}}
> The last dereference to the raster dataset writes the data
> modifications and closes the raster file. The objects may also be
> dereferenced using: {{{del band, dst_ds}}}
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev
--
Geospatial professional services
http://even.rouault.free.fr/services.html
More information about the gdal-dev
mailing list