QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
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 "qgsrasternuller.h"
19
21
26
28{
29 QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
30 QgsRasterNuller *nuller = new QgsRasterNuller( nullptr );
31 nuller->mNoData = mNoData;
32 nuller->mOutputNoData = mOutputNoData;
33 nuller->mHasOutputNoData = mHasOutputNoData;
34 return nuller;
35}
36
38{
39 if ( bandNo > mOutputNoData.size() )
40 {
41 mOutputNoData.resize( bandNo );
42 mHasOutputNoData.resize( bandNo );
43 }
44 mOutputNoData[bandNo - 1] = noData;
45 mHasOutputNoData[bandNo - 1] = true;
46}
47
49{
50 if ( bandNo > mNoData.size() )
51 {
52 mNoData.resize( bandNo );
53 }
54 mNoData[bandNo - 1] = noData;
55}
56
58{
59 if ( mInput ) return mInput->bandCount();
60 return 0;
61}
62
64{
65 if ( mInput ) return mInput->dataType( bandNo );
67}
68
69QgsRasterBlock *QgsRasterNuller::block( int bandNo, QgsRectangle const &extent, int width, int height, QgsRasterBlockFeedback *feedback )
70{
71 QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
72 if ( !mInput )
73 {
74 return new QgsRasterBlock();
75 }
76
77 std::unique_ptr< QgsRasterBlock > inputBlock( mInput->block( bandNo, extent, width, height, feedback ) );
78 if ( !inputBlock )
79 {
80 return new QgsRasterBlock();
81 }
82
83 // We don't support nuller for color types
84 if ( QgsRasterBlock::typeIsColor( inputBlock->dataType() ) )
85 {
86 return inputBlock.release();
87 }
88
89 auto outputBlock = std::make_unique<QgsRasterBlock>( inputBlock->dataType(), width, height );
90 if ( mHasOutputNoData.value( bandNo - 1 ) || inputBlock->hasNoDataValue() )
91 {
92 double noDataValue;
93 if ( mHasOutputNoData.value( bandNo - 1 ) )
94 {
95 noDataValue = mOutputNoData.value( bandNo - 1 );
96 }
97 else
98 {
99 noDataValue = inputBlock->noDataValue();
100 }
101 outputBlock->setNoDataValue( noDataValue );
102 }
103
104 bool isNoData = false;
105 for ( int i = 0; i < height; i++ )
106 {
107 for ( int j = 0; j < width; j++ )
108 {
109 const double value = inputBlock->valueAndNoData( i, j, isNoData );
110
111 if ( QgsRasterRange::contains( value, mNoData.value( bandNo - 1 ) ) )
112 {
113 isNoData = true;
114 }
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
DataType
Raster data types.
Definition qgis.h:372
@ UnknownDataType
Unknown or unspecified type.
Definition qgis.h:373
Feedback object tailored for raster block reading.
Raster data container.
static bool typeIsColor(Qgis::DataType type)
Returns true if a data type is a color type.
QgsRasterInterface(QgsRasterInterface *input=nullptr)
QgsRasterInterface * mInput
virtual QgsRectangle extent() const
Gets the extent of the interface.
virtual QgsRasterInterface * input() const
Current input.
QgsRasterNuller * clone() const override
Clone itself, create deep copy.
void setNoData(int bandNo, const QgsRasterRangeList &noData)
Qgis::DataType dataType(int bandNo) const override
Returns data type for the band specified by number.
QgsRasterBlock * block(int bandNo, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback=nullptr) override
Read block of data using given extent and size.
void setOutputNoDataValue(int bandNo, double noData)
Sets the output no data value.
QgsRasterNuller(QgsRasterInterface *input=nullptr)
int bandCount() const override
Gets number of bands.
QgsRasterRangeList noData(int bandNo) const
bool contains(double value) const
Returns true if this range contains the specified value.
A rectangle specified with double values.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:61
QList< QgsRasterRange > QgsRasterRangeList