Skip to content
Permalink
Browse files

Merge pull request #3456 from arnaud-morvan/processing_apply_use_cove…

…rge_on_execution

[Processing] Calculate min coverage on execution
  • Loading branch information
volaya committed Sep 5, 2016
2 parents 2fbc326 + c369710 commit 8de9757f54b8f9a58994dbec95ba0c309fe29394
@@ -34,10 +34,13 @@
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtCore import QCoreApplication, QSettings

from qgis.core import QgsVectorLayer, QgsRasterLayer

from processing.core.ProcessingLog import ProcessingLog
from processing.core.ProcessingConfig import ProcessingConfig
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.SilentProgress import SilentProgress
from processing.core.parameters import ParameterExtent
from processing.core.parameters import ParameterRaster, ParameterVector, ParameterMultipleInput, ParameterTable, Parameter
from processing.core.outputs import OutputVector, OutputRaster, OutputTable, OutputHTML, Output
from processing.algs.gdal.GdalUtils import GdalUtils
@@ -198,6 +201,7 @@ def execute(self, progress=SilentProgress(), model=None):
self.setOutputCRS()
self.resolveTemporaryOutputs()
self.resolveDataObjects()
self.resolveMinCoveringExtent()
self.checkOutputFileExtensions()
self.runPreExecutionScript(progress)
self.processAlgorithm(progress)
@@ -408,6 +412,61 @@ def resolveDataObjects(self):
break
param.setValue(";".join(inputlayers))

def canUseAutoExtent(self):
for param in self.parameters:
if isinstance(param, (ParameterRaster, ParameterVector)):
return True
if isinstance(param, ParameterMultipleInput):
return True
return False

def resolveMinCoveringExtent(self):
for param in self.parameters:
if isinstance(param, ParameterExtent):
if param.value is None:
param.value = self.getMinCoveringExtent()

def getMinCoveringExtent(self):
first = True
found = False
for param in self.parameters:
if param.value:
if isinstance(param, (ParameterRaster, ParameterVector)):
if isinstance(param.value, (QgsRasterLayer,
QgsVectorLayer)):
layer = param.value
else:
layer = dataobjects.getObject(param.value)
if layer:
found = True
self.addToRegion(layer, first)
first = False
elif isinstance(param, ParameterMultipleInput):
layers = param.value.split(';')
for layername in layers:
layer = dataobjects.getObject(layername)
if layer:
found = True
self.addToRegion(layer, first)
first = False
if found:
return '{},{},{},{}'.format(
self.xmin, self.xmax, self.ymin, self.ymax)
else:
return None

def addToRegion(self, layer, first):
if first:
self.xmin = layer.extent().xMinimum()
self.xmax = layer.extent().xMaximum()
self.ymin = layer.extent().yMinimum()
self.ymax = layer.extent().yMaximum()
else:
self.xmin = min(self.xmin, layer.extent().xMinimum())
self.xmax = max(self.xmax, layer.extent().xMaximum())
self.ymin = min(self.ymin, layer.extent().yMinimum())
self.ymax = max(self.ymax, layer.extent().yMaximum())

