QGIS API Documentation 4.3.0-Master (16eddb2670e)
Loading...
Searching...
No Matches
qgsrasterattributetable.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsrasterattributetable.h - QgsRasterAttributeTable
3
4 ---------------------
5 begin : 3.12.2021
6 copyright : (C) 2021 by Alessandro Pasotti
7 email : elpaso at itopen dot it
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16#ifndef QGSRASTERATTRIBUTETABLE_H
17#define QGSRASTERATTRIBUTETABLE_H
18
19#include "qgis.h"
20#include "qgis_core.h"
21#include "qgis_sip.h"
22#include "qgscolorrampimpl.h"
23#include "qgsfeature.h"
24#include "qgsfields.h"
25
26#include <QCoreApplication>
27#include <QLinearGradient>
28#include <QObject>
29
30class QgsRasterLayer;
33
43class CORE_EXPORT QgsRasterAttributeTable
44{
45 Q_DECLARE_TR_FUNCTIONS( QgsRasterAttributeTable )
46
47 public:
53 class CORE_EXPORT UsageInformation
54 {
55 public:
57 QString description;
58
60 bool unique = false;
61
63 bool required = false;
64
66 bool isColor = false;
67
69 bool isRamp = false;
70
72 bool supported = false;
73
75 bool maybeClass = false;
76
78 QList<QMetaType::Type> allowedTypes;
79 };
80
86 class CORE_EXPORT Field
87 {
88 public:
92 Field( const QString &name, const Qgis::RasterAttributeTableFieldUsage &usage, const QMetaType::Type type )
93 : name( name )
94 , usage( usage )
95 , type( type )
96 {}
97
102 Q_DECL_DEPRECATED Field( const QString &name, const Qgis::RasterAttributeTableFieldUsage &usage, const QVariant::Type type ) SIP_DEPRECATED
104 {}
105
109 bool isColor() const;
110
114 bool isRamp() const;
115
120 bool operator==( const Field &other ) const { return name == other.name && usage == other.usage && type == other.type; }
121
122 QString name;
124 QMetaType::Type type;
125 };
126
132 class CORE_EXPORT MinMaxClass
133 {
134 public:
135 QString name;
136
138 QVector< double > minMaxValues;
139 QColor color;
140 };
141
146
155 bool hasColor() const;
156
166 bool setColor( const int row, const QColor &color );
167
175 bool hasRamp() const;
176
185 bool setRamp( const int row, const QColor &colorMin, const QColor &colorMax );
186
190 QList<Qgis::RasterAttributeTableFieldUsage> usages() const SIP_SKIP;
191
193
197 QList<int> intUsages() const SIP_PYNAME( usages );
198
200
209 QColor color( int row ) const;
210
219 QgsGradientColorRamp ramp( int row ) const;
220
225 QList<QgsRasterAttributeTable::Field> fields() const;
226
231 QgsFields qgisFields() const;
232
237
241 bool isDirty() const;
242
246 void setDirty( bool isDirty );
247
251 bool isValid( QString *errorMessage SIP_OUT = nullptr ) const;
252
257 bool insertField( int position, const QgsRasterAttributeTable::Field &field, QString *errorMessage SIP_OUT = nullptr );
258
262 bool insertField( int position, const QString &name, const Qgis::RasterAttributeTableFieldUsage usage, const QMetaType::Type type, QString *errorMessage SIP_OUT = nullptr );
263
268 Q_DECL_DEPRECATED bool insertField( int position, const QString &name, const Qgis::RasterAttributeTableFieldUsage usage, const QVariant::Type type, QString *errorMessage SIP_OUT = nullptr )
270
274 bool insertColor( int position, QString *errorMessage SIP_OUT = nullptr );
275
280 bool setFieldUsage( int fieldIndex, const Qgis::RasterAttributeTableFieldUsage usage );
281
285 bool insertRamp( int position, QString *errorMessage SIP_OUT = nullptr );
286
290 bool appendField( const QString &name, const Qgis::RasterAttributeTableFieldUsage usage, const QMetaType::Type type, QString *errorMessage SIP_OUT = nullptr );
291
295 bool appendField( const QString &name, const Qgis::RasterAttributeTableFieldUsage usage, const QVariant::Type type, QString *errorMessage SIP_OUT = nullptr );
296
300 bool appendField( const QgsRasterAttributeTable::Field &field, QString *errorMessage SIP_OUT = nullptr );
301
305 bool removeField( const QString &name, QString *errorMessage SIP_OUT = nullptr );
306
311 bool insertRow( int position, const QVariantList &rowData, QString *errorMessage SIP_OUT = nullptr );
312
317 bool removeRow( int position = 0, QString *errorMessage SIP_OUT = nullptr );
318
322 bool appendRow( const QVariantList &data, QString *errorMessage SIP_OUT = nullptr );
323
328 bool writeToFile( const QString &path, QString *errorMessage SIP_OUT = nullptr );
329
333 bool readFromFile( const QString &path, QString *errorMessage SIP_OUT = nullptr );
334
338 const QList<QList<QVariant>> data() const;
339
346 const QgsRasterAttributeTable::Field fieldByName( const QString name, bool *ok SIP_OUT = nullptr ) const;
347
351 const QList<QgsRasterAttributeTable::Field> fieldsByUsage( const Qgis::RasterAttributeTableFieldUsage fieldUsage ) const;
352
357 bool setValue( const int row, const int column, const QVariant &value );
358
362 QVariant value( const int row, const int column ) const;
363
367 double minimumValue() const;
368
372 double maximumValue() const;
373
378 QVariantList row( const double matchValue ) const;
379
385 QList<QgsRasterAttributeTable::MinMaxClass> minMaxClasses( const int classificationColumn = -1 ) const;
386
391 QgsGradientColorRamp colorRamp( QStringList &labels SIP_OUT, const int labelColumn = -1 ) const;
392
403 std::unique_ptr<QgsRasterRenderer> createRenderer( QgsRasterDataProvider *provider, const int bandNumber, const int classificationColumn = -1 );
404
412 QList<QList<QVariant>> orderedRows() const;
413
417 static Qgis::RasterAttributeTableFieldUsage guessFieldUsage( const QString &name, const QMetaType::Type type );
418
422 static Qgis::RasterAttributeTableFieldUsage guessFieldUsage( const QString &name, const QVariant::Type type );
423
430 QString filePath() const;
431
436 static QString usageName( const Qgis::RasterAttributeTableFieldUsage fieldusage );
437
441 static QList<Qgis::RasterAttributeTableFieldUsage> valueAndColorFieldUsages();
442
449 static std::unique_ptr<QgsRasterAttributeTable> createFromRaster( QgsRasterLayer *rasterLayer, int *bandNumber SIP_OUT = nullptr );
450
455 static QHash<Qgis::RasterAttributeTableFieldUsage, QgsRasterAttributeTable::UsageInformation> usageInformation() SIP_SKIP;
456
458
463 static QHash<int, QgsRasterAttributeTable::UsageInformation> usageInformationInt() SIP_PYNAME( usageInformation );
464
465 static QHash<Qgis::RasterAttributeTableFieldUsage, QgsRasterAttributeTable::UsageInformation> sUsageInformation SIP_SKIP;
466
468
469 private:
470 Qgis::RasterAttributeTableType mType = Qgis::RasterAttributeTableType::Thematic;
471 QList<Field> mFields;
472 QList<QVariantList> mData;
473 bool mIsDirty = false;
474 QString mFilePath;
475
476 // Set type from fields.
477 void setType();
478};
479
480#endif // QGSRASTERATTRIBUTETABLE_H
Provides global constants and enumerations for use throughout the application.
Definition qgis.h:62
RasterAttributeTableType
The RasterAttributeTableType enum represents the type of RAT.
Definition qgis.h:1786
RasterAttributeTableFieldUsage
The RasterAttributeTableFieldUsage enum represents the usage of a Raster Attribute Table field.
Definition qgis.h:1757
Container of fields for a vector layer.
Definition qgsfields.h:45
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
The Field class represents a Raster Attribute Table field, including its name, usage and type.
Q_DECL_DEPRECATED Field(const QString &name, const Qgis::RasterAttributeTableFieldUsage &usage, const QVariant::Type type)
Creates a new Field with name, type and usage.
Qgis::RasterAttributeTableFieldUsage usage
bool operator==(const Field &other) const
Comparison operator for Field objects.
Field(const QString &name, const Qgis::RasterAttributeTableFieldUsage &usage, const QMetaType::Type type)
Creates a new Field with name, type and usage.
The Field class represents a Raster Attribute Table classification entry for a thematic Raster Attrib...
QVector< double > minMaxValues
List of values for the class.
The UsageInformation class represents information about a field usage.
bool isRamp
Usage is part of a ramp component.
bool maybeClass
May be suitable for classification.
QList< QMetaType::Type > allowedTypes
Usage allowed types.
bool isColor
Usage is part of a color component.
Represents a Raster Attribute Table (RAT).
const QgsRasterAttributeTable::Field fieldByName(const QString name, bool *ok=nullptr) const
Returns a field by name or a default constructed field with empty name if the field is not found.
bool isDirty() const
Returns true if the Raster Attribute Table was modified from its last reading from the storage.
bool setColor(const int row, const QColor &color)
Sets the color for the row at rowIndex to color.
QList< QgsRasterAttributeTable::MinMaxClass > minMaxClasses(const int classificationColumn=-1) const
Returns the classes for a thematic Raster Attribute Table, classified by classificationColumn,...
QgsGradientColorRamp ramp(int row) const
Returns the gradient color ramp of the rat row or a default constructed gradient if row does not exis...
bool readFromFile(const QString &path, QString *errorMessage=nullptr)
Reads the Raster Attribute Table from a DBF file specified by path, optionally reporting any error in...
QgsGradientColorRamp colorRamp(QStringList &labels, const int labelColumn=-1) const
Returns the color ramp for an athematic Raster Attribute Table setting the labels in labels,...
bool appendField(const QString &name, const Qgis::RasterAttributeTableFieldUsage usage, const QMetaType::Type type, QString *errorMessage=nullptr)
Creates a new field from name, usage and type and appends it to the fields, optionally reporting any ...
bool insertField(int position, const QgsRasterAttributeTable::Field &field, QString *errorMessage=nullptr)
Inserts a new field at position, optionally reporting any error in errorMessage, returns true on succ...
bool hasColor() const
Returns true if the Raster Attribute Table has color RGBA information.
bool setValue(const int row, const int column, const QVariant &value)
Sets the value for row and column.
static QList< Qgis::RasterAttributeTableFieldUsage > valueAndColorFieldUsages()
Returns the list of field usages for colors and values.
QList< QgsRasterAttributeTable::Field > fields() const
Returns the Raster Attribute Table fields.
bool removeRow(int position=0, QString *errorMessage=nullptr)
Removes the row in the Raster Attribute Table at position, optionally reporting any error in errorMes...
bool appendRow(const QVariantList &data, QString *errorMessage=nullptr)
Appends a row of data to the RAT, optionally reporting any error in errorMessage, returns true on suc...
static QHash< Qgis::RasterAttributeTableFieldUsage, QgsRasterAttributeTable::UsageInformation > usageInformation()
Returns information about supported Raster Attribute Table usages.
Qgis::RasterAttributeTableType type() const
Returns the Raster Attribute Table type.
std::unique_ptr< QgsRasterRenderer > createRenderer(QgsRasterDataProvider *provider, const int bandNumber, const int classificationColumn=-1)
Creates and returns a (possibly nullptr) raster renderer for the specified provider and bandNumber an...
QList< Qgis::RasterAttributeTableFieldUsage > usages() const
Returns the list of field usages.
QVariantList row(const double matchValue) const
Returns a row of data for the given matchValue or and empty row if there is not match.
bool insertRow(int position, const QVariantList &rowData, QString *errorMessage=nullptr)
Inserts a row of rowData in the Raster Attribute Table at position, optionally reporting any error in...
bool writeToFile(const QString &path, QString *errorMessage=nullptr)
Writes the Raster Attribute Table to a DBF file specified by path, optionally reporting any error in ...
double minimumValue() const
Returns the minimum value of the MinMax (thematic) or Min (athematic) column, returns NaN on errors.
double maximumValue() const
Returns the maximum value of the MinMax (thematic) or Max (athematic) column, returns NaN on errors.
const QList< QgsRasterAttributeTable::Field > fieldsByUsage(const Qgis::RasterAttributeTableFieldUsage fieldUsage) const
Returns the list of fields matching fieldUsage.
void setDirty(bool isDirty)
Sets the Raster Attribute Table dirty state to isDirty;.
static std::unique_ptr< QgsRasterAttributeTable > createFromRaster(QgsRasterLayer *rasterLayer, int *bandNumber=nullptr)
Creates a new Raster Attribute Table from a raster layer, the renderer must be Paletted or SingleBand...
bool insertColor(int position, QString *errorMessage=nullptr)
Create RGBA fields and inserts them at position, optionally reporting any error in errorMessage,...
bool isValid(QString *errorMessage=nullptr) const
Returns true if the Raster Attribute Table is valid, optionally reporting validity checks results in ...
const QList< QList< QVariant > > data() const
Returns the Raster Attribute Table rows.
bool setRamp(const int row, const QColor &colorMin, const QColor &colorMax)
Sets the color ramp for the row at rowIndex to colorMin and colorMax.
PRIVATE QColor color(int row) const
Returns the color of the rat row or an invalid color if row does not exist or if there is no color de...
QList< QList< QVariant > > orderedRows() const
Returns the data rows ordered by the value column(s) in ascending order, if the attribute table type ...
static QString usageName(const Qgis::RasterAttributeTableFieldUsage fieldusage)
Returns the translated human readable name of fieldUsage.
bool hasRamp() const
Returns true if the Raster Attribute Table has ramp RGBA information.
QgsFeatureList qgisFeatures() const
Returns the Raster Attribute Table rows as a list of QgsFeature.
bool setFieldUsage(int fieldIndex, const Qgis::RasterAttributeTableFieldUsage usage)
Change the usage of the field at index fieldIndex to usage with checks for allowed types.
QString filePath() const
Returns the (possibly empty) path of the file-based RAT, the path is set when a RAT is read or writte...
bool insertRamp(int position, QString *errorMessage=nullptr)
Create RGBA minimum and maximum fields and inserts them at position, optionally reporting any error i...
QVariant value(const int row, const int column) const
Returns the value for row and column.
QgsFields qgisFields() const
Returns the Raster Attribute Table fields as QgsFields.
static Qgis::RasterAttributeTableFieldUsage guessFieldUsage(const QString &name, const QMetaType::Type type)
Try to determine the field usage from its name and type.
bool removeField(const QString &name, QString *errorMessage=nullptr)
Removes the field with name, optionally reporting any error in errorMessage, returns true on success.
Base class for raster data providers.
Represents a raster layer.
Raster renderer pipe that applies colors to a raster.
static QMetaType::Type variantTypeToMetaType(QVariant::Type variantType)
Converts a QVariant::Type to a QMetaType::Type.
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_SKIP
Definition qgis_sip.h:138
#define SIP_PYNAME(name)
Definition qgis_sip.h:88
#define SIP_OUT
Definition qgis_sip.h:57
QList< QgsFeature > QgsFeatureList