Skip to content
Permalink
Browse files
Saga algorithms are now aware of selection in qgis
git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@45 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
volayaf committed Mar 18, 2012
1 parent e4554d5 commit 50a8ce3f0f7216f2f4f9197c3438dcf21ae75b66
@@ -1,4 +1,4 @@
#Tue Feb 21 15:41:17 CET 2012
#Fri Mar 16 10:56:39 CET 2012
eclipse.preferences.version=1
encoding//src/sextante/ftools/ftools_utils.py=utf-8
encoding//src/sextante/gui/ui_ParametersDialog.py=utf-8
BIN +28 Bytes (120%) src/sextante/images/info.png
Binary file not shown.
@@ -15,8 +15,8 @@ class ParameterFactory():
@staticmethod
def getFromString(s):
classes = [ParameterBoolean, ParameterMultipleInput,ParameterNumber,
ParameterRaster, ParameterString, ParameterTable, ParameterVector,
ParameterTableField, ParameterSelection, ParameterRange, ParameterFixedTable]
ParameterRaster, ParameterString, ParameterVector, ParameterTableField,
ParameterTable, ParameterSelection, ParameterRange, ParameterFixedTable]
for clazz in classes:
if s.startswith(clazz().parameterName()):
return clazz().deserialize(s[len(clazz().parameterName())+1:])
@@ -14,7 +14,7 @@ def __init__(self, name="", description="", minValue = None, maxValue = None, de
def setValue(self, n):
##try:
if (float(n) - int(float(n)) == 0):
value = int(n)
value = int(float(n))
else:
value = float(n)
if self.min:
@@ -29,6 +29,7 @@
from sextante.parameters.ParameterFactory import ParameterFactory
from sextante.outputs.OutputFactory import OutputFactory
from sextante.core.SextanteConfig import SextanteConfig
from sextante.core.QGisLayers import QGisLayers

class SagaAlgorithm(GeoAlgorithm):

@@ -63,7 +64,6 @@ def defineCharacteristicsFromFile(self):


def defineCharacteristicsFromFileSagaFormat(self):

lines = open(self._descriptionFile)
line = lines.readline()
while line != "":
@@ -208,46 +208,53 @@ def calculateResamplingExtent(self):


def processAlgorithm(self, progress):

path = SagaUtils.sagaPath()
if path == "":
raise GeoAlgorithmExecutionException("SAGA folder is not configured.\nPlease configure it before running SAGA algorithms.")
useSelection = SextanteConfig.getSetting(SagaUtils.SAGA_USE_SELECTED)
commands = list()
self.exportedLayers = {}
self.numExportedLayers = 0;

#1: Export rasters to sgrd. only ASC and TIF are supported.
# Vector layers must be in shapefile format and tables in dbf format. We check that.
#1: Export rasters to sgrd and vectors to shp
# Tables must be in dbf format. We check that.
if self.resample:
self.calculateResamplingExtent()
for param in self.parameters:
if isinstance(param, ParameterRaster):
if param.value == None:
continue
if isinstance(param.value, QgsRasterLayer):
value = str(param.value.dataProvider().dataSourceUri())
else:
value = param.value
#===============================================================
# if isinstance(param.value, QgsRasterLayer):
# value = str(param.value.dataProvider().dataSourceUri())
# else:
#===============================================================
value = param.value
if not value.endswith("sgrd"):
commands.append(self.exportRasterLayer(value))
if self.resample:
commands.append(self.resampleRasterLayer(value));
if isinstance(param, ParameterVector):
if param.value == None:
continue
if isinstance(param.value, QgsVectorLayer):
value = str(param.value.dataProvider().dataSourceUri())
else:
value = param.value
if not value.endswith("shp"):
raise GeoAlgorithmExecutionException("Unsupported file format")
#===============================================================
# if isinstance(param.value, QgsVectorLayer):
# value = str(param.value.dataProvider().dataSourceUri())
# else:
#===============================================================
value = param.value
if (not value.endswith("shp")) or useSelection:
self.exportVectorLayer(value)
#raise GeoAlgorithmExecutionException("Unsupported file format")
if isinstance(param, ParameterTable):
if param.value == None:
continue
if isinstance(param.value, QgsVectorLayer):
value = str(param.value.dataProvider().dataSourceUri())
else:
value = param.value
#===============================================================
# if isinstance(param.value, QgsVectorLayer):
# value = str(param.value.dataProvider().dataSourceUri())
# else:
#===============================================================
value = param.value
if value.endswith("shp"):
value = value[:-3] + "dbf"
if not value.endswith("dbf"):
@@ -274,7 +281,7 @@ def processAlgorithm(self, progress):
for param in self.parameters:
if param.value == None:
continue
if isinstance(param, ParameterRaster):
if isinstance(param, (ParameterRaster, ParameterVector)):
value = param.value
if value in self.exportedLayers.keys():
command+=(" -" + param.name + " " + self.exportedLayers[value])
@@ -348,13 +355,50 @@ def resampleRasterLayer(self,layer):
" -USER_SIZE " + str(self.cellsize) + " -USER_GRID " + destFilename
return s

def exportRasterLayer(self,layer):

def getObjectFromUri(self, uri):
layers = QGisLayers.getVectorLayers()
for layer in layers:
if layer.source() == uri:
return layer
return None

def exportVectorLayer(self, filename):
layer = self.getObjectFromUri(filename)
if layer:
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
output = self.getTempFilename("shp")
provider = layer.dataProvider()
allAttrs = provider.attributeIndexes()
provider.select( allAttrs )
useSelection = SextanteConfig.getSetting(SagaUtils.SAGA_USE_SELECTED)
if useSelection and layer.selectedFeatureCount() != 0:
writer = QgsVectorFileWriter( output, systemEncoding,provider.fields(), provider.geometryType(), provider.crs() )
selection = layer.selectedFeatures()
for feat in selection:
writer.addFeature(feat)
del writer
self.exportedLayers[filename]=output
else:
if (not filename.endswith("shp")):
writer = QgsVectorFileWriter( output, systemEncoding,provider.fields(), provider.geometryType(), provider.crs() )
feat = QgsFeature()
while provider.nextFeature(feat):
writer.addFeature(feat)
del writer
self.exportedLayers[filename]=output
else:
if (not filename.endswith("shp")):
raise GeoAlgorithmExecutionException("Unsupported file format")

def exportRasterLayer(self, layer):
#=======================================================================
# if not layer.lower().endswith("tif") and not layer.lower().endswith("asc"):
# raise GeoAlgorithmExecutionException("Unsupported input file format: " + layer)
#=======================================================================
#ext = os.path.splitext(layer)[1][1:].strip()
destFilename = self.getTempFilename()
destFilename = self.getTempFilename("sgrd")
self.exportedLayers[layer]= destFilename
#=======================================================================
# if ext.lower() == "tif":
@@ -364,10 +408,10 @@ def exportRasterLayer(self,layer):
return "io_gdal 0 -GRIDS " + destFilename + " -FILES " + layer


def getTempFilename(self):
def getTempFilename(self, ext):
self.numExportedLayers+=1
path = SextanteUtils.tempFolder()
filename = path + os.sep + str(time.time()) + str(SextanteUtils.NUM_EXPORTED) + ".sgrd"
filename = path + os.sep + str(time.time()) + str(SextanteUtils.NUM_EXPORTED) + "." + ext
SextanteUtils.NUM_EXPORTED +=1

return filename
@@ -19,6 +19,7 @@ def __init__(self):
SextanteConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_RESAMPLING_REGION_XMAX, "Resampling region max x", 1000))
SextanteConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_RESAMPLING_REGION_YMAX, "Resampling region max y", 1000))
SextanteConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_RESAMPLING_REGION_CELLSIZE, "Resampling region cellsize", 1))
SextanteConfig.addSetting(Setting("SAGA", SagaUtils.SAGA_USE_SELECTED, "Use only selected features in vector layers", False))

def _loadAlgorithms(self):
folder = SagaUtils.sagaDescriptionPath()
@@ -6,6 +6,7 @@

class SagaUtils:

SAGA_USE_SELECTED = "SAGA_USE_SELECTED"
SAGA_AUTO_RESAMPLING = "SAGA_AUTO_RESAMPLING"
SAGA_RESAMPLING_REGION_XMIN = "SAGA_RESAMPLING_REGION_XMIN"
SAGA_RESAMPLING_REGION_YMIN = "SAGA_RESAMPLING_REGION_YMIN"

0 comments on commit 50a8ce3

Please sign in to comment.