Skip to content

Commit 344383b

Browse files
committed
Merge pull request #1269 from etiennesky/rldhont-netcdf_generateBandName
[Feature][RASTER] Generate band name with NetCDF EXTRA_DIM funded by Ifremer
2 parents 3dfacc7 + 7710882 commit 344383b

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

src/core/raster/qgsrasterinterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class CORE_EXPORT QgsRasterInterface
9797
virtual int ySize() const { if ( mInput ) return mInput->ySize(); else return 0; }
9898

9999
/** \brief helper function to create zero padded band names */
100-
virtual QString generateBandName( int theBandNumber ) const
100+
virtual QString generateBandName( int theBandNumber ) const
101101
{
102102
return tr( "Band" ) + QString( " %1" ) .arg( theBandNumber, 1 + ( int ) log10(( float ) bandCount() ), 10, QChar( '0' ) );
103103
}

src/providers/gdal/qgsgdalprovider.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include <QTime>
4848
#include <QSettings>
4949
#include <QTextDocument>
50+
#include <QDebug>
5051

5152
#include "gdalwarper.h"
5253
#include "ogr_spatialref.h"
@@ -875,6 +876,78 @@ int QgsGdalProvider::yBlockSize() const
875876
int QgsGdalProvider::xSize() const { return mWidth; }
876877
int QgsGdalProvider::ySize() const { return mHeight; }
877878

879+
QString QgsGdalProvider::generateBandName( int theBandNumber ) const
880+
{
881+
#ifdef GDAL_COMPUTE_VERSION /* only available in GDAL 1.10 or later */
882+
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(1,10,0)
883+
if ( strcmp( GDALGetDriverShortName( GDALGetDatasetDriver( mGdalDataset ) ), "netCDF" ) == 0 )
884+
{
885+
char ** GDALmetadata = GDALGetMetadata( mGdalDataset, NULL );
886+
887+
if ( GDALmetadata )
888+
{
889+
QStringList metadata = cStringList2Q_( GDALmetadata );
890+
QStringList dimExtraValues;
891+
QMap< QString, QString > unitsMap;
892+
for ( QStringList::const_iterator i = metadata.begin();
893+
i != metadata.end(); ++i )
894+
{
895+
QString val( *i );
896+
if ( !val.startsWith( "NETCDF_DIM_EXTRA" ) && !val.contains( "#units=" ) )
897+
continue;
898+
QStringList values = val.split( "=" );
899+
val = values.at( 1 );
900+
if ( values.at( 0 ) == "NETCDF_DIM_EXTRA" )
901+
{
902+
dimExtraValues = val.replace( QString( "{" ), QString( "" ) ).replace( QString( "}" ), QString( "" ) ).split( "," );
903+
//http://qt-project.org/doc/qt-4.8/qregexp.html#capturedTexts
904+
}
905+
else
906+
{
907+
unitsMap[ values.at( 0 ).split( "#" ).at( 0 )] = val;
908+
}
909+
}
910+
if ( dimExtraValues.count() > 0 )
911+
{
912+
QStringList bandNameValues;
913+
GDALRasterBandH gdalBand = GDALGetRasterBand( mGdalDataset, theBandNumber );
914+
GDALmetadata = GDALGetMetadata( gdalBand, NULL );
915+
916+
if ( GDALmetadata )
917+
{
918+
metadata = cStringList2Q_( GDALmetadata );
919+
for ( QStringList::const_iterator i = metadata.begin();
920+
i != metadata.end(); ++i )
921+
{
922+
QString val( *i );
923+
if ( !val.startsWith( "NETCDF_DIM_" ) )
924+
continue;
925+
QStringList values = val.split( "=" );
926+
for ( QStringList::const_iterator j = dimExtraValues.begin();
927+
j != dimExtraValues.end(); ++j )
928+
{
929+
QString dim = ( *j );
930+
if ( values.at( 0 ) != "NETCDF_DIM_" + dim )
931+
continue;
932+
if ( unitsMap.contains( dim ) && unitsMap[ dim ] != "" && unitsMap[ dim ] != "none" )
933+
bandNameValues.append( dim + "=" + values.at( 1 ) + " (" + unitsMap[ dim ] + ")" );
934+
else
935+
bandNameValues.append( dim + "=" + values.at( 1 ) );
936+
}
937+
}
938+
}
939+
940+
if ( bandNameValues.count() > 0 )
941+
return tr( "Band" ) + QString( " %1 / %2" ) .arg( theBandNumber, 1 + ( int ) log10(( float ) bandCount() ), 10, QChar( '0' ) ).arg( bandNameValues.join( " / " ) );
942+
}
943+
}
944+
}
945+
#endif
946+
#endif
947+
948+
return QgsRasterDataProvider::generateBandName( theBandNumber );
949+
}
950+
878951
QgsRasterIdentifyResult QgsGdalProvider::identify( const QgsPoint & thePoint, QgsRaster::IdentifyFormat theFormat, const QgsRectangle &theExtent, int theWidth, int theHeight )
879952
{
880953
QgsDebugMsg( QString( "thePoint = %1 %2" ).arg( thePoint.x(), 0, 'g', 10 ).arg( thePoint.y(), 0, 'g', 10 ) );

src/providers/gdal/qgsgdalprovider.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ class QgsGdalProvider : public QgsRasterDataProvider, QgsGdalProviderBase
169169
int xSize() const;
170170
int ySize() const;
171171

172+
QString generateBandName( int theBandNumber ) const;
173+
172174
/**Reimplemented from QgsRasterDataProvider to bypass second resampling (more efficient for local file based sources)*/
173175
QgsRasterBlock *block( int theBandNo, const QgsRectangle &theExtent, int theWidth, int theHeight );
174176

0 commit comments

Comments
 (0)