QGIS API Documentation 4.3.0-Master (ffcfc20b9b4)
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(
81 "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"
82 "If an optional band number is specified then the NoData value for the selected band will also be returned."
83 );
84}
85
86QgsRasterLayerPropertiesAlgorithm *QgsRasterLayerPropertiesAlgorithm::createInstance() const
87{
88 return new QgsRasterLayerPropertiesAlgorithm();
89}
90
91bool QgsRasterLayerPropertiesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
92{
93 QGS_MARK_ALGORITHM_SOURCE
94
95 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, u"INPUT"_s, context );
96
97 if ( !layer )
98 throw QgsProcessingException( invalidRasterError( parameters, u"INPUT"_s ) );
99
100 mBandCount = layer->bandCount();
101
102 if ( parameters.value( u"BAND"_s ).isValid() )
103 {
104 const int band = parameterAsInt( parameters, u"BAND"_s, context );
105 if ( band < 1 || band > mBandCount )
106 throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( band ).arg( layer->bandCount() ) );
107 mHasNoDataValue = layer->dataProvider()->sourceHasNoDataValue( band );
108 if ( mHasNoDataValue )
109 mNoDataValue = layer->dataProvider()->sourceNoDataValue( band );
110 }
111
112 mLayerWidth = layer->width();
113 mLayerHeight = layer->height();
114 mExtent = layer->extent();
115 mCrs = layer->crs();
116 mRasterUnitsPerPixelX = layer->rasterUnitsPerPixelX();
117 mRasterUnitsPerPixelY = layer->rasterUnitsPerPixelY();
118
119 return true;
120}
121
122QVariantMap QgsRasterLayerPropertiesAlgorithm::processAlgorithm( const QVariantMap &, QgsProcessingContext &, QgsProcessingFeedback * )
123{
124 QVariantMap outputs;
125 outputs.insert( u"EXTENT"_s, mExtent.toString() );
126 outputs.insert( u"X_MIN"_s, mExtent.xMinimum() );
127 outputs.insert( u"X_MAX"_s, mExtent.xMaximum() );
128 outputs.insert( u"Y_MIN"_s, mExtent.yMinimum() );
129 outputs.insert( u"Y_MAX"_s, mExtent.yMaximum() );
130
131 outputs.insert( u"PIXEL_WIDTH"_s, mRasterUnitsPerPixelX );
132 outputs.insert( u"PIXEL_HEIGHT"_s, mRasterUnitsPerPixelY );
133
134 outputs.insert( u"CRS_AUTHID"_s, mCrs.authid() );
135 outputs.insert( u"WIDTH_IN_PIXELS"_s, mLayerWidth );
136 outputs.insert( u"HEIGHT_IN_PIXELS"_s, mLayerHeight );
137
138 outputs.insert( u"HAS_NODATA_VALUE"_s, mHasNoDataValue );
139 if ( mHasNoDataValue )
140 outputs.insert( u"NODATA_VALUE"_s, mNoDataValue );
141 outputs.insert( u"BAND_COUNT"_s, mBandCount );
142
143 return outputs;
144}
145
146
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.
Represents a raster layer.