Skip to content

Commit

Permalink
Paletted RAT auto-loading
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso authored and nyalldawson committed Nov 8, 2022
1 parent c07a00f commit 5a88611
Show file tree
Hide file tree
Showing 17 changed files with 657 additions and 200 deletions.
2 changes: 1 addition & 1 deletion python/core/auto_additions/qgis.py
Expand Up @@ -734,7 +734,7 @@
Qgis.RasterAttributeTableFieldUsage.BlueMax.__doc__ = "Field usage BlueMax"
Qgis.RasterAttributeTableFieldUsage.AlphaMax.__doc__ = "Field usage AlphaMax"
Qgis.RasterAttributeTableFieldUsage.MaxCount.__doc__ = "Field usage MaxCount"
Qgis.RasterAttributeTableFieldUsage.__doc__ = 'The RasterAttributeTableFieldUsage enum represents the usage of a RAT field.\n\n.. versionadded:: 3.30\n\n' + '* ``Generic``: ' + Qgis.RasterAttributeTableFieldUsage.Generic.__doc__ + '\n' + '* ``PixelCount``: ' + Qgis.RasterAttributeTableFieldUsage.PixelCount.__doc__ + '\n' + '* ``Name``: ' + Qgis.RasterAttributeTableFieldUsage.Name.__doc__ + '\n' + '* ``Min``: ' + Qgis.RasterAttributeTableFieldUsage.Min.__doc__ + '\n' + '* ``Max``: ' + Qgis.RasterAttributeTableFieldUsage.Max.__doc__ + '\n' + '* ``MinMax``: ' + Qgis.RasterAttributeTableFieldUsage.MinMax.__doc__ + '\n' + '* ``Red``: ' + Qgis.RasterAttributeTableFieldUsage.Red.__doc__ + '\n' + '* ``Green``: ' + Qgis.RasterAttributeTableFieldUsage.Green.__doc__ + '\n' + '* ``Blue``: ' + Qgis.RasterAttributeTableFieldUsage.Blue.__doc__ + '\n' + '* ``Alpha``: ' + Qgis.RasterAttributeTableFieldUsage.Alpha.__doc__ + '\n' + '* ``RedMin``: ' + Qgis.RasterAttributeTableFieldUsage.RedMin.__doc__ + '\n' + '* ``GreenMin``: ' + Qgis.RasterAttributeTableFieldUsage.GreenMin.__doc__ + '\n' + '* ``BlueMin``: ' + Qgis.RasterAttributeTableFieldUsage.BlueMin.__doc__ + '\n' + '* ``AlphaMin``: ' + Qgis.RasterAttributeTableFieldUsage.AlphaMin.__doc__ + '\n' + '* ``RedMax``: ' + Qgis.RasterAttributeTableFieldUsage.RedMax.__doc__ + '\n' + '* ``GreenMax``: ' + Qgis.RasterAttributeTableFieldUsage.GreenMax.__doc__ + '\n' + '* ``BlueMax``: ' + Qgis.RasterAttributeTableFieldUsage.BlueMax.__doc__ + '\n' + '* ``AlphaMax``: ' + Qgis.RasterAttributeTableFieldUsage.AlphaMax.__doc__ + '\n' + '* ``MaxCount``: ' + Qgis.RasterAttributeTableFieldUsage.MaxCount.__doc__
Qgis.RasterAttributeTableFieldUsage.__doc__ = 'The RasterAttributeTableFieldUsage enum represents the usage of a Raster Attribute Table field.\n\n.. versionadded:: 3.30\n\n' + '* ``Generic``: ' + Qgis.RasterAttributeTableFieldUsage.Generic.__doc__ + '\n' + '* ``PixelCount``: ' + Qgis.RasterAttributeTableFieldUsage.PixelCount.__doc__ + '\n' + '* ``Name``: ' + Qgis.RasterAttributeTableFieldUsage.Name.__doc__ + '\n' + '* ``Min``: ' + Qgis.RasterAttributeTableFieldUsage.Min.__doc__ + '\n' + '* ``Max``: ' + Qgis.RasterAttributeTableFieldUsage.Max.__doc__ + '\n' + '* ``MinMax``: ' + Qgis.RasterAttributeTableFieldUsage.MinMax.__doc__ + '\n' + '* ``Red``: ' + Qgis.RasterAttributeTableFieldUsage.Red.__doc__ + '\n' + '* ``Green``: ' + Qgis.RasterAttributeTableFieldUsage.Green.__doc__ + '\n' + '* ``Blue``: ' + Qgis.RasterAttributeTableFieldUsage.Blue.__doc__ + '\n' + '* ``Alpha``: ' + Qgis.RasterAttributeTableFieldUsage.Alpha.__doc__ + '\n' + '* ``RedMin``: ' + Qgis.RasterAttributeTableFieldUsage.RedMin.__doc__ + '\n' + '* ``GreenMin``: ' + Qgis.RasterAttributeTableFieldUsage.GreenMin.__doc__ + '\n' + '* ``BlueMin``: ' + Qgis.RasterAttributeTableFieldUsage.BlueMin.__doc__ + '\n' + '* ``AlphaMin``: ' + Qgis.RasterAttributeTableFieldUsage.AlphaMin.__doc__ + '\n' + '* ``RedMax``: ' + Qgis.RasterAttributeTableFieldUsage.RedMax.__doc__ + '\n' + '* ``GreenMax``: ' + Qgis.RasterAttributeTableFieldUsage.GreenMax.__doc__ + '\n' + '* ``BlueMax``: ' + Qgis.RasterAttributeTableFieldUsage.BlueMax.__doc__ + '\n' + '* ``AlphaMax``: ' + Qgis.RasterAttributeTableFieldUsage.AlphaMax.__doc__ + '\n' + '* ``MaxCount``: ' + Qgis.RasterAttributeTableFieldUsage.MaxCount.__doc__
# --
Qgis.RasterAttributeTableFieldUsage.baseClass = Qgis
# monkey patching scoped based enum
Expand Down
40 changes: 40 additions & 0 deletions python/core/auto_generated/raster/qgspalettedrasterrenderer.sip.in
Expand Up @@ -35,14 +35,32 @@ Constructor for Class
QString label;
};

