Skip to content

Commit

Permalink
started raster layer exporting mechanism
Browse files Browse the repository at this point in the history
git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@72 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
volayaf@gmail.com committed Apr 12, 2012
1 parent c888371 commit 4d25fbe
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 14 deletions.
15 changes: 14 additions & 1 deletion src/sextante/core/GeoAlgorithm.py
Expand Up @@ -47,7 +47,12 @@ def helpFile(self):
return None

def processAlgorithm(self):
'''here goes the algorithm itself'''
'''Here goes the algorithm itself
There is no return value from this method. If the algorithm gets canceled
while running this method, the self.canceled value should be set to false
instead to indicate it.
A GeoAlgorithmExecutionException should be raised in case something goes wrong.
'''
pass

def defineCharacteristics(self):
Expand All @@ -64,6 +69,14 @@ def getCustomModelerParametersDialog(self, modelAlg):
it should be returned here, ready to be executed'''
return None

def checkBeforeOpeningParametersDialog(self):
'''If there is any check to perform before the parameters dialog is opened,
it should be done here. This method returns an error message string if there
is any problem (for instance, an external app not configured yet), or None
if the parameters dialog can be opened.
Note that this check should also be done in the processAlgorithm method,
since algorithms can be called without opening the parameters dialog.'''
return None
#=========================================================

def execute(self, progress):
Expand Down
19 changes: 18 additions & 1 deletion src/sextante/core/LayerExporter.py
Expand Up @@ -3,6 +3,7 @@
from qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from sextante.gdal.GdalUtils import GdalUtils

class LayerExporter():

Expand All @@ -14,7 +15,7 @@ class LayerExporter():
def exportVectorLayer(layer):
'''Takes a QgsVectorLayer and returns the filename to refer to it, which allows external
apps which support only file-based layers to use it. It performs the necessary export
in case the input layer is not in a standard format suitable for most appplications, it is
in case the input layer is not in a standard format suitable for most applications, it is
a remote one or db-based (non-file based) one, or if there is a selection and it should be
used, exporting just the selected features.
Currently, the output is restricted to shapefiles, so anything that is not in a shapefile
Expand Down Expand Up @@ -45,5 +46,21 @@ def exportVectorLayer(layer):
return str(layer.source())


@staticmethod
def exportRasterLayer(layer):
'''Takes a QgsRasterLayer and returns the filename to refer to it, which allows external
apps which support only file-based layers to use it. It performs the necessary export
in case the input layer is not in a standard format suitable for most applications, it is
a remote one or db-based (non-file based) one
Currently, the output is restricted to geotiff, but not all other formats are exported.
Only those formats not supported by GDAL are exported, so it is assumed that the external
app uses GDAL to read the layer'''
exts = GdalUtils.getSupportedRasterExtensions()
for ext in exts:
if (str(layer.source()).endswith(ext)):
return str(layer.source())

#TODO:Do the conversion here
return str(layer.source())


2 changes: 1 addition & 1 deletion src/sextante/core/Sextante.py
Expand Up @@ -283,7 +283,7 @@ def loadFromAlg(layersdict):

@staticmethod
def getObject(uri):
'''Returns the QGIS object identified the given URI'''
'''Returns the QGIS object identified by the given URI'''
return QGisLayers.getObjectFromUri(uri)

@staticmethod
Expand Down
19 changes: 19 additions & 0 deletions src/sextante/gui/NumberInputDialog.py
Expand Up @@ -73,6 +73,25 @@ def fillTree(self):
layerItem.addChild(TreeValueItem("Min value", stats.minimumValue))
layersItem.addChild(layerItem)

canvasItem = QtGui.QTreeWidgetItem()
canvasItem.setText(0, "Values from QGIS map canvas")
self.tree.addTopLevelItem(canvasItem)
extent = QGisLayers.iface.mapCanvas().extent()
extentItem = QtGui.QTreeWidgetItem()
extentItem.setText(0, "Current extent")
extentItem.addChild(TreeValueItem("Min X", extent.xMinimum()))
extentItem.addChild(TreeValueItem("Max X", extent.xMaximum()))
extentItem.addChild(TreeValueItem("Min Y", extent.yMinimum()))
extentItem.addChild(TreeValueItem("Max y", extent.yMaximum()))
canvasItem.addChild(extentItem)
extent = QGisLayers.iface.mapCanvas().fullExtent()
extentItem = QtGui.QTreeWidgetItem()
extentItem.setText(0, "Full extent of all layers in map canvas")
extentItem.addChild(TreeValueItem("Min X", extent.xMinimum()))
extentItem.addChild(TreeValueItem("Max X", extent.xMaximum()))
extentItem.addChild(TreeValueItem("Min Y", extent.yMinimum()))
extentItem.addChild(TreeValueItem("Max y", extent.yMaximum()))
canvasItem.addChild(extentItem)
def addValue(self):
item = self.tree.currentItem()
if isinstance(item, TreeValueItem):
Expand Down
5 changes: 5 additions & 0 deletions src/sextante/gui/ParametersDialog.py
Expand Up @@ -222,6 +222,11 @@ def setTableContent(self):
self.tableWidget.setRowHeight(i,22)
i+=1

self.fillParameterValuesFromHistory()

def fillParameterValuesFromHistory(self):
pass

