QGIS API Documentation  3.6.0-Noosa (5873452)
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 
43 class ANALYSIS_EXPORT QgsAlignRaster
44 {
45 #ifdef SIP_RUN
46 #include <gdal_version.h>
47 #endif
48 
49  public:
51 
53  struct ANALYSIS_EXPORT RasterInfo
54  {
55  public:
57  RasterInfo( const QString &layerpath );
58 
59  RasterInfo( const RasterInfo &rh ) = delete;
60  RasterInfo &operator=( const RasterInfo &rh ) = delete;
61 
63  bool isValid() const { return nullptr != mDataset; }
64 
66  QString crs() const { return mCrsWkt; }
68  QSize rasterSize() const { return QSize( mXSize, mYSize ); }
70  int bandCount() const { return mBandCnt; }
72  QSizeF cellSize() const;
74  QPointF gridOffset() const;
76  QgsRectangle extent() const;
78  QPointF origin() const;
79 
81  void dump() const;
82 
84  double identify( double mx, double my );
85 
86  protected:
90  QString mCrsWkt;
92  double mGeoTransform[6];
94  int mXSize = 0;
96  int mYSize = 0;
98  int mBandCnt = 0;
99 
100  private:
101 #ifdef SIP_RUN
103 #endif
104 
105  friend class QgsAlignRaster;
106  };
107 
108 
114  {
117  RA_Cubic = 2,
121  RA_Mode = 6,
122  RA_Max = 8,
123  RA_Min = 9,
124  RA_Median = 10,
125  RA_Q1 = 11,
126  RA_Q3 = 12,
127  };
128 
130  struct Item
131  {
132  Item( const QString &input, const QString &output )
133  : inputFilename( input )
134  , outputFilename( output )
135  , resampleMethod( RA_NearestNeighbour )
136  , rescaleValues( false )
137  , srcCellSizeInDestCRS( 0.0 )
138  {}
139 
141  QString inputFilename;
143  QString outputFilename;
148 
149  // private part
150 
153  };
154  typedef QList<QgsAlignRaster::Item> List;
155 
158  {
159 
165  virtual bool progress( double complete ) = 0;
166 
167  virtual ~ProgressHandler() = default;
168  };
169 
171  void setProgressHandler( ProgressHandler *progressHandler ) { mProgressHandler = progressHandler; }
173  ProgressHandler *progressHandler() const { return mProgressHandler; }
174 
176  void setRasters( const List &list ) { mRasters = list; }
178  List rasters() const { return mRasters; }
179 
180  void setGridOffset( QPointF offset ) { mGridOffsetX = offset.x(); mGridOffsetY = offset.y(); }
181  QPointF gridOffset() const { return QPointF( mGridOffsetX, mGridOffsetY ); }
182 
184  void setCellSize( double x, double y ) { setCellSize( QSizeF( x, y ) ); }
186  void setCellSize( QSizeF size ) { mCellSizeX = size.width(); mCellSizeY = size.height(); }
188  QSizeF cellSize() const { return QSizeF( mCellSizeX, mCellSizeY ); }
189 
191  void setDestinationCrs( const QString &crsWkt ) { mCrsWkt = crsWkt; }
193  QString destinationCrs() const { return mCrsWkt; }
194 
199  void setClipExtent( double xmin, double ymin, double xmax, double ymax );
200 
205  void setClipExtent( const QgsRectangle &extent );
206 
211  QgsRectangle clipExtent() const;
212 
224  bool setParametersFromRaster( const RasterInfo &rasterInfo, const QString &customCRSWkt = QString(), QSizeF customCellSize = QSizeF(), QPointF customGridOffset = QPointF( -1, -1 ) );
225 
230  bool setParametersFromRaster( const QString &filename, const QString &customCRSWkt = QString(), QSizeF customCellSize = QSizeF(), QPointF customGridOffset = QPointF( -1, -1 ) );
231 
236  bool checkInputParameters();
237 
242  QSize alignedRasterSize() const;
243 
248  QgsRectangle alignedRasterExtent() const;
249 
254  bool run();
255 
260  QString errorMessage() const { return mErrorMessage; }
261 
263  void dump() const;
264 
266  int suggestedReferenceLayer() const;
267 
268  protected:
269 
271  bool createAndWarp( const Item &raster );
272 
274  static bool suggestedWarpOutput( const RasterInfo &info, const QString &destWkt, QSizeF *cellSize = nullptr, QPointF *gridOffset = nullptr, QgsRectangle *rect = nullptr );
275 
276  protected:
277 
278  // set by the client
279 
281  ProgressHandler *mProgressHandler = nullptr;
282 
284  QString mErrorMessage;
285 
287  List mRasters;
288 
290  QString mCrsWkt;
292  double mCellSizeX, mCellSizeY;
294  double mGridOffsetX, mGridOffsetY;
295 
300  double mClipExtent[4];
301 
302  // derived data from other members
303 
305  double mGeoTransform[6];
307  int mXSize;
308 
310  int mYSize;
311 
312 };
313 
314 
315 #endif // QGSALIGNRASTER_H
List rasters() const
Gets list of rasters that will be aligned.
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QString destinationCrs() const
Gets the output CRS in WKT format.
gdal::dataset_unique_ptr mDataset
handle to open GDAL dataset
bool rescaleValues
rescaling of values according to the change of pixel size
Nearest neighbour (select on one input pixel)
QSize rasterSize() const
Returns the size of the raster grid in pixels.
List mRasters
List of rasters to be aligned (with their output files and other options)
QString errorMessage() const
Returns the error from a previous run() call.
Bilinear (2x2 kernel)
Mode (selects the value which appears most often of all the sampled points)
QString outputFilename
filename of the newly created aligned raster (will be overwritten if exists already) ...
Median (selects the median of all non-NODATA contributing pixels)
Cubic Convolution Approximation (4x4 kernel)
int mYSize
Computed raster grid height.
int bandCount() const
Returns the number of raster bands in the file.
void setCellSize(QSizeF size)
Sets output cell size.
void setDestinationCrs(const QString &crsWkt)
Sets the output CRS in WKT format.
QString crs() const
Returns the CRS in WKT format.
void setRasters(const List &list)
Sets list of rasters that will be aligned.
#define SIP_SKIP
Definition: qgis_sip.h:119
Definition of one raster layer for alignment.
QgsAlignRaster takes one or more raster layers and warps (resamples) them so they have the same: ...
Helper struct to be sub-classed for progress reporting.
double srcCellSizeInDestCRS
used for rescaling of values (if necessary)
void setGridOffset(QPointF offset)
void * GDALDatasetH
Third quartile (selects the third quartile of all non-NODATA contributing pixels) ...
QString mCrsWkt
CRS stored in WKT format.
ResampleAlg
Resampling algorithm to be used (equivalent to GDAL&#39;s enum GDALResampleAlg)
ProgressHandler * progressHandler() const
Gets associated progress handler. May be nullptr (default)
Average (computes the average of all non-NODATA contributing pixels)
int mXSize
Computed raster grid width.
Maximum (selects the maximum of all non-NODATA contributing pixels)
QList< QgsAlignRaster::Item > List
Item(const QString &input, const QString &output)
First quartile (selects the first quartile of all non-NODATA contributing pixels) ...
void setProgressHandler(ProgressHandler *progressHandler)
Assign a progress handler instance. Does not take ownership. nullptr can be passed.
void setCellSize(double x, double y)
Sets output cell size.
bool isValid() const
Check whether the given path is a valid raster.
QString mErrorMessage
Last error message from run()
Cubic B-Spline Approximation (4x4 kernel)
std::unique_ptr< std::remove_pointer< GDALDatasetH >::type, GDALDatasetCloser > dataset_unique_ptr
Scoped GDAL dataset.
Definition: qgsogrutils.h:134
QgsAlignRaster::ResampleAlg resampleMethod
resampling method to be used
QString inputFilename
filename of the source raster
Lanczos windowed sinc interpolation (6x6 kernel)
Minimum (selects the minimum of all non-NODATA contributing pixels)
Utility class for gathering information about rasters.
QString mCrsWkt
Destination CRS - stored in well-known text (WKT) format.
QPointF gridOffset() const
QSizeF cellSize() const
Gets output cell size.