QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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
20#include "qgsfeedback.h"
21#include "qgsinterpolator.h"
22#include "qgsrasterblock.h"
24#include "qgsrasterfilewriter.h"
25#include "qgsvectorlayer.h"
26
27#include <QFileInfo>
28#include <QString>
29
30using namespace Qt::StringLiterals;
31
32QgsGridFileWriter::QgsGridFileWriter( QgsInterpolator *i, const QString &outputPath, const QgsRectangle &extent, int nCols, int nRows, const QString &outputFormat )
33 : mInterpolator( i )
34 , mOutputFilePath( outputPath )
35 , mInterpolationExtent( extent )
36 , mNumColumns( nCols )
37 , mNumRows( nRows )
38 , mCellSizeX( extent.width() / nCols )
39 , mCellSizeY( extent.height() / nRows )
40 , mOutputFormat( outputFormat.isEmpty() ? QgsRasterFileWriter::driverForExtension( QFileInfo( mOutputFilePath ).suffix() ) : outputFormat )
41{}
42
44{
45 QgsInterpolator::LayerData ld = mInterpolator->layerData().at( 0 );
47
48 auto writer = std::make_unique<QgsRasterFileWriter>( mOutputFilePath );
49 writer->setOutputProviderKey( u"gdal"_s );
50 writer->setOutputFormat( mOutputFormat );
51 writer->setCreationOptions( mCreationOptions );
52
53 std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster( Qgis::DataType::Float32, mNumColumns, mNumRows, mInterpolationExtent, crs ) );
54 if ( !provider )
55 {
56 QgsDebugMsgLevel( u"Could not create raster output: %1"_s.arg( mOutputFilePath ), 2 );
57 return 1;
58 }
59 if ( !provider->isValid() )
60 {
61 QgsDebugMsgLevel( u"Could not create raster output: %1: %2"_s.arg( mOutputFilePath, provider->error().message( QgsErrorMessage::Text ) ), 2 );
62 return 2;
63 }
64
65 provider->setNoDataValue( 1, mNoDataValue );
66
67 double currentYValue = mInterpolationExtent.yMaximum() - mCellSizeY / 2.0; //calculate value in the center of the cell
68 double currentXValue;
69 double interpolatedValue;
70
71 std::vector<float> float32Row( mNumColumns );
72 const double step = mNumRows > 0 ? 100.0 / mNumRows : 1;
73 for ( int row = 0; row < mNumRows; row++ )
74 {
75 if ( feedback && feedback->isCanceled() )
76 {
77 break;
78 }
79
80 currentXValue = mInterpolationExtent.xMinimum() + mCellSizeX / 2.0; //calculate value in the center of the cell
81 QgsRasterBlock block( Qgis::DataType::Float32, mNumColumns, 1 );
82
83 for ( int col = 0; col < mNumColumns; col++ )
84 {
85 if ( mInterpolator->interpolatePoint( currentXValue, currentYValue, interpolatedValue, feedback ) == 0 )
86 {
87 float32Row[col] = interpolatedValue;
88 }
89 else
90 {
91 float32Row[col] = mNoDataValue;
92 }
93 currentXValue += mCellSizeX;
94 }
95 block.setData( QByteArray( reinterpret_cast<const char *>( float32Row.data() ), QgsRasterBlock::typeSize( Qgis::DataType::Float32 ) * mNumColumns ) );
96 if ( !provider->writeBlock( &block, 1, 0, row ) )
97 {
98 throw QgsProcessingException( QObject::tr( "Could not write raster block: %1" ).arg( provider->error().summary() ) );
99 }
100 currentYValue -= mCellSizeY;
101 if ( feedback )
102 {
103 feedback->setProgress( row * step );
104 }
105 }
106
107 return 0;
108}
@ Float32
Thirty two bit floating point (float).
Definition qgis.h:387
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:55
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:63
QgsGridFileWriter(QgsInterpolator *interpolator, const QString &outputPath, const QgsRectangle &extent, int nCols, int nRows, const QString &outputFormat=QString())
Constructor for QgsGridFileWriter, for the specified interpolator.
int writeFile(QgsFeedback *feedback=nullptr)
Writes the grid file.
Interface class for interpolations.
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.
The raster file writer which allows you to save a raster to a new file.
A rectangle specified with double values.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
A source together with the information about interpolation attribute / z-coordinate interpolation and...
QgsFeatureSource * source
Feature source.