QGIS API Documentation  master-59fd5e0
src/core/raster/qgsrasternuller.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                          qgsrasternuller.cpp
00003                          ---------------------
00004     begin                : August 2012
00005     copyright            : (C) 2012 by Radim Blazek
00006     email                : radim dot blazek at gmail dot com
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "qgsrasterdataprovider.h"
00019 #include "qgsrasternuller.h"
00020 
00021 QgsRasterNuller::QgsRasterNuller( QgsRasterInterface* input )
00022     : QgsRasterInterface( input )
00023 {
00024 }
00025 
00026 QgsRasterNuller::~QgsRasterNuller()
00027 {
00028 }
00029 
00030 QgsRasterInterface * QgsRasterNuller::clone() const
00031 {
00032   QgsDebugMsg( "Entered" );
00033   QgsRasterNuller * nuller = new QgsRasterNuller( 0 );
00034   nuller->mNoData = mNoData;
00035   nuller->mOutputNoData = mOutputNoData;
00036   nuller->mHasOutputNoData = mHasOutputNoData;
00037   return nuller;
00038 }
00039 
00040 void QgsRasterNuller::setOutputNoDataValue( int bandNo, double noData )
00041 {
00042   if ( bandNo > mOutputNoData.size() )
00043   {
00044     mOutputNoData.resize( bandNo );
00045     mHasOutputNoData.resize( bandNo );
00046   }
00047   mOutputNoData[bandNo-1] = noData;
00048   mHasOutputNoData[bandNo-1] = true;
00049 }
00050 
00051 void QgsRasterNuller::setNoData( int bandNo, QgsRasterRangeList noData )
00052 {
00053   if ( bandNo > mNoData.size() )
00054   {
00055     mNoData.resize( bandNo );
00056   }
00057   mNoData[bandNo-1] = noData;
00058 }
00059 
00060 int QgsRasterNuller::bandCount() const
00061 {
00062   if ( mInput ) return mInput->bandCount();
00063   return 0;
00064 }
00065 
00066 QGis::DataType QgsRasterNuller::dataType( int bandNo ) const
00067 {
00068   if ( mInput ) return mInput->dataType( bandNo );
00069   return QGis::UnknownDataType;
00070 }
00071 
00072 QgsRasterBlock * QgsRasterNuller::block( int bandNo, QgsRectangle  const & extent, int width, int height )
00073 {
00074   QgsDebugMsg( "Entered" );
00075   if ( !mInput )
00076   {
00077     return new QgsRasterBlock();
00078   }
00079 
00080   QgsRasterBlock *inputBlock = mInput->block( bandNo, extent, width, height );
00081   if ( !inputBlock )
00082   {
00083     return new QgsRasterBlock();
00084   }
00085 
00086   // We don't support nuller for color types
00087   if ( QgsRasterBlock::typeIsColor( inputBlock->dataType() ) )
00088   {
00089     return inputBlock;
00090   }
00091 
00092   QgsRasterBlock *outputBlock = 0;
00093 
00094   if ( mHasOutputNoData.value( bandNo - 1 ) || inputBlock->hasNoDataValue() )
00095   {
00096     double noDataValue;
00097     if ( mHasOutputNoData.value( bandNo - 1 ) )
00098     {
00099       noDataValue = mOutputNoData.value( bandNo - 1 );
00100     }
00101     else
00102     {
00103       noDataValue = inputBlock->noDataValue();
00104     }
00105     outputBlock = new QgsRasterBlock( inputBlock->dataType(), width, height, noDataValue );
00106   }
00107   else
00108   {
00109     outputBlock = new QgsRasterBlock( inputBlock->dataType(), width, height );
00110   }
00111 
00112   for ( int i = 0; i < height; i++ )
00113   {
00114     for ( int j = 0; j < width; j++ )
00115     {
00116       double value = inputBlock->value( i, j );
00117 
00118       bool isNoData = inputBlock->isNoData( i, j );
00119       if ( QgsRasterRange::contains( value, mNoData.value( bandNo - 1 ) ) )
00120       {
00121         isNoData = true;
00122       }
00123       outputBlock->setValue( i, j, inputBlock->value( i, j ) );
00124       if ( isNoData )
00125       {
00126         outputBlock->setIsNoData( i, j );
00127       }
00128       else
00129       {
00130         outputBlock->setValue( i, j, value );
00131       }
00132     }
00133   }
00134   delete inputBlock;
00135 
00136   return outputBlock;
00137 }
00138 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines