Skip to content

Commit f91e9d3

Browse files
committed
minor sextante fixes
1 parent 994dc1c commit f91e9d3

16 files changed

+135
-31
lines changed

python/plugins/sextante/algs/FieldsCalculator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ def processAlgorithm(self, progress):
7979
try:
8080
result = eval(expression)
8181
except Exception:
82-
raise GeoAlgorithmExecutionException("Problem evaluation formula: Wrong field values or formula")
82+
result = None
83+
#raise GeoAlgorithmExecutionException("Problem evaluation formula: Wrong field values or formula")
8384
nElement += 1
8485
inGeom = inFeat.geometry()
8586
outFeat.setGeometry(inGeom)

python/plugins/sextante/algs/ftools/Centroids.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
# This will get replaced with a git SHA1 when you do a git archive
2424
__revision__ = '$Format:%H$'
2525

26-
from PyQt4 import QtGui
2726
from PyQt4.QtCore import *
2827

2928
from qgis.core import *

python/plugins/sextante/algs/ftools/FixedDistanceBuffer.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ def defineCharacteristics(self):
6464

6565
def processAlgorithm(self, progress):
6666
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
67-
useSelection = self.getParameterValue(self.USE_SELECTED)
6867
distance = self.getParameterValue(self.DISTANCE)
6968
dissolve = self.getParameterValue(self.DISSOLVE)
7069
segments = int(self.getParameterValue(self.SEGMENTS))

python/plugins/sextante/core/AlgorithmProvider.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ def getSupportedOutputVectorLayerExtensions(self):
101101
extension = extension[:extension.find(" ")]
102102
if extension.lower() != "shp":
103103
extensions.append(extension)
104-
return extensions
105-
#return ["shp"]
104+
return extensions
106105

107106
def getSupportedOutputTableExtensions(self):
108107
return ["csv"]

python/plugins/sextante/core/GeoAlgorithm.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
GeoAlgorithmExecutionException.py
6+
---------------------
7+
Date : August 2012
8+
Copyright : (C) 2012 by Victor Olaya
9+
Email : volayaf at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Victor Olaya'
21+
__date__ = 'August 2012'
22+
__copyright__ = '(C) 2012, Victor Olaya'
23+
# This will get replaced with a git SHA1 when you do a git archive
24+
__revision__ = '$Format:%H$'
25+
26+
import os.path
27+
import traceback
28+
import copy
129
from sextante.outputs.Output import Output
230
from sextante.parameters.Parameter import Parameter
331
from sextante.core.QGisLayers import QGisLayers
@@ -6,18 +34,14 @@
634
from PyQt4 import QtGui
735
from PyQt4.QtCore import *
836
from qgis.core import *
9-
import os.path
1037
from sextante.core.SextanteUtils import SextanteUtils
1138
from sextante.parameters.ParameterMultipleInput import ParameterMultipleInput
1239
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
13-
import traceback
1440
from sextante.core.SextanteLog import SextanteLog
1541
from sextante.outputs.OutputVector import OutputVector
1642
from sextante.outputs.OutputRaster import OutputRaster
1743
from sextante.outputs.OutputTable import OutputTable
1844
from sextante.outputs.OutputHTML import OutputHTML
19-
import copy
20-
from sextante.core.SextanteVectorWriter import SextanteVectorWriter
2145
from sextante.core.SextanteConfig import SextanteConfig
2246
from sextante.gdal.GdalUtils import GdalUtils
2347

@@ -177,13 +201,22 @@ def convertUnsupportedFormats(self, progress):
177201
for feature in features:
178202
writer.addFeature(feature)
179203
elif isinstance(out, OutputRaster):
204+
out.close()
180205
if out.compatible is not None:
181206
layer = QGisLayers.getObjectFromUri(out.compatible)
182207
provider = layer.dataProvider()
183208
writer = QgsRasterFileWriter(out.value)
184209
format = self.getFormatShortNameFromFilename(out.value)
185210
writer.setOutputFormat(format);
186211
writer.writeRaster(layer.pipe(), layer.width(), layer.height(), layer.extent(), layer.crs())
212+
elif isinstance(out, OutputTable):
213+
if out.compatible is not None:
214+
layer = QGisLayers.getObjectFromUri(out.compatible)
215+
provider = layer.dataProvider()
216+
writer = out.getTableWriter(provider.fields())
217+
features = QGisLayers.features(layer)
218+
for feature in features:
219+
writer.addRecord(feature)
187220
progress.setPercentage(100 * i / float(len(self.outputs)))
188221

189222
def getFormatShortNameFromFilename(self, filename):

python/plugins/sextante/core/QGisLayers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
from qgis.core import *
2828
from PyQt4.QtCore import *
2929
from PyQt4.QtGui import *
30-
from PyQt4 import QtGui
3130
from os import path
3231
from sextante.core.SextanteConfig import SextanteConfig
3332

@@ -61,7 +60,7 @@ def getSupportedOutputRasterLayerExtensions():
6160

