Ticket #3151 (closed defect: fixed)
When PIXELTYPE=SIGNEDBYTE, band.GetStatistics returns wrong values
| Reported by: | jjr8 | Owned by: | rouault |
|---|---|---|---|
| Priority: | normal | Milestone: | 1.6.3 |
| Component: | GDAL_Raster | Version: | 1.6.2 |
| Severity: | normal | Keywords: | |
| Cc: |
Description
Evan R. requested that I open a ticket on this issue that I raised on gdal-dev.
With GDAL 1.6.2, I can successfully create a signed 8-bit raster with the HFA or GTiff drivers using the PIXELTYPE=SIGNEDBYTE option:
>>> a = numpy.array([[-128, -1, 0, 1, 127]], dtype='int8') >>> a array([[-128, -1, 0, 1, 127]], dtype=int8) >>> b = numpy.cast['uint8'](a) >>> b array([[128, 255, 0, 1, 127]], dtype=uint8) >>> >>> gdal.UseExceptions() >>> driver = gdal.GetDriverByName('HFA') >>> dataset = driver.Create(r'C:\Temp\test_int8.img', 5, 1, 1, gdal.GDT_Byte, ['PIXELTYPE=SIGNEDBYTE']) >>> band = dataset.GetRasterBand(1) >>> band.WriteArray(b) 0 >>> del band, dataset, driver
When I look at that raster with ArcGIS, it shows it is signed 8-bit integer and the five values show up properly. But when I open it with GDAL and call band.GetStatistics(), it looks like the calculation is performed using unsigned 8-bit integers:
>>> dataset = gdal.Open(r'C:\Temp\test_int8.img', gdalconst.GA_ReadOnly) >>> band = dataset.GetRasterBand(1) >>> band.GetMetadata('IMAGE_STRUCTURE') {'PIXELTYPE': 'SIGNEDBYTE'} >>> band.GetStatistics(False, True) [0.0, 255.0, 102.2, 95.199579831005551]
Evan commented that "this should be fixable within GDAL itself by testing the presence of PIXELTYPE=SIGNEDBYTE and by casting the value to a signed byte in that case."
Thank you very much for looking at this. Please adjust Trac fields appropriately. It would be nice if this was fixed very soon, so I set the milestone to 1.6.3. But if you must fix it later I can implement a workaround of having my application calculate the statistics itself for SIGNEDBYTE rasters.
One final note: band.SetNoDataValue() and band.GetNoDataValue() appear to work correctly:
>>> driver = gdal.GetDriverByName('HFA') >>> dataset = driver.Create(r'C:\Temp\test_int8d.img', 5, 1, 1, gdal.GDT_Byte, ['PIXELTYPE=SIGNEDBYTE']) >>> band = dataset.GetRasterBand(1) >>> band.SetNoDataValue(-20) 0 >>> band.WriteArray(b) 0 >>> del band, dataset, driver >>> dataset = gdal.Open(r'C:\Temp\test_int8d.img', gdalconst.GA_ReadOnly) >>> band = dataset.GetRasterBand(1) >>> band.GetMetadata('IMAGE_STRUCTURE') {'PIXELTYPE': 'SIGNEDBYTE'} >>> band.GetNoDataValue() -20.0
I tried several different values (not just -20) and also verified that ArcGIS reported the same NoData value and that it showed NoData pixels as NoData.
