[gdal-dev] moving averaging filter in GDAL.

Belaid MOA belaid_moa at hotmail.com
Thu Jul 2 15:14:58 EDT 2009


Even, you really made my day today!!! Thanks a lot for spotting that out. I spent almost one day staring at my code in vain!   
BTW, in the GDAL VRT tutorial (http://www.gdal.org/gdal_vrttut.html), I noticed that there is an extra character (1)
in the XML description of KernalFilterSource as in: 
"  <SourceFilename>utm.tif</SourceFilename>1<SourceBand>1</SourceBand>"

I think that character (i.e. 1) should not be there, otherwise the XML is not well-formed.

Again, thank you Even for your help.

With best regards.
Belaid Moa.

  
With best regards.
~Belaid...
> From: even.rouault at mines-paris.org
> To: gdal-dev at lists.osgeo.org
> Subject: Re: [gdal-dev] moving averaging filter in GDAL.
> Date: Thu, 2 Jul 2009 20:20:09 +0200
> CC: belaid_moa at hotmail.com
> 
> Belaid Moa,
> 
> Good point! Yes it should work as your code is directly derived from the VRT 
> tutorial. I discovered that in fact support for SetMetadataItem( "source_0", 
> szFilterSourceXML, "vrt_sources" ) was missing in the VRT driver. I've fixed 
> that as ticket #3052.
> 
> In the meantime you've an easy workaround by using SetMetadata() instead of 
> SetMetadataItem(). Here's the modified code snippet :
> 
> /**
> * Apply an averaging filter to a GDALDataset
> * sFileName: source file name
> * dstFileName: destination file name
> * size: size of the filter (kernal)
> * coefficients: the coefficients of the filter.
> **/
> int average(char* sFileName, char* dstFileName, int size, const char* 
> coefficients) {
>   GDALDriver *poDriver = (GDALDriver *) GDALGetDriverByName( "VRT" );
>   if (poDriver ==  NULL) {
>       cout <<"VRT Driver not found!"<<endl;
>       return -1;
>   }
>   GDALDataset *poVRTDS, *poSrcDS;
>   poSrcDS = (GDALDataset *) GDALOpen( sFileName, GA_ReadOnly);
>   poVRTDS = poDriver->CreateCopy( "", poSrcDS, FALSE, NULL, NULL, NULL );
>   int   nBand;
>   for( nBand = 1; nBand <= poVRTDS->GetRasterCount(); nBand++ ) {
>       GDALRasterBand *poBand = poVRTDS->GetRasterBand( nBand );
>       char szFilterSourceXML[10000];
> /* note the addition of source_0= */
>       sprintf( szFilterSourceXML,
>         "source_0=<KernelFilteredSource>" 
>         "  <SourceFilename>%s</SourceFilename><SourceBand>%d</SourceBand>"
>         "  <Kernel>"
>         "    <Size>%d</Size>"
>         "    <Coefs>%s</Coefs>"
>         "  </Kernel>"
>         "</KernelFilteredSource>",
>         sFileName,nBand,size,coefficients);
>       cout << szFilterSourceXML <<endl;
>       char* papszMetadata[2] = { (char*)szFilterSourceXML, NULL };
>       poBand->SetMetadata( papszMetadata, "vrt_sources" );
>       //poBand->FlushCache();
>   }
>   GDALDriver *poTIFFDriver = (GDALDriver *) GDALGetDriverByName( "GTiff" );
>    if (poTIFFDriver ==  NULL) {
>       cout <<"GTIFF Driver not found!"<<endl;
>       return -1;
>   }
>   GDALDataset *poTiffDS;
>   poTiffDS = poTIFFDriver->CreateCopy( dstFileName, poVRTDS, FALSE, NULL, 
> NULL, NULL );
>   poVRTDS->GetRasterBand( 1 )->FlushCache();
> 
>   GDALClose(poSrcDS);
>   GDALClose(poTiffDS);
>   GDALClose(poVRTDS);
>   return 1;
> }
> 
> Best regards,
> 
> Even

_________________________________________________________________
We are your photos. Share us now with Windows Live Photos.
http://go.microsoft.com/?linkid=9666047
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20090702/50a391f6/attachment.html


More information about the gdal-dev mailing list