6261
@staticmethod
6362
def getSupportedOutputTableExtensions():
64-
exts = ["dbf, csv"]
63+
exts = ["csv"]
6564
return exts
6665

6766
@staticmethod

python/plugins/sextante/core/Sextante.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,16 @@ class Sextante:
7070
modeler = ModelerAlgorithmProvider()
7171

7272
@staticmethod
73-
def addProvider(provider):
73+
def addProvider(provider, updateList = False):
7474
'''use this method to add algorithms from external providers'''
7575
'''Adding a new provider automatically initializes it, so there is no need to do it in advance'''
7676
#Note: this might slow down the initialization process if there are many new providers added.
7777
#Should think of a different solution
7878
provider.initializeSettings()
7979
Sextante.providers.append(provider)
8080
SextanteConfig.loadSettings()
81-
Sextante.updateAlgsList()
81+
if updateList:
82+
Sextante.updateAlgsList()
8283

8384
@staticmethod
8485
def removeProvider(provider):
@@ -141,13 +142,14 @@ def updateAlgsList():
141142
Sextante.fireAlgsListHasChanged()
142143

143144
@staticmethod
144-
def loadFromProviders():
145+
def loadFromProviders():
145146
Sextante.loadAlgorithms()
146147
Sextante.loadActions()
147148
Sextante.loadContextMenuActions()
148149

149150
@staticmethod
150151
def updateProviders():
152+
151153
for provider in Sextante.providers:
152154
provider.loadAlgorithms()
153155

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
SextanteRasterWriter.py
6+
---------------------
7+
Date : January 2013
8+
Copyright : (C) 2013 by Victor Olaya
9+
Email : volayaf at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Victor Olaya'
21+
__date__ = 'September 2012'
22+
__copyright__ = '(C) 2012, Victor Olaya'
23+
# This will get replaced with a git SHA1 when you do a git archive
24+
__revision__ = '$Format:%H$'
25+
26+
from qgis.core import *
27+
import numpy
28+
from PyQt4.QtCore import *
29+
30+
class SextanteRasterWriter:
31+
32+
NODATA = -99999.0
33+
34+
def __init__(self, fileName, minx, miny, maxx, maxy, cellsize, nbands, crs):
35+
self.fileName = fileName
36+
self.nx = (maxx - minx) / float(cellsize)
37+
self.ny = (maxy - miny) / float(cellsize)
38+
self.matrix = numpy.empty(shape=(self.nx, self.ny, nbands))
39+
self.matrix[:] = self.NODATA
40+
self.cellsize = cellsize
41+
self.crs = crs
42+
43+
def setValue(self, value, x, y, band = 0):
44+
try:
45+
self.matrix[x, y, band] = value
46+
except IndexError:
47+
pass
48+
49+
def getValue(self, x, y, band = 0):
50+
try:
51+
return matrix[x, y, band]
52+
except IndexError:
53+
return self.NODATA
54+
55+
def close(self):
56+
#todo
57+
pass

python/plugins/sextante/core/SextanteTableWriter.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ def __init__(self, fileName, encoding, fields):
3737
settings = QSettings()
3838
encoding = settings.value("/SextanteQGIS/encoding", "System").toString()
3939

40-
if fileName.endswith("csv"):
40+
if not fileName.endswith("csv"):
4141
fileName += ".csv"
42-
file = open(fileName, "w")
43-
file.write(";".join(field.name() for field in fields))
44-
file.write("\n")
45-
file.close()
42+
file = open(fileName, "w")
43+
file.write(";".join(field.name() for field in fields))
44+
file.write("\n")
45+
file.close()
4646

47-
def addFeature(self, values):
47+
def addRecord(self, values):
4848
file = open(self.fileName, "a")
4949
file.write(";".join([value.toString() for value in values]))
5050
file.write("\n")

python/plugins/sextante/gdal/warp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def defineCharacteristics(self):
5757

5858
def processAlgorithm(self, progress):
5959
srs = self.getParameterValue(warp.DEST_SRS)
60-
self.crs = QgsCoordinateReferenceSystem(int(srs))
60+
self.crs = QgsCoordinateReferenceSystem(srs)
6161
commands = ["gdalwarp"]
6262
commands.append("-s_srs")
6363
commands.append(str(self.getParameterValue(warp.SOURCE_SRS)))

python/plugins/sextante/grass/GrassAlgorithm.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,17 @@ def defineCharacteristicsFromFile(self):
119119
line = lines.readline().strip("\n").strip()
120120
self.group = line
121121
hasRasterOutput = False
122+
hasVectorOutput = False
122123
while line != "":
123124
try:
124125
line = line.strip("\n").strip()
125126
if line.startswith("Parameter"):
126127
parameter = ParameterFactory.getFromString(line);
127128
self.addParameter(parameter)
128129
if isinstance(parameter, ParameterVector):
129-
hasVectorOutput = True
130+
hasVectorOutput = True
130131
if isinstance(parameter, ParameterMultipleInput) and parameter.datatype < 3:
131-
hasVectorOutput = True
132+
hasVectorOutput = True
132133
elif line.startswith("*Parameter"):
133134
param = ParameterFactory.getFromString(line[1:])
134135
param.isAdvanced = True
@@ -151,10 +152,11 @@ def defineCharacteristicsFromFile(self):
151152
param = ParameterNumber(self.GRASS_SNAP_TOLERANCE_PARAMETER, "v.in.ogr snap tolerance (-1 = no snap)", -1, None, -1.0)
152153
param.isAdvanced = True
153154
self.addParameter(param)
154-
ParameterNumber(self.GRASS_MIN_AREA_PARAMETER, "v.in.ogr min area", 0, None, 0.0001)
155+
param = ParameterNumber(self.GRASS_MIN_AREA_PARAMETER, "v.in.ogr min area", 0, None, 0.0001)
155156
param.isAdvanced = True
156157
self.addParameter(param)
157158

