QGIS API Documentation 3.41.0-Master (652554233d6)
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#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
49 std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster( Qgis::DataType::Float32, mNumColumns, mNumRows, mInterpolationExtent, crs ) );
50 if ( !provider )
51 {
52 QgsDebugMsgLevel( QStringLiteral( "Could not create raster output: %1" ).arg( mOutputFilePath ), 2 );
53 return 1;
54 }
55 if ( !provider->isValid() )
56 {
57 QgsDebugMsgLevel( QStringLiteral( "Could not create raster output: %1: %2" ).arg( mOutputFilePath, provider->error().message( QgsErrorMessage::Text ) ), 2 );
58 return 2;
59 }
60
61 provider->setNoDataValue( 1, -9999 );
62
63 double currentYValue = mInterpolationExtent.yMaximum() - mCellSizeY / 2.0; //calculate value in the center of the cell
64 double currentXValue;
65 double interpolatedValue;
66
67 std::vector<float> float32Row( mNumColumns );
68 const double step = mNumRows > 0 ? 100.0 / mNumRows : 1;
69 for ( int row = 0; row < mNumRows; row++ )
70 {
71 if ( feedback && feedback->isCanceled() )
72 {
73 break;
74 }
75
76 currentXValue = mInterpolationExtent.xMinimum() + mCellSizeX / 2.0; //calculate value in the center of the cell
77 QgsRasterBlock block( Qgis::DataType::Float32, mNumColumns, 1 );
78
79 for ( int col = 0; col < mNumColumns; col++ )
80 {
81 if ( mInterpolator->interpolatePoint( currentXValue, currentYValue, interpolatedValue, feedback ) == 0 )
82 {
83 float32Row[col] = interpolatedValue;
84 }
85 else
86 {
87 float32Row[col] = -9999;
88 }
89 currentXValue += mCellSizeX;
90 }
91 block.setData( QByteArray( reinterpret_cast<const char *>( float32Row.data() ), QgsRasterBlock::typeSize( Qgis::DataType::Float32 ) * mNumColumns ) );
92 provider->writeBlock( &block, 1, 0, row );
93 currentYValue -= mCellSizeY;
94 if ( feedback )
95 {
96 feedback->setProgress( row * step );
97 }
98 }
99
100 return 0;
101}
@ 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
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.