QGIS API Documentation 4.5.0-Master (c9e0ddc514d)
Loading...
Searching...
No Matches
qgsrasterattributetable.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrasterattributetable.cpp - 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
18
19#include <cmath>
20#include <memory>
21#include <mutex>
22
23#include "qgsfileutils.h"
24#include "qgsogrprovider.h"
26#include "qgsrasterlayer.h"
27#include "qgsrastershader.h"
30#include "qgsvectorfilewriter.h"
31
32#include <QLocale>
33#include <QString>
34
35using namespace Qt::StringLiterals;
36
38std::once_flag usageInformationLoaderFlag;
39QHash<Qgis::RasterAttributeTableFieldUsage, QgsRasterAttributeTable::UsageInformation> QgsRasterAttributeTable::sUsageInformation;
41
42
47
48
50{
51 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages { usages() };
52 return fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::Red )
53 && fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::Green )
54 && fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::Blue );
55}
56
57bool QgsRasterAttributeTable::setColor( const int row, const QColor &color )
58{
59 if ( !hasColor() || row < 0 || row >= mData.count() )
60 {
61 return false;
62 }
63
64 for ( int idx = 0; idx < mFields.count(); ++idx )
65 {
66 const Field f { mFields.at( idx ) };
67 switch ( f.usage )
68 {
70 setValue( row, idx, color.red() );
71 break;
73 setValue( row, idx, color.green() );
74 break;
76 setValue( row, idx, color.blue() );
77 break;
79 setValue( row, idx, color.alpha() );
80 break;
81 default:
82 break;
83 }
84 }
85 return true;
86}
87
88
89bool QgsRasterAttributeTable::setRamp( const int row, const QColor &colorMin, const QColor &colorMax )
90{
91 if ( !hasRamp() || row < 0 || row >= mData.count() )
92 {
93 return false;
94 }
95
96 int idx = 0;
97 for ( Field &f : mFields )
98 {
99 switch ( f.usage )
100 {
102 setValue( row, idx, colorMin.red() );
103 break;
105 setValue( row, idx, colorMin.green() );
106 break;
108 setValue( row, idx, colorMin.blue() );
109 break;
111 setValue( row, idx, colorMin.alpha() );
112 break;
114 setValue( row, idx, colorMax.red() );
115 break;
117 setValue( row, idx, colorMax.green() );
118 break;
120 setValue( row, idx, colorMax.blue() );
121 break;
123 setValue( row, idx, colorMax.alpha() );
124 break;
125 default:
126 break;
127 }
128 idx++;
129 }
130 return true;
131}
132
134{
135 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages { usages() };
136 return fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::RedMin )
137 && fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::GreenMin )
138 && fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::BlueMin )
139 && fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::RedMax )
140 && fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::GreenMax )
141 && fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::BlueMax );
142}
143
144
145QList<Qgis::RasterAttributeTableFieldUsage> QgsRasterAttributeTable::usages() const
146{
147 QList<Qgis::RasterAttributeTableFieldUsage> usages;
148 for ( const QgsRasterAttributeTable::Field &field : std::as_const( mFields ) )
149 {
150 usages.push_back( field.usage );
151 }
152 return usages;
153}
154
156QList<int> QgsRasterAttributeTable::intUsages() const
157{
158 QList<int> usages;
159 for ( const QgsRasterAttributeTable::Field &field : std::as_const( mFields ) )
160 {
161 usages.push_back( static_cast<int>( field.usage ) );
162 }
163 return usages;
164}
166
168{
169 QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages { usages() };
170 // No ramps support here
171 if ( hasColor()
172 && row < mData.count()
173 && fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::Red )
174 && fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::Green )
175 && fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::Blue ) )
176 {
177 const QVariantList rowData = mData.at( row );
178 QColor color {
179 rowData.at( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::Red ) ).toInt(),
180 rowData.at( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::Green ) ).toInt(),
181 rowData.at( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::Blue ) ).toInt()
182 };
183 if ( fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::Alpha ) )
184 {
185 color.setAlpha( rowData.at( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::Alpha ) ).toInt() );
186 }
187 return color;
188 }
189 return QColor();
190}
191
193{
194 if ( !hasRamp() || row < 0 || row >= mData.count() )
195 {
196 return QgsGradientColorRamp();
197 }
198 QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages { usages() };
199 const QVariantList rowData = mData.at( row );
200 QColor colorMin {
201 rowData.at( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::RedMin ) ).toInt(),
202 rowData.at( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::GreenMin ) ).toInt(),
203 rowData.at( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::BlueMin ) ).toInt()
204 };
205 if ( fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::AlphaMin ) )
206 {
207 colorMin.setAlpha( rowData.at( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::AlphaMin ) ).toInt() );
208 }
209 QColor colorMax {
210 rowData.at( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::RedMax ) ).toInt(),
211 rowData.at( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::GreenMax ) ).toInt(),
212 rowData.at( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::BlueMax ) ).toInt()
213 };
214 if ( fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::AlphaMax ) )
215 {
216 colorMax.setAlpha( rowData.at( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::AlphaMax ) ).toInt() );
217 }
218 return QgsGradientColorRamp( colorMin, colorMax );
219}
220
221QList<QgsRasterAttributeTable::Field> QgsRasterAttributeTable::fields() const
222{
223 return mFields;
224}
225
227{
228 QgsFields qFields;
229
230 for ( const QgsRasterAttributeTable::Field &field : std::as_const( mFields ) )
231 {
232 qFields.append( QgsField( field.name, field.type ) );
233 }
234 return qFields;
235}
236
238{
239 QgsFeatureList features;
240 for ( const QVariantList &row : std::as_const( mData ) )
241 {
242 QgsAttributes attributes;
243 for ( const auto &cell : std::as_const( row ) )
244 {
245 attributes.append( cell );
246 }
247 QgsFeature feature { qgisFields() };
248 feature.setAttributes( attributes );
249 features.append( feature );
250 }
251 return features;
252}
253
255{
256 return mIsDirty;
257}
258
260{
261 mIsDirty = isDirty;
262}
263
264bool QgsRasterAttributeTable::insertField( int position, const Field &field, QString *errorMessage )
265{
266 const int realPos { std::clamp( position, 0, static_cast<int>( mFields.count() ) ) };
267
268 if ( field.name.isEmpty() )
269 {
270 if ( errorMessage )
271 {
272 *errorMessage = tr( "Field name must not be empty." );
273 }
274 return false;
275 }
276
277 // Check for duplicate names
278 bool ok;
279 fieldByName( field.name, &ok );
280
281 if ( ok )
282 {
283 if ( errorMessage )
284 {
285 *errorMessage = tr( "A field with name '%1' already exists." ).arg( field.name );
286 }
287 return false;
288 }
289
290 // Check for duplicate unique usages
291 static const QList<Qgis::RasterAttributeTableFieldUsage> uniqueUsages {
309 };
310
311 if ( uniqueUsages.contains( field.usage ) && !fieldsByUsage( field.usage ).isEmpty() )
312 {
313 if ( errorMessage )
314 {
315 *errorMessage = tr( "A field with unique usage '%1' already exists." ).arg( usageName( field.usage ) );
316 }
317 return false;
318 }
319
320 mFields.insert( realPos, field );
321
322 for ( auto it = mData.begin(); it != mData.end(); ++it )
323 {
324 QVariant defaultValue = QgsVariantUtils::createNullVariant( field.type );
325 // Set default values
326 switch ( field.type )
327 {
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:
334 defaultValue = 0;
335 break;
336 default:
337 defaultValue = QString();
338 }
339 it->insert( realPos, defaultValue );
340 }
341
342 // Set/change the table type from the value field type
344 {
346 }
348 {
350 }
351
352 setType();
353 setDirty( true );
354
355 return true;
356}
357
358bool QgsRasterAttributeTable::insertField( int position, const QString &name, const Qgis::RasterAttributeTableFieldUsage usage, const QMetaType::Type type, QString *errorMessage )
359{
360 return insertField( position, { name, usage, type }, errorMessage );
361}
362
363bool QgsRasterAttributeTable::insertField( int position, const QString &name, const Qgis::RasterAttributeTableFieldUsage usage, const QVariant::Type type, QString *errorMessage )
364{
365 return insertField( position, name, usage, QgsVariantUtils::variantTypeToMetaType( type ), errorMessage );
366}
367
368bool QgsRasterAttributeTable::insertColor( int position, QString *errorMessage )
369{
370 const QList<Qgis::RasterAttributeTableFieldUsage> colors {
372 };
373 int idx { position };
374 for ( const Qgis::RasterAttributeTableFieldUsage usage : std::as_const( colors ) )
375 {
376 if ( !insertField( idx, usageName( usage ), usage, QMetaType::Type::Int, errorMessage ) )
377 {
378 return false;
379 }
380 ++idx;
381 }
382 return true;
383}
384
386{
387 if ( fieldIndex < 0 || fieldIndex >= fields().count() )
388 {
389 return false;
390 }
391
392 const Field field { fields().at( fieldIndex ) };
393 if ( !usageInformation()[usage].allowedTypes.contains( field.type ) )
394 {
395 return false;
396 }
397
398 mFields[fieldIndex].usage = usage;
399 setType();
400
401 return true;
402}
403
404bool QgsRasterAttributeTable::insertRamp( int position, QString *errorMessage )
405{
407 {
408 if ( errorMessage )
409 {
410 *errorMessage = tr( "A color ramp can only be added to an athematic attribute table." );
411 }
412 }
413 const QList<Qgis::RasterAttributeTableFieldUsage> colors {
422 };
423 int idx { position };
424 for ( const Qgis::RasterAttributeTableFieldUsage usage : std::as_const( colors ) )
425 {
426 if ( !insertField( idx, usageName( usage ), usage, QMetaType::Type::Int, errorMessage ) )
427 {
428 return false;
429 }
430 ++idx;
431 }
432 return true;
433}
434
435bool QgsRasterAttributeTable::appendField( const QString &name, const Qgis::RasterAttributeTableFieldUsage usage, const QMetaType::Type type, QString *errorMessage )
436{
437 return insertField( static_cast<int>( mFields.count() ), name, usage, type, errorMessage );
438}
439
440bool QgsRasterAttributeTable::appendField( const QString &name, const Qgis::RasterAttributeTableFieldUsage usage, const QVariant::Type type, QString *errorMessage )
441{
442 return appendField( name, usage, QgsVariantUtils::variantTypeToMetaType( type ), errorMessage );
443}
444
445bool QgsRasterAttributeTable::appendField( const Field &field, QString *errorMessage )
446{
447 return insertField( static_cast<int>( mFields.count() ), field, errorMessage );
448}
449
450bool QgsRasterAttributeTable::removeField( const QString &name, QString *errorMessage )
451{
452 const auto toRemove { std::find_if( mFields.begin(), mFields.end(), [&name]( Field &f ) -> bool { return f.name == name; } ) };
453
454 if ( toRemove != mFields.end() )
455 {
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 )
459 {
460 it->removeAt( idx );
461 }
462 setType();
463 setDirty( true );
464 return true;
465 }
466
467 if ( errorMessage )
468 {
469 *errorMessage = tr( "A field with name '%1' was not found." ).arg( name );
470 }
471 return false;
472}
473
474bool QgsRasterAttributeTable::insertRow( int position, const QVariantList &rowData, QString *errorMessage )
475{
476 const int realPos { std::clamp( position, 0, static_cast<int>( mData.count() ) ) };
477
478 if ( rowData.size() != mFields.size() )
479 {
480 if ( errorMessage )
481 {
482 *errorMessage = tr( "Row element count differs from field count (%1)." ).arg( mFields.size() );
483 }
484 return false;
485 }
486
487 QVariantList dataValid;
488
489 for ( int idx = 0; idx < mFields.count(); ++idx )
490 {
491 const QMetaType::Type type = mFields.at( idx ).type;
492 QVariant cell( rowData[idx] );
493 if ( type == QMetaType::Type::QDateTime && cell.toString().isEmpty() )
494 {
495 dataValid.append( QVariant() );
496 }
497 else if ( !cell.canConvert( QMetaType( type ) ) || !cell.convert( QMetaType( type ) ) )
498 {
499 if ( errorMessage )
500 {
501 *errorMessage = tr( "Row data at column %1 cannot be converted to field type (%2)." ).arg( idx ).arg( QMetaType( type ).name() );
502 }
503 return false;
504 }
505 else
506 {
507 dataValid.append( cell );
508 }
509 }
510
511 mData.insert( realPos, dataValid );
512 setDirty( true );
513 return true;
514}
515
516bool QgsRasterAttributeTable::removeRow( int position, QString *errorMessage )
517{
518 if ( position >= mData.count() || position < 0 || mData.isEmpty() )
519 {
520 if ( errorMessage )
521 {
522 *errorMessage = tr( "Position is not valid or the table is empty." );
523 }
524 return false;
525 }
526 mData.removeAt( position );
527 setDirty( true );
528 return true;
529}
530
531bool QgsRasterAttributeTable::appendRow( const QVariantList &data, QString *errorMessage )
532{
533 return insertRow( static_cast<int>( mData.count() ), data, errorMessage );
534}
535
536bool QgsRasterAttributeTable::writeToFile( const QString &path, QString *errorMessage )
537{
540 options.driverName = u"ESRI Shapefile"_s;
541 options.fileEncoding = u"UTF-8"_s;
542 options.layerOptions = QStringList() << u"SHPT=NULL"_s;
543
544 std::unique_ptr<QgsVectorFileWriter> writer;
545
546 // Strip .dbf from path because OGR adds it back
547 QString cleanedPath { path };
548 if ( path.endsWith( u".dbf"_s, Qt::CaseSensitivity::CaseInsensitive ) )
549 {
550 cleanedPath.chop( 4 );
551 }
552
553 cleanedPath = QgsFileUtils::ensureFileNameHasExtension( cleanedPath, { { u".vat"_s } } );
554
556
557 cleanedPath.append( u".dbf"_s );
558
559 const QgsVectorFileWriter::WriterError error { writer->hasError() };
561 {
562 if ( errorMessage )
563 {
564 *errorMessage = tr( "Error creating Raster Attribute Table table: %1." ).arg( writer->errorMessage() );
565 }
566 return false;
567 }
568
569 QgsFeatureList features { qgisFeatures() };
570 bool result { writer->addFeatures( features ) };
571
572 if ( !result )
573 {
574 if ( errorMessage )
575 {
576 *errorMessage = tr( "Error creating Raster Attribute Table table: could not add rows." );
577 }
578 return false;
579 }
580
581 result = writer->flushBuffer();
582
583 if ( result )
584 {
585 mFilePath = cleanedPath;
586 setDirty( false );
587 }
588
589 return result;
590}
591
592bool QgsRasterAttributeTable::readFromFile( const QString &path, QString *errorMessage )
593{
594 QgsOgrProvider ratDbfSource { path, QgsDataProvider::ProviderOptions() };
595 if ( !ratDbfSource.isValid() )
596 {
597 if ( errorMessage )
598 {
599 *errorMessage = tr( "Error reading Raster Attribute Table table from file: invalid layer." );
600 }
601 return false;
602 }
603
604 QList<Field> oldFields = mFields;
605 QList<QVariantList> oldData = mData;
606
607 mFields.clear();
608 mData.clear();
609
610 bool hasValueField { false };
611 for ( const QgsField &field : ratDbfSource.fields() )
612 {
613 const Qgis::RasterAttributeTableFieldUsage usage { guessFieldUsage( field.name(), field.type() ) };
614 QMetaType::Type type { field.type() };
615 // DBF sets all int fields to long but for RGBA it doesn't make sense
616 if ( type == QMetaType::Type::LongLong
618 {
619 type = QMetaType::Type::Int;
620 }
621
623 {
624 hasValueField = true;
625 }
626
627 QgsRasterAttributeTable::Field ratField { field.name(), usage, type };
628 if ( !appendField( ratField, errorMessage ) )
629 {
630 mFields = oldFields;
631 mData = oldData;
632 return false;
633 }
634 }
635
636 // Do we have a value field? If not, try to guess one
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 ) )
638 {
640 }
641
642 const int fieldCount { static_cast<int>( ratDbfSource.fields().count() ) };
643 QgsFeature f;
644 QgsFeatureIterator fit { ratDbfSource.getFeatures( QgsFeatureRequest() ) };
645 while ( fit.nextFeature( f ) )
646 {
647 if ( f.attributeCount() != fieldCount )
648 {
649 if ( errorMessage )
650 {
651 *errorMessage = tr( "Error reading Raster Attribute Table table from file: number of fields and number of attributes do not match." );
652 }
653 mFields = oldFields;
654 mData = oldData;
655 return false;
656 }
657 appendRow( f.attributes().toList() );
658 }
659
660 mFilePath = path;
661 setDirty( false );
662
663 return true;
664}
665
666
667bool QgsRasterAttributeTable::isValid( QString *errorMessage ) const
668{
669 QStringList errors;
670
671 if ( mFields.isEmpty() )
672 {
673 errors.push_back( tr( "The attribute table has no fields." ) );
674 }
675
676 if ( mData.isEmpty() )
677 {
678 errors.push_back( tr( "The attribute table has no rows." ) );
679 }
680
681 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages { usages() };
682 const bool isMinMax { fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::MinMax ) };
683 const bool isValueRamp { fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::Min ) && fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::Max ) };
684 if ( !isMinMax && !isValueRamp )
685 {
686 errors.push_back( tr( "The attribute table has no MinMax nor a pair of Min and Max fields." ) );
687 }
688
689 // Check color
690 if ( fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::Red )
691 || fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::Green )
692 || fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::Blue ) )
693 {
694 if ( !( fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::Red )
695 && fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::Green )
696 && fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::Blue ) ) )
697 {
698 errors.push_back( tr( "The attribute table has some but not all the fields required for color definition (Red, Green, Blue)." ) );
699 }
700 }
701
702 // Check ramp
703 if ( fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::RedMin )
704 || fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::GreenMin )
705 || fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::BlueMin )
706 || fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::RedMax )
707 || fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::GreenMax )
708 || fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::BlueMax ) )
709 {
710 if ( !( fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::RedMin )
711 && fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::GreenMin )
712 && fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::BlueMin )
713 && fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::RedMax )
714 && fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::GreenMax )
715 && fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::BlueMax ) ) )
716 {
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)." ) );
718 }
719 else if ( !isValueRamp )
720 {
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." ) );
722 }
723 }
724
725 if ( errorMessage && !errors.isEmpty() )
726 {
727 *errorMessage = errors.join( QChar( '\n' ) );
728 }
729
730 return errors.isEmpty();
731}
732
733const QList<QList<QVariant> > QgsRasterAttributeTable::data() const
734{
735 return mData;
736}
737
739{
740 for ( const Field &f : std::as_const( mFields ) )
741 {
742 if ( f.name == name )
743 {
744 if ( ok )
745 {
746 *ok = true;
747 }
748 return f;
749 }
750 }
751 if ( ok )
752 {
753 *ok = false;
754 }
755 return Field( QString(), Qgis::RasterAttributeTableFieldUsage::Generic, QMetaType::Type::QString );
756}
757
758const QList<QgsRasterAttributeTable::Field> QgsRasterAttributeTable::fieldsByUsage( const Qgis::RasterAttributeTableFieldUsage fieldUsage ) const
759{
760 QList<QgsRasterAttributeTable::Field> result;
761 for ( const Field &f : std::as_const( mFields ) )
762 {
763 if ( f.usage == fieldUsage )
764 {
765 result.push_back( f );
766 }
767 }
768 return result;
769}
770
771bool QgsRasterAttributeTable::setValue( const int row, const int column, const QVariant &value )
772{
773 if ( row < 0 || row >= mData.count() || column < 0 || column >= mData[row].count() )
774 {
775 return false;
776 }
777
778 QVariant newVal = value;
779 QMetaType fieldType = QMetaType( mFields.at( column ).type );
780 if ( column >= mFields.length() || !value.canConvert( fieldType ) || !newVal.convert( fieldType ) )
781 {
782 return false;
783 }
784
785 const QVariant oldVal = mData[row][column];
786
787 if ( newVal != oldVal )
788 {
789 mData[row][column] = newVal;
790 setDirty( true );
791 }
792
793 return true;
794}
795
796QVariant QgsRasterAttributeTable::value( const int row, const int column ) const
797{
798 if ( row < 0 || row >= mData.count() || column < 0 || column >= mData[row].count() )
799 {
800 return QVariant();
801 }
802 return mData[row][column];
803}
804
806{
807 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages { usages() };
808 bool ok { false };
809 int fieldIdx { -1 };
810
811 if ( fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::MinMax ) )
812 {
813 fieldIdx = static_cast<int>( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::MinMax ) );
814 }
815 else if ( fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::Min ) )
816 {
817 fieldIdx = static_cast<int>( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::Min ) );
818 }
819
820 double min { std::numeric_limits<double>::max() };
821 for ( int rowIdx = 0; rowIdx < mData.count(); ++rowIdx )
822 {
823 min = std::min( min, value( rowIdx, fieldIdx ).toDouble( &ok ) );
824 if ( !ok )
825 {
826 return std::numeric_limits<double>::quiet_NaN();
827 }
828 }
829
830 if ( fieldIdx == -1 || !ok )
831 {
832 return std::numeric_limits<double>::quiet_NaN();
833 }
834 else
835 {
836 return min;
837 }
838}
839
841{
842 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages { usages() };
843 bool ok { false };
844 int fieldIdx { -1 };
845
846 if ( fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::MinMax ) )
847 {
848 fieldIdx = static_cast<int>( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::MinMax ) );
849 }
850 else if ( fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::Max ) )
851 {
852 fieldIdx = static_cast<int>( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::Max ) );
853 }
854
855 double max { std::numeric_limits<double>::lowest() };
856 for ( int rowIdx = 0; rowIdx < mData.count(); ++rowIdx )
857 {
858 max = std::max( max, value( rowIdx, fieldIdx ).toDouble( &ok ) );
859 if ( !ok )
860 {
861 return std::numeric_limits<double>::quiet_NaN();
862 }
863 }
864
865 if ( fieldIdx == -1 || !ok )
866 {
867 return std::numeric_limits<double>::quiet_NaN();
868 }
869 else
870 {
871 return max;
872 }
873}
874
875QVariantList QgsRasterAttributeTable::row( const double matchValue ) const
876{
877 if ( !isValid() )
878 {
879 return QVariantList();
880 }
881
882 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages { usages() };
883
884 if ( fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::MinMax ) )
885 {
886 const int colIdx { static_cast<int>( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::MinMax ) ) };
887 for ( int rowIdx = 0; rowIdx < mData.count(); ++rowIdx )
888 {
889 bool ok;
890 if ( matchValue == value( rowIdx, colIdx ).toDouble( &ok ) && ok )
891 {
892 return mData.at( rowIdx );
893 }
894 }
895 }
896 else
897 {
898 const int minColIdx { static_cast<int>( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::Min ) ) };
899 const int maxColIdx { static_cast<int>( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::Max ) ) };
900 for ( int rowIdx = 0; rowIdx < mData.count(); ++rowIdx )
901 {
902 bool ok;
903 if ( matchValue >= value( rowIdx, minColIdx ).toDouble( &ok ) && ok )
904 {
905 if ( matchValue < value( rowIdx, maxColIdx ).toDouble( &ok ) && ok )
906 {
907 return mData.at( rowIdx );
908 }
909 }
910 }
911 }
912 return QVariantList();
913}
914
916{
917 static const QStringList minValueNames { {
918 u"min"_s,
919 u"min_value"_s,
920 u"min value"_s,
921 u"value min"_s,
922 u"value_min"_s,
923 } };
924
925 static const QStringList maxValueNames { {
926 u"max"_s,
927 u"max_value"_s,
928 u"max value"_s,
929 u"value max"_s,
930 u"value_max"_s,
931 } };
932
933 const QString fieldLower { name.toLower() };
934
935 if ( type == QMetaType::Type::Double || type == QMetaType::Type::Int || type == QMetaType::Type::UInt || type == QMetaType::Type::LongLong || type == QMetaType::Type::ULongLong )
936 {
937 if ( minValueNames.contains( fieldLower ) )
938 {
940 }
941 else if ( maxValueNames.contains( fieldLower ) )
942 {
944 }
945 else if ( fieldLower == "value"_L1 )
946 {
948 }
949 else if ( fieldLower == "count"_L1 )
950 {
951 // This could really be max count but it's more likely pixel count
953 }
954 // Colors (not double)
955 else if ( type != QMetaType::Type::Double )
956 {
957 if ( fieldLower.contains( "red" ) || fieldLower == "r"_L1 )
958 {
959 if ( fieldLower.contains( "min" ) )
960 {
962 }
963 else if ( fieldLower.contains( "max" ) )
964 {
966 }
967 else
968 {
970 }
971 }
972 else if ( fieldLower.contains( "green" ) || fieldLower == "g"_L1 )
973 {
974 if ( fieldLower.contains( "min" ) )
975 {
977 }
978 else if ( fieldLower.contains( "max" ) )
979 {
981 }
982 else
983 {
985 }
986 }
987 else if ( fieldLower.contains( "blue" ) || fieldLower == "b"_L1 )
988 {
989 if ( fieldLower.contains( "min" ) )
990 {
992 }
993 else if ( fieldLower.contains( "max" ) )
994 {
996 }
997 else
998 {
1000 }
1001 }
1002 else if ( fieldLower.contains( "alpha" ) || fieldLower == "a"_L1 )
1003 {
1004 if ( fieldLower.contains( "min" ) )
1005 {
1007 }
1008 else if ( fieldLower.contains( "max" ) )
1009 {
1011 }
1012 else
1013 {
1015 }
1016 }
1017 }
1018 // end colors
1019 }
1020
1021 if ( type == QMetaType::Type::QString ) // default to name for strings
1022 {
1024 }
1025
1026 // default to generic for all other cases
1028}
1029
1034
1036{
1037 switch ( usage )
1038 {
1040 return tr( "Red" );
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" );
1077 }
1078 return QString();
1079}
1080
1102
1103std::unique_ptr<QgsRasterAttributeTable> QgsRasterAttributeTable::createFromRaster( QgsRasterLayer *raster, int *bandNumber )
1104{
1105 if ( !raster || !raster->dataProvider() || !raster->isValid() )
1106 {
1107 return nullptr;
1108 }
1109
1110 const QgsRasterRenderer *renderer = raster->renderer();
1111
1112 if ( !renderer )
1113 {
1114 return nullptr;
1115 }
1116
1117 if ( const QgsPalettedRasterRenderer *palettedRenderer = dynamic_cast<const QgsPalettedRasterRenderer *>( renderer ) )
1118 {
1119 auto rat = std::make_unique<QgsRasterAttributeTable>();
1120 rat->appendField( u"Value"_s, Qgis::RasterAttributeTableFieldUsage::MinMax, QMetaType::Type::Double );
1121 rat->appendField( u"Class"_s, Qgis::RasterAttributeTableFieldUsage::Name, QMetaType::Type::QString );
1122 rat->appendField( u"Red"_s, Qgis::RasterAttributeTableFieldUsage::Red, QMetaType::Type::Int );
1123 rat->appendField( u"Green"_s, Qgis::RasterAttributeTableFieldUsage::Green, QMetaType::Type::Int );
1124 rat->appendField( u"Blue"_s, Qgis::RasterAttributeTableFieldUsage::Blue, QMetaType::Type::Int );
1125 rat->appendField( u"Alpha"_s, Qgis::RasterAttributeTableFieldUsage::Alpha, QMetaType::Type::Int );
1126
1127 const QgsPalettedRasterRenderer::ClassData classes { palettedRenderer->classes() };
1128
1129 for ( const QgsPalettedRasterRenderer::Class &klass : std::as_const( classes ) )
1130 {
1131 rat->appendRow( QVariantList() << klass.value << klass.label << 0 << 0 << 0 << 255 );
1132 rat->setColor( static_cast<int>( rat->data().length() - 1 ), klass.color );
1133 }
1134
1135 if ( bandNumber )
1136 {
1137 *bandNumber = palettedRenderer->inputBand();
1138 }
1139 return rat;
1140 }
1141 else if ( const QgsSingleBandPseudoColorRenderer *pseudoColorRenderer = dynamic_cast<const QgsSingleBandPseudoColorRenderer *>( renderer ) )
1142 {
1143 if ( const QgsRasterShader *shader = pseudoColorRenderer->shader() )
1144 {
1145 if ( const QgsColorRampShader *shaderFunction = dynamic_cast<const QgsColorRampShader *>( shader->rasterShaderFunction() ) )
1146 {
1147 auto rat = std::make_unique<QgsRasterAttributeTable>();
1148 switch ( shaderFunction->colorRampType() )
1149 {
1151 {
1152 rat->appendField( u"Min"_s, Qgis::RasterAttributeTableFieldUsage::Min, QMetaType::Type::Double );
1153 rat->appendField( u"Max"_s, Qgis::RasterAttributeTableFieldUsage::Max, QMetaType::Type::Double );
1154 rat->appendField( u"Class"_s, Qgis::RasterAttributeTableFieldUsage::Name, QMetaType::Type::QString );
1155 rat->appendField( u"RedMin"_s, Qgis::RasterAttributeTableFieldUsage::RedMin, QMetaType::Type::Int );
1156 rat->appendField( u"GreenMin"_s, Qgis::RasterAttributeTableFieldUsage::GreenMin, QMetaType::Type::Int );
1157 rat->appendField( u"BlueMin"_s, Qgis::RasterAttributeTableFieldUsage::BlueMin, QMetaType::Type::Int );
1158 rat->appendField( u"AlphaMin"_s, Qgis::RasterAttributeTableFieldUsage::AlphaMin, QMetaType::Type::Int );
1159 rat->appendField( u"RedMax"_s, Qgis::RasterAttributeTableFieldUsage::RedMax, QMetaType::Type::Int );
1160 rat->appendField( u"GreenMax"_s, Qgis::RasterAttributeTableFieldUsage::GreenMax, QMetaType::Type::Int );
1161 rat->appendField( u"BlueMax"_s, Qgis::RasterAttributeTableFieldUsage::BlueMax, QMetaType::Type::Int );
1162 rat->appendField( u"AlphaMax"_s, Qgis::RasterAttributeTableFieldUsage::AlphaMax, QMetaType::Type::Int );
1163 const QList<QgsColorRampShader::ColorRampItem> rampItems { shaderFunction->colorRampItemList() };
1164 if ( rampItems.size() > 1 )
1165 {
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 )
1171 {
1172 const QgsColorRampShader::ColorRampItem &rampItem { rampItems.at( 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;
1178 }
1179 }
1180 break;
1181 }
1182
1184 {
1185 rat->appendField( u"Min"_s, Qgis::RasterAttributeTableFieldUsage::Min, QMetaType::Type::Double );
1186 rat->appendField( u"Max"_s, Qgis::RasterAttributeTableFieldUsage::Max, QMetaType::Type::Double );
1187 rat->appendField( u"Class"_s, Qgis::RasterAttributeTableFieldUsage::Name, QMetaType::Type::QString );
1188 rat->appendField( u"Red"_s, Qgis::RasterAttributeTableFieldUsage::Red, QMetaType::Type::Int );
1189 rat->appendField( u"Green"_s, Qgis::RasterAttributeTableFieldUsage::Green, QMetaType::Type::Int );
1190 rat->appendField( u"Blue"_s, Qgis::RasterAttributeTableFieldUsage::Blue, QMetaType::Type::Int );
1191 rat->appendField( u"Alpha"_s, Qgis::RasterAttributeTableFieldUsage::Alpha, QMetaType::Type::Int );
1192 const QList<QgsColorRampShader::ColorRampItem> rampItems { shaderFunction->colorRampItemList() };
1193 if ( rampItems.size() > 1 )
1194 {
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 )
1200 {
1201 const QgsColorRampShader::ColorRampItem &rampItem { rampItems.at( 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;
1207 }
1208 }
1209 break;
1210 }
1211
1213 {
1214 rat->appendField( u"Value"_s, Qgis::RasterAttributeTableFieldUsage::MinMax, QMetaType::Type::Double );
1215 rat->appendField( u"Class"_s, Qgis::RasterAttributeTableFieldUsage::Name, QMetaType::Type::QString );
1216 rat->appendField( u"Red"_s, Qgis::RasterAttributeTableFieldUsage::Red, QMetaType::Type::Int );
1217 rat->appendField( u"Green"_s, Qgis::RasterAttributeTableFieldUsage::Green, QMetaType::Type::Int );
1218 rat->appendField( u"Blue"_s, Qgis::RasterAttributeTableFieldUsage::Blue, QMetaType::Type::Int );
1219 rat->appendField( u"Alpha"_s, Qgis::RasterAttributeTableFieldUsage::Alpha, QMetaType::Type::Int );
1220 const QList<QgsColorRampShader::ColorRampItem> rampItems { shaderFunction->colorRampItemList() };
1221 for ( const QgsColorRampShader::ColorRampItem &rampItem : std::as_const( rampItems ) )
1222 {
1223 rat->appendRow( QVariantList() << rampItem.value << rampItem.label << 0 << 0 << 0 << 255 );
1224 rat->setColor( static_cast<int>( rat->data().length() - 1 ), rampItem.color );
1225 }
1226 break;
1227 }
1228 }
1229
1230 if ( bandNumber )
1231 {
1232 *bandNumber = pseudoColorRenderer->inputBand();
1233 }
1234
1235 return rat;
1236 }
1237 else
1238 {
1239 return nullptr;
1240 }
1241 }
1242 else
1243 {
1244 return nullptr;
1245 }
1246 }
1247 else
1248 {
1249 return nullptr;
1250 }
1251}
1252
1253QHash<Qgis::RasterAttributeTableFieldUsage, QgsRasterAttributeTable::UsageInformation> QgsRasterAttributeTable::usageInformation()
1254{
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 }
1259 );
1260 QgsRasterAttributeTable::sUsageInformation
1261 .insert( Qgis::RasterAttributeTableFieldUsage::PixelCount, { tr( "Histogram Pixel Count" ), true, false, false, false, true, false, QList<QMetaType::Type>() << QMetaType::Type::LongLong } );
1262 QgsRasterAttributeTable::sUsageInformation
1263 .insert( Qgis::RasterAttributeTableFieldUsage::Name, { tr( "Class Name" ), false, false, false, false, true, true, QList<QMetaType::Type>() << QMetaType::Type::QString } );
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
1271 .insert( Qgis::RasterAttributeTableFieldUsage::Red, { tr( "Red Color Value (0-255)" ), true, false, true, false, true, false, QList<QMetaType::Type>() << QMetaType::Type::Int } );
1272 QgsRasterAttributeTable::sUsageInformation
1273 .insert( Qgis::RasterAttributeTableFieldUsage::Green, { tr( "Green Color Value (0-255)" ), true, false, true, false, true, false, QList<QMetaType::Type>() << QMetaType::Type::Int } );
1274 QgsRasterAttributeTable::sUsageInformation
1275 .insert( Qgis::RasterAttributeTableFieldUsage::Blue, { tr( "Blue Color Value (0-255)" ), true, false, true, false, true, false, QList<QMetaType::Type>() << QMetaType::Type::Int } );
1276 QgsRasterAttributeTable::sUsageInformation
1277 .insert( Qgis::RasterAttributeTableFieldUsage::Alpha, { tr( "Alpha Color Value (0-255)" ), true, false, true, false, true, false, QList<QMetaType::Type>() << QMetaType::Type::Int } );
1278 QgsRasterAttributeTable::sUsageInformation
1279 .insert( Qgis::RasterAttributeTableFieldUsage::RedMin, { tr( "Red Color Minimum Value (0-255)" ), true, false, false, true, true, false, QList<QMetaType::Type>() << QMetaType::Type::Int } );
1280 QgsRasterAttributeTable::sUsageInformation
1281 .insert( Qgis::RasterAttributeTableFieldUsage::GreenMin, { tr( "Green Color Minimum Value (0-255)" ), true, false, false, true, true, false, QList<QMetaType::Type>() << QMetaType::Type::Int } );
1282 QgsRasterAttributeTable::sUsageInformation
1283 .insert( Qgis::RasterAttributeTableFieldUsage::BlueMin, { tr( "Blue Color Minimum Value (0-255)" ), true, false, false, true, true, false, QList<QMetaType::Type>() << QMetaType::Type::Int } );
1284 QgsRasterAttributeTable::sUsageInformation
1285 .insert( Qgis::RasterAttributeTableFieldUsage::AlphaMin, { tr( "Alpha Color Minimum Value (0-255)" ), true, false, false, true, true, false, QList<QMetaType::Type>() << QMetaType::Type::Int } );
1286 QgsRasterAttributeTable::sUsageInformation
1287 .insert( Qgis::RasterAttributeTableFieldUsage::RedMax, { tr( "Red Color Minimum Value (0-255)" ), true, false, false, true, true, false, QList<QMetaType::Type>() << QMetaType::Type::Int } );
1288 QgsRasterAttributeTable::sUsageInformation
1289 .insert( Qgis::RasterAttributeTableFieldUsage::GreenMax, { tr( "Green Color Minimum Value (0-255)" ), true, false, false, true, true, false, QList<QMetaType::Type>() << QMetaType::Type::Int } );
1290 QgsRasterAttributeTable::sUsageInformation
1291 .insert( Qgis::RasterAttributeTableFieldUsage::BlueMax, { tr( "Blue Color Minimum Value (0-255)" ), true, false, false, true, true, false, QList<QMetaType::Type>() << QMetaType::Type::Int } );
1292 QgsRasterAttributeTable::sUsageInformation
1293 .insert( Qgis::RasterAttributeTableFieldUsage::AlphaMax, { tr( "Alpha Color Minimum Value (0-255)" ), true, false, false, true, true, false, QList<QMetaType::Type>() << QMetaType::Type::Int } );
1294 // Unsupported!!
1295 QgsRasterAttributeTable::sUsageInformation
1296 .insert( Qgis::RasterAttributeTableFieldUsage::MaxCount, { tr( "Maximum GFU value(equals to GFU_AlphaMax+1 currently)" ), true, false, false, true, false, false, QList<QMetaType::Type>() << QMetaType::Type::Int } );
1297 } );
1298 return QgsRasterAttributeTable::sUsageInformation;
1299}
1300
1301void QgsRasterAttributeTable::setType()
1302{
1303 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages { usages() };
1305}
1306
1307
1309QHash<int, QgsRasterAttributeTable::UsageInformation> QgsRasterAttributeTable::usageInformationInt()
1310{
1311 QHash<int, QgsRasterAttributeTable::UsageInformation> usageInfoInt;
1312 const QHash<Qgis::RasterAttributeTableFieldUsage, QgsRasterAttributeTable::UsageInformation> usageInfo { QgsRasterAttributeTable::usageInformation() };
1313 for ( auto it = usageInfo.cbegin(); it != usageInfo.cend(); ++it )
1314 {
1315 usageInfoInt.insert( static_cast<int>( it.key() ), it.value() );
1316 }
1317 return usageInfoInt;
1318}
1320
1322{
1323 return mFilePath;
1324}
1325
1326QList<QgsRasterAttributeTable::MinMaxClass> QgsRasterAttributeTable::minMaxClasses( const int classificationColumn ) const
1327{
1328 QList<QgsRasterAttributeTable::MinMaxClass> classes;
1329 if ( !isValid() )
1330 {
1331 QgsDebugError( "minMaxClasses was called on an invalid RAT" );
1332 return classes;
1333 }
1334
1335 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages { usages() };
1336
1337 if ( !fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::MinMax ) )
1338 {
1339 QgsDebugError( "minMaxClasses was called on a ramp raster" );
1340 return classes;
1341 }
1342
1343 const int minMaxIndex { static_cast<int>( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::MinMax ) ) };
1344
1345 Q_ASSERT( minMaxIndex >= 0 );
1346
1347 int classificationIndex = classificationColumn;
1348 if ( classificationIndex >= 0 && classificationIndex < mFields.count() )
1349 {
1350 const Field classificationField { mFields.at( classificationIndex ) };
1351 if ( ( classificationField.usage != Qgis::RasterAttributeTableFieldUsage::Name && classificationField.usage != Qgis::RasterAttributeTableFieldUsage::Generic ) )
1352 {
1353 QgsDebugError( "minMaxClasses was called with a classification column which is not suitable for classification" );
1354 return classes;
1355 }
1356 }
1357 else if ( classificationIndex == -1 ) // Special value for not-set
1358 {
1359 // Find first value or generic field
1360 if ( fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::Name ) )
1361 {
1362 classificationIndex = static_cast<int>( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::Name ) );
1363 }
1364 else if ( fieldUsages.contains( Qgis::RasterAttributeTableFieldUsage::Generic ) )
1365 {
1366 classificationIndex = static_cast<int>( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::Generic ) );
1367 }
1368 else
1369 {
1370 classificationIndex = minMaxIndex;
1371 }
1372 }
1373 else if ( classificationIndex >= mFields.count() )
1374 {
1375 QgsDebugError( "minMaxClasses was called with a classification column out of range" );
1376 return classes;
1377 }
1378
1379 if ( classificationIndex >= 0 )
1380 {
1381 QStringList labels;
1382 int rowIdx { 0 };
1383 for ( const QVariantList &row : std::as_const( mData ) )
1384 {
1385 const QString label { row.at( classificationIndex ).toString() };
1386 bool ok;
1387 const double value { row.at( minMaxIndex ).toDouble( &ok ) };
1388 // This should never happen, could eventually become a Q_ASSERT
1389 if ( !ok )
1390 {
1391 QgsDebugError( "minMaxClasses could not convert a MinMax value to double" );
1392 return classes;
1393 }
1394 if ( labels.contains( label ) )
1395 {
1396 classes[labels.indexOf( label )].minMaxValues.push_back( value );
1397 }
1398 else
1399 {
1400 labels.push_back( label );
1401 classes.push_back( { label, { value }, color( rowIdx ) } );
1402 }
1403 rowIdx++;
1404 }
1405 }
1406 return classes;
1407}
1408
1409QgsGradientColorRamp QgsRasterAttributeTable::colorRamp( QStringList &labels, const int labelColumn ) const
1410{
1411 QgsGradientColorRamp ramp { Qt::GlobalColor::white, Qt::GlobalColor::black };
1412 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages { usages() };
1413 const int minIdx { static_cast<int>( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::Min ) ) };
1414 const int maxIdx { static_cast<int>( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::Max ) ) };
1415 const bool isRange { minIdx >= 0 && maxIdx >= 0 };
1416
1417 int labelIdx { labelColumn };
1418 if ( labelColumn < 0
1419 || labelColumn >= fields().count()
1420 || ( fieldUsages.at( labelColumn ) != Qgis::RasterAttributeTableFieldUsage::Name && fieldUsages.at( labelColumn ) != Qgis::RasterAttributeTableFieldUsage::Generic ) )
1421 {
1422 labelIdx = -1;
1423 }
1424
1425 if ( !mData.isEmpty() && ( minIdx >= 0 && maxIdx >= 0 ) )
1426 {
1428 const bool hasColorOrRamp { hasColor() || hasRamp() };
1429 {
1430 const double min { minimumValue() };
1431 const double max { maximumValue() };
1432 const double range { max - min };
1433
1434 if ( range != 0 )
1435 {
1436 if ( !std::isnan( min ) && !std::isnan( max ) )
1437 {
1438 const QList<QVariantList> dataCopy( orderedRows() );
1439
1440 QgsRasterAttributeTable orderedRat;
1441 for ( const Field &f : std::as_const( mFields ) )
1442 {
1443 orderedRat.appendField( f );
1444 }
1445 for ( const QVariantList &r : std::as_const( dataCopy ) )
1446 {
1447 orderedRat.appendRow( r );
1448 }
1449
1450 QColor lastColor { ramp.color1() };
1451
1452 if ( hasColorOrRamp )
1453 {
1454 ramp.setColor1( orderedRat.hasColor() ? orderedRat.color( 0 ) : orderedRat.ramp( 0 ).color1() );
1455 ramp.setColor2( orderedRat.hasColor() ? orderedRat.color( static_cast<int>( orderedRat.data().count() - 1 ) ) : orderedRat.ramp( static_cast<int>( orderedRat.data().count() - 1 ) ).color2() );
1456 lastColor = orderedRat.hasColor() ? orderedRat.color( 0 ) : orderedRat.ramp( 0 ).color2();
1457 }
1458
1459 auto labelFromField = [&]( int rowIdx ) -> QString {
1460 if ( labelIdx < 0 )
1461 {
1462 return u"%L1 - %L2"_s.arg( orderedRat.value( rowIdx, minIdx ).toDouble() ).arg( orderedRat.value( rowIdx, maxIdx ).toDouble() );
1463 }
1464 const QVariant val( orderedRat.value( rowIdx, labelIdx ) );
1465 bool ok { true };
1466 QString res;
1467 switch ( val.userType() )
1468 {
1469 case QMetaType::Type::QChar:
1470 return QString( val.toChar() );
1471 case QMetaType::Type::Int:
1472 res = QLocale().toString( val.toInt( &ok ) );
1473 break;
1474 case QMetaType::Type::LongLong:
1475 res = QLocale().toString( val.toLongLong( &ok ) );
1476 break;
1477 case QMetaType::Type::UInt:
1478 res = QLocale().toString( val.toUInt( &ok ) );
1479 break;
1480 case QMetaType::Type::ULongLong:
1481 res = QLocale().toString( val.toULongLong( &ok ) );
1482 break;
1483 case QMetaType::Type::Double:
1484 res = QLocale().toString( val.toDouble( &ok ), 'g' );
1485 break;
1486 case QMetaType::Type::QString:
1487 default:
1488 return val.toString();
1489 }
1490 return ok ? res : val.toString();
1491 };
1492
1493 // Case 1: range classes, discrete colors
1494 // - create stops for the lower value of each class except for the first,
1495 // - use the color from the previous class
1496 if ( orderedRat.hasColor() && isRange )
1497 {
1498 labels.push_back( labelFromField( 0 ) );
1499
1500 for ( int rowIdx = 1; rowIdx < orderedRat.data().count(); ++rowIdx )
1501 {
1502 const double offset { ( orderedRat.value( rowIdx, minIdx ).toDouble() - min ) / range };
1503 const QColor color { orderedRat.color( rowIdx - 1 ) };
1504 stops.append( QgsGradientStop( offset, color ) );
1505 labels.push_back( labelFromField( rowIdx ) );
1506 }
1507 }
1508 // Case 2: range classes, gradients colors
1509 // Take the class bounds (average value between max of previous class and min of the next)
1510 // to avoid potential overlapping or gaps between classes.
1511 // Create stop:
1512 // - first stop at value taking the max color of the previous class
1513 // - second stop at value + epsilon taking the min color of the next class, unless colors and offset are equal
1514 else if ( orderedRat.hasRamp() && isRange )
1515 {
1516 double prevOffset { 0 };
1517 labels.push_back( labelFromField( 0 ) );
1518 for ( int rowIdx = 1; rowIdx < orderedRat.data().count(); ++rowIdx )
1519 {
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 };
1523 const QgsGradientColorRamp previousRamp { orderedRat.ramp( prevRowIdx ) };
1524 stops.append( QgsGradientStop( offset, previousRamp.color2() ) );
1525
1526 const QgsGradientColorRamp currentRamp { orderedRat.ramp( rowIdx ) };
1527 // An additional stop is added if the colors are different or offsets are different by 1e-6 (offset varies from 0 to 1).
1528 if ( currentRamp.color1() != previousRamp.color2() && qgsDoubleNear( offset, prevOffset, 1e-6 ) )
1529 {
1530 stops.append( QgsGradientStop( offset + std::numeric_limits<double>::epsilon(), currentRamp.color1() ) );
1531 }
1532 prevOffset = offset;
1533 }
1534 }
1535 // Case 3: range classes but no colors at all
1536 // Take the class borders (average value between max of previous class and min of the next)
1537 // Create stop for the lower class, actually skipping the upper bound of the last class
1538 else
1539 {
1540 labels.push_back( labelFromField( 0 ) );
1541
1542 for ( int rowIdx = 1; rowIdx < orderedRat.data().count(); ++rowIdx )
1543 {
1544 const int prevRowIdx { rowIdx - 1 };
1545 const double offset { ( ( orderedRat.value( rowIdx, minIdx ).toDouble() + orderedRat.value( prevRowIdx, maxIdx ).toDouble() ) / 2.0 - min ) / range };
1546 stops.append( QgsGradientStop( offset, ramp.color( offset ) ) );
1547 labels.push_back( labelFromField( rowIdx ) );
1548 }
1549 }
1550 }
1551 }
1552 }
1553
1554 ramp.setStops( stops );
1555 ramp.setDiscrete( hasColor() );
1556 }
1557
1558 return ramp;
1559}
1560
1561std::unique_ptr<QgsRasterRenderer> QgsRasterAttributeTable::createRenderer( QgsRasterDataProvider *provider, const int bandNumber, const int classificationColumn )
1562{
1563 if ( !provider )
1564 {
1565 return nullptr;
1566 }
1567
1568 std::unique_ptr<QgsRasterRenderer> renderer;
1569
1571 {
1572 std::unique_ptr<QgsColorRamp> ramp;
1573 if ( !hasColor() )
1574 {
1575 ramp = std::make_unique<QgsRandomColorRamp>();
1576 }
1578 if ( classes.isEmpty() )
1579 return nullptr;
1580
1581 renderer = std::make_unique<QgsPalettedRasterRenderer>( provider, bandNumber, classes );
1582 }
1583 else
1584 {
1585 auto pseudoColorRenderer = std::make_unique<QgsSingleBandPseudoColorRenderer>( provider, bandNumber );
1586 QStringList labels;
1587 // Athematic classification is not supported, but the classificationColumn will be used for labels.
1588 // if it's not specified, try to guess it here.
1589 int labelColumn { classificationColumn };
1590 if ( labelColumn < 0 )
1591 {
1592 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages { usages() };
1593 labelColumn = static_cast<int>( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::Name ) );
1594 if ( labelColumn < 0 )
1595 {
1596 labelColumn = static_cast<int>( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::Generic ) );
1597 }
1598 }
1599 QgsGradientColorRamp *ramp { colorRamp( labels, labelColumn ).clone() };
1600 pseudoColorRenderer->setClassificationMin( minimumValue() );
1601 pseudoColorRenderer->setClassificationMax( maximumValue() );
1602 // Use discrete for single colors, interpolated for ramps
1603 pseudoColorRenderer->createShader(
1605 );
1606 if ( pseudoColorRenderer->shader() )
1607 {
1608 pseudoColorRenderer->shader()->setMaximumValue( maximumValue() );
1609 pseudoColorRenderer->shader()->setMinimumValue( minimumValue() );
1610 // Set labels
1611 if ( QgsColorRampShader *shaderFunction = static_cast<QgsColorRampShader *>( pseudoColorRenderer->shader()->rasterShaderFunction() ) )
1612 {
1613 shaderFunction->setMinimumValue( minimumValue() );
1614 shaderFunction->setMaximumValue( maximumValue() );
1615 const bool labelsAreUsable { ramp->count() > 2 && labels.count() == ramp->count() - 1 };
1616
1617 if ( labelsAreUsable )
1618 {
1619 QList<QgsColorRampShader::ColorRampItem> newItemList;
1620 const double range { maximumValue() - minimumValue() };
1621 int stopIdx { 0 };
1622 for ( const QString &label : std::as_const( labels ) )
1623 {
1624 if ( stopIdx >= ramp->count() - 2 )
1625 {
1626 break;
1627 }
1628 double value { minimumValue() + ramp->stops().at( stopIdx ).offset * range };
1629 QgsColorRampShader::ColorRampItem item { value, ramp->stops().at( stopIdx ).color, label };
1630 newItemList.push_back( item );
1631 stopIdx++;
1632 }
1633
1634 QgsColorRampShader::ColorRampItem item { maximumValue(), ramp->color2(), labels.last() };
1635 newItemList.push_back( item );
1636 shaderFunction->setColorRampItemList( newItemList );
1637 }
1638 }
1639 }
1640 renderer = std::move( pseudoColorRenderer );
1641 }
1642
1643 return renderer;
1644}
1645
1646QList<QList<QVariant> > QgsRasterAttributeTable::orderedRows() const
1647{
1648 const QList<Qgis::RasterAttributeTableFieldUsage> fieldUsages { usages() };
1649 const int minIdx { static_cast<int>( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::Min ) ) };
1650 const int maxIdx { static_cast<int>( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::Max ) ) };
1651 const bool isRange { minIdx >= 0 && maxIdx >= 0 };
1652 QList<QVariantList> dataCopy( mData );
1653
1654 if ( isRange )
1655 {
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() );
1658 } );
1659 }
1660 else
1661 {
1662 const int minMaxIdx { static_cast<int>( fieldUsages.indexOf( Qgis::RasterAttributeTableFieldUsage::MinMax ) ) };
1663 if ( minMaxIdx < 0 )
1664 {
1665 return dataCopy;
1666 }
1667 else
1668 {
1669 std::sort( dataCopy.begin(), dataCopy.end(), [&]( const QVariantList &first, const QVariantList &second ) -> bool { return first.at( minMaxIdx ).toDouble() < second.at( minMaxIdx ).toDouble(); } );
1670 }
1671 }
1672
1673 return dataCopy;
1674}
1675
1683
@ Exact
Assigns the color of the exact matching value in the color ramp item list.
Definition qgis.h:1598
@ Linear
Interpolates the color between two class breaks linearly.
Definition qgis.h:1596
@ Discrete
Assigns the color of the higher class for every pixel between two class breaks.
Definition qgis.h:1597
RasterAttributeTableType
The RasterAttributeTableType enum represents the type of RAT.
Definition qgis.h:1787
@ Continuous
Uses breaks from color palette.
Definition qgis.h:1611
RasterAttributeTableFieldUsage
The RasterAttributeTableFieldUsage enum represents the usage of a Raster Attribute Table field.
Definition qgis.h:1758
@ RedMax
Field usage RedMax.
Definition qgis.h:1773
@ PixelCount
Field usage PixelCount.
Definition qgis.h:1760
@ AlphaMax
Field usage AlphaMax.
Definition qgis.h:1776
@ BlueMin
Field usage BlueMin.
Definition qgis.h:1771
@ Alpha
Field usage Alpha.
Definition qgis.h:1768
@ Generic
Field usage Generic.
Definition qgis.h:1759
@ GreenMax
Field usage GreenMax.
Definition qgis.h:1774
@ RedMin
Field usage RedMin.
Definition qgis.h:1769
@ MinMax
Field usage MinMax.
Definition qgis.h:1764
@ BlueMax
Field usage BlueMax.
Definition qgis.h:1775
@ AlphaMin
Field usage AlphaMin.
Definition qgis.h:1772
@ Green
Field usage Green.
Definition qgis.h:1766
@ MaxCount
Not used by QGIS: GDAL Maximum GFU value (equals to GFU_AlphaMax+1 currently).
Definition qgis.h:1777
@ GreenMin
Field usage GreenMin.
Definition qgis.h:1770
@ NoGeometry
No geometry.
Definition qgis.h:332
A vector of attributes.
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...
Definition qgsfeature.h:60
QgsAttributes attributes
Definition qgsfeature.h:64
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.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:45
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:75
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().
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).
Definition qgis.h:7826
QList< QgsGradientStop > QgsGradientStopsList
List of gradient stops.
QList< QgsFeature > QgsFeatureList
#define QgsDebugError(str)
Definition qgslogger.h:71
Setting options for creating vector data providers.
Properties of a single value class.