Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use a QHash instead of std::unordered_map
Because it turns out MUCH MUCH faster
  • Loading branch information
nyalldawson committed Jan 22, 2019
1 parent 60a6fc1 commit ee34431
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Expand Up @@ -6706,6 +6706,8 @@ tests:
OUTPUT_TABLE:
name: expected/raster_zonal_stats.csv
type: vector
pk:
- zone

- algorithm: native:rasterlayerzonalstats
name: Raster layer zonal stats, reprojected
Expand All @@ -6722,6 +6724,8 @@ tests:
OUTPUT_TABLE:
name: expected/raster_zonal_stats_reproj.csv
type: vector
pk:
- zone

- algorithm: native:rasterlayerzonalstats
name: Raster layer zonal stats, zones ref
Expand All @@ -6739,6 +6743,8 @@ tests:
OUTPUT_TABLE:
name: expected/raster_zonal_stats_zone_ref.csv
type: vector
pk:
- zone

- algorithm: native:rasterlayerzonalstats
name: Raster layer zonal stats reprojected, zones ref
Expand All @@ -6756,6 +6762,8 @@ tests:
OUTPUT_TABLE:
name: expected/raster_zonal_stats_zone_crs_ref.csv
type: vector
pk:
- zone

- algorithm: native:antimeridiansplit
name: Antimeridian split, lines
Expand Down
9 changes: 4 additions & 5 deletions src/analysis/processing/qgsalgorithmrasterzonalstats.cpp
Expand Up @@ -19,7 +19,6 @@
#include "qgsstringutils.h"
#include "qgsstatisticalsummary.h"
#include "qgsrasterprojector.h"
#include <unordered_map>

///@cond PRIVATE

Expand Down Expand Up @@ -196,7 +195,7 @@ QVariantMap QgsRasterLayerZonalStatsAlgorithm::processAlgorithm( const QVariantM
// up trying to store EVERY pixel value from the input in memory
QgsStatisticalSummary s{ QgsStatisticalSummary::Count | QgsStatisticalSummary::Sum | QgsStatisticalSummary::Min | QgsStatisticalSummary::Max | QgsStatisticalSummary::Mean };
};
std::unordered_map<double, StatCalculator, std::hash<double>, std::equal_to<double> > zoneStats;
QHash<double, StatCalculator > zoneStats;
qgssize noDataCount = 0;

qgssize layerSize = static_cast< qgssize >( mLayerWidth ) * static_cast< qgssize >( mLayerHeight );
Expand Down Expand Up @@ -280,9 +279,9 @@ QVariantMap QgsRasterLayerZonalStatsAlgorithm::processAlgorithm( const QVariantM
for ( auto it = zoneStats.begin(); it != zoneStats.end(); ++it )
{
QgsFeature f;
it->second.s.finalize();
f.setAttributes( QgsAttributes() << it->first << it->second.s.count() * pixelArea << it->second.s.sum() << it->second.s.count() <<
it->second.s.min() << it->second.s.max() << it->second.s.mean() );
it->s.finalize();
f.setAttributes( QgsAttributes() << it.key() << it->s.count() * pixelArea << it->s.sum() << it->s.count() <<
it->s.min() << it->s.max() << it->s.mean() );
sink->addFeature( f, QgsFeatureSink::FastInsert );
}
outputs.insert( QStringLiteral( "OUTPUT_TABLE" ), tableDest );
Expand Down

0 comments on commit ee34431

Please sign in to comment.