QGIS API Documentation 3.43.0-Master (3ee7834ace6)
qgsgridfilewriter.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgridfilewriter.cpp
3 ---------------------
4 begin : Marco 10, 2008
5 copyright : (C) 2008 by Marco Hugentobler
6 email : marco dot hugentobler at karto dot baug dot ethz dot ch
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
18#include "qgsgridfilewriter.h"
19#include "qgsinterpolator.h"
20#include "qgsvectorlayer.h"
21#include "qgsfeedback.h"
22#include "qgsrasterfilewriter.h"
24#include "qgsrasterblock.h"
25#include <QFileInfo>
26
27QgsGridFileWriter::QgsGridFileWriter( QgsInterpolator *i, const QString &outputPath, const QgsRectangle &extent, int nCols, int nRows )
28 : mInterpolator( i )
29 , mOutputFilePath( outputPath )
30 , mInterpolationExtent( extent )
31 , mNumColumns( nCols )
32 , mNumRows( nRows )
33 , mCellSizeX( extent.width() / nCols )
34 , mCellSizeY( extent.height() / nRows )
35{}
36
38{
39 const QFileInfo fi( mOutputFilePath );
40 const QString outputFormat = QgsRasterFileWriter::driverForExtension( fi.suffix() );
41
42 QgsInterpolator::LayerData ld = mInterpolator->layerData().at( 0 );
44
45 auto writer = std::make_unique<QgsRasterFileWriter>( mOutputFilePath );
46 writer->setOutputProviderKey( QStringLiteral( "gdal" ) );
47 writer->setOutputFormat( outputFormat );
48 writer->setCreateOptions( mCreateOptions );
49
50 std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster( Qgis::DataType::Float32, mNumColumns, mNumRows, mInterpolationExtent, crs ) );
51 if ( !provider )
52 {
53 QgsDebugMsgLevel( QStringLiteral( "Could not create raster output: %1" ).arg( mOutputFilePath ), 2 );
54 return 1;
55 }
56 if ( !provider->isValid() )
57 {
58 QgsDebugMsgLevel( QStringLiteral( "Could not create raster output: %1: %2" ).arg( mOutputFilePath, provider->error().message( QgsErrorMessage::Text ) ), 2 );
59 return 2;
60 }
61
62 provider->setNoDataValue( 1, mNoDataValue );
63
64 double currentYValue = mInterpolationExtent.yMaximum() - mCellSizeY / 2.0; //calculate value in the center of the cell
65 double currentXValue;
66 double interpolatedValue;
67
68 std::vector<float> float32Row( mNumColumns );
69 const double step = mNumRows > 0 ? 100.0 / mNumRows : 1;
70 for ( int row = 0; row < mNumRows; row++ )
71 {
72 if ( feedback && feedback->isCanceled() )
73 {
74 break;
75 }
76
77 currentXValue = mInterpolationExtent.xMinimum() + mCellSizeX / 2.0; //calculate value in the center of the cell
78 QgsRasterBlock block( Qgis::DataType::Float32, mNumColumns, 1 );
79
80 for ( int col = 0; col < mNumColumns; col++ )
81 {
82 if ( mInterpolator->interpolatePoint( currentXValue, currentYValue, interpolatedValue, feedback ) == 0 )
83 {
84 float32Row[col] = interpolatedValue;
85 }
86 else
87 {
88 float32Row[col] = mNoDataValue;
89 }
90 currentXValue += mCellSizeX;
91 }
92 block.setData( QByteArray( reinterpret_cast<const char *>( float32Row.data() ), QgsRasterBlock::typeSize( Qgis::DataType::Float32 ) * mNumColumns ) );
93 if ( !provider->writeBlock( &block, 1, 0, row ) )
94 {
95 throw QgsProcessingException( QObject::tr( "Could not write raster block: %1" ).arg( provider->error().summary() ) );
96 }
97 currentYValue -= mCellSizeY;
98 if ( feedback )
99 {
100 feedback->setProgress( row * step );
101 }
102 }
103
104 return 0;
105}
@ Float32
Thirty two bit floating point (float)
This class represents a coordinate reference system (CRS).
virtual QgsCoordinateReferenceSystem sourceCrs() const =0
Returns the coordinate reference system for features in the source.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:61
QgsGridFileWriter(QgsInterpolator *interpolator, const QString &outputPath, const QgsRectangle &extent, int nCols, int nRows)
Constructor for QgsGridFileWriter, for the specified interpolator.
int writeFile(QgsFeedback *feedback=nullptr)
Writes the grid file.
Interface class for interpolations.
virtual int interpolatePoint(double x, double y, double &result, QgsFeedback *feedback=nullptr)=0
Calculates interpolation value for map coordinates x, y.
QList< LayerData > layerData() const
Custom exception class for processing related exceptions.
Raster data container.
static int typeSize(Qgis::DataType dataType)
Returns the size in bytes for the specified dataType.
void setData(const QByteArray &data, int offset=0)
Rewrite raw pixel data.
static QString driverForExtension(const QString &extension)
Returns the GDAL driver name for a specified file extension.
A rectangle specified with double values.
double xMinimum
double yMaximum
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:41
const QgsCoordinateReferenceSystem & crs
A source together with the information about interpolation attribute / z-coordinate interpolation and...
QgsFeatureSource * source
Feature source.