[gdal-dev] Warp to GeoTIFF with Transparency?
Nik Sands
nixanz at nixanz.com
Mon Sep 17 20:15:03 PDT 2012
I'm very new to GDAL so please be gentle. My searching has found many references to this problem, but no complete solutions that are explained clearly enough for me to work through.
I'm using the GDAL API to read in a source image (could be from a variety of formats), warp it to Spherical Mercator, and then write out the reprojected image as a GeoTIFF. I would like the parts of the warped TIFF that are not included in the original image to be represented by fully transparent pixels instead of the black pixels that are being generated.
I've set "ALPHA" to "YES" in the warp options but this has not helped. I've also tried adding an extra transparent band, but this made the entire output image transparent.
Can anybody please explain in very simple terms (for a complete GDAL novice) how to go about this?
The relevant portion of the code that I'm currently using is below (based largely on the GDAL warp tutorial).
Thanks in anticipation,
Nik.
void *hTransformArg = GDALCreateGenImgProjTransformer( hSrcDS, pszSrcWKT, NULL, pszDstWKT, FALSE, 0, 1 );
if ( hTransformArg == NULL )
{
NSLog(@"Failed to create transformation.");
return NULL;
}
// Get approximate output georeferenced bounds and resolution for file.
double adfDstGeoTransform[6];
int nPixels=0, nLines=0;
if ( GDALSuggestedWarpOutput( hSrcDS, GDALGenImgProjTransform, hTransformArg, adfDstGeoTransform, &nPixels, &nLines ) != CE_None )
{
NSLog(@"Failed to get suggested warp output.");
return NULL;
}
GDALDestroyGenImgProjTransformer( hTransformArg );
// Create the output file.
char **papszOptions = NULL;
papszOptions = CSLSetNameValue( papszOptions, "ALPHA", "YES" );
hDstDS = GDALCreate( hDriver, [dstPath cStringUsingEncoding:NSASCIIStringEncoding], nPixels, nLines, GDALGetRasterCount(hSrcDS), eDT, papszOptions );
if ( hDstDS == NULL )
{
NSLog(@"Failed to open destination file '%@' with GDALCreate().", [dstPath lastPathComponent]);
return NULL;
}
// Write out the projection definition.
GDALSetProjection( hDstDS, pszDstWKT );
GDALSetGeoTransform( hDstDS, adfDstGeoTransform );
// Copy the color table, if required.
GDALColorTableH hCT;
hCT = GDALGetRasterColorTable( GDALGetRasterBand(hSrcDS,1) );
if( hCT != NULL )
GDALSetRasterColorTable( GDALGetRasterBand(hDstDS,1), hCT );
// Setup warp options.
GDALWarpOptions *psWarpOptions = GDALCreateWarpOptions();
psWarpOptions->papszWarpOptions = CSLSetNameValue(psWarpOptions->papszWarpOptions, "INIT_DEST", "NO_DATA" );
psWarpOptions->hSrcDS = hSrcDS;
psWarpOptions->hDstDS = hDstDS;
psWarpOptions->nBandCount = 1;
psWarpOptions->panSrcBands =
(int *) CPLMalloc(sizeof(int) * psWarpOptions->nBandCount );
psWarpOptions->panSrcBands[0] = 1;
psWarpOptions->panDstBands =
(int *) CPLMalloc(sizeof(int) * psWarpOptions->nBandCount );
psWarpOptions->panDstBands[0] = 1;
psWarpOptions->pfnProgress = GDALTermProgress;
// Establish reprojection transformer.
psWarpOptions->pTransformerArg =
GDALCreateGenImgProjTransformer( hSrcDS,
GDALGetProjectionRef(hSrcDS),
hDstDS,
GDALGetProjectionRef(hDstDS),
FALSE, 0.0, 1 );
psWarpOptions->pfnTransformer = GDALGenImgProjTransform;
// Initialize and execute the warp operation.
GDALWarpOperationH oOperation = GDALCreateWarpOperation( psWarpOptions );
GDALChunkAndWarpImage( oOperation, 0, 0,
GDALGetRasterXSize( hDstDS ),
GDALGetRasterYSize( hDstDS ) );
GDALDestroyGenImgProjTransformer( psWarpOptions->pTransformerArg );
GDALDestroyWarpOptions( psWarpOptions );
GDALDestroyWarpOperation ( oOperation );
More information about the gdal-dev
mailing list