QGIS API Documentation 3.99.0-Master (357b655ed83)
Loading...
Searching...
No Matches
qgsalgorithmrasterlayerproperties.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmrasterlayerproperties.cpp
3 ---------------------
4 begin : April 2021
5 copyright : (C) 2021 by Nyall Dawson
6 email : nyall dot dawson 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
19
20#include "qgsstringutils.h"
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27
28QString QgsRasterLayerPropertiesAlgorithm::name() const
29{
30 return u"rasterlayerproperties"_s;
31}
32
33QString QgsRasterLayerPropertiesAlgorithm::displayName() const
34{
35 return QObject::tr( "Raster layer properties" );
36}
37
38QStringList QgsRasterLayerPropertiesAlgorithm::tags() const
39{
40 return QObject::tr( "extent,pixel,size,width,height" ).split( ',' );
41}
42
43QString QgsRasterLayerPropertiesAlgorithm::group() const
44{
45 return QObject::tr( "Raster analysis" );
46}
47
48QString QgsRasterLayerPropertiesAlgorithm::groupId() const
49{
50 return u"rasteranalysis"_s;
51}
52
53void QgsRasterLayerPropertiesAlgorithm::initAlgorithm( const QVariantMap & )
54{
55 addParameter( new QgsProcessingParameterRasterLayer( u"INPUT"_s, QObject::tr( "Input layer" ) ) );
56 addParameter( new QgsProcessingParameterBand( u"BAND"_s, QObject::tr( "Band number" ), QVariant(), u"INPUT"_s, true ) );
57
58 addOutput( new QgsProcessingOutputNumber( u"X_MIN"_s, QObject::tr( "Minimum x-coordinate" ) ) );
59 addOutput( new QgsProcessingOutputNumber( u"X_MAX"_s, QObject::tr( "Maximum x-coordinate" ) ) );
60 addOutput( new QgsProcessingOutputNumber( u"Y_MIN"_s, QObject::tr( "Minimum y-coordinate" ) ) );
61 addOutput( new QgsProcessingOutputNumber( u"Y_MAX"_s, QObject::tr( "Maximum y-coordinate" ) ) );
62 addOutput( new QgsProcessingOutputString( u"EXTENT"_s, QObject::tr( "Extent" ) ) );
63 addOutput( new QgsProcessingOutputNumber( u"PIXEL_WIDTH"_s, QObject::tr( "Pixel size (width) in map units" ) ) );
64 addOutput( new QgsProcessingOutputNumber( u"PIXEL_HEIGHT"_s, QObject::tr( "Pixel size (height) in map units" ) ) );
65 addOutput( new QgsProcessingOutputString( u"CRS_AUTHID"_s, QObject::tr( "CRS authority identifier" ) ) );
66 addOutput( new QgsProcessingOutputNumber( u"WIDTH_IN_PIXELS"_s, QObject::tr( "Width in pixels" ) ) );
67 addOutput( new QgsProcessingOutputNumber( u"HEIGHT_IN_PIXELS"_s, QObject::tr( "Height in pixels" ) ) );
68 addOutput( new QgsProcessingOutputBoolean( u"HAS_NODATA_VALUE"_s, QObject::tr( "Band has a NoData value set" ) ) );
69 addOutput( new QgsProcessingOutputNumber( u"NODATA_VALUE"_s, QObject::tr( "Band NoData value" ) ) );
70 addOutput( new QgsProcessingOutputNumber( u"BAND_COUNT"_s, QObject::tr( "Number of bands in raster" ) ) );
71}
72
73QString QgsRasterLayerPropertiesAlgorithm::shortDescription() const
74{
75 return QObject::tr( "Returns basic properties of a given raster layer, including the extent, size in pixels and dimensions of pixels (in map units)." );
76}
77
78QString QgsRasterLayerPropertiesAlgorithm::shortHelpString() const
79{
80 return QObject::tr( "This algorithm returns basic properties of the given raster layer, including the extent, size in pixels and dimensions of pixels (in map units).\n\n"
81 "If an optional band number is specified then the NoData value for the selected band will also be returned." );
82}
83
84QgsRasterLayerPropertiesAlgorithm *QgsRasterLayerPropertiesAlgorithm::createInstance() const
85{
86 return new QgsRasterLayerPropertiesAlgorithm();
87}
88
89bool QgsRasterLayerPropertiesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
90{
91 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, u"INPUT"_s, context );
92
93 if ( !layer )
94 throw QgsProcessingException( invalidRasterError( parameters, u"INPUT"_s ) );
95
96 mBandCount = layer->bandCount();
97
98 if ( parameters.value( u"BAND"_s ).isValid() )
99 {
100 const int band = parameterAsInt( parameters, u"BAND"_s, context );
101 if ( band < 1 || band > mBandCount )
102 throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( band ).arg( layer->bandCount() ) );
103 mHasNoDataValue = layer->dataProvider()->sourceHasNoDataValue( band );
104 if ( mHasNoDataValue )
105 mNoDataValue = layer->dataProvider()->sourceNoDataValue( band );
106 }
107
108 mLayerWidth = layer->width();
109 mLayerHeight = layer->height();
110 mExtent = layer->extent();
111 mCrs = layer->crs();
112 mRasterUnitsPerPixelX = layer->rasterUnitsPerPixelX();
113 mRasterUnitsPerPixelY = layer->rasterUnitsPerPixelY();
114
115 return true;
116}
117
118QVariantMap QgsRasterLayerPropertiesAlgorithm::processAlgorithm( const QVariantMap &, QgsProcessingContext &, QgsProcessingFeedback * )
119{
120 QVariantMap outputs;
121 outputs.insert( u"EXTENT"_s, mExtent.toString() );
122 outputs.insert( u"X_MIN"_s, mExtent.xMinimum() );
123 outputs.insert( u"X_MAX"_s, mExtent.xMaximum() );
124 outputs.insert( u"Y_MIN"_s, mExtent.yMinimum() );
125 outputs.insert( u"Y_MAX"_s, mExtent.yMaximum() );
126
127 outputs.insert( u"PIXEL_WIDTH"_s, mRasterUnitsPerPixelX );
128 outputs.insert( u"PIXEL_HEIGHT"_s, mRasterUnitsPerPixelY );
129
130 outputs.insert( u"CRS_AUTHID"_s, mCrs.authid() );
131 outputs.insert( u"WIDTH_IN_PIXELS"_s, mLayerWidth );
132 outputs.insert( u"HEIGHT_IN_PIXELS"_s, mLayerHeight );
133
134 outputs.insert( u"HAS_NODATA_VALUE"_s, mHasNoDataValue );
135 if ( mHasNoDataValue )
136 outputs.insert( u"NODATA_VALUE"_s, mNoDataValue );
137 outputs.insert( u"BAND_COUNT"_s, mBandCount );
138
139 return outputs;
140}
141
142
virtual QgsRectangle extent() const
Returns the extent of the layer.
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:90
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A boolean output for processing algorithms.
A numeric output for processing algorithms.
A string output for processing algorithms.
A raster band parameter for Processing algorithms.
A raster layer parameter for processing algorithms.
virtual bool sourceHasNoDataValue(int bandNo) const
Returns true if source band has no data value.
virtual double sourceNoDataValue(int bandNo) const
Value representing no data value.
Represents a raster layer.
int height() const
Returns the height of the (unclipped) raster.
int bandCount() const
Returns the number of bands in this layer.
double rasterUnitsPerPixelX() const
Returns the number of raster units per each raster pixel in X axis.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
double rasterUnitsPerPixelY() const
Returns the number of raster units per each raster pixel in Y axis.
int width() const
Returns the width of the (unclipped) raster.