QGIS API Documentation  2.3.0-Master
 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 
virtual int bandCount() const =0
Get number of bands.
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QGis::DataType dataType(int bandNo) const
Returns data type for the band specified by number.
QVector< bool > mHasOutputNoData
static bool contains(double value, const QgsRasterRangeList &rangeList)
Test if value is within the list of ranges.
void setNoData(int bandNo, QgsRasterRangeList noData)
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
bool setValue(int row, int column, double value)
Set value on position.
Raster pipe that deals with null values.
double noDataValue() const
Return no data value.
QGis::DataType dataType() const
Returns data type.
bool isNoData(int row, int column)
Check if value at position is no data.
bool setIsNoData(int row, int column)
Set no data on pixel.
QgsRasterInterface * clone() const
Clone itself, create deep copy.
QVector< double > mOutputNoData
Raster data container.
QVector< QgsRasterRangeList > mNoData
double value(int row, int column) const
Read a single value if type of block is numeric.
static bool typeIsColor(QGis::DataType type)
Returns true if data type is color.
virtual QGis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
Base class for processing filters like renderers, reprojector, resampler etc.
void setOutputNoDataValue(int bandNo, double noData)
Set output no data value.
bool hasNoDataValue() const
True if the block has no data value.
QList< QgsRasterRange > QgsRasterRangeList
QgsRasterRangeList noData(int bandNo) const
DataType
Raster data types.
Definition: qgis.h:204
virtual QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height)=0
Read block of data using given extent and size.
QgsRasterNuller(QgsRasterInterface *input=0)
QgsRasterInterface * mInput
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height)
Read block of data using given extent and size.
int bandCount() const
Get number of bands.