QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
29  QgsRasterNuller *nuller = new QgsRasterNuller( nullptr );
30  nuller->mNoData = mNoData;
31  nuller->mOutputNoData = mOutputNoData;
32  nuller->mHasOutputNoData = mHasOutputNoData;
33  return nuller;
34 }
35 
36 void QgsRasterNuller::setOutputNoDataValue( int bandNo, double noData )
37 {
38  if ( bandNo > mOutputNoData.size() )
39  {
40  mOutputNoData.resize( bandNo );
41  mHasOutputNoData.resize( bandNo );
42  }
43  mOutputNoData[bandNo - 1] = noData;
44  mHasOutputNoData[bandNo - 1] = true;
45 }
46 
47 void QgsRasterNuller::setNoData( int bandNo, const QgsRasterRangeList &noData )
48 {
49  if ( bandNo > mNoData.size() )
50  {
51  mNoData.resize( bandNo );
52  }
53  mNoData[bandNo - 1] = noData;
54 }
55 
57 {
58  if ( mInput ) return mInput->bandCount();
59  return 0;
60 }
61 
63 {
64  if ( mInput ) return mInput->dataType( bandNo );
65  return Qgis::UnknownDataType;
66 }
67 
68 QgsRasterBlock *QgsRasterNuller::block( int bandNo, QgsRectangle const &extent, int width, int height, QgsRasterBlockFeedback *feedback )
69 {
70  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
71  if ( !mInput )
72  {
73  return new QgsRasterBlock();
74  }
75 
76  std::unique_ptr< QgsRasterBlock > inputBlock( mInput->block( bandNo, extent, width, height, feedback ) );
77  if ( !inputBlock )
78  {
79  return new QgsRasterBlock();
80  }
81 
82  // We don't support nuller for color types
83  if ( QgsRasterBlock::typeIsColor( inputBlock->dataType() ) )
84  {
85  return inputBlock.release();
86  }
87 
88  std::unique_ptr< QgsRasterBlock > outputBlock( new QgsRasterBlock( inputBlock->dataType(), width, height ) );
89  if ( mHasOutputNoData.value( bandNo - 1 ) || inputBlock->hasNoDataValue() )
90  {
91  double noDataValue;
92  if ( mHasOutputNoData.value( bandNo - 1 ) )
93  {
94  noDataValue = mOutputNoData.value( bandNo - 1 );
95  }
96  else
97  {
98  noDataValue = inputBlock->noDataValue();
99  }
100  outputBlock->setNoDataValue( noDataValue );
101  }
102 
103  bool isNoData = false;
104  for ( int i = 0; i < height; i++ )
105  {
106  for ( int j = 0; j < width; j++ )
107  {
108  double value = inputBlock->valueAndNoData( i, j, isNoData );
109 
110  if ( QgsRasterRange::contains( value, mNoData.value( bandNo - 1 ) ) )
111  {
112  isNoData = true;
113  }
114  outputBlock->setValue( i, j, inputBlock->value( i, j ) );
115  if ( isNoData )
116  {
117  outputBlock->setIsNoData( i, j );
118  }
119  else
120  {
121  outputBlock->setValue( i, j, value );
122  }
123  }
124  }
125  return outputBlock.release();
126 }
127 
QgsRasterNuller::bandCount
int bandCount() const override
Gets number of bands.
Definition: qgsrasternuller.cpp:56
Qgis::DataType
DataType
Raster data types.
Definition: qgis.h:102
QgsRasterInterface::mInput
QgsRasterInterface * mInput
Definition: qgsrasterinterface.h:467
QgsDebugMsgLevel
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
QgsRasterNuller::setOutputNoDataValue
void setOutputNoDataValue(int bandNo, double noData)
Sets the output no data value.
Definition: qgsrasternuller.cpp:36
QgsRasterNuller
Raster pipe that deals with null values.
Definition: qgsrasternuller.h:33
QgsRasterNuller::block
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
Definition: qgsrasternuller.cpp:68
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsRasterNuller::QgsRasterNuller
QgsRasterNuller(QgsRasterInterface *input=nullptr)
Definition: qgsrasternuller.cpp:21
QgsRasterInterface::dataType
virtual Qgis::DataType dataType(int bandNo) const =0
Returns data type for the band specified by number.
QgsRasterNuller::clone
QgsRasterNuller * clone() const override
Clone itself, create deep copy.
Definition: qgsrasternuller.cpp:26
QgsRasterRangeList
QList< QgsRasterRange > QgsRasterRangeList
Definition: qgsrasterrange.h:26
QgsRasterBlock::typeIsColor
static bool typeIsColor(Qgis::DataType type)
Returns true if data type is color.
Definition: qgsrasterblock.cpp:164
QgsRasterRange::contains
bool contains(double value) const
Returns true if this range contains the specified value.
Definition: qgsrasterrange.h:114
QgsRasterNuller::setNoData
void setNoData(int bandNo, const QgsRasterRangeList &noData)
Definition: qgsrasternuller.cpp:47
QgsRasterInterface
Base class for processing filters like renderers, reprojector, resampler etc.
Definition: qgsrasterinterface.h:117
QgsRasterBlockFeedback
Feedback object tailored for raster block reading.
Definition: qgsrasterinterface.h:41
QgsRasterNuller::dataType
Qgis::DataType dataType(int bandNo) const override
Returns data type for the band specified by number.
Definition: qgsrasternuller.cpp:62
Qgis::UnknownDataType
@ UnknownDataType
Unknown or unspecified type.
Definition: qgis.h:103
QgsRasterInterface::bandCount
virtual int bandCount() const =0
Gets number of bands.
QgsRasterInterface::block
virtual QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr)=0
Read block of data using given extent and size.
qgsrasternuller.h
QgsRasterBlock
Raster data container.
Definition: qgsrasterblock.h:37
QgsRasterInterface::extent
virtual QgsRectangle extent() const
Gets the extent of the interface.
Definition: qgsrasterinterface.h:229
qgsrasterdataprovider.h
QgsRasterNuller::noData
QgsRasterRangeList noData(int bandNo) const
Definition: qgsrasternuller.h:53