Skip to content

Commit

Permalink
changed region mechanism in grass. Now it is set in each execution, u…
Browse files Browse the repository at this point in the history
…sing an extent parameter. This is a more flexible mechanism, with more possibilities

ExtentPanel now can be set to the min covering extent of input values. This is the default value, when the text box is blank, except when it is not possible to do so.

git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@318 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
volayaf committed Jul 30, 2012
1 parent b5d83c0 commit f10f755
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 90 deletions.
3 changes: 2 additions & 1 deletion .settings/org.eclipse.core.resources.prefs
@@ -1,6 +1,7 @@
#Thu Jun 07 09:09:17 CEST 2012
#Sat Jul 28 13:07:03 CEST 2012
eclipse.preferences.version=1
encoding//src/sextante/ftools/ftools_utils.py=utf-8
encoding//src/sextante/gui/AlgorithmExecutionDialog.py=latin1
encoding//src/sextante/gui/ParametersDialog.py=latin1
encoding//src/sextante/gui/ui_ParametersDialog.py=utf-8
encoding//src/sextante/gui/ui_SextanteToolbox.py=utf-8
Expand Down
155 changes: 89 additions & 66 deletions src/sextante/grass/GrassAlgorithm.py
Expand Up @@ -23,9 +23,14 @@
from sextante.core.LayerExporter import LayerExporter
from sextante.core.WrongHelpFileException import WrongHelpFileException
from sextante.outputs.OutputFile import OutputFile
from sextante.parameters.ParameterExtent import ParameterExtent
from sextante.parameters.ParameterNumber import ParameterNumber

class GrassAlgorithm(GeoAlgorithm):

GRASS_REGION_EXTENT_PARAMETER = "GRASS_REGION_PARAMETER"
GRASS_REGION_CELLSIZE_PARAMETER = "GRASS_REGION_CELLSIZE_PARAMETER"

def __init__(self, descriptionfile):
GeoAlgorithm.__init__(self)
self.descriptionFile = descriptionfile
Expand Down Expand Up @@ -98,55 +103,67 @@ def defineCharacteristicsFromFile(self):
raise e
lines.close()

def calculateRegion(self):
auto = SextanteConfig.getSetting(GrassUtils.GRASS_AUTO_REGION)
if auto:
try:
self.cellsize = SextanteConfig.getSetting(GrassUtils.GRASS_REGION_CELLSIZE)
except Exception:
self.cellsize = 0;
first = True;
for param in self.parameters:
if param.value:
if isinstance(param, (ParameterRaster, ParameterVector)):
if isinstance(param.value, (QgsRasterLayer, QgsVectorLayer)):
layer = param.value
else:
layer = QGisLayers.getObjectFromUri(param.value)
self.addToRegion(layer, first)
first = False
elif isinstance(param, ParameterMultipleInput):
layers = param.value.split(";")
for layername in layers:
layer = QGisLayers.getObjectFromUri(layername, first)
self.addToRegion(layer, first)
first = False
if self.cellsize == 0:
self.cellsize = 1
else:
self.xmin = SextanteConfig.getSetting(GrassUtils.GRASS_REGION_XMIN)
self.xmax = SextanteConfig.getSetting(GrassUtils.GRASS_REGION_XMAX)
self.ymin = SextanteConfig.getSetting(GrassUtils.GRASS_REGION_YMIN)
self.ymax = SextanteConfig.getSetting(GrassUtils.GRASS_REGION_YMAX)
self.cellsize = SextanteConfig.getSetting(GrassUtils.GRASS_REGION_CELLSIZE)


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()
if isinstance(layer, QgsRasterLayer):
self.cellsize = (layer.extent().xMaximum() - layer.extent().xMinimum())/layer.width()
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())
if isinstance(layer, QgsRasterLayer):
self.cellsize = max(self.cellsize, (layer.extent().xMaximum() - layer.extent().xMinimum())/layer.width())
#=======================================================================
# self.xmin = SextanteConfig.getSetting(GrassUtils.GRASS_REGION_XMIN)
# self.xmax = SextanteConfig.getSetting(GrassUtils.GRASS_REGION_XMAX)
# self.ymin = SextanteConfig.getSetting(GrassUtils.GRASS_REGION_YMIN)
# self.ymax = SextanteConfig.getSetting(GrassUtils.GRASS_REGION_YMAX)
# extentString = str(self.xmin) + "," + str(self.xmax) + str(self.xmin) + "," + str(self.xmax)
# self.cellsize = SextanteConfig.getSetting(GrassUtils.GRASS_REGION_CELLSIZE)
#=======================================================================
self.addParameter(ParameterExtent(self.GRASS_REGION_EXTENT_PARAMETER, "GRASS region extent"))
self.addParameter(ParameterNumber(self.GRASS_REGION_CELLSIZE_PARAMETER, "GRASS region cellsize", 0, None, 1))

