QGIS API Documentation 3.41.0-Master (cea29feecf2)
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#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" ), QObject::tr( "Input layer" ) ) );
51 addParameter( new QgsProcessingParameterBand( QStringLiteral( "BAND" ), QObject::tr( "Band number" ), QVariant(), QStringLiteral( "INPUT" ), true ) );
52
53 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "X_MIN" ), QObject::tr( "Minimum x-coordinate" ) ) );
54 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "X_MAX" ), QObject::tr( "Maximum x-coordinate" ) ) );
55 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "Y_MIN" ), QObject::tr( "Minimum y-coordinate" ) ) );
56 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "Y_MAX" ), QObject::tr( "Maximum y-coordinate" ) ) );
57 addOutput( new QgsProcessingOutputString( QStringLiteral( "EXTENT" ), QObject::tr( "Extent" ) ) );
58 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "PIXEL_WIDTH" ), QObject::tr( "Pixel size (width) in map units" ) ) );
59 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "PIXEL_HEIGHT" ), QObject::tr( "Pixel size (height) in map units" ) ) );
60 addOutput( new QgsProcessingOutputString( QStringLiteral( "CRS_AUTHID" ), QObject::tr( "CRS authority identifier" ) ) );
61 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "WIDTH_IN_PIXELS" ), QObject::tr( "Width in pixels" ) ) );
62 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "HEIGHT_IN_PIXELS" ), QObject::tr( "Height in pixels" ) ) );
63 addOutput( new QgsProcessingOutputBoolean( QStringLiteral( "HAS_NODATA_VALUE" ), QObject::tr( "Band has a NoData value set" ) ) );
64 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "NODATA_VALUE" ), QObject::tr( "Band NoData value" ) ) );
65 addOutput( new QgsProcessingOutputNumber( QStringLiteral( "BAND_COUNT" ), QObject::tr( "Number of bands in raster" ) ) );
66}
67
68QString QgsRasterLayerPropertiesAlgorithm::shortHelpString() const
69{
70 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"
71 "If an optional band number is specified then the NoData value for the selected band will also be returned." );
72}
73
74QgsRasterLayerPropertiesAlgorithm *QgsRasterLayerPropertiesAlgorithm::createInstance() const
75{
76 return new QgsRasterLayerPropertiesAlgorithm();
77}
78
79bool QgsRasterLayerPropertiesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
80{
81 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT" ), context );
82
83 if ( !layer )
84 throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );
85
86 mBandCount = layer->bandCount();
87
88 if ( parameters.value( QStringLiteral( "BAND" ) ).isValid() )
89 {
90 const int band = parameterAsInt( parameters, QStringLiteral( "BAND" ), context );
91 if ( band < 1 || band > mBandCount )
92 throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( band ).arg( layer->bandCount() ) );
93 mHasNoDataValue = layer->dataProvider()->sourceHasNoDataValue( band );
94 if ( mHasNoDataValue )
95 mNoDataValue = layer->dataProvider()->sourceNoDataValue( band );
96 }
97
98 mLayerWidth = layer->width();
99 mLayerHeight = layer->height();
100 mExtent = layer->extent();
101 mCrs = layer->crs();
102 mRasterUnitsPerPixelX = layer->rasterUnitsPerPixelX();
103 mRasterUnitsPerPixelY = layer->rasterUnitsPerPixelY();
104
105 return true;
106}
107
108QVariantMap QgsRasterLayerPropertiesAlgorithm::processAlgorithm( const QVariantMap &, QgsProcessingContext &, QgsProcessingFeedback * )
109{
110 QVariantMap outputs;
111 outputs.insert( QStringLiteral( "EXTENT" ), mExtent.toString() );
112 outputs.insert( QStringLiteral( "X_MIN" ), mExtent.xMinimum() );
113 outputs.insert( QStringLiteral( "X_MAX" ), mExtent.xMaximum() );
114 outputs.insert( QStringLiteral( "Y_MIN" ), mExtent.yMinimum() );
115 outputs.insert( QStringLiteral( "Y_MAX" ), mExtent.yMaximum() );
116
117 outputs.insert( QStringLiteral( "PIXEL_WIDTH" ), mRasterUnitsPerPixelX );
118 outputs.insert( QStringLiteral( "PIXEL_HEIGHT" ), mRasterUnitsPerPixelY );
119
120 outputs.insert( QStringLiteral( "CRS_AUTHID" ), mCrs.authid() );
121 outputs.insert( QStringLiteral( "WIDTH_IN_PIXELS" ), mLayerWidth );
122 outputs.insert( QStringLiteral( "HEIGHT_IN_PIXELS" ), mLayerHeight );
123
124 outputs.insert( QStringLiteral( "HAS_NODATA_VALUE" ), mHasNoDataValue );
125 if ( mHasNoDataValue )
126 outputs.insert( QStringLiteral( "NODATA_VALUE" ), mNoDataValue );
127 outputs.insert( QStringLiteral( "BAND_COUNT" ), mBandCount );
128
129 return outputs;
130}
131
132
virtual QgsRectangle extent() const
Returns the extent of the layer.
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:83
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.