QGIS API Documentation  3.12.1-București (121cc00ff0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
qgsalgorithmconstantraster.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmconstantraster.cpp
3  ---------------------
4  begin : November 2019
5  copyright : (C) 2019 by Alexander Bruy
6  email : alexander dot bruy 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 "qgsrasterfilewriter.h"
20 
22 
23 QString QgsConstantRasterAlgorithm::name() const
24 {
25  return QStringLiteral( "createconstantrasterlayer" );
26 }
27 
28 QString QgsConstantRasterAlgorithm::displayName() const
29 {
30  return QObject::tr( "Create constant raster layer" );
31 }
32 
33 QStringList QgsConstantRasterAlgorithm::tags() const
34 {
35  return QObject::tr( "raster,create,constant" ).split( ',' );
36 }
37 
38 QString QgsConstantRasterAlgorithm::group() const
39 {
40  return QObject::tr( "Raster tools" );
41 }
42 
43 QString QgsConstantRasterAlgorithm::groupId() const
44 {
45  return QStringLiteral( "rastertools" );
46 }
47 
48 QString QgsConstantRasterAlgorithm::shortHelpString() const
49 {
50  return QObject::tr( "Generates raster layer for given extent and cell "
51  "size filled with the specified value." );
52 }
53 
54 QgsConstantRasterAlgorithm *QgsConstantRasterAlgorithm::createInstance() const
55 {
56  return new QgsConstantRasterAlgorithm();
57 }
58 
59 void QgsConstantRasterAlgorithm::initAlgorithm( const QVariantMap & )
60 {
61  addParameter( new QgsProcessingParameterExtent( QStringLiteral( "EXTENT" ), QObject::tr( "Desired extent" ) ) );
62  addParameter( new QgsProcessingParameterCrs( QStringLiteral( "TARGET_CRS" ), QObject::tr( "Target CRS" ), QStringLiteral( "ProjectCrs" ) ) );
63  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "PIXEL_SIZE" ), QObject::tr( "Pixel size" ),
64  QgsProcessingParameterNumber::Double, 0.00001, false, 0.01 ) );
65  addParameter( new QgsProcessingParameterNumber( QStringLiteral( "NUMBER" ), QObject::tr( "Constant value" ),
67 
68  addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Constant" ) ) );
69 }
70 
71 QVariantMap QgsConstantRasterAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
72 {
73  QgsCoordinateReferenceSystem crs = parameterAsCrs( parameters, QStringLiteral( "TARGET_CRS" ), context );
74  QgsRectangle extent = parameterAsExtent( parameters, QStringLiteral( "EXTENT" ), context, crs );
75  double pixelSize = parameterAsDouble( parameters, QStringLiteral( "PIXEL_SIZE" ), context );
76  double value = parameterAsDouble( parameters, QStringLiteral( "NUMBER" ), context );
77 
78  const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context );
79  QFileInfo fi( outputFile );
80  const QString outputFormat = QgsRasterFileWriter::driverForExtension( fi.suffix() );
81 
82  int rows = std::max( std::ceil( extent.height() / pixelSize ), 1.0 );
83  int cols = std::max( std::ceil( extent.width() / pixelSize ), 1.0 );
84 
85  //build new raster extent based on number of columns and cellsize
86  //this prevents output cellsize being calculated too small
87  QgsRectangle rasterExtent = QgsRectangle( extent.xMinimum(), extent.yMaximum() - ( rows * pixelSize ), extent.xMinimum() + ( cols * pixelSize ), extent.yMaximum() );
88 
89  std::unique_ptr< QgsRasterFileWriter > writer = qgis::make_unique< QgsRasterFileWriter >( outputFile );
90  writer->setOutputProviderKey( QStringLiteral( "gdal" ) );
91  writer->setOutputFormat( outputFormat );
92  std::unique_ptr<QgsRasterDataProvider > provider( writer->createOneBandRaster( Qgis::Float32, cols, rows, rasterExtent, crs ) );
93  if ( !provider )
94  throw QgsProcessingException( QObject::tr( "Could not create raster output: %1" ).arg( outputFile ) );
95  if ( !provider->isValid() )
96  throw QgsProcessingException( QObject::tr( "Could not create raster output %1: %2" ).arg( outputFile, provider->error().message( QgsErrorMessage::Text ) ) );
97 
98  provider->setNoDataValue( 1, -9999 );
99 
100  std::vector<float> line( cols );
101  std::fill( line.begin(), line.end(), value );
102  QgsRasterBlock block( Qgis::Float32, cols, 1 );
103  block.setData( QByteArray::fromRawData( ( char * )&line[0], QgsRasterBlock::typeSize( Qgis::Float32 ) * cols ) );
104 
105  double step = rows > 0 ? 100.0 / rows : 1;
106 
107  for ( int i = 0; i < rows ; i++ )
108  {
109  if ( feedback->isCanceled() )
110  {
111  break;
112  }
113 
114  provider->writeBlock( &block, 1, 0, i );
115  feedback->setProgress( i * step );
116  }
117 
118  QVariantMap outputs;
119  outputs.insert( QStringLiteral( "OUTPUT" ), outputFile );
120  return outputs;
121 }
122 
A rectangle specified with double values.
Definition: qgsrectangle.h:41
Base class for providing feedback from a processing algorithm.
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:64
Thirty two bit floating point (float)
Definition: qgis.h:109
const QgsCoordinateReferenceSystem & crs
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
static QString driverForExtension(const QString &extension)
Returns the GDAL driver name for a specified file extension.
Raster data container.
double width() const
Returns the width of the rectangle.
Definition: qgsrectangle.h:202
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
static int typeSize(int dataType)
A coordinate reference system parameter for processing algorithms.
A rectangular map extent parameter for processing algorithms.
A numeric parameter for processing algorithms.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:55
This class represents a coordinate reference system (CRS).
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:167
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:172
Contains information about the context in which a processing algorithm is executed.
double height() const
Returns the height of the rectangle.
Definition: qgsrectangle.h:209