Skip to content

Commit

Permalink
Merge pull request #1269 from etiennesky/rldhont-netcdf_generateBandName
Browse files Browse the repository at this point in the history
[Feature][RASTER] Generate band name with NetCDF EXTRA_DIM funded by Ifremer
  • Loading branch information
etiennesky committed Mar 27, 2014
2 parents 3dfacc7 + 7710882 commit 344383b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/raster/qgsrasterinterface.h
Expand Up @@ -97,7 +97,7 @@ class CORE_EXPORT QgsRasterInterface
virtual int ySize() const { if ( mInput ) return mInput->ySize(); else return 0; }

/** \brief helper function to create zero padded band names */
virtual QString generateBandName( int theBandNumber ) const
virtual QString generateBandName( int theBandNumber ) const
{
return tr( "Band" ) + QString( " %1" ) .arg( theBandNumber, 1 + ( int ) log10(( float ) bandCount() ), 10, QChar( '0' ) );
}
Expand Down
73 changes: 73 additions & 0 deletions src/providers/gdal/qgsgdalprovider.cpp
Expand Up @@ -47,6 +47,7 @@
#include <QTime>
#include <QSettings>
#include <QTextDocument>
#include <QDebug>

#include "gdalwarper.h"
#include "ogr_spatialref.h"
Expand Down Expand Up @@ -875,6 +876,78 @@ int QgsGdalProvider::yBlockSize() const
int QgsGdalProvider::xSize() const { return mWidth; }
int QgsGdalProvider::ySize() const { return mHeight; }

QString QgsGdalProvider::generateBandName( int theBandNumber ) const
{
#ifdef GDAL_COMPUTE_VERSION /* only available in GDAL 1.10 or later */
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(1,10,0)
if ( strcmp( GDALGetDriverShortName( GDALGetDatasetDriver( mGdalDataset ) ), "netCDF" ) == 0 )
{
char ** GDALmetadata = GDALGetMetadata( mGdalDataset, NULL );

if ( GDALmetadata )
{
QStringList metadata = cStringList2Q_( GDALmetadata );
QStringList dimExtraValues;
QMap< QString, QString > unitsMap;
for ( QStringList::const_iterator i = metadata.begin();
i != metadata.end(); ++i )
{
QString val( *i );
if ( !val.startsWith( "NETCDF_DIM_EXTRA" ) && !val.contains( "#units=" ) )
continue;
QStringList values = val.split( "=" );
val = values.at( 1 );
if ( values.at( 0 ) == "NETCDF_DIM_EXTRA" )
{
dimExtraValues = val.replace( QString( "{" ), QString( "" ) ).replace( QString( "}" ), QString( "" ) ).split( "," );
//http://qt-project.org/doc/qt-4.8/qregexp.html#capturedTexts
}
else
{
unitsMap[ values.at( 0 ).split( "#" ).at( 0 )] = val;
}
}
if ( dimExtraValues.count() > 0 )
{
QStringList bandNameValues;
GDALRasterBandH gdalBand = GDALGetRasterBand( mGdalDataset, theBandNumber );
GDALmetadata = GDALGetMetadata( gdalBand, NULL );

if ( GDALmetadata )
{
metadata = cStringList2Q_( GDALmetadata );
for ( QStringList::const_iterator i = metadata.begin();
i != metadata.end(); ++i )
{
QString val( *i );
if ( !val.startsWith( "NETCDF_DIM_" ) )
continue;
QStringList values = val.split( "=" );
for ( QStringList::const_iterator j = dimExtraValues.begin();
j != dimExtraValues.end(); ++j )
{
QString dim = ( *j );
if ( values.at( 0 ) != "NETCDF_DIM_" + dim )
continue;
if ( unitsMap.contains( dim ) && unitsMap[ dim ] != "" && unitsMap[ dim ] != "none" )
bandNameValues.append( dim + "=" + values.at( 1 ) + " (" + unitsMap[ dim ] + ")" );
else
bandNameValues.append( dim + "=" + values.at( 1 ) );
}
}
}

if ( bandNameValues.count() > 0 )
return tr( "Band" ) + QString( " %1 / %2" ) .arg( theBandNumber, 1 + ( int ) log10(( float ) bandCount() ), 10, QChar( '0' ) ).arg( bandNameValues.join( " / " ) );
}
}
}
#endif
#endif

return QgsRasterDataProvider::generateBandName( theBandNumber );
}

QgsRasterIdentifyResult QgsGdalProvider::identify( const QgsPoint & thePoint, QgsRaster::IdentifyFormat theFormat, const QgsRectangle &theExtent, int theWidth, int theHeight )
{
QgsDebugMsg( QString( "thePoint = %1 %2" ).arg( thePoint.x(), 0, 'g', 10 ).arg( thePoint.y(), 0, 'g', 10 ) );
Expand Down
2 changes: 2 additions & 0 deletions src/providers/gdal/qgsgdalprovider.h
Expand Up @@ -169,6 +169,8 @@ class QgsGdalProvider : public QgsRasterDataProvider, QgsGdalProviderBase
int xSize() const;
int ySize() const;

QString generateBandName( int theBandNumber ) const;

/**Reimplemented from QgsRasterDataProvider to bypass second resampling (more efficient for local file based sources)*/
QgsRasterBlock *block( int theBandNo, const QgsRectangle &theExtent, int theWidth, int theHeight );

Expand Down

0 comments on commit 344383b

Please sign in to comment.