这几天用GDAL处理DRG数据时发现,有数据被处理成了黑板,什么都没有,发现是因为DRG图像颜色比较简单,使用了调色板的缘故。
使用SetColorTable将调色板写入后,一部分基本正常,但还有些图像还是有黑板的现象。仔细察看出错的DRG与正常的DRG的区别,发现
C:\Program Files\FWTools1.0.5>
C:\Program Files\FWTools1.0.5>gdalinfo D:\RasterTest\DRG\I48E010019DRG.TIF
Driver: GTiff/GeoTIFF
Size is 6946, 5715
Coordinate System is `'
Origin = (636178.308000,3822519.830000)
Pixel Size = (4.00000000,-4.00000000)
Metadata:
TIFFTAG_XRESOLUTION=317.5
TIFFTAG_YRESOLUTION=317.5
TIFFTAG_RESOLUTIONUNIT=2 (pixels/inch)
Corner Coordinates:
Upper Left ( 636178.308, 3822519.830)
Lower Left ( 636178.308, 3799659.830)
Upper Right ( 663962.308, 3822519.830)
Lower Right ( 663962.308, 3799659.830)
Center ( 650070.308, 3811089.830)
Band 1 Block=6946x1 Type=Byte, ColorInterp=Palette
Color Table (RGB with 256 entries)
0: 128,0,128,255
1: 78,125,208,255
2: 194,226,255,255
3: 150,240,100,255
4: 200,240,170,255
5: 150,105,66,255
6: 255,255,255,255
7: 150,150,150,255
8: 0,0,0,255
9: 0,0,0,255
C:\Program Files\FWTools1.0.5>gdalinfo D:\RasterTest\DRG\I49E012003DRG\2.TIF
Driver: GTiff/GeoTIFF
Size is 1705, 1425
Coordinate System is `'
Corner Coordinates:
Upper Left ( 0.0, 0.0)
Lower Left ( 0.0, 1425.0)
Upper Right ( 1705.0, 0.0)
Lower Right ( 1705.0, 1425.0)
Center ( 852.5, 712.5)
Band 1 Block=1705x4 Type=Byte, ColorInterp=Gray
还是调色板没处理好,查遍资料发现没有什么特别的地方,最后仔细跟踪错误代码,在GDAL代码中找到
/* -------------------------------------------------------------------- */
/* Check if this is even a candidate for applying a PCT. */
/* -------------------------------------------------------------------- */
if( poGDS->bCrystalized )
{
CPLError( CE_Failure, CPLE_NotSupported,
"SetColorTable() not supported for existing TIFF files." );
return CE_Failure;
}
百思不得其解,折腾了半天最后在GDAL-DEV看到这段:
I think the key here is that the GeoTIFF driver in GDAL库 only allows you to assign a pseudocolor table to a file before you start any raster data.
察看代码,发现把SetColorTable放在了RasterIO,Write之后了,修改后就好了。