Skip to content
Permalink
Browse files
added explicit CRS checking in sextante algorithms
  • Loading branch information
volaya committed Feb 6, 2013
1 parent 63ed164 commit cf85937
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 14 deletions.
@@ -272,6 +272,24 @@ def setOutputCRS(self):
return
qgis = QGisLayers.iface
self.crs = qgis.mapCanvas().mapRenderer().destinationCrs()

def checkInputCRS(self):
'''it checks that all input layers use the same CRS. If so, returns True. False otherwise'''
crs = None;
layers = QGisLayers.getAllLayers()
for param in self.parameters:
if isinstance(param, (ParameterRaster, ParameterVector, ParameterMultipleInput)):
if param.value:
inputlayers = param.value.split(";")
for inputlayer in inputlayers:
for layer in layers:
if layer.source() == inputlayer:
if crs is None:
crs = layer.crs()
else:
if crs != layer.crs():
return False
return True

def addOutput(self, output):
#TODO: check that name does not exist
@@ -174,7 +174,7 @@ def loadAlgorithms():
for alg in providerAlgs:
algs[alg.commandLineName()] = alg
Sextante.algs[provider.getName()] = algs

#this is a special provider, since it depends on others
#TODO Fix circular imports, so this provider can be incorporated
#as a normal one
@@ -287,9 +287,13 @@ def runAlgorithm(algOrName, onFinish, *args):

msg = alg.checkParameterValuesBeforeExecuting()
if msg:
print ("Unable to execute algorithm\n" + msg)
return
print ("Unable to execute algorithm\n" + msg)
return

if not alg.checkInputCRS():
print ("Warning: Not all input layers use the same CRS.\n" +
"This can cause unexpected results.")

SextanteLog.addToLog(SextanteLog.LOG_ALGORITHM, alg.getAsCommand())

# don't set the wait cursor twice, because then when you restore it
@@ -300,7 +304,12 @@ def runAlgorithm(algOrName, onFinish, *args):
elif cursor.shape() != Qt.WaitCursor:
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))

if SextanteConfig.getSetting(SextanteConfig.USE_THREADS):
useThreads = SextanteConfig.getSetting(SextanteConfig.USE_THREADS)

#this is doing strange things, so temporarily the thread execution is disabled from the console
useThreads = False

if useThreads:
algEx = AlgorithmExecutor(alg)
progress = QProgressDialog()
progress.setWindowTitle(alg.name)
@@ -45,6 +45,8 @@ class SextanteConfig():
RECENT_ALGORITHMS = "RECENT_ALGORITHMS"
PRE_EXECUTION_SCRIPT = "PRE_EXECUTION_SCRIPT"
POST_EXECUTION_SCRIPT = "POST_EXECUTION_SCRIPT"
SHOW_CRS_DEF = "SHOW_CRS_DEF"
WARN_UNMATCHING_CRS = "WARN_UNMATCHING_CRS"

settings = {}
settingIcons= {}
@@ -61,8 +63,9 @@ def initialize():
SextanteConfig.addSetting(Setting("General", SextanteConfig.USE_FILENAME_AS_LAYER_NAME, "Use filename as layer name", True))
SextanteConfig.addSetting(Setting("General", SextanteConfig.SHOW_RECENT_ALGORITHMS, "Show recently executed algorithms", True))
SextanteConfig.addSetting(Setting("General", SextanteConfig.USE_CATEGORIES, "Use categories to classify algorithms, instead of providers", False))
SextanteConfig.addSetting(Setting("General", SextanteConfig.OUTPUT_FOLDER,
"Output folder", SextanteUtils.tempFolder()))
SextanteConfig.addSetting(Setting("General", SextanteConfig.OUTPUT_FOLDER, "Output folder", SextanteUtils.tempFolder()))
SextanteConfig.addSetting(Setting("General", SextanteConfig.SHOW_CRS_DEF, "Show layer CRS definition in selection boxes", True))
SextanteConfig.addSetting(Setting("General", SextanteConfig.WARN_UNMATCHING_CRS, "Warn before executing if layer CRS's do not match", True))
SextanteConfig.addSetting(Setting("General", SextanteConfig.RASTER_STYLE,"Style for raster layers",""))
SextanteConfig.addSetting(Setting("General", SextanteConfig.VECTOR_POINT_STYLE,"Style for point layers",""))
SextanteConfig.addSetting(Setting("General", SextanteConfig.VECTOR_LINE_STYLE,"Style for line layers",""))
@@ -47,7 +47,6 @@
from sextante.parameters.ParameterCrs import ParameterCrs
from sextante.core.SextanteConfig import SextanteConfig
from sextante.parameters.ParameterExtent import ParameterExtent
from sextante.outputs.OutputHTML import OutputHTML
from sextante.outputs.OutputRaster import OutputRaster
from sextante.outputs.OutputVector import OutputVector
from sextante.outputs.OutputTable import OutputTable
@@ -202,10 +201,19 @@ def setParamValue(self, param, widget):

