From 8b2d5d0374337c702fea8d7a19ffb5bd757db9f4 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Mon, 16 Jul 2018 21:12:02 +1000 Subject: [PATCH] Use sample instead of identify in raster sampling alg About 25x faster on large layers --- .../processing/algs/qgis/RasterSampling.py | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/python/plugins/processing/algs/qgis/RasterSampling.py b/python/plugins/processing/algs/qgis/RasterSampling.py index 7bcfcbc8bc6..eaca1b19238 100644 --- a/python/plugins/processing/algs/qgis/RasterSampling.py +++ b/python/plugins/processing/algs/qgis/RasterSampling.py @@ -31,7 +31,8 @@ import os from qgis.PyQt.QtGui import QIcon from qgis.PyQt.QtCore import QVariant -from qgis.core import (QgsApplication, +from qgis.core import (NULL, + QgsApplication, QgsField, QgsFeatureSink, QgsRaster, @@ -179,20 +180,12 @@ class RasterSampling(QgisAlgorithm): feedback.reportError(self.tr('Could not reproject feature {} to raster CRS').format(i.id())) continue - if sampled_raster.bandCount() > 1: - - for b in range(sampled_raster.bandCount()): - attrs.append( - sampled_raster.dataProvider().identify( - point, - QgsRaster.IdentifyFormatValue).results()[b + 1] - ) - - attrs.append( - sampled_raster.dataProvider().identify( - point, - QgsRaster.IdentifyFormatValue).results()[1] - ) + for b in range(sampled_raster.bandCount()): + value, ok = sampled_raster.dataProvider().sample(point, b + 1) + if ok: + attrs.append(value) + else: + attrs.append(NULL) i.setAttributes(attrs)