24#include "qgsogrprovider.h"
35using namespace Qt::StringLiterals;
38std::once_flag usageInformationLoaderFlag;
39QHash<Qgis::RasterAttributeTableFieldUsage, QgsRasterAttributeTable::UsageInformation> QgsRasterAttributeTable::sUsageInformation;
51 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages {
usages() };
64 for (
int idx = 0; idx < mFields.count(); ++idx )
66 const Field f { mFields.at( idx ) };
97 for (
Field &f : mFields )
135 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages {
usages() };
147 QList<Qgis::RasterAttributeTableFieldUsage>
usages;
150 usages.push_back( field.usage );
156QList<int> QgsRasterAttributeTable::intUsages()
const
161 usages.push_back(
static_cast<int>( field.usage ) );
169 QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages {
usages() };
172 &&
row < mData.count()
177 const QVariantList rowData = mData.at(
row );
198 QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages {
usages() };
199 const QVariantList rowData = mData.at(
row );
240 for (
const QVariantList &
row : std::as_const( mData ) )
243 for (
const auto &cell : std::as_const(
row ) )
245 attributes.append( cell );
249 features.append( feature );
266 const int realPos { std::clamp( position, 0,
static_cast<int>( mFields.count() ) ) };
268 if ( field.
name.isEmpty() )
272 *errorMessage = tr(
"Field name must not be empty." );
285 *errorMessage = tr(
"A field with name '%1' already exists." ).arg( field.
name );
291 static const QList<Qgis::RasterAttributeTableFieldUsage> uniqueUsages {
315 *errorMessage = tr(
"A field with unique usage '%1' already exists." ).arg(
usageName( field.
usage ) );
320 mFields.insert( realPos, field );
322 for (
auto it = mData.begin(); it != mData.end(); ++it )
326 switch ( field.
type )
328 case QMetaType::Type::QChar:
329 case QMetaType::Type::Int:
330 case QMetaType::Type::UInt:
331 case QMetaType::Type::LongLong:
332 case QMetaType::Type::ULongLong:
333 case QMetaType::Type::Double:
337 defaultValue = QString();
339 it->insert( realPos, defaultValue );
370 const QList<Qgis::RasterAttributeTableFieldUsage> colors {
373 int idx { position };
387 if ( fieldIndex < 0 || fieldIndex >=
fields().count() )
398 mFields[fieldIndex].
usage = usage;
410 *errorMessage = tr(
"A color ramp can only be added to an athematic attribute table." );
413 const QList<Qgis::RasterAttributeTableFieldUsage> colors {
423 int idx { position };
437 return insertField(
static_cast<int>( mFields.count() ), name, usage,
type, errorMessage );
447 return insertField(
static_cast<int>( mFields.count() ), field, errorMessage );
452 const auto toRemove { std::find_if( mFields.begin(), mFields.end(), [&name](
Field &f ) ->
bool { return f.name == name; } ) };
454 if ( toRemove != mFields.end() )
456 const int idx {
static_cast<int>( std::distance( mFields.begin(), toRemove ) ) };
457 mFields.erase( toRemove );
458 for (
auto it = mData.begin(); it != mData.end(); ++it )
469 *errorMessage = tr(
"A field with name '%1' was not found." ).arg( name );
476 const int realPos { std::clamp( position, 0,
static_cast<int>( mData.count() ) ) };
478 if ( rowData.size() != mFields.size() )
482 *errorMessage = tr(
"Row element count differs from field count (%1)." ).arg( mFields.size() );
487 QVariantList dataValid;
489 for (
int idx = 0; idx < mFields.count(); ++idx )
491 const QMetaType::Type
type = mFields.at( idx ).type;
492 QVariant cell( rowData[idx] );
493 if (
type == QMetaType::Type::QDateTime && cell.toString().isEmpty() )
495 dataValid.append( QVariant() );
497 else if ( !cell.canConvert( QMetaType(
type ) ) || !cell.convert( QMetaType(
type ) ) )
501 *errorMessage = tr(
"Row data at column %1 cannot be converted to field type (%2)." ).arg( idx ).arg( QMetaType(
type ).name() );
507 dataValid.append( cell );
511 mData.insert( realPos, dataValid );
518 if ( position >= mData.count() || position < 0 || mData.isEmpty() )
522 *errorMessage = tr(
"Position is not valid or the table is empty." );
526 mData.removeAt( position );
533 return insertRow(
static_cast<int>( mData.count() ),
data, errorMessage );
544 std::unique_ptr<QgsVectorFileWriter> writer;
547 QString cleanedPath { path };
548 if ( path.endsWith( u
".dbf"_s, Qt::CaseSensitivity::CaseInsensitive ) )
550 cleanedPath.chop( 4 );
557 cleanedPath.append( u
".dbf"_s );
564 *errorMessage = tr(
"Error creating Raster Attribute Table table: %1." ).arg( writer->errorMessage() );
570 bool result { writer->addFeatures( features ) };
576 *errorMessage = tr(
"Error creating Raster Attribute Table table: could not add rows." );
581 result = writer->flushBuffer();
585 mFilePath = cleanedPath;
595 if ( !ratDbfSource.isValid() )
599 *errorMessage = tr(
"Error reading Raster Attribute Table table from file: invalid layer." );
604 QList<Field> oldFields = mFields;
605 QList<QVariantList> oldData = mData;
610 bool hasValueField {
false };
611 for (
const QgsField &field : ratDbfSource.fields() )
614 QMetaType::Type
type { field.type() };
616 if (
type == QMetaType::Type::LongLong
619 type = QMetaType::Type::Int;
624 hasValueField =
true;
637 if ( ! hasValueField && mFields.count() > 1 && ( mFields.at( 0 ).type == QMetaType::Type::Int || mFields.at( 0 ).type == QMetaType::Type::QChar || mFields.at( 0 ).type == QMetaType::Type::UInt || mFields.at( 0 ).type == QMetaType::Type::LongLong || mFields.at( 0 ).type == QMetaType::Type::ULongLong ) )
642 const int fieldCount {
static_cast<int>( ratDbfSource.fields().count() ) };
651 *errorMessage = tr(
"Error reading Raster Attribute Table table from file: number of fields and number of attributes do not match." );
671 if ( mFields.isEmpty() )
673 errors.push_back( tr(
"The attribute table has no fields." ) );
676 if ( mData.isEmpty() )
678 errors.push_back( tr(
"The attribute table has no rows." ) );
681 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages {
usages() };
684 if ( !isMinMax && !isValueRamp )
686 errors.push_back( tr(
"The attribute table has no MinMax nor a pair of Min and Max fields." ) );
698 errors.push_back( tr(
"The attribute table has some but not all the fields required for color definition (Red, Green, Blue)." ) );
717 errors.push_back( tr(
"The attribute table has some but not all the fields required for color ramp definition (RedMin, GreenMin, BlueMin, RedMax, GreenMax, BlueMax)." ) );
719 else if ( !isValueRamp )
721 errors.push_back( tr(
"The attribute table has all the fields required for color ramp definition (RedMin, GreenMin, BlueMin, RedMax, GreenMax, BlueMax) but no Min and Max field." ) );
725 if ( errorMessage && !errors.isEmpty() )
727 *errorMessage = errors.join( QChar(
'\n' ) );
730 return errors.isEmpty();
740 for (
const Field &f : std::as_const( mFields ) )
742 if ( f.name == name )
760 QList<QgsRasterAttributeTable::Field> result;
761 for (
const Field &f : std::as_const( mFields ) )
763 if ( f.usage == fieldUsage )
765 result.push_back( f );
778 QVariant newVal =
value;
779 QMetaType fieldType = QMetaType( mFields.at( column ).type );
780 if ( column >= mFields.length() || !
value.canConvert( fieldType ) || !newVal.convert( fieldType ) )
785 const QVariant oldVal = mData[
row][column];
787 if ( newVal != oldVal )
789 mData[
row][column] = newVal;
802 return mData[
row][column];
807 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages {
usages() };
820 double min { std::numeric_limits<double>::max() };
821 for (
int rowIdx = 0; rowIdx < mData.count(); ++rowIdx )
823 min = std::min( min,
value( rowIdx, fieldIdx ).toDouble( &ok ) );
826 return std::numeric_limits<double>::quiet_NaN();
830 if ( fieldIdx == -1 || !ok )
832 return std::numeric_limits<double>::quiet_NaN();
842 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages {
usages() };
855 double max { std::numeric_limits<double>::lowest() };
856 for (
int rowIdx = 0; rowIdx < mData.count(); ++rowIdx )
858 max = std::max( max,
value( rowIdx, fieldIdx ).toDouble( &ok ) );
861 return std::numeric_limits<double>::quiet_NaN();
865 if ( fieldIdx == -1 || !ok )
867 return std::numeric_limits<double>::quiet_NaN();
879 return QVariantList();
882 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages {
usages() };
887 for (
int rowIdx = 0; rowIdx < mData.count(); ++rowIdx )
890 if ( matchValue ==
value( rowIdx, colIdx ).toDouble( &ok ) && ok )
892 return mData.at( rowIdx );
900 for (
int rowIdx = 0; rowIdx < mData.count(); ++rowIdx )
903 if ( matchValue >=
value( rowIdx, minColIdx ).toDouble( &ok ) && ok )
905 if ( matchValue <
value( rowIdx, maxColIdx ).toDouble( &ok ) && ok )
907 return mData.at( rowIdx );
912 return QVariantList();
917 static const QStringList minValueNames { {
925 static const QStringList maxValueNames { {
933 const QString fieldLower { name.toLower() };
935 if (
type == QMetaType::Type::Double ||
type == QMetaType::Type::Int ||
type == QMetaType::Type::UInt ||
type == QMetaType::Type::LongLong ||
type == QMetaType::Type::ULongLong )
937 if ( minValueNames.contains( fieldLower ) )
941 else if ( maxValueNames.contains( fieldLower ) )
945 else if ( fieldLower ==
"value"_L1 )
949 else if ( fieldLower ==
"count"_L1 )
955 else if (
type != QMetaType::Type::Double )
957 if ( fieldLower.contains(
"red" ) || fieldLower ==
"r"_L1 )
959 if ( fieldLower.contains(
"min" ) )
963 else if ( fieldLower.contains(
"max" ) )
972 else if ( fieldLower.contains(
"green" ) || fieldLower ==
"g"_L1 )
974 if ( fieldLower.contains(
"min" ) )
978 else if ( fieldLower.contains(
"max" ) )
987 else if ( fieldLower.contains(
"blue" ) || fieldLower ==
"b"_L1 )
989 if ( fieldLower.contains(
"min" ) )
993 else if ( fieldLower.contains(
"max" ) )
1002 else if ( fieldLower.contains(
"alpha" ) || fieldLower ==
"a"_L1 )
1004 if ( fieldLower.contains(
"min" ) )
1008 else if ( fieldLower.contains(
"max" ) )
1021 if (
type == QMetaType::Type::QString )
1042 return tr(
"Green" );
1044 return tr(
"Blue" );
1046 return tr(
"Alpha" );
1048 return tr(
"Red Minimum" );
1050 return tr(
"Green Minimum" );
1052 return tr(
"Blue Minimum" );
1054 return tr(
"Alpha Minimum" );
1056 return tr(
"Red Maximum" );
1058 return tr(
"Green Maximum" );
1060 return tr(
"Blue Maximum" );
1062 return tr(
"Alpha Maximum" );
1064 return tr(
"Generic" );
1066 return tr(
"Name" );
1068 return tr(
"Pixel Count" );
1070 return tr(
"Maximum Count" );
1072 return tr(
"Value" );
1074 return tr(
"Minimum Value" );
1076 return tr(
"Maximum Value" );
1083 static const QList<Qgis::RasterAttributeTableFieldUsage> valueColorUsages { {
1100 return valueColorUsages;
1119 auto rat = std::make_unique<QgsRasterAttributeTable>();
1131 rat->appendRow( QVariantList() << klass.value << klass.label << 0 << 0 << 0 << 255 );
1132 rat->setColor(
static_cast<int>( rat->data().length() - 1 ), klass.color );
1137 *bandNumber = palettedRenderer->inputBand();
1147 auto rat = std::make_unique<QgsRasterAttributeTable>();
1148 switch ( shaderFunction->colorRampType() )
1163 const QList<QgsColorRampShader::ColorRampItem> rampItems { shaderFunction->colorRampItemList() };
1164 if ( rampItems.size() > 1 )
1166 QColor color1 { rampItems.at( 0 ).color };
1167 QString label1 { rampItems.at( 0 ).label };
1168 QVariant value1( rampItems.at( 0 ).value );
1169 const int rampItemSize =
static_cast<int>( rampItems.size() );
1170 for (
int i = 1; i < rampItemSize; ++i )
1173 rat->appendRow( QVariantList() << value1 << rampItem.
value << u
"%1 - %2"_s.arg( label1, rampItem.
label ) << 0 << 0 << 0 << 255 << 0 << 0 << 0 << 255 );
1174 rat->setRamp(
static_cast<int>( rat->data().length() - 1 ), color1, rampItem.
color );
1175 label1 = rampItem.
label;
1176 value1 = rampItem.
value;
1177 color1 = rampItem.
color;
1192 const QList<QgsColorRampShader::ColorRampItem> rampItems { shaderFunction->colorRampItemList() };
1193 if ( rampItems.size() > 1 )
1195 QColor color1 { rampItems.at( 0 ).color };
1196 QString label1 { rampItems.at( 0 ).label };
1197 QVariant value1( rampItems.at( 0 ).value );
1198 const int rampItemSize =
static_cast<int>( rampItems.size() );
1199 for (
int i = 1; i < rampItemSize; ++i )
1202 rat->appendRow( QVariantList() << value1 << rampItem.
value << u
"%1 - %2"_s.arg( label1, rampItem.
label ) << 0 << 0 << 0 << 255 << 0 << 0 << 0 << 255 );
1203 rat->setRamp(
static_cast<int>( rat->data().length() - 1 ), color1, rampItem.
color );
1204 label1 = rampItem.
label;
1205 value1 = rampItem.
value;
1206 color1 = rampItem.
color;
1220 const QList<QgsColorRampShader::ColorRampItem> rampItems { shaderFunction->colorRampItemList() };
1223 rat->appendRow( QVariantList() << rampItem.value << rampItem.label << 0 << 0 << 0 << 255 );
1224 rat->setColor(
static_cast<int>( rat->data().length() - 1 ), rampItem.color );
1232 *bandNumber = pseudoColorRenderer->inputBand();
1255 std::call_once( usageInformationLoaderFlag, [] {
1256 QgsRasterAttributeTable::sUsageInformation.insert(
1258 { tr(
"General Purpose Field" ),
false,
false,
false,
false,
true,
true, QList<QMetaType::Type>() << QMetaType::Type::QString << QMetaType::Type::Int << QMetaType::Type::LongLong << QMetaType::Type::Double }
1260 QgsRasterAttributeTable::sUsageInformation
1262 QgsRasterAttributeTable::sUsageInformation
1264 QgsRasterAttributeTable::sUsageInformation
1265 .insert(
Qgis::RasterAttributeTableFieldUsage::MinMax, { tr(
"Class Value (min=max)" ),
true,
true,
false,
false,
true,
false, QList<QMetaType::Type>() << QMetaType::Type::Int << QMetaType::Type::LongLong << QMetaType::Type::Double } );
1266 QgsRasterAttributeTable::sUsageInformation
1267 .insert(
Qgis::RasterAttributeTableFieldUsage::Min, { tr(
"Class Minimum Value" ),
true,
true,
false,
false,
true,
false, QList<QMetaType::Type>() << QMetaType::Type::Int << QMetaType::Type::LongLong << QMetaType::Type::Double } );
1268 QgsRasterAttributeTable::sUsageInformation
1269 .insert(
Qgis::RasterAttributeTableFieldUsage::Max, { tr(
"Class Maximum Value" ),
true,
true,
false,
false,
true,
false, QList<QMetaType::Type>() << QMetaType::Type::Int << QMetaType::Type::LongLong << QMetaType::Type::Double } );
1270 QgsRasterAttributeTable::sUsageInformation
1272 QgsRasterAttributeTable::sUsageInformation
1274 QgsRasterAttributeTable::sUsageInformation
1276 QgsRasterAttributeTable::sUsageInformation
1278 QgsRasterAttributeTable::sUsageInformation
1280 QgsRasterAttributeTable::sUsageInformation
1282 QgsRasterAttributeTable::sUsageInformation
1284 QgsRasterAttributeTable::sUsageInformation
1286 QgsRasterAttributeTable::sUsageInformation
1288 QgsRasterAttributeTable::sUsageInformation
1290 QgsRasterAttributeTable::sUsageInformation
1292 QgsRasterAttributeTable::sUsageInformation
1295 QgsRasterAttributeTable::sUsageInformation
1298 return QgsRasterAttributeTable::sUsageInformation;
1301void QgsRasterAttributeTable::setType()
1303 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages {
usages() };
1309QHash<int, QgsRasterAttributeTable::UsageInformation> QgsRasterAttributeTable::usageInformationInt()
1311 QHash<int, QgsRasterAttributeTable::UsageInformation> usageInfoInt;
1313 for (
auto it = usageInfo.cbegin(); it != usageInfo.cend(); ++it )
1315 usageInfoInt.insert(
static_cast<int>( it.key() ), it.value() );
1317 return usageInfoInt;
1328 QList<QgsRasterAttributeTable::MinMaxClass> classes;
1331 QgsDebugError(
"minMaxClasses was called on an invalid RAT" );
1335 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages {
usages() };
1339 QgsDebugError(
"minMaxClasses was called on a ramp raster" );
1345 Q_ASSERT( minMaxIndex >= 0 );
1347 int classificationIndex = classificationColumn;
1348 if ( classificationIndex >= 0 && classificationIndex < mFields.count() )
1350 const Field classificationField { mFields.at( classificationIndex ) };
1353 QgsDebugError(
"minMaxClasses was called with a classification column which is not suitable for classification" );
1357 else if ( classificationIndex == -1 )
1370 classificationIndex = minMaxIndex;
1373 else if ( classificationIndex >= mFields.count() )
1375 QgsDebugError(
"minMaxClasses was called with a classification column out of range" );
1379 if ( classificationIndex >= 0 )
1383 for (
const QVariantList &
row : std::as_const( mData ) )
1385 const QString label {
row.at( classificationIndex ).toString() };
1387 const double value {
row.at( minMaxIndex ).toDouble( &ok ) };
1391 QgsDebugError(
"minMaxClasses could not convert a MinMax value to double" );
1394 if ( labels.contains( label ) )
1396 classes[labels.indexOf( label )].minMaxValues.push_back(
value );
1400 labels.push_back( label );
1401 classes.push_back( { label, {
value },
color( rowIdx ) } );
1412 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages {
usages() };
1415 const bool isRange { minIdx >= 0 && maxIdx >= 0 };
1417 int labelIdx { labelColumn };
1418 if ( labelColumn < 0
1419 || labelColumn >=
fields().count()
1425 if ( !mData.isEmpty() && ( minIdx >= 0 && maxIdx >= 0 ) )
1432 const double range { max - min };
1436 if ( !std::isnan( min ) && !std::isnan( max ) )
1438 const QList<QVariantList> dataCopy(
orderedRows() );
1441 for (
const Field &f : std::as_const( mFields ) )
1445 for (
const QVariantList &r : std::as_const( dataCopy ) )
1450 QColor lastColor {
ramp.color1() };
1452 if ( hasColorOrRamp )
1455 ramp.setColor2( orderedRat.
hasColor() ? orderedRat.
color(
static_cast<int>( orderedRat.
data().count() - 1 ) ) : orderedRat.
ramp(
static_cast<int>( orderedRat.
data().count() - 1 ) ).
color2() );
1459 auto labelFromField = [&](
int rowIdx ) -> QString {
1462 return u
"%L1 - %L2"_s.arg( orderedRat.
value( rowIdx, minIdx ).toDouble() ).arg( orderedRat.
value( rowIdx, maxIdx ).toDouble() );
1464 const QVariant val( orderedRat.
value( rowIdx, labelIdx ) );
1467 switch ( val.userType() )
1469 case QMetaType::Type::QChar:
1470 return QString( val.toChar() );
1471 case QMetaType::Type::Int:
1472 res = QLocale().toString( val.toInt( &ok ) );
1474 case QMetaType::Type::LongLong:
1475 res = QLocale().toString( val.toLongLong( &ok ) );
1477 case QMetaType::Type::UInt:
1478 res = QLocale().toString( val.toUInt( &ok ) );
1480 case QMetaType::Type::ULongLong:
1481 res = QLocale().toString( val.toULongLong( &ok ) );
1483 case QMetaType::Type::Double:
1484 res = QLocale().toString( val.toDouble( &ok ),
'g' );
1486 case QMetaType::Type::QString:
1488 return val.toString();
1490 return ok ? res : val.toString();
1496 if ( orderedRat.
hasColor() && isRange )
1498 labels.push_back( labelFromField( 0 ) );
1500 for (
int rowIdx = 1; rowIdx < orderedRat.
data().count(); ++rowIdx )
1502 const double offset { ( orderedRat.
value( rowIdx, minIdx ).toDouble() - min ) / range };
1503 const QColor
color { orderedRat.
color( rowIdx - 1 ) };
1505 labels.push_back( labelFromField( rowIdx ) );
1514 else if ( orderedRat.
hasRamp() && isRange )
1516 double prevOffset { 0 };
1517 labels.push_back( labelFromField( 0 ) );
1518 for (
int rowIdx = 1; rowIdx < orderedRat.
data().count(); ++rowIdx )
1520 labels.push_back( labelFromField( rowIdx ) );
1521 const int prevRowIdx { rowIdx - 1 };
1522 const double offset { ( ( orderedRat.
value( rowIdx, minIdx ).toDouble() + orderedRat.
value( prevRowIdx, maxIdx ).toDouble() ) / 2.0 - min ) / range };
1530 stops.append(
QgsGradientStop( offset + std::numeric_limits<double>::epsilon(), currentRamp.
color1() ) );
1532 prevOffset = offset;
1540 labels.push_back( labelFromField( 0 ) );
1542 for (
int rowIdx = 1; rowIdx < orderedRat.
data().count(); ++rowIdx )
1544 const int prevRowIdx { rowIdx - 1 };
1545 const double offset { ( ( orderedRat.
value( rowIdx, minIdx ).toDouble() + orderedRat.
value( prevRowIdx, maxIdx ).toDouble() ) / 2.0 - min ) / range };
1547 labels.push_back( labelFromField( rowIdx ) );
1554 ramp.setStops( stops );
1568 std::unique_ptr<QgsRasterRenderer> renderer;
1572 std::unique_ptr<QgsColorRamp>
ramp;
1575 ramp = std::make_unique<QgsRandomColorRamp>();
1578 if ( classes.isEmpty() )
1581 renderer = std::make_unique<QgsPalettedRasterRenderer>( provider, bandNumber, classes );
1585 auto pseudoColorRenderer = std::make_unique<QgsSingleBandPseudoColorRenderer>( provider, bandNumber );
1589 int labelColumn { classificationColumn };
1590 if ( labelColumn < 0 )
1592 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages {
usages() };
1594 if ( labelColumn < 0 )
1600 pseudoColorRenderer->setClassificationMin(
minimumValue() );
1601 pseudoColorRenderer->setClassificationMax(
maximumValue() );
1603 pseudoColorRenderer->createShader(
1606 if ( pseudoColorRenderer->shader() )
1608 pseudoColorRenderer->shader()->setMaximumValue(
maximumValue() );
1609 pseudoColorRenderer->shader()->setMinimumValue(
minimumValue() );
1615 const bool labelsAreUsable {
ramp->count() > 2 && labels.count() ==
ramp->count() - 1 };
1617 if ( labelsAreUsable )
1619 QList<QgsColorRampShader::ColorRampItem> newItemList;
1622 for (
const QString &label : std::as_const( labels ) )
1624 if ( stopIdx >=
ramp->count() - 2 )
1630 newItemList.push_back( item );
1635 newItemList.push_back( item );
1636 shaderFunction->setColorRampItemList( newItemList );
1640 renderer = std::move( pseudoColorRenderer );
1648 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages {
usages() };
1651 const bool isRange { minIdx >= 0 && maxIdx >= 0 };
1652 QList<QVariantList> dataCopy( mData );
1656 std::sort( dataCopy.begin(), dataCopy.end(), [&](
const QVariantList &first,
const QVariantList &second ) ->
bool {
1657 return ( first.at( maxIdx ).toDouble() + first.at( minIdx ).toDouble() ) < ( second.at( maxIdx ).toDouble() + second.at( minIdx ).toDouble() );
1663 if ( minMaxIdx < 0 )
1669 std::sort( dataCopy.begin(), dataCopy.end(), [&](
const QVariantList &first,
const QVariantList &second ) ->
bool { return first.at( minMaxIdx ).toDouble() < second.at( minMaxIdx ).toDouble(); } );
@ Exact
Assigns the color of the exact matching value in the color ramp item list.
@ Linear
Interpolates the color between two class breaks linearly.
@ Discrete
Assigns the color of the higher class for every pixel between two class breaks.
RasterAttributeTableType
The RasterAttributeTableType enum represents the type of RAT.
@ Continuous
Uses breaks from color palette.
RasterAttributeTableFieldUsage
The RasterAttributeTableFieldUsage enum represents the usage of a Raster Attribute Table field.
@ RedMax
Field usage RedMax.
@ PixelCount
Field usage PixelCount.
@ AlphaMax
Field usage AlphaMax.
@ BlueMin
Field usage BlueMin.
@ Alpha
Field usage Alpha.
@ Generic
Field usage Generic.
@ GreenMax
Field usage GreenMax.
@ RedMin
Field usage RedMin.
@ MinMax
Field usage MinMax.
@ BlueMax
Field usage BlueMax.
@ AlphaMin
Field usage AlphaMin.
@ Green
Field usage Green.
@ MaxCount
Not used by QGIS: GDAL Maximum GFU value (equals to GFU_AlphaMax+1 currently).
@ GreenMin
Field usage GreenMin.
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
Represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
int attributeCount() const
Returns the number of attributes attached to the feature.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
static QString ensureFileNameHasExtension(const QString &fileName, const QStringList &extensions)
Ensures that a fileName ends with an extension from the provided list of extensions.
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
QColor color1() const
Returns the gradient start color.
QgsGradientColorRamp * clone() const override
Creates a clone of the color ramp.
QColor color2() const
Returns the gradient end color.
Represents a color stop within a QgsGradientColorRamp color ramp.
Renderer for paletted raster images.
static QgsPalettedRasterRenderer::MultiValueClassData rasterAttributeTableToClassData(const QgsRasterAttributeTable *attributeTable, int classificationColumn=-1, QgsColorRamp *ramp=nullptr)
Reads and returns classes from the Raster Attribute Table attributeTable, optionally classifying the ...
QList< QgsPalettedRasterRenderer::Class > ClassData
Map of value to class properties.
QList< QgsPalettedRasterRenderer::MultiValueClass > MultiValueClassData
Map of multi value to class properties.
The Field class represents a Raster Attribute Table field, including its name, usage and type.
Qgis::RasterAttributeTableFieldUsage usage
bool isRamp() const
Returns true if the field carries a color ramp component information (RedMin/RedMax,...
bool isColor() const
Returns true if the field carries a color component (Red, Green, Blue and optionally Alpha) informati...
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.
QgsRasterRenderer * renderer() const
Returns the raster's renderer.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
Raster renderer pipe that applies colors to a raster.
Interface for all raster shaders.
Raster renderer pipe for single band pseudocolor.
static QMetaType::Type variantTypeToMetaType(QVariant::Type variantType)
Converts a QVariant::Type to a QMetaType::Type.
static QVariant createNullVariant(QMetaType::Type metaType)
Helper method to properly create a null QVariant from a metaType Returns the created QVariant.
Options to pass to QgsVectorFileWriter::writeAsVectorFormat().
QString fileEncoding
Encoding to use.
QString driverName
OGR driver to use.
QStringList layerOptions
List of OGR layer creation options.
QgsVectorFileWriter::ActionOnExistingFile actionOnExistingFile
Action on existing file.
static std::unique_ptr< QgsVectorFileWriter > create(const QString &fileName, const QgsFields &fields, Qgis::WkbType geometryType, const QgsCoordinateReferenceSystem &srs, const QgsCoordinateTransformContext &transformContext, const QgsVectorFileWriter::SaveVectorOptions &options, QgsFeatureSink::SinkFlags sinkFlags=QgsFeatureSink::SinkFlags(), QString *newFilename=nullptr, QString *newLayer=nullptr)
Create a new vector file writer.
@ CreateOrOverwriteFile
Create or overwrite file.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
QList< QgsGradientStop > QgsGradientStopsList
List of gradient stops.
QList< QgsFeature > QgsFeatureList
#define QgsDebugError(str)
Setting options for creating vector data providers.
Properties of a single value class.