Skip to content

Commit

Permalink
Set RAT type
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso authored and nyalldawson committed Nov 8, 2022
1 parent 41983fa commit f62ffbf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/core/providers/gdal/qgsgdalprovider.cpp
Expand Up @@ -3209,8 +3209,10 @@ bool QgsGdalProvider::readNativeAttributeTable( QString *errorMessage )
}

hasAtLeastOnedRat = rat->fields().count( ) > 0;

if ( hasAtLeastOnedRat )
{
rat->setType( static_cast<Qgis::RasterAttributeTableType>( GDALRATGetTableType( hRat ) ) );
setAttributeTable( bandNumber, rat.release() );
}
else if ( errorMessage )
Expand Down Expand Up @@ -3312,6 +3314,8 @@ bool QgsGdalProvider::writeNativeAttributeTable( QString *errorMessage ) const /
rowIdx++;
}

GDALRATSetTableType( hRat, static_cast<GDALRATTableType>( rat->type() ) );

if ( GDALSetDefaultRAT( hBand, hRat ) != CE_None )
{
if ( errorMessage )
Expand Down
43 changes: 32 additions & 11 deletions src/core/raster/qgsrasterattributetable.cpp
Expand Up @@ -296,6 +296,16 @@ bool QgsRasterAttributeTable::insertField( int position, const Field &field, QSt
it->insert( realPos, defaultValue );
}

// Set/change the table type from the value field type
if ( field.usage == Qgis::RasterAttributeTableFieldUsage::MinMax )
{
setType( Qgis::RasterAttributeTableType::Thematic );
}
else if ( field.usage == Qgis::RasterAttributeTableFieldUsage::Max || field.usage == Qgis::RasterAttributeTableFieldUsage::Max )
{
setType( Qgis::RasterAttributeTableType::Athematic );
}

setIsDirty( true );

return true;
Expand Down Expand Up @@ -522,6 +532,15 @@ bool QgsRasterAttributeTable::readFromFile( const QString &path, QString *errorM
appendRow( f.attributes().toList() );
}

if ( usages().contains( Qgis::RasterAttributeTableFieldUsage::MinMax ) )
{
setType( Qgis::RasterAttributeTableType::Thematic );
}
else
{
setType( Qgis::RasterAttributeTableType::Athematic );
}

setIsDirty( false );

return true;
Expand Down Expand Up @@ -719,6 +738,15 @@ Qgis::RasterAttributeTableFieldUsage QgsRasterAttributeTable::guessFieldUsage( c
{
return Qgis::RasterAttributeTableFieldUsage::Max;
}
else if ( fieldLower == QStringLiteral( "value" ) )
{
return Qgis::RasterAttributeTableFieldUsage::MinMax;
}
else if ( fieldLower == QStringLiteral( "count" ) )
{
// This could really be max count but it's more likely pixel count
return Qgis::RasterAttributeTableFieldUsage::PixelCount;
}
// Colors (not double)
else if ( type != QVariant::Double )
{
Expand Down Expand Up @@ -784,22 +812,14 @@ Qgis::RasterAttributeTableFieldUsage QgsRasterAttributeTable::guessFieldUsage( c
}
}
// end colors
else if ( fieldLower == QStringLiteral( "value" ) )
{
return Qgis::RasterAttributeTableFieldUsage::MinMax;
}
else if ( fieldLower == QLatin1String( "count" ) )
{
// This could really be max count but it's more likely pixel count
return Qgis::RasterAttributeTableFieldUsage::PixelCount;
}
}
else if ( type == QVariant::String ) // default to name for strings

if ( type == QVariant::String ) // default to name for strings
{
return Qgis::RasterAttributeTableFieldUsage::Name;
}

// default to generic for not strings
// default to generic for all other cases
return Qgis::RasterAttributeTableFieldUsage::Generic;

}
Expand Down Expand Up @@ -882,6 +902,7 @@ QList<QgsRasterAttributeTable::MinMaxClass> QgsRasterAttributeTable::minMaxClass
}

const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages { usages() };

if ( ! fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::MinMax ) )
{
QgsDebugMsg( "minMaxClasses was called on a ramp raster" );
Expand Down
2 changes: 1 addition & 1 deletion src/core/raster/qgsrasterattributetable.h
Expand Up @@ -305,7 +305,7 @@ class CORE_EXPORT QgsRasterAttributeTable

private:

Qgis::RasterAttributeTableType mType = Qgis::RasterAttributeTableType::Athematic;
Qgis::RasterAttributeTableType mType = Qgis::RasterAttributeTableType::Thematic;
QList<Field> mFields;
QList<QVariantList> mData;
bool mIsDirty;
Expand Down
7 changes: 4 additions & 3 deletions tests/src/python/test_qgsrasterattributetable.py
Expand Up @@ -147,7 +147,7 @@ def setUp(self):
dst_ds.FlushCache() # write to disk
dst_ds = None

def ___testRat(self):
def testRat(self):

# Create RAT
rat = QgsRasterAttributeTable()
Expand Down Expand Up @@ -188,6 +188,7 @@ def ___testRat(self):
self.assertIsNone(d.attributeTable(2))

rat = d.attributeTable(1)
self.assertEqual(rat.type(), Qgis.RasterAttributeTableType.Thematic)
self.assertTrue(rat.isValid()[0])
self.assertEqual([f.name for f in rat.fields()], ['Value', 'Count', 'Class', 'Class2', 'Class3', 'Red', 'Green', 'Blue', 'Double'])
self.assertEqual([f.type for f in rat.fields()], [QVariant.Int, QVariant.Int, QVariant.String, QVariant.String, QVariant.String, QVariant.Int, QVariant.Int, QVariant.Int, QVariant.Double])
Expand Down Expand Up @@ -283,7 +284,7 @@ def ___testRat(self):
self.assertTrue(rat.fieldByName("Red")[1])
self.assertFalse(rat.fieldByName("xxx")[1])

def ____testTableOperationsAndValidation(self):
def testTableOperationsAndValidation(self):

rat = QgsRasterAttributeTable()
valid, error = rat.isValid()
Expand Down Expand Up @@ -411,7 +412,7 @@ def ____testTableOperationsAndValidation(self):
self.assertEqual(rat.ramp(1).min.name(), '#010203')
self.assertEqual(rat.ramp(1).max.name(), '#020304')

def ____testWriteReadDbfRat(self):
def testWriteReadDbfRat(self):

# Create RAT
rat = QgsRasterAttributeTable()
Expand Down

0 comments on commit f62ffbf

Please sign in to comment.