Skip to content

Commit f62ffbf

Browse files
elpasonyalldawson
authored andcommittedNov 8, 2022
Set RAT type
1 parent 41983fa commit f62ffbf

File tree

4 files changed

+41
-15
lines changed

4 files changed

+41
-15
lines changed
 

‎src/core/providers/gdal/qgsgdalprovider.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3209,8 +3209,10 @@ bool QgsGdalProvider::readNativeAttributeTable( QString *errorMessage )
32093209
}
32103210

32113211
hasAtLeastOnedRat = rat->fields().count( ) > 0;
3212+
32123213
if ( hasAtLeastOnedRat )
32133214
{
3215+
rat->setType( static_cast<Qgis::RasterAttributeTableType>( GDALRATGetTableType( hRat ) ) );
32143216
setAttributeTable( bandNumber, rat.release() );
32153217
}
32163218
else if ( errorMessage )
@@ -3312,6 +3314,8 @@ bool QgsGdalProvider::writeNativeAttributeTable( QString *errorMessage ) const /
33123314
rowIdx++;
33133315
}
33143316

3317+
GDALRATSetTableType( hRat, static_cast<GDALRATTableType>( rat->type() ) );
3318+
33153319
if ( GDALSetDefaultRAT( hBand, hRat ) != CE_None )
33163320
{
33173321
if ( errorMessage )

‎src/core/raster/qgsrasterattributetable.cpp

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,16 @@ bool QgsRasterAttributeTable::insertField( int position, const Field &field, QSt
296296
it->insert( realPos, defaultValue );
297297
}
298298

299+
// Set/change the table type from the value field type
300+
if ( field.usage == Qgis::RasterAttributeTableFieldUsage::MinMax )
301+
{
302+
setType( Qgis::RasterAttributeTableType::Thematic );
303+
}
304+
else if ( field.usage == Qgis::RasterAttributeTableFieldUsage::Max || field.usage == Qgis::RasterAttributeTableFieldUsage::Max )
305+
{
306+
setType( Qgis::RasterAttributeTableType::Athematic );
307+
}
308+
299309
setIsDirty( true );
300310

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

535+
if ( usages().contains( Qgis::RasterAttributeTableFieldUsage::MinMax ) )
536+
{
537+
setType( Qgis::RasterAttributeTableType::Thematic );
538+
}
539+
else
540+
{
541+
setType( Qgis::RasterAttributeTableType::Athematic );
542+
}
543+
525544
setIsDirty( false );
526545

527546
return true;
@@ -719,6 +738,15 @@ Qgis::RasterAttributeTableFieldUsage QgsRasterAttributeTable::guessFieldUsage( c
719738
{
720739
return Qgis::RasterAttributeTableFieldUsage::Max;
721740
}
741+
else if ( fieldLower == QStringLiteral( "value" ) )
742+
{
743+
return Qgis::RasterAttributeTableFieldUsage::MinMax;
744+
}
745+
else if ( fieldLower == QStringLiteral( "count" ) )
746+
{
747+
// This could really be max count but it's more likely pixel count
748+
return Qgis::RasterAttributeTableFieldUsage::PixelCount;
749+
}
722750
// Colors (not double)
723751
else if ( type != QVariant::Double )
724752
{
@@ -784,22 +812,14 @@ Qgis::RasterAttributeTableFieldUsage QgsRasterAttributeTable::guessFieldUsage( c
784812
}
785813
}
786814
// end colors
787-
else if ( fieldLower == QStringLiteral( "value" ) )
788-
{
789-
return Qgis::RasterAttributeTableFieldUsage::MinMax;
790-
}
791-
else if ( fieldLower == QLatin1String( "count" ) )
792-
{
793-
// This could really be max count but it's more likely pixel count
794-
return Qgis::RasterAttributeTableFieldUsage::PixelCount;
795-
}
796815
}
797-
else if ( type == QVariant::String ) // default to name for strings
816+
817+
if ( type == QVariant::String ) // default to name for strings
798818
{
799819
return Qgis::RasterAttributeTableFieldUsage::Name;
800820
}
801821

802-
// default to generic for not strings
822+
// default to generic for all other cases
803823
return Qgis::RasterAttributeTableFieldUsage::Generic;
804824

805825
}
@@ -882,6 +902,7 @@ QList<QgsRasterAttributeTable::MinMaxClass> QgsRasterAttributeTable::minMaxClass
882902
}
883903

884904
const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages { usages() };
905+
885906
if ( ! fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::MinMax ) )
886907
{
887908
QgsDebugMsg( "minMaxClasses was called on a ramp raster" );

‎src/core/raster/qgsrasterattributetable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ class CORE_EXPORT QgsRasterAttributeTable
305305

306306
private:
307307

308-
Qgis::RasterAttributeTableType mType = Qgis::RasterAttributeTableType::Athematic;
308+
Qgis::RasterAttributeTableType mType = Qgis::RasterAttributeTableType::Thematic;
309309
QList<Field> mFields;
310310
QList<QVariantList> mData;
311311
bool mIsDirty;

‎tests/src/python/test_qgsrasterattributetable.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def setUp(self):
147147
dst_ds.FlushCache() # write to disk
148148
dst_ds = None
149149

150-
def ___testRat(self):
150+
def testRat(self):
151151

152152
# Create RAT
153153
rat = QgsRasterAttributeTable()
@@ -188,6 +188,7 @@ def ___testRat(self):
188188
self.assertIsNone(d.attributeTable(2))
189189

190190
rat = d.attributeTable(1)
191+
self.assertEqual(rat.type(), Qgis.RasterAttributeTableType.Thematic)
191192
self.assertTrue(rat.isValid()[0])
192193
self.assertEqual([f.name for f in rat.fields()], ['Value', 'Count', 'Class', 'Class2', 'Class3', 'Red', 'Green', 'Blue', 'Double'])
193194
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])
@@ -283,7 +284,7 @@ def ___testRat(self):
283284
self.assertTrue(rat.fieldByName("Red")[1])
284285
self.assertFalse(rat.fieldByName("xxx")[1])
285286

286-
def ____testTableOperationsAndValidation(self):
287+
def testTableOperationsAndValidation(self):
287288

288289
rat = QgsRasterAttributeTable()
289290
valid, error = rat.isValid()
@@ -411,7 +412,7 @@ def ____testTableOperationsAndValidation(self):
411412
self.assertEqual(rat.ramp(1).min.name(), '#010203')
412413
self.assertEqual(rat.ramp(1).max.name(), '#020304')
413414

414-
def ____testWriteReadDbfRat(self):
415+
def testWriteReadDbfRat(self):
415416

416417
# Create RAT
417418
rat = QgsRasterAttributeTable()

0 commit comments

Comments
 (0)
Please sign in to comment.