struct MultiValueClass
{
MultiValueClass( QVector< double > values, const QColor &color = QColor(), const QString &label = QString() );
%Docstring
Constructor for Class
%End

QVector< double > values;

QColor color;

QString label;
};


typedef QList< QgsPalettedRasterRenderer::Class > ClassData;

typedef QList< QgsPalettedRasterRenderer::MultiValueClass > MultiValueClassData;

QgsPalettedRasterRenderer( QgsRasterInterface *input, int bandNumber, const ClassData &classes );
%Docstring
Constructor for QgsPalettedRasterRenderer.
%End



virtual QgsPalettedRasterRenderer *clone() const /Factory/;

%Docstring
Expand All @@ -64,6 +82,20 @@ Returns number of colors
ClassData classes() const;
%Docstring
Returns a map of value to classes (colors) used by the renderer.
%End

MultiValueClassData multiValueClasses( ) const;
%Docstring
Returns a map of multi value to classes (colors) used by the renderer.

.. versionadded:: 3.30
%End

void setMultiValueClasses( const MultiValueClassData &classes );
%Docstring
Sets the multi value classes to ``setMultiValueClasses``.

.. versionadded:: 3.30
%End

QString label( double idx ) const;
Expand Down Expand Up @@ -123,6 +155,13 @@ Gets the source color ramp
Converts a raster color ``table`` to paletted renderer class data.

.. versionadded:: 3.0
%End

static QgsPalettedRasterRenderer::MultiValueClassData rasterAttributeTableToClassData( const QgsRasterAttributeTable *attributeTable, QgsColorRamp *ramp = 0 );
%Docstring
Read classes from the Raster Attribute Table ``attributeTable``, optionally settings the colors from ``ramp``.