159+
158160
def getDefaultCellsize(self):
159161
cellsize = 0
160162
for param in self.parameters:

python/plugins/sextante/gui/SextanteToolbox.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def executeAlgorithm(self):
132132
if message:
133133
QMessageBox.warning(self, self.tr("Warning"), message)
134134
return
135-
alg = alg.getCopy()#copy.deepcopy(alg)
135+
alg = alg.getCopy()
136136
dlg = alg.getCustomParametersDialog()
137137
if not dlg:
138138
dlg = ParametersDialog(alg)

python/plugins/sextante/modeler/ModelerAlgorithmProvider.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
* *
1717
***************************************************************************
1818
"""
19-
2019
__author__ = 'Victor Olaya'
2120
__date__ = 'August 2012'
2221
__copyright__ = '(C) 2012, Victor Olaya'
@@ -63,7 +62,7 @@ def getName(self):
6362
def getIcon(self):
6463
return QtGui.QIcon(os.path.dirname(__file__) + "/../images/model.png")
6564

66-
def _loadAlgorithms(self):
65+
def _loadAlgorithms(self):
6766
folder = ModelerUtils.modelsFolder()
6867
self.loadFromFolder(folder)
6968
folder = os.path.join(os.path.dirname(__file__), "models")

python/plugins/sextante/modeler/ModelerParametersDialog.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,6 @@ def setPreviousValues(self):
540540
if value is not None:
541541
widget = self.valueItems[out.name].setText(unicode(value))
542542

543-
#TODO
544543

545544

546545
def setParamValues(self):

python/plugins/sextante/outputs/OutputRaster.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
***************************************************************************
1818
"""
1919
from sextante.core.QGisLayers import QGisLayers
20+
from sextante.core.SextanteRasterWriter import SextanteRasterWriter
2021

2122
__author__ = 'Victor Olaya'
2223
__date__ = 'August 2012'
@@ -44,9 +45,22 @@ def getCompatibleFileName(self, alg):
4445
'''Returns a filename that is compatible with the algorithm that is going to generate this output.
4546
If the algorithm supports the file format of the current output value, it returns that value. If not,
4647
it returns a temporary file with a supported file format, to be used to generate the output result.'''
47-
if self.value.endswith(self.getDefaultFileExtension(alg)):
48+
ext = self.value[self.value.rfind(".") + 1:]
49+
if ext in alg.provider.getSupportedOutputRasterLayerExtensions():
4850
return self.value
4951
else:
5052
if self.compatible is None:
5153
self.compatible = SextanteUtils.getTempFilename(self.getDefaultFileExtension(alg))
5254
return self.compatible;
55+
56+
def getVectorWriter(self, minx, miny, maxx, maxy, cellsize, nbands, crs):
57+
'''Returns a suitable raster. Use this to transparently handle output
58+
values instead of creating your own method.
59+
60+
It should be called just once, since a new call might
61+
result in previous data being replaced, thus rendering a previously
62+
obtained writer useless
63+
'''
64+
65+
w = SextanteRasterWriter(self.value, minx, miny, maxx, maxy, cellsize, nbands, crs)
66+
return w

python/plugins/sextante/outputs/OutputVector.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class OutputVector(Output):
3737
compatible = None
3838

3939
def getFileFilter(self,alg):
40-
exts = QGisLayers.getSupportedOutputRasterLayerExtensions()
40+
exts = QGisLayers.getSupportedOutputVectorLayerExtensions()
4141
for i in range(len(exts)):
4242
exts[i] = exts[i].upper() + " files(*." + exts[i].lower() + ")"
4343
return ";;".join(exts)
@@ -50,7 +50,8 @@ def getCompatibleFileName(self, alg):
5050
'''Returns a filename that is compatible with the algorithm that is going to generate this output.
5151
If the algorithm supports the file format of the current output value, it returns that value. If not,
5252
it returns a temporary file with a supported file format, to be used to generate the output result.'''
53-
if self.value.endswith(self.getDefaultFileExtension(alg)):
53+
ext = self.value[self.value.rfind(".") + 1:]
54+
if ext in alg.provider.getSupportedOutputVectorLayerExtensions():
5455
return self.value
5556
else:
5657
if self.compatible is None:

0 commit comments

Comments
 (0)