[gdal-dev] JP2k, ReadError, Python

Even Rouault even.rouault at mines-paris.org
Sat Jul 30 12:12:02 EDT 2011


Le samedi 30 juillet 2011 17:37:48, Jay L. a écrit :
> Morning all,
> 
> I have a rather large JP2 file (created using the Kakadu driver I believe)
> that I am trying to process with a python script.  I am able to open the
> dataset and grab the first band.
> 
> To calculate band statistics I use the following code which returns 1000
> lines of error before exiting
> 
> def get_band_stats(band):
>     NDV = band.GetNoDataValue()
>     bandMin = band.GetMinimum()
>     bandMax = band.GetMaximum()
>     if bandMin is None or bandMax is None:
>         #Approx has to be set to false to ensure that an accurate Min/Max
> are calculated...
>         (bandMin, bandMax) = band.ComputeRasterMinMax(False)
>     return NDV, bandMin, bandMax
> 
> ....
> ERROR 1: IReadBlock failed at X offset 20, Y offset 25
> ERROR 1: IReadBlock failed at X offset 21, Y offset 25
> ....
> 
> This cuts off after 1000 lines.
> 
> Commenting out the if statement, thereby using the "fast" method for
> getting the min/max returns the following error:
> 
> error: cannot decode code stream
> ERROR 1: IReadBlock failed at X offset 0, Y offset 0
> ERROR 1: GetBlockRef failed at X block offset 0, Y block offset 0

Not sure to follow you. What is the  "fast" method for getting the min/max ? 
band.ComputeRasterMinMax(False) ?

Did you try running with CPL_DEBUG=ON defined as an environment variable to 
check which driver is used and if there are other interesting warnings ?

> 
> Finally, using gdalinfo I see that the image is being opened with the
> ECWJP2000 driver.  I am also able to calculate statistics without a problem
> and the returned values are within the expected range.

Hum, strange, ComputeRasterMinMax() and ComputeRasterStatistics() are very 
silmilar : they use the same way of fetching the data, so I'd assume that if 
one works, the other one would too.

> I am also able to
> use gdal_translate to convert these to .tif prior to processing, but I
> would rather avoid this method.  Do I need to alter my code to work with
> JP2 as they are compressed?

Theroretically no. If it doesn't work, it's a defect of the driver. Could you 
share your JP2 file ?

> 
> Can I implicitly define the driver to be used with JP2 within my python
> code without performing the unload all / load what I want method?

You can unload a driver this way : 
gdal.GetDriverByName("short_name_of_driver").Deregister()

> 
> Many thanks,
> Jay


More information about the gdal-dev mailing list