Skip to content

Commit 8b2d5d0

Browse files
committed
Use sample instead of identify in raster sampling alg
About 25x faster on large layers
1 parent 0f41ca9 commit 8b2d5d0

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

python/plugins/processing/algs/qgis/RasterSampling.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
from qgis.PyQt.QtGui import QIcon
3232
from qgis.PyQt.QtCore import QVariant
3333

34-
from qgis.core import (QgsApplication,
34+
from qgis.core import (NULL,
35+
QgsApplication,
3536
QgsField,
3637
QgsFeatureSink,
3738
QgsRaster,
@@ -179,20 +180,12 @@ def processAlgorithm(self, parameters, context, feedback):
179180
feedback.reportError(self.tr('Could not reproject feature {} to raster CRS').format(i.id()))
180181
continue
181182

182-
if sampled_raster.bandCount() > 1:
183-
184-
for b in range(sampled_raster.bandCount()):
185-
attrs.append(
186-
sampled_raster.dataProvider().identify(
187-
point,
188-
QgsRaster.IdentifyFormatValue).results()[b + 1]
189-
)
190-
191-
attrs.append(
192-
sampled_raster.dataProvider().identify(
193-
point,
194-
QgsRaster.IdentifyFormatValue).results()[1]
195-
)
183+
for b in range(sampled_raster.bandCount()):
184+
value, ok = sampled_raster.dataProvider().sample(point, b + 1)
185+
if ok:
186+
attrs.append(value)
187+
else:
188+
attrs.append(NULL)
196189

197190
i.setAttributes(attrs)
198191

0 commit comments

Comments
 (0)