.. versionadded:: 3.30
%End

static QgsPalettedRasterRenderer::ClassData classDataFromString( const QString &string );
Expand Down Expand Up @@ -163,6 +202,7 @@ color ``ramp`` can be specified to automatically assign colors from the ramp.
.. versionadded:: 3.0
%End


private:
QgsPalettedRasterRenderer( const QgsPalettedRasterRenderer & );
const QgsPalettedRasterRenderer &operator=( const QgsPalettedRasterRenderer & );
Expand Down
46 changes: 29 additions & 17 deletions python/core/auto_generated/raster/qgsrasterattributetable.sip.in
Expand Up @@ -13,8 +13,8 @@ class QgsRasterAttributeTable
%Docstring(signature="appended")
The :py:class:`QgsRasterAttributeTable` class represents a raster attribute table (RAT).

This class is modeled after the GDAL RAT implementation, it adds some convenience
methods to handle data from QGIS and to import/export a RAT from/to a DBF VAT file.
This class is modeled after the GDAL Raster Attribute Table implementation, it adds some convenience
methods to handle data from QGIS and to import/export a Raster Attribute Table from/to a DBF VAT file.

.. versionadded:: 3.30
%End
Expand Down Expand Up @@ -53,19 +53,26 @@ Returns ``True`` if the field carries a color ramp component information (RedMin
QVariant::Type type;
};

struct MinMaxClass
{
QString name;
QVector< double > minMaxValues;
QColor color;
};

Qgis::RasterAttributeTableType type() const;
%Docstring
Returns the RAT type.
Returns the Raster Attribute Table type.
%End

void setType( const Qgis::RasterAttributeTableType type );
%Docstring
Sets the RAT ``type``
Sets the Raster Attribute Table ``type``
%End

bool hasColor() const;
%Docstring
Returns ``True`` if the RAT has color RGBA information.
Returns ``True`` if the Raster Attribute Table has color RGBA information.

.. seealso:: :py:func:`color`

Expand Down Expand Up @@ -96,7 +103,7 @@ Sets the color for the row at ``rowIndex`` to ``color``.

bool hasRamp() const;
%Docstring
Returns ``True`` if the RAT has ramp RGBA information.
Returns ``True`` if the Raster Attribute Table has ramp RGBA information.

.. seealso:: :py:func:`setRamp`

Expand Down Expand Up @@ -158,36 +165,36 @@ Returns the color of the rat ``row`` or an invalid color if row does not exist o

QList<QgsRasterAttributeTable::Field> fields() const;
%Docstring
Returns the RAT fields.
Returns the Raster Attribute Table fields.

.. seealso:: :py:func:`qgisFields`
%End

QgsFields qgisFields() const;
%Docstring
Returns the RAT fields as :py:class:`QgsFields`.
Returns the Raster Attribute Table fields as :py:class:`QgsFields`.

.. seealso:: :py:func:`fields`
%End

QgsFeatureList qgisFeatures( ) const;
%Docstring
Returns the RAT rows as a list of :py:class:`QgsFeature`.
Returns the Raster Attribute Table rows as a list of :py:class:`QgsFeature`.
%End

bool isDirty() const;
%Docstring
Returns ``True`` if the RAT was modified from its last reading from the storage.
Returns ``True`` if the Raster Attribute Table was modified from its last reading from the storage.
%End

void setIsDirty( bool isDirty );
%Docstring
Sets the RAT dirty state to ``isDirty``;
Sets the Raster Attribute Table dirty state to ``isDirty``;
%End

bool isValid( QString *errorMessage /Out/ = 0 ) const;
%Docstring
Returns ``True`` if the RAT is valid, optionally reporting validity checks results in ``errorMessage``.
Returns ``True`` if the Raster Attribute Table is valid, optionally reporting validity checks results in ``errorMessage``.
%End

