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