def setParamValues(self):
params = self.alg.parameters
outputs = self.alg.outputs
Expand Down
3 changes: 3 additions & 0 deletions src/sextante/gui/SextanteToolbox.py
Expand Up @@ -100,6 +100,9 @@ def executeAlgorithm(self):
item = self.algorithmTree.currentItem()
if isinstance(item, TreeAlgorithmItem):
alg = Sextante.getAlgorithm(item.alg.commandLineName())
if alg.checkBeforeOpeningParametersDialog():
QtGui.QMessageBox.warning(self, "Warning", message)
return
alg = copy.deepcopy(alg)
dlg = alg.getCustomParametersDialog()
if not dlg:
Expand Down
5 changes: 0 additions & 5 deletions src/sextante/parameters/ParameterDataObject.py
Expand Up @@ -8,11 +8,6 @@ def getValueAsCommandLineParameter(self):
if self.value == None:
return str(None)
else:
#===================================================================
# obj = QGisLayers.getObjectFromUri(str(self.value))
# if obj:
# return "\"" + obj.name() + "\""
#===================================================================
if not SextanteUtils.isWindows():
return "\"" + str(self.value) + "\""
else:
Expand Down
17 changes: 12 additions & 5 deletions src/sextante/parameters/ParameterMultipleInput.py
Expand Up @@ -26,6 +26,7 @@ def __init__(self, name="", description="", datatype=-1, optional = False):
self.exported = None

def setValue(self, obj):
self.exported = None
if obj == None:
if self.optional:
self.value = None
Expand Down Expand Up @@ -54,15 +55,16 @@ def setValue(self, obj):

def getSafeExportedLayers(self):
'''Returns not the value entered by the user, but a string with semicolon-separated filenames
which contains the data of the selectec layers, but saved in a standard format (currently always
shapefiles) so that they can be opened by most external applications.
which contains the data of the selected layers, but saved in a standard format (currently
shapefiles for vector layers and GeoTiff for raster) so that they can be opened by most
external applications.
If there is a selection and SEXTANTE is configured to use just the selection, if exports
the layer even if it is already in a suitable format.
Works only if the layer represented by the parameter value is currently loaded in QGIS.
Otherwise, it will not perform any export and return the current value string.
If the current value represents a layer in a suitable format, it does no export at all
and returns that value.
Currently, it works just for vector layer. In the case of raster layers, it returs the
Currently, it works just for vector layer. In the case of raster layers, it returns the
parameter value.
The layers are exported just the first time the method is called. The method can be called
several times and it will always return the same string, performing the export only the first time.'''
Expand All @@ -72,14 +74,19 @@ def getSafeExportedLayers(self):
layers = self.value.split(";")
if layers == None or len(layers) == 0:
return self.value
if self.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY:
if self.datatype == ParameterMultipleInput.TYPE_RASTER:
for layerfile in layers:
layer = QGisLayers.getObjectFromUri(layerfile, False)
if layer:
filename = LayerExporter.exportVectorLayer(layer)
filename = LayerExporter.exportRasterLayer(layer)
self.exported = self.exported.replace(layerfile, filename)
return self.exported
else:
for layerfile in layers:
layer = QGisLayers.getObjectFromUri(layerfile, False)
if layer:
filename = LayerExporter.exportVectorLayer(layer)
self.exported = self.exported.replace(layerfile, filename)
return self.exported

def getAsString(self,value):
Expand Down
22 changes: 22 additions & 0 deletions src/sextante/parameters/ParameterRaster.py
@@ -1,6 +1,7 @@
from sextante.parameters.ParameterDataObject import ParameterDataObject
from sextante.core.QGisLayers import QGisLayers
from qgis.core import *
from sextante.core.LayerExporter import LayerExporter

class ParameterRaster(ParameterDataObject):

Expand All @@ -9,8 +10,29 @@ def __init__(self, name="", description="", optional=False):
self.description = description
self.optional = optional
self.value = None
self.exported = None

def getSafeExportedLayer(self):
'''Returns not the value entered by the user, but a string with a filename which
contains the data of this layer, but saved in a standard format (currently always
a geotiff file) so that it can be opened by most external applications.
Works only if the layer represented by the parameter value is currently loaded in QGIS.
Otherwise, it will not perform any export and return the current value string.
If the current value represents a layer in a suitable format, it does not export at all
and returns that value.
The layer is exported just the first time the method is called. The method can be called
several times and it will always return the same file, performing the export only the first time.'''
if self.exported:
return self.exported
layer = QGisLayers.getObjectFromUri(self.value, False)
if layer:
self.exported = LayerExporter.exportRasterLayer(layer)
else:
self.exported = self.value
return self.exported

def setValue(self, obj):
self.exported = None
if obj == None:
if self.optional:
self.value = None
Expand Down
1 change: 1 addition & 0 deletions src/sextante/parameters/ParameterVector.py
Expand Up @@ -19,6 +19,7 @@ def __init__(self, name="", description="", shapetype=-1, optional=False):
self.exported = None

def setValue(self, obj):
self.exported = None
if obj == None:
if self.optional:
self.value = None
Expand Down
2 changes: 1 addition & 1 deletion src/sextante/saga/SagaAlgorithm.py
Expand Up @@ -91,7 +91,7 @@ def calculateResamplingExtent(self):
if param.datatype == ParameterMultipleInput.TYPE_RASTER:
layers = param.value.split(";")
for layername in layers:
layer = QGisLayers.getObjectFromUri(layername, first)
layer = QGisLayers.getObjectFromUri(layername)
self.addToResamplingExtent(layer, first)
first = False
else:
Expand Down

0 comments on commit 4d25fbe

Please sign in to comment.