bool insertField( int position, const QgsRasterAttributeTable::Field &field, QString *errorMessage /Out/ = 0 );
Expand Down Expand Up @@ -221,7 +228,7 @@ Removes the field with ``name``, optionally reporting any error in ``errorMessag

bool insertRow( int position, const QVariantList &rowData, QString *errorMessage /Out/ = 0 );
%Docstring
Inserts a row of ``rowData`` in the RAT at ``position``, optionally reporting any error in ``errorMessage``, returns ``True`` on success.
Inserts a row of ``rowData`` in the Raster Attribute Table at ``position``, optionally reporting any error in ``errorMessage``, returns ``True`` on success.

.. note::

Expand All @@ -230,7 +237,7 @@ Inserts a row of ``rowData`` in the RAT at ``position``, optionally reporting an

bool removeRow( int position = 0, QString *errorMessage /Out/ = 0 );
%Docstring
Removes the row in the RAT at ``position``, optionally reporting any error in ``errorMessage``, returns ``True`` on success.
Removes the row in the Raster Attribute Table at ``position``, optionally reporting any error in ``errorMessage``, returns ``True`` on success.

.. note::

Expand All @@ -244,17 +251,17 @@ Appends a row of ``data`` to the RAT, optionally reporting any error in ``errorM

bool writeToFile( const QString &path, QString *errorMessage /Out/ = 0 );
%Docstring
Writes the RAT to a DBF file specified by ``path``, optionally reporting any error in ``errorMessage``, returns ``True`` on success.
Writes the Raster Attribute Table to a DBF file specified by ``path``, optionally reporting any error in ``errorMessage``, returns ``True`` on success.
%End

bool readFromFile( const QString &path, QString *errorMessage /Out/ = 0 );
%Docstring
Reads the RAT from a DBF file specified by ``path``, optionally reporting any error in ``errorMessage``, returns ``True`` on success.
Reads the Raster Attribute Table from a DBF file specified by ``path``, optionally reporting any error in ``errorMessage``, returns ``True`` on success.
%End

const QList<QList<QVariant>> data() const;
%Docstring
Returns the RAT rows.
Returns the Raster Attribute Table rows.
%End

const QgsRasterAttributeTable::Field fieldByName( const QString name, bool *ok /Out/ = 0 ) const;
Expand Down Expand Up @@ -292,6 +299,11 @@ Try to determine the field usage from its ``name`` and ``type``.
static QString usageName( const Qgis::RasterAttributeTableFieldUsage fieldusage );
%Docstring
Returns the translated human readable name of ``fieldUsage``.
%End

QList<QgsRasterAttributeTable::MinMaxClass> minMaxClasses( const int classificationColumn = -1 ) const;
%Docstring
Returns the classes for a thematic RAT, classified by ``classificationColumn``, the default value of -1 makes the method guess the classification column based on the field usage.
%End

};
Expand Down
Expand Up @@ -734,7 +734,7 @@ Writes the filesystem-based attribute table for the specified ``bandNumber`` to

.. note::

No checks for RAT validity are performed when saving, it is client code responsibility to handle validation.
No checks for Raster Attribute Table validity are performed when saving, it is client code responsibility to handle validation.

.. versionadded:: 3.30
%End
Expand Down
Expand Up @@ -27,27 +27,27 @@ Creates a new QgsRasterAttributeTableModel from raster attribute table ``rat`` a

bool editable() const;
%Docstring
Returns true if the RAT is editable.
Returns true if the Raster Attribute Table is editable.
%End

void setEditable( bool editable );
%Docstring
Sets the RAT editable state to ``editable``.
Sets the Raster Attribute Table editable state to ``editable``.
%End

bool hasColor() const;
%Docstring
Returns ``True`` if the RAT has color information.
Returns ``True`` if the Raster Attribute Table has color information.
%End

bool hasRamp() const;
%Docstring
Returns ``True`` if the RAT has ramp information.
Returns ``True`` if the Raster Attribute Table has ramp information.
%End

