QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsalignraster.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalignraster.h
3  --------------------------------------
4  Date : June 2015
5  Copyright : (C) 2015 by Martin Dobias
6  Email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSALIGNRASTER_H
17 #define QGSALIGNRASTER_H
18 
19 #include <QList>
20 #include <QPointF>
21 #include <QSizeF>
22 #include <QString>
23 #include <gdal_version.h>
24 #include "qgis_analysis.h"
25 #include "qgis_sip.h"
26 #include "qgsogrutils.h"
27 
29 
30 typedef void *GDALDatasetH SIP_SKIP;
31 
32 
44 class ANALYSIS_EXPORT QgsAlignRaster
45 {
46 #ifdef SIP_RUN
47 #include <gdal_version.h>
48 #endif
49 
50  public:
52 
54  struct ANALYSIS_EXPORT RasterInfo
55  {
56  public:
58  RasterInfo( const QString &layerpath );
59 
60  RasterInfo( const RasterInfo &rh ) = delete;
61  RasterInfo &operator=( const RasterInfo &rh ) = delete;
62 
64  bool isValid() const { return nullptr != mDataset; }
65 
67  QString crs() const { return mCrsWkt; }
69  QSize rasterSize() const { return QSize( mXSize, mYSize ); }
71  int bandCount() const { return mBandCnt; }
73  QSizeF cellSize() const;
75  QPointF gridOffset() const;
77  QgsRectangle extent() const;
79  QPointF origin() const;
80 
82  void dump() const;
83 
85  double identify( double mx, double my );
86 
87  protected:
91  QString mCrsWkt;
93  double mGeoTransform[6];
95  int mXSize = 0;
97  int mYSize = 0;
99  int mBandCnt = 0;
100 
101  private:
102 #ifdef SIP_RUN
104 #endif
105 
106  friend class QgsAlignRaster;
107  };
108 
109 
115  {
118  RA_Cubic = 2,
122  RA_Mode = 6,
123  RA_Max = 8,
124  RA_Min = 9,
125  RA_Median = 10,
126  RA_Q1 = 11,
127  RA_Q3 = 12,
128  };
129 
131  struct Item
132  {
133  Item( const QString &input, const QString &output )
134  : inputFilename( input )
135  , outputFilename( output )
136  , resampleMethod( RA_NearestNeighbour )
137  , rescaleValues( false )
138  , srcCellSizeInDestCRS( 0.0 )
139  {}
140 
142  QString inputFilename;
144  QString outputFilename;
149 
150  // private part
151 
154  };
155  typedef QList<QgsAlignRaster::Item> List;
156 
159  {
160 
166  virtual bool progress( double complete ) = 0;
167 
168  virtual ~ProgressHandler() = default;
169  };
170 
172  void setProgressHandler( ProgressHandler *progressHandler ) { mProgressHandler = progressHandler; }
174  ProgressHandler *progressHandler() const { return mProgressHandler; }
175 
177  void setRasters( const List &list ) { mRasters = list; }
179  List rasters() const { return mRasters; }
180 
181  void setGridOffset( QPointF offset ) { mGridOffsetX = offset.x(); mGridOffsetY = offset.y(); }
182  QPointF gridOffset() const { return QPointF( mGridOffsetX, mGridOffsetY ); }
183 
185  void setCellSize( double x, double y ) { setCellSize( QSizeF( x, y ) ); }
187  void setCellSize( QSizeF size ) { mCellSizeX = size.width(); mCellSizeY = size.height(); }
189  QSizeF cellSize() const { return QSizeF( mCellSizeX, mCellSizeY ); }
190 
192  void setDestinationCrs( const QString &crsWkt ) { mCrsWkt = crsWkt; }
194  QString destinationCrs() const { return mCrsWkt; }
195 
200  void setClipExtent( double xmin, double ymin, double xmax, double ymax );
201 
206  void setClipExtent( const QgsRectangle &extent );
207 
212  QgsRectangle clipExtent() const;
213 
225  bool setParametersFromRaster( const RasterInfo &rasterInfo, const QString &customCRSWkt = QString(), QSizeF customCellSize = QSizeF(), QPointF customGridOffset = QPointF( -1, -1 ) );
226 
231  bool setParametersFromRaster( const QString &filename, const QString &customCRSWkt = QString(), QSizeF customCellSize = QSizeF(), QPointF customGridOffset = QPointF( -1, -1 ) );
232 
237  bool checkInputParameters();
238 
243  QSize alignedRasterSize() const;
244 
249  QgsRectangle alignedRasterExtent() const;
250 
255  bool run();
256 
261  QString errorMessage() const { return mErrorMessage; }
262 
264  void dump() const;
265 
267  int suggestedReferenceLayer() const;
268 
269  protected:
270 
272  bool createAndWarp( const Item &raster );
273 
275  static bool suggestedWarpOutput( const RasterInfo &info, const QString &destWkt, QSizeF *cellSize = nullptr, QPointF *gridOffset = nullptr, QgsRectangle *rect = nullptr );
276 
277  protected:
278 
279  // set by the client
280 
282  ProgressHandler *mProgressHandler = nullptr;
283 
285  QString mErrorMessage;
286 
289 
291  QString mCrsWkt;
293  double mCellSizeX, mCellSizeY;
295  double mGridOffsetX, mGridOffsetY;
296 
301  double mClipExtent[4];
302 
303  // derived data from other members
304 
306  double mGeoTransform[6];
308  int mXSize;
309 
311  int mYSize;
312 
313 };
314 
315 
316 #endif // QGSALIGNRASTER_H
QgsAlignRaster::gridOffset
QPointF gridOffset() const
Definition: qgsalignraster.h:182
QgsAlignRaster::RasterInfo::rasterSize
QSize rasterSize() const
Returns the size of the raster grid in pixels.
Definition: qgsalignraster.h:69
QgsAlignRaster::RA_Lanczos
@ RA_Lanczos
Lanczos windowed sinc interpolation (6x6 kernel)
Definition: qgsalignraster.h:120
QgsAlignRaster::cellSize
QSizeF cellSize() const
Gets output cell size.
Definition: qgsalignraster.h:189
QgsAlignRaster
QgsAlignRaster takes one or more raster layers and warps (resamples) them so they have the same:
Definition: qgsalignraster.h:44
QgsAlignRaster::Item::inputFilename
QString inputFilename
filename of the source raster
Definition: qgsalignraster.h:142
QgsAlignRaster::RA_Bilinear
@ RA_Bilinear
Bilinear (2x2 kernel)
Definition: qgsalignraster.h:117
QgsAlignRaster::RasterInfo
Utility class for gathering information about rasters.
Definition: qgsalignraster.h:54
QgsAlignRaster::RasterInfo::mDataset
gdal::dataset_unique_ptr mDataset
handle to open GDAL dataset
Definition: qgsalignraster.h:89
QgsAlignRaster::mCellSizeY
double mCellSizeY
Definition: qgsalignraster.h:293
QgsAlignRaster::errorMessage
QString errorMessage() const
Returns the error from a previous run() call.
Definition: qgsalignraster.h:261
QgsAlignRaster::RasterInfo::bandCount
int bandCount() const
Returns the number of raster bands in the file.
Definition: qgsalignraster.h:71
QgsAlignRaster::Item::rescaleValues
bool rescaleValues
rescaling of values according to the change of pixel size
Definition: qgsalignraster.h:148
qgsogrutils.h
QgsAlignRaster::mRasters
List mRasters
List of rasters to be aligned (with their output files and other options)
Definition: qgsalignraster.h:288
QgsAlignRaster::mGridOffsetY
double mGridOffsetY
Definition: qgsalignraster.h:295
QgsRectangle
Definition: qgsrectangle.h:41
QgsAlignRaster::setCellSize
void setCellSize(QSizeF size)
Sets output cell size.
Definition: qgsalignraster.h:187
QgsAlignRaster::RA_NearestNeighbour
@ RA_NearestNeighbour
Nearest neighbour (select on one input pixel)
Definition: qgsalignraster.h:116
QgsAlignRaster::mXSize
int mXSize
Computed raster grid width.
Definition: qgsalignraster.h:308
QgsAlignRaster::setGridOffset
void setGridOffset(QPointF offset)
Definition: qgsalignraster.h:181
QgsAlignRaster::RasterInfo::crs
QString crs() const
Returns the CRS in WKT format.
Definition: qgsalignraster.h:67
QgsAlignRaster::setDestinationCrs
void setDestinationCrs(const QString &crsWkt)
Sets the output CRS in WKT format.
Definition: qgsalignraster.h:192
QgsAlignRaster::RA_Q3
@ RA_Q3
Third quartile (selects the third quartile of all non-NODATA contributing pixels)
Definition: qgsalignraster.h:127
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsAlignRaster::setCellSize
void setCellSize(double x, double y)
Sets output cell size.
Definition: qgsalignraster.h:185
QgsAlignRaster::RA_Mode
@ RA_Mode
Mode (selects the value which appears most often of all the sampled points)
Definition: qgsalignraster.h:122
gdal::dataset_unique_ptr
std::unique_ptr< std::remove_pointer< GDALDatasetH >::type, GDALDatasetCloser > dataset_unique_ptr
Scoped GDAL dataset.
Definition: qgsogrutils.h:134
QgsAlignRaster::destinationCrs
QString destinationCrs() const
Gets the output CRS in WKT format.
Definition: qgsalignraster.h:194
QgsAlignRaster::RA_Median
@ RA_Median
Median (selects the median of all non-NODATA contributing pixels)
Definition: qgsalignraster.h:125
QgsAlignRaster::RA_Cubic
@ RA_Cubic
Cubic Convolution Approximation (4x4 kernel)
Definition: qgsalignraster.h:118
QgsAlignRaster::RA_Max
@ RA_Max
Maximum (selects the maximum of all non-NODATA contributing pixels)
Definition: qgsalignraster.h:123
qgis_sip.h
QgsAlignRaster::Item::outputFilename
QString outputFilename
filename of the newly created aligned raster (will be overwritten if exists already)
Definition: qgsalignraster.h:144
QgsAlignRaster::mYSize
int mYSize
Computed raster grid height.
Definition: qgsalignraster.h:311
QgsAlignRaster::Item::srcCellSizeInDestCRS
double srcCellSizeInDestCRS
used for rescaling of values (if necessary)
Definition: qgsalignraster.h:153
QgsAlignRaster::ProgressHandler
Helper struct to be sub-classed for progress reporting.
Definition: qgsalignraster.h:158
QgsAlignRaster::setRasters
void setRasters(const List &list)
Sets list of rasters that will be aligned.
Definition: qgsalignraster.h:177
QgsAlignRaster::List
QList< QgsAlignRaster::Item > List
Definition: qgsalignraster.h:155
QgsAlignRaster::mErrorMessage
QString mErrorMessage
Last error message from run()
Definition: qgsalignraster.h:285
QgsAlignRaster::RasterInfo::mCrsWkt
QString mCrsWkt
CRS stored in WKT format.
Definition: qgsalignraster.h:91
QgsAlignRaster::Item::Item
Item(const QString &input, const QString &output)
Definition: qgsalignraster.h:133
QgsAlignRaster::Item
Definition of one raster layer for alignment.
Definition: qgsalignraster.h:131
QgsAlignRaster::RA_Min
@ RA_Min
Minimum (selects the minimum of all non-NODATA contributing pixels)
Definition: qgsalignraster.h:124
QgsAlignRaster::RA_Average
@ RA_Average
Average (computes the average of all non-NODATA contributing pixels)
Definition: qgsalignraster.h:121
QgsAlignRaster::RA_Q1
@ RA_Q1
First quartile (selects the first quartile of all non-NODATA contributing pixels)
Definition: qgsalignraster.h:126
QgsAlignRaster::ResampleAlg
ResampleAlg
Resampling algorithm to be used (equivalent to GDAL's enum GDALResampleAlg)
Definition: qgsalignraster.h:114
QgsAlignRaster::Item::resampleMethod
QgsAlignRaster::ResampleAlg resampleMethod
resampling method to be used
Definition: qgsalignraster.h:146
QgsAlignRaster::mCrsWkt
QString mCrsWkt
Destination CRS - stored in well-known text (WKT) format.
Definition: qgsalignraster.h:291
QgsAlignRaster::RA_CubicSpline
@ RA_CubicSpline
Cubic B-Spline Approximation (4x4 kernel)
Definition: qgsalignraster.h:119
QgsAlignRaster::progressHandler
ProgressHandler * progressHandler() const
Gets associated progress handler. May be nullptr (default)
Definition: qgsalignraster.h:174
QgsAlignRaster::setProgressHandler
void setProgressHandler(ProgressHandler *progressHandler)
Assign a progress handler instance. Does not take ownership. nullptr can be passed.
Definition: qgsalignraster.h:172
QgsAlignRaster::RasterInfo::isValid
bool isValid() const
Check whether the given path is a valid raster.
Definition: qgsalignraster.h:64
QgsAlignRaster::rasters
List rasters() const
Gets list of rasters that will be aligned.
Definition: qgsalignraster.h:179
GDALDatasetH
void * GDALDatasetH
Definition: qgsalignraster.h:28