QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsrasternuller.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrasternuller.cpp
3  ---------------------
4  begin : August 2012
5  copyright : (C) 2012 by Radim Blazek
6  email : radim dot blazek at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsrasterdataprovider.h"
19 #include "qgsrasternuller.h"
20 
22  : QgsRasterInterface( input )
23 {
24 }
25 
27 {
28 }
29 
31 {
32  QgsDebugMsg( "Entered" );
33  QgsRasterNuller * nuller = new QgsRasterNuller( 0 );
34  nuller->mNoData = mNoData;
35  nuller->mOutputNoData = mOutputNoData;
37  return nuller;
38 }
39 
40 void QgsRasterNuller::setOutputNoDataValue( int bandNo, double noData )
41 {
42  if ( bandNo > mOutputNoData.size() )
43  {
44  mOutputNoData.resize( bandNo );
45  mHasOutputNoData.resize( bandNo );
46  }
47  mOutputNoData[bandNo-1] = noData;
48  mHasOutputNoData[bandNo-1] = true;
49 }
50 
52 {
53  if ( bandNo > mNoData.size() )
54  {
55  mNoData.resize( bandNo );
56  }
57  mNoData[bandNo-1] = noData;
58 }
59 
61 {
62  if ( mInput ) return mInput->bandCount();
63  return 0;
64 }
65 
67 {
68  if ( mInput ) return mInput->dataType( bandNo );
69  return QGis::UnknownDataType;
70 }
71 
72 QgsRasterBlock * QgsRasterNuller::block( int bandNo, QgsRectangle const & extent, int width, int height )
73 {
74  QgsDebugMsg( "Entered" );
75  if ( !mInput )
76  {
77  return new QgsRasterBlock();
78  }
79 
80  QgsRasterBlock *inputBlock = mInput->block( bandNo, extent, width, height );
81  if ( !inputBlock )
82  {
83  return new QgsRasterBlock();
84  }
85 
86  // We don't support nuller for color types
87  if ( QgsRasterBlock::typeIsColor( inputBlock->dataType() ) )
88  {
89  return inputBlock;
90  }
91 
92  QgsRasterBlock *outputBlock = 0;
93 
94  if ( mHasOutputNoData.value( bandNo - 1 ) || inputBlock->hasNoDataValue() )
95  {
96  double noDataValue;
97  if ( mHasOutputNoData.value( bandNo - 1 ) )
98  {
99  noDataValue = mOutputNoData.value( bandNo - 1 );
100  }
101  else
102  {
103  noDataValue = inputBlock->noDataValue();
104  }
105  outputBlock = new QgsRasterBlock( inputBlock->dataType(), width, height, noDataValue );
106  }
107  else
108  {
109  outputBlock = new QgsRasterBlock( inputBlock->dataType(), width, height );
110  }
111 
112  for ( int i = 0; i < height; i++ )
113  {
114  for ( int j = 0; j < width; j++ )
115  {
116  double value = inputBlock->value( i, j );
117 
118  bool isNoData = inputBlock->isNoData( i, j );
119  if ( QgsRasterRange::contains( value, mNoData.value( bandNo - 1 ) ) )
120  {
121  isNoData = true;
122  }
123  outputBlock->setValue( i, j, inputBlock->value( i, j ) );
124  if ( isNoData )
125  {
126  outputBlock->setIsNoData( i, j );
127  }
128  else
129  {
130  outputBlock->setValue( i, j, value );
131  }
132  }
133  }
134  delete inputBlock;
135 
136  return outputBlock;
137 }
138