QStringList headerNames( ) const;
%Docstring
Returns all the header names, including the "virtual" color header if the RAT has color or ramp.
Returns all the header names, including the "virtual" color header if the Raster Attribute Table has color or ramp.
%End

QString headerTooltip( const int section ) const;
Expand All @@ -57,12 +57,12 @@ Returns the tooltip for the given ``section``.

bool isValid( QString *errorMessage /Out/ = 0 );
%Docstring
Checks if the RAT is valid, optionally returns validation errors in ``errorMessage``.
Checks if the Raster Attribute Table is valid, optionally returns validation errors in ``errorMessage``.
%End

bool isDirty( );
%Docstring
Returns ``True`` if the RAT was modified since it was last saved or read.
Returns ``True`` if the Raster Attribute Table was modified since it was last saved or read.
%End


Expand Down
20 changes: 13 additions & 7 deletions src/core/providers/gdal/qgsgdalprovider.cpp
Expand Up @@ -3117,7 +3117,7 @@ bool QgsGdalProvider::readNativeAttributeTable( QString *errorMessage )
QStringList ratFieldNames { ratFields.names( ) };
QStringList lNames;

// Try to identify fields in case of RAT with wrong usages
// Try to identify fields in case of Raster Attribute Table with wrong usages
if ( ! usages.contains( Qgis::RasterAttributeTableFieldUsage::MinMax ) ||
!( usages.contains( Qgis::RasterAttributeTableFieldUsage::Min ) && usages.contains( Qgis::RasterAttributeTableFieldUsage::Max ) ) )
{
Expand Down Expand Up @@ -3208,15 +3208,21 @@ bool QgsGdalProvider::readNativeAttributeTable( QString *errorMessage )
rat->appendRow( rowData );
}

hasAtLeastOnedRat = true;
setAttributeTable( bandNumber, rat.release() );

hasAtLeastOnedRat = rat->fields().count( ) > 0;
if ( hasAtLeastOnedRat )
{
setAttributeTable( bandNumber, rat.release() );
}
else if ( errorMessage )
{
*errorMessage = QObject::tr( "Raster Attribute Table has no columns: skipping." );
}
}
}
}
else if ( errorMessage )
{
*errorMessage = QObject::tr( "Dataset is not valid and RAT could not be loaded." );
*errorMessage = QObject::tr( "Dataset is not valid and Raster Attribute Table could not be loaded." );
}

return hasAtLeastOnedRat;
Expand All @@ -3241,7 +3247,7 @@ bool QgsGdalProvider::writeNativeAttributeTable( QString *errorMessage ) const /
{
if ( errorMessage )
{
*errorMessage = QObject::tr( "RAT table type could not be set, RAT was not saved (GDAL error)." );
*errorMessage = QObject::tr( "RAT table type could not be set, Raster Attribute Table was not saved (GDAL error)." );
}
GDALDestroyRasterAttributeTable( hRat );
return false;
Expand Down Expand Up @@ -3275,7 +3281,7 @@ bool QgsGdalProvider::writeNativeAttributeTable( QString *errorMessage ) const /
{
if ( errorMessage )
{
*errorMessage = QObject::tr( "RAT column '%1 could not be created, RAT was not saved (GDAL error)." ).arg( field.name );
*errorMessage = QObject::tr( "RAT column '%1 could not be created, Raster Attribute Table was not saved (GDAL error)." ).arg( field.name );
}
GDALDestroyRasterAttributeTable( hRat );
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgis.h
Expand Up @@ -748,7 +748,7 @@ class CORE_EXPORT Qgis
Q_FLAG( RasterRendererFlags )

/*
* \brief The RasterAttributeTableFieldUsage enum represents the usage of a RAT field.
* \brief The RasterAttributeTableFieldUsage enum represents the usage of a Raster Attribute Table field.
* \since QGIS 3.30
*/
enum class RasterAttributeTableFieldUsage : int
Expand Down

0 comments on commit 5a88611

Please sign in to comment.