|
21 | 21 | #include "qgsprocessingfeedback.h"
|
22 | 22 | #include "qgsprocessingutils.h"
|
23 | 23 | #include "qgsvectorlayer.h"
|
| 24 | +#include "qgsrasterlayer.h" |
24 | 25 | #include "qgsgeometry.h"
|
25 | 26 | #include "qgsgeometryengine.h"
|
26 | 27 | #include "qgswkbtypes.h"
|
@@ -88,6 +89,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
|
88 | 89 | addAlgorithm( new QgsLineIntersectionAlgorithm() );
|
89 | 90 | addAlgorithm( new QgsSplitWithLinesAlgorithm() );
|
90 | 91 | addAlgorithm( new QgsMeanCoordinatesAlgorithm() );
|
| 92 | + addAlgorithm( new QgsRasterLayerUniqueValuesCountAlgorithm() ); |
91 | 93 | }
|
92 | 94 |
|
93 | 95 | void QgsSaveSelectedFeatures::initAlgorithm( const QVariantMap & )
|
@@ -2586,6 +2588,95 @@ QVariantMap QgsMeanCoordinatesAlgorithm::processAlgorithm( const QVariantMap &pa
|
2586 | 2588 | return outputs;
|
2587 | 2589 | }
|
2588 | 2590 |
|
| 2591 | + |
| 2592 | +void QgsRasterLayerUniqueValuesCountAlgorithm::initAlgorithm( const QVariantMap & ) |
| 2593 | +{ |
| 2594 | + addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT" ), |
| 2595 | + QObject::tr( "Input layer" ) ) ); |
| 2596 | + addParameter( new QgsProcessingParameterBand( QStringLiteral( "BAND" ), |
| 2597 | + QObject::tr( "Band number" ), 1, QStringLiteral( "INPUT" ) ) ); |
| 2598 | + addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT_HTML_FILE" ), |
| 2599 | + QObject::tr( "Unique values count" ), QObject::tr( "HTML files (*.html)" ), QVariant(), true ) ); |
| 2600 | + addOutput( new QgsProcessingOutputHtml( QStringLiteral( "OUTPUT_HTML_FILE" ), QObject::tr( "Unique values count" ) ) ); |
| 2601 | +} |
| 2602 | + |
| 2603 | +QString QgsRasterLayerUniqueValuesCountAlgorithm::shortHelpString() const |
| 2604 | +{ |
| 2605 | + return QObject::tr( "This algorithm returns the count of each unique value in a given raster layer." ); |
| 2606 | +} |
| 2607 | + |
| 2608 | +QgsRasterLayerUniqueValuesCountAlgorithm *QgsRasterLayerUniqueValuesCountAlgorithm::createInstance() const |
| 2609 | +{ |
| 2610 | + return new QgsRasterLayerUniqueValuesCountAlgorithm(); |
| 2611 | +} |
| 2612 | + |
| 2613 | +QVariantMap QgsRasterLayerUniqueValuesCountAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) |
| 2614 | +{ |
| 2615 | + QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT" ), context ); |
| 2616 | + int band = parameterAsInt( parameters, QStringLiteral( "BAND" ), context ); |
| 2617 | + QString outputFile = parameterAsFileOutput( parameters, QStringLiteral( "OUTPUT_HTML_FILE" ), context ); |
| 2618 | + |
| 2619 | + |
| 2620 | + QHash< double, int > uniqueValues; |
| 2621 | + int width = layer->width(); |
| 2622 | + int height = layer->height(); |
| 2623 | + |
| 2624 | + QgsRasterBlock *rasterBlock = layer->dataProvider()->block( band, layer->extent(), width, height ); |
| 2625 | + int noDataCount = -1; |
| 2626 | + if ( rasterBlock->hasNoDataValue() ) |
| 2627 | + noDataCount = 0; |
| 2628 | + |
| 2629 | + for ( int row = 0; row < height; row++ ) |
| 2630 | + { |
| 2631 | + feedback->setProgress( 100 * row / height ); |
| 2632 | + for ( int column = 0; column < width; column++ ) |
| 2633 | + { |
| 2634 | + if ( feedback->isCanceled() ) |
| 2635 | + break; |
| 2636 | + if ( noDataCount > -1 && rasterBlock->isNoData( row, column ) ) |
| 2637 | + { |
| 2638 | + noDataCount += 1; |
| 2639 | + } |
| 2640 | + else |
| 2641 | + { |
| 2642 | + double value = rasterBlock->value( row, column ); |
| 2643 | + uniqueValues[ value ]++; |
| 2644 | + } |
| 2645 | + } |
| 2646 | + } |
| 2647 | + |
| 2648 | + QMap< double, int > sortedUniqueValues; |
| 2649 | + for ( auto it = uniqueValues.constBegin(); it != uniqueValues.constEnd(); ++it ) |
| 2650 | + { |
| 2651 | + sortedUniqueValues.insert( it.key(), it.value() ); |
| 2652 | + } |
| 2653 | + |
| 2654 | + QVariantMap outputs; |
| 2655 | + if ( !outputFile.isEmpty() ) |
| 2656 | + { |
| 2657 | + QFile file( outputFile ); |
| 2658 | + if ( file.open( QIODevice::WriteOnly | QIODevice::Text ) ) |
| 2659 | + { |
| 2660 | + QTextStream out( &file ); |
| 2661 | + out << QString( "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/></head><body>\n" ); |
| 2662 | + out << QObject::tr( "<p>Analyzed file: %1 (band %2)</p>\n" ).arg( layer->source() ).arg( band ); |
| 2663 | + out << QObject::tr( "<p>Total cell count: %1</p>\n" ).arg( width * height ); |
| 2664 | + if ( noDataCount > -1 ) |
| 2665 | + out << QObject::tr( "<p>NODATA count: %1</p>\n" ).arg( noDataCount ); |
| 2666 | + out << QString( "<table><tr><td>%1</td><td>%2</td></tr>\n" ).arg( QObject::tr( "Value" ) ).arg( QObject::tr( "Count" ) ); |
| 2667 | + |
| 2668 | + for ( double key : sortedUniqueValues.keys() ) |
| 2669 | + { |
| 2670 | + out << QString( "<tr><td>%1</td><td>%2</td></tr>\n" ).arg( key ).arg( sortedUniqueValues[key] ); |
| 2671 | + } |
| 2672 | + out << QString( "</table>\n</body></html>" ); |
| 2673 | + outputs.insert( QStringLiteral( "OUTPUT_HTML_FILE" ), outputFile ); |
| 2674 | + } |
| 2675 | + } |
| 2676 | + |
| 2677 | + return outputs; |
| 2678 | +} |
| 2679 | + |
2589 | 2680 | ///@endcond
|
2590 | 2681 |
|
2591 | 2682 |
|
0 commit comments