def checkInputCRS(self):
"""It checks that all input layers use the same CRS. If so,
returns True. False otherwise.
@@ -101,20 +101,11 @@ def setParamValues(self):
for param in params:
if param.hidden:
continue
if isinstance(param, ParameterExtent):
continue
if not self.setParamValue(
param, self.mainWidget.valueItems[param.name]):
raise AlgorithmDialogBase.InvalidParameterValue(
param, self.mainWidget.valueItems[param.name])

for param in params:
if isinstance(param, ParameterExtent):
if not self.setParamValue(
param, self.mainWidget.valueItems[param.name]):
raise AlgorithmDialogBase.InvalidParameterValue(
param, self.mainWidget.valueItems[param.name])

for output in outputs:
if output.hidden:
continue
@@ -71,28 +71,13 @@ def accept(self):
for param in alg.parameters:
if param.hidden:
continue
if isinstance(param, ParameterExtent):
col += 1
continue
widget = self.mainWidget.tblParameters.cellWidget(row, col)
if not self.mainWidget.setParamValue(param, widget, alg):
self.lblProgress.setText(
self.tr('<b>Missing parameter value: %s (row %d)</b>') % (param.description, row + 1))
self.algs = None
return
col += 1
col = 0
for param in alg.parameters:
if param.hidden:
continue
if isinstance(param, ParameterExtent):
widget = self.mainWidget.tblParameters.cellWidget(row, col)
if not self.mainWidget.setParamValue(param, widget, alg):
self.lblProgress.setText(
self.tr('<b>Missing parameter value: %s (row %d)</b>') % (param.description, row + 1))
self.algs = None
return
col += 1
for out in alg.outputs:
if out.hidden:
continue
@@ -156,7 +156,7 @@ def getWidgetFromParameter(self, param, row, col):
elif isinstance(param, ParameterFixedTable):
item = FixedTablePanel(param)
elif isinstance(param, ParameterExtent):
item = ExtentSelectionPanel(self.parent, self.alg, param.default)
item = ExtentSelectionPanel(self.parent, param.default)
elif isinstance(param, ParameterPoint):
item = PointSelectionPanel(self.parent, param.default)
elif isinstance(param, ParameterCrs):
@@ -247,28 +247,13 @@ def save(self):
for param in alg.parameters:
if param.hidden:
continue
if isinstance(param, ParameterExtent):
col += 1
continue
widget = self.tblParameters.cellWidget(row, col)
if not self.setParamValue(param, widget, alg):
self.parent.lblProgress.setText(
self.tr('<b>Missing parameter value: %s (row %d)</b>') % (param.description, row + 1))
return
algParams[param.name] = param.getValueAsCommandLineParameter()
col += 1
col = 0
for param in alg.parameters:
if param.hidden:
continue
if isinstance(param, ParameterExtent):
widget = self.tblParameters.cellWidget(row, col)
if not self.setParamValue(param, widget, alg):
self.parent.lblProgress.setText(
self.tr('<b>Missing parameter value: %s (row %d)</b>') % (param.description, row + 1))
return
algParams[param.name] = unicode(param.value())
col += 1
for out in alg.outputs:
if out.hidden:
continue
@@ -54,8 +54,8 @@ def __init__(self, dialog, alg, default=None):
self.setupUi(self)

self.dialog = dialog
self.params = alg.parameters
if self.canUseAutoExtent():
self.alg = alg
if alg.canUseAutoExtent():
if hasattr(self.leText, 'setPlaceholderText'):
self.leText.setPlaceholderText(
self.tr('[Leave blank to use min covering extent]'))
@@ -79,15 +79,6 @@ def __init__(self, dialog, alg, default=None):
except:
pass

def canUseAutoExtent(self):
for param in self.params:
if isinstance(param, (ParameterRaster, ParameterVector)):
return True
if isinstance(param, ParameterMultipleInput):
return True

return False

def selectExtent(self):
popupmenu = QMenu()
useLayerExtentAction = QAction(
@@ -114,50 +105,6 @@ def selectExtent(self):
def useMinCoveringExtent(self):
self.leText.setText('')

def getMinCoveringExtent(self):
first = True
found = False
for param in self.params:
if param.value:
if isinstance(param, (ParameterRaster, ParameterVector)):
if isinstance(param.value, (QgsRasterLayer,
QgsVectorLayer)):
layer = param.value
else:
layer = dataobjects.getObject(param.value)
if layer:
found = True
self.addToRegion(layer, first)
first = False
elif isinstance(param, ParameterMultipleInput):
layers = param.value.split(';')
for layername in layers:
layer = dataobjects.getObject(layername)
if layer:
found = True
self.addToRegion(layer, first)
first = False
if found:
return '{},{},{},{}'.format(
self.xmin, self.xmax, self.ymin, self.ymax)
else:
return None

def useNewAlg(self, alg):
self.params = alg.parameters

def addToRegion(self, layer, first):
if first:
self.xmin = layer.extent().xMinimum()
self.xmax = layer.extent().xMaximum()
self.ymin = layer.extent().yMinimum()
self.ymax = layer.extent().yMaximum()
else:
self.xmin = min(self.xmin, layer.extent().xMinimum())
self.xmax = max(self.xmax, layer.extent().xMaximum())
self.ymin = min(self.ymin, layer.extent().yMinimum())
self.ymax = max(self.ymax, layer.extent().yMaximum())

def useLayerExtent(self):
CANVAS_KEY = 'Use canvas extent'
extentsDict = {}
@@ -205,10 +152,9 @@ def setValueFromRect(self, r):
self.dialog.activateWindow()

def getValue(self):
if unicode(self.leText.text()).strip() != '':
return unicode(self.leText.text())
else:
return self.getMinCoveringExtent()
if unicode(self.leText.text()).strip() == '':
return None
return unicode(self.leText.text())

def setExtentFromString(self, s):
self.leText.setText(s)
@@ -392,8 +392,6 @@ def prepareAlgorithm(self, alg):
self.tr("Parameter %s in algorithm %s in the model is run with default value! Edit the model to make sure that this is correct.") % (param.name, alg.name),
QgsMessageBar.WARNING, 4)
value = param.default
if value is None and isinstance(param, ParameterExtent):
value = self.getMinCoveringExtent()
# We allow unexistent filepaths, since that allows
# algorithms to skip some conversion routines
if not param.setValue(value) and not isinstance(param,
@@ -442,43 +440,6 @@ def resolveValue(self, value):
else:
return value

def getMinCoveringExtent(self):
first = True
found = False
for param in self.parameters:
if param.value:
if isinstance(param, (ParameterRaster, ParameterVector)):
found = True
if isinstance(param.value, (QgsRasterLayer, QgsVectorLayer)):
layer = param.value
else:
layer = dataobjects.getObjectFromUri(param.value)
self.addToRegion(layer, first)
first = False
elif isinstance(param, ParameterMultipleInput):
found = True
layers = param.value.split(';')
for layername in layers:
layer = dataobjects.getObjectFromUri(layername)
self.addToRegion(layer, first)
first = False
if found:
return ','.join([unicode(v) for v in [self.xmin, self.xmax, self.ymin, self.ymax]])
else:
return None

def addToRegion(self, layer, first):
if first:
self.xmin = layer.extent().xMinimum()
self.xmax = layer.extent().xMaximum()
self.ymin = layer.extent().yMinimum()
self.ymax = layer.extent().yMaximum()
else:
self.xmin = min(self.xmin, layer.extent().xMinimum())
self.xmax = max(self.xmax, layer.extent().xMaximum())
self.ymin = min(self.ymin, layer.extent().yMinimum())
self.ymax = max(self.ymax, layer.extent().yMaximum())

def processAlgorithm(self, progress):
executed = []
toExecute = [alg for alg in self.algs.values() if alg.active]

0 comments on commit 8de9757

Please sign in to comment.
You can’t perform that action at this time.