#===============================================================================
# def calculateRegion(self):
# auto = SextanteConfig.getSetting(GrassUtils.GRASS_AUTO_REGION)
# if auto:
# try:
# self.cellsize = SextanteConfig.getSetting(GrassUtils.GRASS_REGION_CELLSIZE)
# except Exception:
# self.cellsize = 0;
# first = True;
# for param in self.parameters:
# if param.value:
# if isinstance(param, (ParameterRaster, ParameterVector)):
# if isinstance(param.value, (QgsRasterLayer, QgsVectorLayer)):
# layer = param.value
# else:
# layer = QGisLayers.getObjectFromUri(param.value)
# self.addToRegion(layer, first)
# first = False
# elif isinstance(param, ParameterMultipleInput):
# layers = param.value.split(";")
# for layername in layers:
# layer = QGisLayers.getObjectFromUri(layername, first)
# self.addToRegion(layer, first)
# first = False
# if self.cellsize == 0:
# self.cellsize = 1
# else:
# self.xmin = SextanteConfig.getSetting(GrassUtils.GRASS_REGION_XMIN)
# self.xmax = SextanteConfig.getSetting(GrassUtils.GRASS_REGION_XMAX)
# self.ymin = SextanteConfig.getSetting(GrassUtils.GRASS_REGION_YMIN)
# self.ymax = SextanteConfig.getSetting(GrassUtils.GRASS_REGION_YMAX)
# self.cellsize = SextanteConfig.getSetting(GrassUtils.GRASS_REGION_CELLSIZE)
#
#
# 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()
# if isinstance(layer, QgsRasterLayer):
# self.cellsize = (layer.extent().xMaximum() - layer.extent().xMinimum())/layer.width()
# 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())
# if isinstance(layer, QgsRasterLayer):
# self.cellsize = max(self.cellsize, (layer.extent().xMaximum() - layer.extent().xMinimum())/layer.width())
#===============================================================================

def processAlgorithm(self, progress):
if SextanteUtils.isWindows():
Expand All @@ -157,15 +174,17 @@ def processAlgorithm(self, progress):
commands = []
self.exportedLayers = {}

self.calculateRegion()
#self.calculateRegion()
region = str(self.getParameterValue(self.GRASS_REGION_EXTENT_PARAMETER))
regionCoords = region.split(",")
GrassUtils.createTempMapset();

command = "g.region"
command += " n=" + str(self.ymax)
command +=" s=" + str(self.ymin)
command +=" e=" + str(self.xmax)
command +=" w=" + str(self.xmin)
command +=" res=" + str(self.cellsize);
command += " n=" + str(regionCoords[3])
command +=" s=" + str(regionCoords[2])
command +=" e=" + str(regionCoords[1])
command +=" w=" + str(regionCoords[0])
command +=" res=" + str(self.getParameterValue(self.GRASS_REGION_CELLSIZE_PARAMETER));
commands.append(command)

#1: Export layer to grass mapset
Expand Down Expand Up @@ -200,6 +219,8 @@ def processAlgorithm(self, progress):
for param in self.parameters:
if param.value == None:
continue
if param.name == self.GRASS_REGION_CELLSIZE_PARAMETER or param.name == self.GRASS_REGION_EXTENT_PARAMETER:
continue
if isinstance(param, (ParameterRaster, ParameterVector)):
value = param.value
if value in self.exportedLayers.keys():
Expand Down Expand Up @@ -306,15 +327,17 @@ def getTempFilename(self):
def commandLineName(self):
return "grass:" + self.name[:self.name.find(" ")]

def checkBeforeOpeningParametersDialog(self):
for param in self.parameters:
if isinstance(param, (ParameterRaster, ParameterVector)):
return None
if isinstance(param, ParameterMultipleInput):
if not param.optional:
return None

if SextanteConfig.getSetting(GrassUtils.GRASS_AUTO_REGION):
return "This algorithm cannot be run with the 'auto-region' setting\nPlease set a GRASS region before running it"
else:
return None
#===============================================================================
# def checkBeforeOpeningParametersDialog(self):
# for param in self.parameters:
# if isinstance(param, (ParameterRaster, ParameterVector)):
# return None
# if isinstance(param, ParameterMultipleInput):
# if not param.optional:
# return None
#
# if SextanteConfig.getSetting(GrassUtils.GRASS_AUTO_REGION):
# return "This algorithm cannot be run with the 'auto-region' setting\nPlease set a GRASS region before running it"
# else:
# return None
#===============================================================================
34 changes: 20 additions & 14 deletions src/sextante/grass/GrassAlgorithmProvider.py
Expand Up @@ -14,8 +14,10 @@ class GrassAlgorithmProvider(AlgorithmProvider):

