QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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#include "qgsstringutils.h"
20
22
23QString QgsRasterLayerPropertiesAlgorithm::name() const
24{
25 return QStringLiteral( "rasterlayerproperties" );
26}
27
28QString QgsRasterLayerPropertiesAlgorithm::displayName() const
29{
30 return QObject::tr( "Raster layer properties" );
31}
32
33QStringList QgsRasterLayerPropertiesAlgorithm::tags() const
34{
35 return QObject::tr( "extent,pixel,size,width,height" ).split( ',' );
36}
37
38QString QgsRasterLayerPropertiesAlgorithm::group() const
39{
40 return QObject::tr( "Raster analysis" );
41}
42
43QString QgsRasterLayerPropertiesAlgorithm::groupId() const
44{
45 return QStringLiteral( "rasteranalysis" );
46}
47
48void QgsRasterLayerPropertiesAlgorithm::initAlgorithm( const QVariantMap & )
49{
50 addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT" ),
51 QObject::tr( "Input layer" ) ) );
52 addParameter( new QgsProcessingParameterBand( QStringLiteral( "BAND" ),
53 QObject::tr( "Band number" ), QVariant(), QStringLiteral( "INPUT" ), true ) );
54
55 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "X_MIN" ), QObject::tr( "Minimum x-coordinate" ) ) );
56 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "X_MAX" ), QObject::tr( "Maximum x-coordinate" ) ) );
57 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "Y_MIN" ), QObject::tr( "Minimum y-coordinate" ) ) );
58 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "Y_MAX" ), QObject::tr( "Maximum y-coordinate" ) ) );
59 addOutput( new QgsProcessingOutputString( QStringLiteral( "EXTENT" ), QObject::tr( "Extent" ) ) );
60 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "PIXEL_WIDTH" ), QObject::tr( "Pixel size (width) in map units" ) ) );
61 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "PIXEL_HEIGHT" ), QObject::tr( "Pixel size (height) in map units" ) ) );
62 addOutput( new QgsProcessingOutputString( QStringLiteral( "CRS_AUTHID" ), QObject::tr( "CRS authority identifier" ) ) );
63 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "WIDTH_IN_PIXELS" ), QObject::tr( "Width in pixels" ) ) );
64 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "HEIGHT_IN_PIXELS" ), QObject::tr( "Height in pixels" ) ) );
65 addOutput( new QgsProcessingOutputBoolean( QStringLiteral( "HAS_NODATA_VALUE" ), QObject::tr( "Band has a nodata value set" ) ) );
66 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "NODATA_VALUE" ), QObject::tr( "Band nodata value" ) ) );
67 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "BAND_COUNT" ), QObject::tr( "Number of bands in raster" ) ) );
68}
69
70QString QgsRasterLayerPropertiesAlgorithm::shortHelpString() const
71{
72 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"
73 "If an optional band number is specified then the nodata value for the selected band will also be returned." );
74}
75
76QgsRasterLayerPropertiesAlgorithm *QgsRasterLayerPropertiesAlgorithm::createInstance() const
77{
78 return new QgsRasterLayerPropertiesAlgorithm();
79}
80
81bool QgsRasterLayerPropertiesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
82{
83 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT" ), context );
84
85 if ( !layer )
86 throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );
87
88 mBandCount = layer->bandCount();
89
90 if ( parameters.value( QStringLiteral( "BAND" ) ).isValid() )
91 {
92 const int band = parameterAsInt( parameters, QStringLiteral( "BAND" ), context );
93 if ( band < 1 || band > mBandCount )
94 throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( band )
95 .arg( layer->bandCount() ) );
96 mHasNoDataValue = layer->dataProvider()->sourceHasNoDataValue( band );
97 if ( mHasNoDataValue )
98 mNoDataValue = layer->dataProvider()->sourceNoDataValue( band );
99 }
100
101 mLayerWidth = layer->width();
102 mLayerHeight = layer->height();
103 mExtent = layer->extent();
104 mCrs = layer->crs();
105 mRasterUnitsPerPixelX = layer->rasterUnitsPerPixelX();
106 mRasterUnitsPerPixelY = layer->rasterUnitsPerPixelY();
107
108 return true;
109}
110
111QVariantMap QgsRasterLayerPropertiesAlgorithm::processAlgorithm( const QVariantMap &, QgsProcessingContext &, QgsProcessingFeedback * )
112{
113 QVariantMap outputs;
114 outputs.insert( QStringLiteral( "EXTENT" ), mExtent.toString() );
115 outputs.insert( QStringLiteral( "X_MIN" ), mExtent.xMinimum() );
116 outputs.insert( QStringLiteral( "X_MAX" ), mExtent.xMaximum() );
117 outputs.insert( QStringLiteral( "Y_MIN" ), mExtent.yMinimum() );
118 outputs.insert( QStringLiteral( "Y_MAX" ), mExtent.yMaximum() );
119
120 outputs.insert( QStringLiteral( "PIXEL_WIDTH" ), mRasterUnitsPerPixelX );
121 outputs.insert( QStringLiteral( "PIXEL_HEIGHT" ), mRasterUnitsPerPixelY );
122
123 outputs.insert( QStringLiteral( "CRS_AUTHID" ), mCrs.authid() );
124 outputs.insert( QStringLiteral( "WIDTH_IN_PIXELS" ), mLayerWidth );
125 outputs.insert( QStringLiteral( "HEIGHT_IN_PIXELS" ), mLayerHeight );
126
127 outputs.insert( QStringLiteral( "HAS_NODATA_VALUE" ), mHasNoDataValue );
128 if ( mHasNoDataValue )
129 outputs.insert( QStringLiteral( "NODATA_VALUE" ), mNoDataValue );
130 outputs.insert( QStringLiteral( "BAND_COUNT" ), mBandCount );
131
132 return outputs;
133}
134
135
137
138
139
virtual QgsRectangle extent() const
Returns the extent of the layer.
QgsCoordinateReferenceSystem crs
Definition: qgsmaplayer.h:79
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
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.