Remove layer extent and statistic variables from processing contexts

These variables take a lot of time to calculate and cause lots of
lengthy hangs in processing. (Eg add some moderately large rasters
to a project, then try to run any processing algorithm and QGIS
will freeze).

The layer extent can already be used in expressions via the
layer_property function, which only evalutes the extent if required
and only for layers it is used for.

The band stats for raster layers should be moved to a
band_statistic function in core which behaves the same way.
This commit is contained in:
Nyall Dawson 2016-11-16 10:25:07 +10:00
parent 377cba0b74
commit 86ab3022b5

View File

@ -76,26 +76,6 @@ def _expressionContext():
context.appendScope(QgsExpressionContextUtils.mapSettingsScope(iface.mapCanvas().mapSettings()))
processingScope = QgsExpressionContextScope()
layers = dataobjects.getAllLayers()
for layer in layers:
name = layer.name()
processingScope.setVariable('%s_minx' % name, layer.extent().xMinimum())
processingScope.setVariable('%s_miny' % name, layer.extent().yMinimum())
processingScope.setVariable('%s_maxx' % name, layer.extent().xMaximum())
processingScope.setVariable('%s_maxy' % name, layer.extent().yMaximum())
if isinstance(layer, QgsRasterLayer):
cellsize = (layer.extent().xMaximum()
- layer.extent().xMinimum()) / layer.width()
processingScope.setVariable('%s_cellsize' % name, cellsize)
layers = dataobjects.getRasterLayers()
for layer in layers:
for i in range(layer.bandCount()):
stats = layer.dataProvider().bandStatistics(i + 1)
processingScope.setVariable('%s_band%i_avg' % (name, i + 1), stats.mean)
processingScope.setVariable('%s_band%i_stddev' % (name, i + 1), stats.stdDev)
processingScope.setVariable('%s_band%i_min' % (name, i + 1), stats.minimumValue)
processingScope.setVariable('%s_band%i_max' % (name, i + 1), stats.maximumValue)
extent = iface.mapCanvas().fullExtent()
processingScope.setVariable('fullextent_minx', extent.xMinimum())