/
qgsrasterdataprovider.h
590 lines (484 loc) · 22.1 KB
/
qgsrasterdataprovider.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/***************************************************************************
qgsrasterdataprovider.h - DataProvider Interface for raster layers
--------------------------------------
Date : Mar 11, 2005
Copyright : (C) 2005 by Brendan Morley
email : morb at ozemail dot com dot au
async legend fetcher : Sandro Santilli < strk at keybit dot net >
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/* Thank you to Marco Hugentobler for the original vector DataProvider */
#ifndef QGSRASTERDATAPROVIDER_H
#define QGSRASTERDATAPROVIDER_H
#include "qgis_core.h"
#include "qgis_sip.h"
#include <cmath>
#include <QDateTime>
#include <QVariant>
#include <QImage>
#include "qgscolorrampshader.h"
#include "qgsdataprovider.h"
#include "qgsfields.h"
#include "qgsraster.h"
#include "qgsrasterinterface.h"
#include "qgsrasterpyramid.h"
#include "qgsrasterrange.h"
#include "qgsrectangle.h"
#include "qgsrasteriterator.h"
class QImage;
class QByteArray;
class QgsPointXY;
class QgsRasterIdentifyResult;
class QgsMapSettings;
/**
* \brief Handles asynchronous download of images
* \ingroup core
* \since QGIS 2.8
*/
class CORE_EXPORT QgsImageFetcher : public QObject
{
Q_OBJECT
public:
//! Constructor
QgsImageFetcher( QObject *parent = nullptr ) : QObject( parent ) {}
/**
* Starts the image download
* \note Make sure to connect to "finish" and "error" before starting */
virtual void start() = 0;
signals:
/**
* Emitted when the download completes
* \param legend The downloaded legend image */
void finish( const QImage &legend );
//! Emitted to report progress
void progress( qint64 received, qint64 total );
//! Emitted when an error occurs
void error( const QString &msg );
};
/**
* \ingroup core
* Base class for raster data providers.
*/
class CORE_EXPORT QgsRasterDataProvider : public QgsDataProvider, public QgsRasterInterface
{
Q_OBJECT
public:
/**
* Enumeration with capabilities that raster providers might implement.
* \since QGIS 3.0
*/
enum ProviderCapability
{
NoProviderCapabilities = 0, //!< Provider has no capabilities
ReadLayerMetadata = 1 << 1, //!< Provider can read layer metadata from data store. Since QGIS 3.0. See QgsDataProvider::layerMetadata()
WriteLayerMetadata = 1 << 2, //!< Provider can write layer metadata to the data store. Since QGIS 3.0. See QgsDataProvider::writeLayerMetadata()
};
//! Provider capabilities
Q_DECLARE_FLAGS( ProviderCapabilities, ProviderCapability )
QgsRasterDataProvider();
/**
* Constructor for QgsRasterDataProvider.
*
* The \a uri argument gives a provider-specific uri indicating the underlying data
* source and it's parameters.
*
* The \a options argument specifies generic provider options.
*/
QgsRasterDataProvider( const QString &uri, const QgsDataProvider::ProviderOptions &options = QgsDataProvider::ProviderOptions() );
QgsRasterInterface *clone() const override = 0;
/**
* Returns flags containing the supported capabilities of the data provider.
* \since QGIS 3.0
*/
virtual QgsRasterDataProvider::ProviderCapabilities providerCapabilities() const;
/* It makes no sense to set input on provider */
bool setInput( QgsRasterInterface *input ) override { Q_UNUSED( input ); return false; }
QgsRectangle extent() const override = 0;
//! Returns data type for the band specified by number
Qgis::DataType dataType( int bandNo ) const override = 0;
/**
* Returns source data type for the band specified by number,
* source data type may be shorter than dataType
*/
Qgis::DataType sourceDataType( int bandNo ) const override = 0;
//! Returns data type for the band specified by number
virtual int colorInterpretation( int bandNo ) const
{
Q_UNUSED( bandNo );
return QgsRaster::UndefinedColorInterpretation;
}
QString colorName( int colorInterpretation ) const
{
// Modified copy from GDAL
switch ( colorInterpretation )
{
case QgsRaster::UndefinedColorInterpretation:
return QStringLiteral( "Undefined" );
case QgsRaster::GrayIndex:
return QStringLiteral( "Gray" );
case QgsRaster::PaletteIndex:
return QStringLiteral( "Palette" );
case QgsRaster::RedBand:
return QStringLiteral( "Red" );
case QgsRaster::GreenBand:
return QStringLiteral( "Green" );
case QgsRaster::BlueBand:
return QStringLiteral( "Blue" );
case QgsRaster::AlphaBand:
return QStringLiteral( "Alpha" );
case QgsRaster::HueBand:
return QStringLiteral( "Hue" );
case QgsRaster::SaturationBand:
return QStringLiteral( "Saturation" );
case QgsRaster::LightnessBand:
return QStringLiteral( "Lightness" );
case QgsRaster::CyanBand:
return QStringLiteral( "Cyan" );
case QgsRaster::MagentaBand:
return QStringLiteral( "Magenta" );
case QgsRaster::YellowBand:
return QStringLiteral( "Yellow" );
case QgsRaster::BlackBand:
return QStringLiteral( "Black" );
case QgsRaster::YCbCr_YBand:
return QStringLiteral( "YCbCr_Y" );
case QgsRaster::YCbCr_CbBand:
return QStringLiteral( "YCbCr_Cb" );
case QgsRaster::YCbCr_CrBand:
return QStringLiteral( "YCbCr_Cr" );
default:
return QStringLiteral( "Unknown" );
}
}
//! Reload data (data could change)
virtual bool reload() { return true; }
virtual QString colorInterpretationName( int bandNo ) const
{
return colorName( colorInterpretation( bandNo ) );
}
/**
* Read band scale for raster value
* \since QGIS 2.3
*/
virtual double bandScale( int bandNo ) const { Q_UNUSED( bandNo ); return 1.0; }
/**
* Read band offset for raster value
* \since QGIS 2.3
*/
virtual double bandOffset( int bandNo ) const { Q_UNUSED( bandNo ); return 0.0; }
// TODO: remove or make protected all readBlock working with void*
//! Read block of data using given extent and size.
QgsRasterBlock *block( int bandNo, const QgsRectangle &boundingBox, int width, int height, QgsRasterBlockFeedback *feedback = nullptr ) override;
//! Returns true if source band has no data value
virtual bool sourceHasNoDataValue( int bandNo ) const { return mSrcHasNoDataValue.value( bandNo - 1 ); }
//! Returns the source nodata value usage
virtual bool useSourceNoDataValue( int bandNo ) const { return mUseSrcNoDataValue.value( bandNo - 1 ); }
//! Sets the source nodata value usage
virtual void setUseSourceNoDataValue( int bandNo, bool use );
//! Value representing no data value.
virtual double sourceNoDataValue( int bandNo ) const { return mSrcNoDataValue.value( bandNo - 1 ); }
virtual void setUserNoDataValue( int bandNo, const QgsRasterRangeList &noData );
//! Returns a list of user no data value ranges.
virtual QgsRasterRangeList userNoDataValues( int bandNo ) const { return mUserNoDataValue.value( bandNo - 1 ); }
virtual QList<QgsColorRampShader::ColorRampItem> colorTable( int bandNo ) const
{ Q_UNUSED( bandNo ); return QList<QgsColorRampShader::ColorRampItem>(); }
/**
* \brief Returns the sublayers of this layer - useful for providers that manage
* their own layers, such as WMS */
QStringList subLayers() const override
{
return QStringList();
}
//! \brief Returns whether the provider supplies a legend graphic
virtual bool supportsLegendGraphic() const { return false; }
/**
* Returns the legend rendered as pixmap
*
* This is useful for layers which need to get legend layers remotely as WMS.
*
* \param scale Optional parameter that is the Scale of the layer
* \param forceRefresh Optional bool parameter to force refresh getLegendGraphic call
* \param visibleExtent Visible extent for providers supporting contextual legends, in layer CRS
* \note Parameter visibleExtent added in QGIS 2.8
* \note Not available in Python bindings
*/
virtual QImage getLegendGraphic( double scale = 0, bool forceRefresh = false, const QgsRectangle *visibleExtent = nullptr ) SIP_SKIP
{
Q_UNUSED( scale );
Q_UNUSED( forceRefresh );
Q_UNUSED( visibleExtent );
return QImage();
}
/**
* Returns a new image downloader for the raster legend.
*
* \param mapSettings map settings for legend providers supporting
* contextual legends.
*
* \returns a download handler or null if the provider does not support
* legend at all. Ownership of the returned object is transferred
* to caller.
*
*
* \since QGIS 2.8
*/
virtual QgsImageFetcher *getLegendGraphicFetcher( const QgsMapSettings *mapSettings ) SIP_FACTORY
{
Q_UNUSED( mapSettings );
return nullptr;
}
//! \brief Create pyramid overviews
virtual QString buildPyramids( const QList<QgsRasterPyramid> &pyramidList,
const QString &resamplingMethod = "NEAREST",
QgsRaster::RasterPyramidsFormat format = QgsRaster::PyramidsGTiff,
const QStringList &configOptions = QStringList(),
QgsRasterBlockFeedback *feedback = nullptr )
{
Q_UNUSED( pyramidList );
Q_UNUSED( resamplingMethod );
Q_UNUSED( format );
Q_UNUSED( configOptions );
Q_UNUSED( feedback );
return QStringLiteral( "FAILED_NOT_SUPPORTED" );
}
/**
* Returns the raster layers pyramid list.
* \param overviewList used to construct the pyramid list (optional), when empty the list is defined by the provider.
* A pyramid list defines the
* POTENTIAL pyramids that can be in a raster. To know which of the pyramid layers
* ACTUALLY exists you need to look at the existsFlag member in each struct stored in the
* list.
*/
virtual QList<QgsRasterPyramid> buildPyramidList( QList<int> overviewList = QList<int>() ) // clazy:exclude=function-args-by-ref
{ Q_UNUSED( overviewList ); return QList<QgsRasterPyramid>(); }
//! \brief Returns true if raster has at least one populated histogram.
bool hasPyramids();
/**
* Returns metadata in a format suitable for feeding directly
* into a subset of the GUI raster properties "Metadata" tab.
*/
virtual QString htmlMetadata() = 0;
/**
* Identify raster value(s) found on the point position. The context
* parameters extent, width and height are important to identify
* on the same zoom level as a displayed map and to do effective
* caching (WCS). If context params are not specified the highest
* resolution is used. capabilities() may be used to test if format
* is supported by provider. Values are set to 'no data' or empty string
* if point is outside data source extent.
*
* \param point coordinates in data source CRS
* \param format result format
* \param boundingBox context bounding box
* \param width context width
* \param height context height
* \param dpi context dpi
* \return QgsRaster::IdentifyFormatValue: map of values for each band, keys are band numbers
* (from 1).
* QgsRaster::IdentifyFormatFeature: map of QgsRasterFeatureList for each sublayer
* (WMS) - TODO: it is not consistent with QgsRaster::IdentifyFormatValue.
* QgsRaster::IdentifyFormatHtml: map of HTML strings for each sublayer (WMS).
* Empty if failed or there are no results (TODO: better error reporting).
* \note The arbitraryness of the returned document is enforced by WMS standards
* up to at least v1.3.0
* \see sample(), which is much more efficient for simple "value at point" queries.
*/
virtual QgsRasterIdentifyResult identify( const QgsPointXY &point, QgsRaster::IdentifyFormat format, const QgsRectangle &boundingBox = QgsRectangle(), int width = 0, int height = 0, int dpi = 96 );
/**
* Samples a raster value from the specified \a band found at the \a point position. The context
* parameters \a boundingBox, \a width and \a height are important to identify
* on the same zoom level as a displayed map and to do effective
* caching (WCS). If context params are not specified the highest
* resolution is used. capabilities() may be used to test if format
* is supported by provider.
*
* A null QVariant will be returned if the point is outside data source extent, or an invalid
* band number was specified.
*
* \see identify(), which is much more flexible but considerably less efficient.
* \since QGIS 3.4
*/
virtual QVariant sample( const QgsPointXY &point, int band, const QgsRectangle &boundingBox = QgsRectangle(), int width = 0, int height = 0, int dpi = 96 );
/**
* \brief Returns the caption error text for the last error in this provider
*
* If an operation returns 0 (e.g. draw()), this function
* returns the text of the error associated with the failure.
* Interactive users of this provider can then, for example,
* call a QMessageBox to display the contents.
*/
virtual QString lastErrorTitle() = 0;
/**
* \brief Returns the verbose error text for the last error in this provider
*
* If an operation returns 0 (e.g. draw()), this function
* returns the text of the error associated with the failure.
* Interactive users of this provider can then, for example,
* call a QMessageBox to display the contents.
*
*/
virtual QString lastError() = 0;
//! Returns the format of the error text for the last error in this provider
virtual QString lastErrorFormat();
//! Returns the dpi of the output device.
int dpi() const { return mDpi; }
//! Sets the output device resolution.
void setDpi( int dpi ) { mDpi = dpi; }
//! Time stamp of data source in the moment when data/metadata were loaded by provider
QDateTime timestamp() const override { return mTimestamp; }
//! Current time stamp of data source
QDateTime dataTimestamp() const override { return QDateTime(); }
/**
* Checks whether the provider is in editing mode, i.e. raster write operations will be accepted.
* By default providers are not editable. Use setEditable() method to enable/disable editing.
* \see setEditable(), writeBlock()
* \since QGIS 3.0
*/
virtual bool isEditable() const { return false; }
/**
* Turns on/off editing mode of the provider. When in editing mode, it is possible
* to overwrite data of the provider using writeBlock() calls.
* \returns true if the switch to/from editing mode was successful
* \note Only some providers support editing mode and even those may fail to turn
* the underlying data source into editing mode, so it is necessary to check the return
* value whether the operation was successful.
* \see isEditable(), writeBlock()
* \since QGIS 3.0
*/
virtual bool setEditable( bool enabled ) { Q_UNUSED( enabled ); return false; }
//! Writes into the provider datasource
// TODO: add data type (may be different from band type)
virtual bool write( void *data, int band, int width, int height, int xOffset, int yOffset )
{
Q_UNUSED( data );
Q_UNUSED( band );
Q_UNUSED( width );
Q_UNUSED( height );
Q_UNUSED( xOffset );
Q_UNUSED( yOffset );
return false;
}
/**
* Writes pixel data from a raster block into the provider data source.
*
* This will override previously stored pixel values. It is assumed that cells in the passed
* raster block are aligned with the cells of the data source. If raster block does not cover
* the whole area of the data source, only a subset of pixels covered by the raster block
* will be overwritten. By default, writing of raster data starts from the first cell
* of the raster - it is possible to set offset in pixels by specifying non-zero
* xOffset and yOffset values.
*
* Writing is supported only by some data providers. Provider has to be in editing mode
* in order to allow write operations.
* \see isEditable(), setEditable()
* \returns true on success
* \since QGIS 3.0
*/
bool writeBlock( QgsRasterBlock *block, int band, int xOffset = 0, int yOffset = 0 );
//! Creates a new dataset with mDataSourceURI
static QgsRasterDataProvider *create( const QString &providerKey,
const QString &uri,
const QString &format, int nBands,
Qgis::DataType type,
int width, int height, double *geoTransform,
const QgsCoordinateReferenceSystem &crs,
const QStringList &createOptions = QStringList() );
/**
* Set no data value on created dataset
* \param bandNo band number
* \param noDataValue no data value
*/
virtual bool setNoDataValue( int bandNo, double noDataValue ) { Q_UNUSED( bandNo ); Q_UNUSED( noDataValue ); return false; }
//! Remove dataset
virtual bool remove() { return false; }
/**
* Returns a list of pyramid resampling method name and label pairs
* for given provider
*/
static QList<QPair<QString, QString> > pyramidResamplingMethods( const QString &providerKey );
/**
* Validates creation options for a specific dataset and destination format.
* \note used by GDAL provider only
* \note see also validateCreationOptionsFormat() in gdal provider for validating options based on format only
*/
virtual QString validateCreationOptions( const QStringList &createOptions, const QString &format )
{ Q_UNUSED( createOptions ); Q_UNUSED( format ); return QString(); }
/**
* Validates pyramid creation options for a specific dataset and destination format
* \note used by GDAL provider only
*/
virtual QString validatePyramidsConfigOptions( QgsRaster::RasterPyramidsFormat pyramidsFormat,
const QStringList &configOptions, const QString &fileFormat )
{ Q_UNUSED( pyramidsFormat ); Q_UNUSED( configOptions ); Q_UNUSED( fileFormat ); return QString(); }
static QString identifyFormatName( QgsRaster::IdentifyFormat format );
static QgsRaster::IdentifyFormat identifyFormatFromName( const QString &formatName );
static QString identifyFormatLabel( QgsRaster::IdentifyFormat format );
static Capability identifyFormatToCapability( QgsRaster::IdentifyFormat format );
/**
* Step width for raster iterations.
* \see stepHeight()
* \since QGIS 3.0
*/
virtual int stepWidth() const { return QgsRasterIterator::DEFAULT_MAXIMUM_TILE_WIDTH; }
/**
* Step height for raster iterations.
* \see stepWidth()
* \since QGIS 3.0
*/
virtual int stepHeight() const { return QgsRasterIterator::DEFAULT_MAXIMUM_TILE_HEIGHT; }
signals:
/**
* Emit a message to be displayed on status bar, usually used by network providers (WMS,WCS)
* \since QGIS 2.14
*/
void statusChanged( const QString & ) const;
protected:
/**
* Read block of data
* \note not available in Python bindings
*/
virtual void readBlock( int bandNo, int xBlock, int yBlock, void *data ) SIP_SKIP
{ Q_UNUSED( bandNo ); Q_UNUSED( xBlock ); Q_UNUSED( yBlock ); Q_UNUSED( data ); }
/**
* Read block of data using give extent and size
* \note not available in Python bindings
*/
virtual void readBlock( int bandNo, QgsRectangle const &viewExtent, int width, int height, void *data, QgsRasterBlockFeedback *feedback = nullptr ) SIP_SKIP
{ Q_UNUSED( bandNo ); Q_UNUSED( viewExtent ); Q_UNUSED( width ); Q_UNUSED( height ); Q_UNUSED( data ); Q_UNUSED( feedback ); }
//! Returns true if user no data contains value
bool userNoDataValuesContains( int bandNo, double value ) const;
//! Copy member variables from other raster data provider. Useful for implementation of clone() method in subclasses
void copyBaseSettings( const QgsRasterDataProvider &other );
/**
* Dots per inch. Extended WMS (e.g. QGIS mapserver) support DPI dependent output and therefore
are suited for printing. A value of -1 means it has not been set */
int mDpi = -1;
/**
* Source no data value is available and is set to be used or internal no data
* is available. Used internally only */
//bool hasNoDataValue ( int bandNo );
//! \brief Cell value representing original source no data. e.g. -9999, indexed from 0
QList<double> mSrcNoDataValue;
//! \brief Source no data value exists.
QList<bool> mSrcHasNoDataValue;
/**
* \brief Use source nodata value. User can disable usage of source nodata
* value as nodata. It may happen that a value is wrongly given by GDAL
* as nodata (e.g. 0) and it has to be treated as regular value. */
QList<bool> mUseSrcNoDataValue;
/**
* \brief List of lists of user defined additional no data values
* for each band, indexed from 0 */
QList< QgsRasterRangeList > mUserNoDataValue;
mutable QgsRectangle mExtent;
};
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsRasterDataProvider::ProviderCapabilities )
// clazy:excludeall=qstring-allocations
#endif