def __init__(self):
AlgorithmProvider.__init__(self)
self.actions.append(DefineGrassRegionAction())
self.actions.append(DefineGrassRegionFromLayerAction())
#=======================================================================
# self.actions.append(DefineGrassRegionAction())
# self.actions.append(DefineGrassRegionFromLayerAction())
#=======================================================================
self.createAlgsList() #preloading algorithms to speed up

def initializeSettings(self):
Expand All @@ -25,27 +27,31 @@ def initializeSettings(self):
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_WIN_SHELL, "Msys folder", GrassUtils.grassWinShell()))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LOG_COMMANDS, "Log execution commands", False))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LOG_CONSOLE, "Log console output", False))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_AUTO_REGION, "Use min covering region", True))
#SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_AUTO_REGION, "Use min covering region", True))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_LATLON, "Coordinates are lat/lon", False))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_XMIN, "GRASS Region min x", 0))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_YMIN, "GRASS Region min y", 0))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_XMAX, "GRASS Region max x", 1000))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_YMAX, "GRASS Region max y", 1000))
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_CELLSIZE, "GRASS Region cellsize", 100))
#=======================================================================
# SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_XMIN, "GRASS Region min x", 0))
# SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_YMIN, "GRASS Region min y", 0))
# SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_XMAX, "GRASS Region max x", 1000))
# SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_YMAX, "GRASS Region max y", 1000))
# SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_REGION_CELLSIZE, "GRASS Region cellsize", 100))
#=======================================================================
SextanteConfig.addSetting(Setting(self.getDescription(), GrassUtils.GRASS_HELP_FOLDER, "GRASS help folder", GrassUtils.grassHelpPath()))

def unload(self):
AlgorithmProvider.unload(self)
if SextanteUtils.isWindows() or SextanteUtils.isMac():
SextanteConfig.removeSetting(GrassUtils.GRASS_FOLDER)
SextanteConfig.removeSetting(GrassUtils.GRASS_WIN_SHELL)
SextanteConfig.removeSetting(GrassUtils.GRASS_AUTO_REGION)
#SextanteConfig.removeSetting(GrassUtils.GRASS_AUTO_REGION)
SextanteConfig.removeSetting(GrassUtils.GRASS_LATLON)
SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_XMIN)
SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_YMIN)
SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_XMAX)
SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_YMAX)
SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_CELLSIZE)
#=======================================================================
# SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_XMIN)
# SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_YMIN)
# SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_XMAX)
# SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_YMAX)
# SextanteConfig.removeSetting(GrassUtils.GRASS_REGION_CELLSIZE)
#=======================================================================
SextanteConfig.removeSetting(GrassUtils.GRASS_HELP_FOLDER)
SextanteConfig.removeSetting(GrassUtils.GRASS_LOG_COMMANDS)
SextanteConfig.removeSetting(GrassUtils.GRASS_LOG_CONSOLE)
Expand Down
2 changes: 1 addition & 1 deletion src/sextante/grass/GrassUtils.py
Expand Up @@ -10,7 +10,7 @@
class GrassUtils:

GRASS_LATLON = "GRASS_LATLON"
GRASS_AUTO_REGION = "GRASS_AUTO_REGION"
#GRASS_AUTO_REGION = "GRASS_AUTO_REGION"
GRASS_REGION_XMIN = "GRASS_REGION_XMIN"
GRASS_REGION_YMIN = "GRASS_REGION_YMIN"
GRASS_REGION_XMAX = "GRASS_REGION_XMAX"
Expand Down
2 changes: 1 addition & 1 deletion src/sextante/grass/description/r.carve.txt
Expand Up @@ -7,4 +7,4 @@ ParameterNumber|width|Stream width (in meters). Default is raster cell width|Non
ParameterNumber|depth|Additional stream depth (in meters)|None|None|1
ParameterBoolean|-n|No flat areas allowed in flow direction|False
OutputRaster|output|Name for output raster map
OutputVector|points|ame for output vector map for adjusted stream points
OutputVector|points|Name for output vector map for adjusted stream points
10 changes: 10 additions & 0 deletions src/sextante/gui/AlgorithmExecutionDialog.py
Expand Up @@ -105,9 +105,19 @@ def setParamValues(self):
for param in params:
if param.hidden:
continue
if isinstance(param, ParameterExtent):
continue
if not self.setParamValue(param, self.paramTable.valueItems[param.name]):
return False

for param in params:
if isinstance(param, ParameterExtent):
value = self.paramTable.valueItems[param.name].getValue()
if value is not None:
param.value = value
else:
return False

for output in outputs:
if output.hidden:
continue
Expand Down

0 comments on commit f10f755

Please sign in to comment.