Skip to content

Commit

Permalink
Merge pull request #3456 from arnaud-morvan/processing_apply_use_cove…
Browse files Browse the repository at this point in the history
…rge_on_execution

[Processing] Calculate min coverage on execution
  • Loading branch information
volaya authored Sep 5, 2016
2 parents 2fbc326 + c369710 commit 8de9757
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 138 deletions.
59 changes: 59 additions & 0 deletions python/plugins/processing/core/GeoAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 0 additions & 9 deletions python/plugins/processing/gui/AlgorithmDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 0 additions & 15 deletions python/plugins/processing/gui/BatchAlgorithmDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 1 addition & 16 deletions python/plugins/processing/gui/BatchPanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
64 changes: 5 additions & 59 deletions python/plugins/processing/gui/ExtentSelectionPanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]'))
Expand All @@ -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(
Expand All @@ -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 = {}
Expand Down Expand Up @@ -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)
39 changes: 0 additions & 39 deletions python/plugins/processing/modeler/ModelerAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 8de9757

Please sign in to comment.