QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
29
31{
32 QgsDebugMsgLevel( u"Entered"_s, 4 );
33 QgsRasterNuller *nuller = new QgsRasterNuller( nullptr );
34 nuller->mNoData = mNoData;
35 nuller->mOutputNoData = mOutputNoData;
36 nuller->mHasOutputNoData = mHasOutputNoData;
37 return nuller;
38}
39
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 )
63 return mInput->bandCount();
64 return 0;
65}
66
68{
69 if ( mInput )
70 return mInput->dataType( bandNo );
72}
73
74QgsRasterBlock *QgsRasterNuller::block( int bandNo, QgsRectangle const &extent, int width, int height, QgsRasterBlockFeedback *feedback )
75{
76 QgsDebugMsgLevel( u"Entered"_s, 4 );
77 if ( !mInput )
78 {
79 return new QgsRasterBlock();
80 }
81
82 std::unique_ptr< QgsRasterBlock > inputBlock( mInput->block( bandNo, extent, width, height, feedback ) );
83 if ( !inputBlock )
84 {
85 return new QgsRasterBlock();
86 }
87
88 // We don't support nuller for color types
89 if ( QgsRasterBlock::typeIsColor( inputBlock->dataType() ) )
90 {
91 return inputBlock.release();
92 }
93
94 auto outputBlock = std::make_unique<QgsRasterBlock>( inputBlock->dataType(), width, height );
95 if ( mHasOutputNoData.value( bandNo - 1 ) || inputBlock->hasNoDataValue() )
96 {
97 double noDataValue;
98 if ( mHasOutputNoData.value( bandNo - 1 ) )
99 {
100 noDataValue = mOutputNoData.value( bandNo - 1 );
101 }
102 else
103 {
104 noDataValue = inputBlock->noDataValue();
105 }
106 outputBlock->setNoDataValue( noDataValue );
107 }
108
109 bool isNoData = false;
110 for ( int i = 0; i < height; i++ )
111 {
112 for ( int j = 0; j < width; j++ )
113 {
114 const double value = inputBlock->valueAndNoData( i, j, isNoData );
115
116 if ( QgsRasterRange::contains( value, mNoData.value( bandNo - 1 ) ) )
117 {
118 isNoData = true;
119 }
120 if ( isNoData )
121 {
122 outputBlock->setIsNoData( i, j );
123 }
124 else
125 {
126 outputBlock->setValue( i, j, value );
127 }
128 }
129 }
130 return outputBlock.release();
131}
DataType
Raster data types.
Definition qgis.h:393
@ UnknownDataType
Unknown or unspecified type.
Definition qgis.h:394
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