QGIS API Documentation 3.99.0-Master (e9821da5c6b)
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
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
30
32{
33 QgsDebugMsgLevel( u"Entered"_s, 4 );
34 QgsRasterNuller *nuller = new QgsRasterNuller( nullptr );
35 nuller->mNoData = mNoData;
36 nuller->mOutputNoData = mOutputNoData;
37 nuller->mHasOutputNoData = mHasOutputNoData;
38 return nuller;
39}
40
42{
43 if ( bandNo > mOutputNoData.size() )
44 {
45 mOutputNoData.resize( bandNo );
46 mHasOutputNoData.resize( bandNo );
47 }
48 mOutputNoData[bandNo - 1] = noData;
49 mHasOutputNoData[bandNo - 1] = true;
50}
51
53{
54 if ( bandNo > mNoData.size() )
55 {
56 mNoData.resize( bandNo );
57 }
58 mNoData[bandNo - 1] = noData;
59}
60
62{
63 if ( mInput ) return mInput->bandCount();
64 return 0;
65}
66
68{
69 if ( mInput ) return mInput->dataType( bandNo );
71}
72
73QgsRasterBlock *QgsRasterNuller::block( int bandNo, QgsRectangle const &extent, int width, int height, QgsRasterBlockFeedback *feedback )
74{
75 QgsDebugMsgLevel( u"Entered"_s, 4 );
76 if ( !mInput )
77 {
78 return new QgsRasterBlock();
79 }
80
81 std::unique_ptr< QgsRasterBlock > inputBlock( mInput->block( bandNo, extent, width, height, feedback ) );
82 if ( !inputBlock )
83 {
84 return new QgsRasterBlock();
85 }
86
87 // We don't support nuller for color types
88 if ( QgsRasterBlock::typeIsColor( inputBlock->dataType() ) )
89 {
90 return inputBlock.release();
91 }
92
93 auto outputBlock = std::make_unique<QgsRasterBlock>( inputBlock->dataType(), width, height );
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->setNoDataValue( noDataValue );
106 }
107
108 bool isNoData = false;
109 for ( int i = 0; i < height; i++ )
110 {
111 for ( int j = 0; j < width; j++ )
112 {
113 const double value = inputBlock->valueAndNoData( i, j, isNoData );
114
115 if ( QgsRasterRange::contains( value, mNoData.value( bandNo - 1 ) ) )
116 {
117 isNoData = true;
118 }
119 if ( isNoData )
120 {
121 outputBlock->setIsNoData( i, j );
122 }
123 else
124 {
125 outputBlock->setValue( i, j, value );
126 }
127 }
128 }
129 return outputBlock.release();
130}
131
DataType
Raster data types.
Definition qgis.h:379
@ UnknownDataType
Unknown or unspecified type.
Definition qgis.h:380
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:63
QList< QgsRasterRange > QgsRasterRangeList