@pyqtSlot()
def accept(self):
checkCRS = SextanteConfig.getSetting(SextanteConfig.WARN_UNMATCHING_CRS)
keepOpen = SextanteConfig.getSetting(SextanteConfig.KEEP_DIALOG_OPEN)
useThread = SextanteConfig.getSetting(SextanteConfig.USE_THREADS)
try:
self.setParamValues()
if checkCRS and not self.alg.checkInputCRS():
reply = QMessageBox.question(self, "Unmatching CRS's",
"Layers do not all use the same CRS.\n" +
"This can cause unexpected results.\n" +
"Do you want to continue?",
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.No:
return
msg = self.alg.checkParameterValuesBeforeExecuting()
if msg:
if keepOpen or useThread:
@@ -192,23 +192,29 @@ def buttonToggled(self, value):
if button is not sender:
button.setChecked(False)

def getExtendedLayerName(self, layer):
if SextanteConfig.getSetting(SextanteConfig.SHOW_CRS_DEF):
return layer.name() + " [" + layer.crs().description() +"]"
else:
return layer.name()

def getWidgetFromParameter(self, param):
if isinstance(param, ParameterRaster):
layers = QGisLayers.getRasterLayers()
items = []
if (param.optional):
items.append((self.NOT_SELECTED, None))
for layer in layers:
items.append((layer.name(), layer))
items.append((self.getExtendedLayerName(layer), layer))
item = InputLayerSelectorPanel(items)
elif isinstance(param, ParameterVector):
if self.somethingDependsOnThisParameter(param):
item = QtGui.QComboBox()
layers = QGisLayers.getVectorLayers(param.shapetype)
if (param.optional):
item.addItem(self.NOT_SELECTED, None)
item.addItem((self.NOT_SELECTED, None))
for layer in layers:
item.addItem(layer.name(), layer)
item.addItem((self.getExtendedLayerName(layer), layer))
item.currentIndexChanged.connect(self.updateDependentFields)
item.name = param.name
else:
@@ -217,16 +223,16 @@ def getWidgetFromParameter(self, param):
if (param.optional):
items.append((self.NOT_SELECTED, None))
for layer in layers:
items.append((layer.name(), layer))
items.append((self.getExtendedLayerName(layer), layer))
item = InputLayerSelectorPanel(items)
elif isinstance(param, ParameterTable):
if self.somethingDependsOnThisParameter(param):
item = QtGui.QComboBox()
layers = QGisLayers.getTables()
if (param.optional):
item.addItem(self.NOT_SELECTED, None)
item.addItem((self.NOT_SELECTED, None))
for layer in layers:
item.addItem(layer.name(), layer)
item.addItem((layer.name(), layer))
item.currentIndexChanged.connect(self.updateDependentFields)
item.name = param.name
else:
@@ -279,7 +285,7 @@ def getWidgetFromParameter(self, param):
options = QGisLayers.getVectorLayers(param.datatype)
opts = []
for opt in options:
opts.append(opt.name())
opts.append(self.getExtendedLayerName(opt))
item = MultipleInputPanel(opts)
elif isinstance(param, ParameterNumber):
item = NumberInputPanel(param.default, param.isInteger)
@@ -97,6 +97,10 @@ def getSupportedOutputVectorLayerExtensions(self):

def getSupportedOutputRasterLayerExtensions(self):
return ["tif"]

def getSupportedOutputTableLayerExtensions(self):
return ["dbf"]


def getIcon(self):
return QIcon(os.path.dirname(__file__) + "/../images/saga.png")

0 comments on commit cf85937

Please sign in to comment.