Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Severql bug fixes qnd smqll improvements in qgis bindings
git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@43 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
volayaf committed Mar 17, 2012
1 parent 1865097 commit e4554d5
Show file tree
Hide file tree
Showing 42 changed files with 574 additions and 300 deletions.
5 changes: 4 additions & 1 deletion .pydevproject
Expand Up @@ -2,6 +2,9 @@
<?eclipse-pydev version="1.0"?>

<pydev_project>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
<path>/sextante/src</path>
</pydev_pathproperty>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
</pydev_project>
27 changes: 27 additions & 0 deletions src/sextante/SextantePlugin.py
Expand Up @@ -13,6 +13,8 @@
from sextante.gui.ConfigDialog import ConfigDialog
from sextante.modeler.ModelerDialog import ModelerDialog
from sextante.gui.ResultsDialog import ResultsDialog
from sextante.about.AboutDialog import AboutDialog
import subprocess

cmd_folder = os.path.split(inspect.getfile( inspect.currentframe() ))[0]
if cmd_folder not in sys.path:
Expand Down Expand Up @@ -63,6 +65,20 @@ def initGui(self):
QObject.connect(self.resultsAction, SIGNAL("triggered()"), self.openResults)
self.menu.addAction(self.resultsAction)

icon = QIcon(os.path.dirname(__file__) + "/images/help.png")
self.helpAction = QAction(icon, \
"&SEXTANTE help", self.iface.mainWindow())
QObject.connect(self.helpAction, SIGNAL("triggered()"), self.openHelp)
self.menu.addAction(self.helpAction)

icon = QIcon(os.path.dirname(__file__) + "/images/info.png")
self.aboutAction = QAction(icon, \
"&About SEXTANTE", self.iface.mainWindow())
QObject.connect(self.aboutAction, SIGNAL("triggered()"), self.openAbout)
self.menu.addAction(self.aboutAction)




menuBar = self.iface.mainWindow().menuBar()
menuBar.insertMenu(menuBar.actions()[-1], self.menu)
Expand Down Expand Up @@ -102,4 +118,15 @@ def openConfig(self):
dlg = ConfigDialog(self.toolbox)
dlg.exec_()

def openAbout(self):
dlg = AboutDialog()
dlg.exec_()

def openHelp(self):
filename = os.path.dirname(__file__) + "/manual.pdf"
if os.name == "nt":
os.startfile(filename)
else:
subprocess.call(('xdg-open', filename))


28 changes: 28 additions & 0 deletions src/sextante/about/AboutDialog.py
@@ -0,0 +1,28 @@
from PyQt4 import QtCore, QtGui, QtWebKit
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os

class AboutDialog(QtGui.QDialog):

def __init__(self):
QtGui.QDialog.__init__(self)
self.setModal(True)
self.setupUi()

def setupUi(self):
self.setObjectName("AboutDialog")
self.resize(500, 400)
self.webView = QtWebKit.QWebView()
self.webView.setObjectName("webView")
self.setWindowTitle("About SEXTANTE")
self.horizontalLayout= QtGui.QHBoxLayout()
self.horizontalLayout.setSpacing(2)
self.horizontalLayout.setMargin(0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.horizontalLayout.addWidget(self.webView)
self.setLayout(self.horizontalLayout)
filename = os.path.dirname(__file__) + "/about.htm"
url = QtCore.QUrl(filename)
self.webView.load(url)

Empty file added src/sextante/about/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions src/sextante/about/about.htm
@@ -0,0 +1,13 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>
<title>SEXTANTE Spatial Data Analysis Library</title>
</head>
<body>
<img src="sextante_logo.png" />
<h2>SEXTANTE for QGIS</h2>
<p>SEXTANTE, a geoprocessing platform for QGIS</p>
<p>A development by Victor Olaya (volayaf@gmail.com)</p>
<p>You are currently using SEXTANTE v1.0</p>
<p>For more information, please visit our website at <a href="http://sextantegis.com">http://sextantegis.com</a></p>
</body>
</html>
Binary file added src/sextante/about/sextante_logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 14 additions & 6 deletions src/sextante/core/GeoAlgorithm.py
Expand Up @@ -78,6 +78,12 @@ def setOutputValue(self, outputName, value):
if out.name == outputName:
out.value = value

def getOutputValuesAsDictionary(self):
d = {}
for out in self.outputs:
d[out.name] = out.value
return d

def canBeExecuted(self, layersCount):
return True

Expand Down Expand Up @@ -108,12 +114,14 @@ def getParameterFromName(self, name):
def getParameterValue(self, name):
for param in self.parameters:
if param.name == name:
if isinstance(param, ParameterNumber):
return float(param.value)
elif isinstance(param, ParameterBoolean):
return param.value == str(True)
else:
return param.value
#===============================================================
# if isinstance(param, ParameterNumber):
# return float(param.value)
# elif isinstance(param, ParameterBoolean):
# return param.value == str(True)
# else:
#===============================================================
return param.value
return None

def getOutputValue(self, name):
Expand Down
3 changes: 1 addition & 2 deletions src/sextante/core/Sextante.py
Expand Up @@ -201,7 +201,7 @@ def runalg(name, *args):
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
AlgorithmExecutor.runalg(alg, SilentProgress())
QApplication.restoreOverrideCursor()
return alg.getOuputsValuesAsMap()
return alg.getOutputValuesAsDictionary()
except GeoAlgorithmExecutionException, e:
print "*****Error executing algorithm*****"
print e.msg
Expand All @@ -218,7 +218,6 @@ def loadFromAlg(layersdict):
def getObject(string):
QGisLayers.getObjectFromUri(string)


@staticmethod
def runandload(name, *args):
#a quick fix to call algorithms from the history dialog
Expand Down
2 changes: 1 addition & 1 deletion src/sextante/core/SextanteLog.py
Expand Up @@ -28,7 +28,7 @@ def addToLog(msgtype, msg):
if isinstance(msg, list):
text=""
for i in range(0, len(msg)):
text+=msg[i] + "|"
text+=msg[i].strip("\n") + "|"
text = text[:-1]
else:
text = str(msg).replace("\n", "|")
Expand Down
77 changes: 77 additions & 0 deletions src/sextante/ftools/ExtentFromLayer.py
@@ -0,0 +1,77 @@
from sextante.core.GeoAlgorithm import GeoAlgorithm
import os.path
from PyQt4 import QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from sextante.parameters.ParameterVector import ParameterVector
from sextante.core.QGisLayers import QGisLayers
from sextante.outputs.OutputVector import OutputVector

class ExtentFromLayer(GeoAlgorithm):

INPUT = "INPUT"
OUTPUT = "OUTPUT"

def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/icons/layer_extent.png")

def processAlgorithm(self, progress):
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
output = self.getOutputValue(ExtentFromLayer.OUTPUT)
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(ExtentFromLayer.INPUT))
fields = {
0 : QgsField( "MINX", QVariant.Double ),
1 : QgsField( "MINY", QVariant.Double ),
2 : QgsField( "MAXX", QVariant.Double ),
3 : QgsField( "MAXY", QVariant.Double ),
4 : QgsField( "CNTX", QVariant.Double ),
5 : QgsField( "CNTY", QVariant.Double ),
6 : QgsField( "AREA", QVariant.Double ),
7 : QgsField( "PERIM", QVariant.Double ),
8 : QgsField( "HEIGHT", QVariant.Double ),
9 : QgsField( "WIDTH", QVariant.Double ) }

writer = QgsVectorFileWriter(output, systemEncoding, fields, QGis.WKBPolygon, vlayer.crs() )
rect = vlayer.extent()
minx = rect.xMinimum()
miny = rect.yMinimum()
maxx = rect.xMaximum()
maxy = rect.yMaximum()
height = rect.height()
width = rect.width()
cntx = minx + ( width / 2.0 )
cnty = miny + ( height / 2.0 )
area = width * height
perim = ( 2 * width ) + (2 * height )
rect = [
QgsPoint( minx, miny ),
QgsPoint( minx, maxy ),
QgsPoint( maxx, maxy ),
QgsPoint( maxx, miny ),
QgsPoint( minx, miny ) ]
geometry = QgsGeometry().fromPolygon( [ rect ] )
feat = QgsFeature()
feat.setGeometry( geometry )
feat.setAttributeMap( {
0 : QVariant( minx ),
1 : QVariant( miny ),
2 : QVariant( maxx ),
3 : QVariant( maxy ),
4 : QVariant( cntx ),
5 : QVariant( cnty ),
6 : QVariant( area ),
7 : QVariant( perim ),
8 : QVariant( height ),
9 : QVariant( width ) } )
writer.addFeature( feat )
del writer


def defineCharacteristics(self):
self.name = "Extent from layer"
self.group = "Research tools"
self.addParameter(ParameterVector(ExtentFromLayer.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY))
self.addOutput(OutputVector(ExtentFromLayer.OUTPUT, "Extent layer"))
#=========================================================
7 changes: 6 additions & 1 deletion src/sextante/ftools/FToolsAlgorithmProvider.py
Expand Up @@ -25,6 +25,10 @@
from sextante.ftools.Intersection import Intersection
from sextante.ftools.Union import Union
from sextante.ftools.Clip import Clip
from sextante.ftools.ExtentFromLayer import ExtentFromLayer
from sextante.ftools.RandomSelection import RandomSelection
from sextante.ftools.SelectByLocation import SelectByLocation
from sextante.ftools.RandomSelectionWithinSubsets import RandomSelectionWithinSubsets

class FToolsAlgorithmProvider(AlgorithmProvider):

Expand All @@ -41,7 +45,8 @@ def _loadAlgorithms(self):
SumLines(), BasicStatistics(), PointsInPolygon(),
NearestNeighbourAnalysis(), MeanCoords(), LinesIntersection(),
ConvexHull(), FixedDistanceBuffer(), VariableDistanceBuffer(),
Dissolve(), Difference(), Intersection(), Union(), Clip()]
Dissolve(), Difference(), Intersection(), Union(), Clip(), ExtentFromLayer(),
RandomSelection(), RandomSelectionWithinSubsets(), SelectByLocation()]
for alg in self.algs:
alg.provider = self

Expand Down
2 changes: 1 addition & 1 deletion src/sextante/ftools/PointsInPolygon.py
Expand Up @@ -42,7 +42,7 @@ def processAlgorithm(self, progress):
index = polyProvider.fieldNameIndex(unicode(inField))
if index == -1:
index = polyProvider.fieldCount()
field = QgsField(unicode(inField), QVariant.Double, "real", 24, 15, self.tr("point count field"))
field = QgsField(unicode(inField), QVariant.Double, "real", 24, 15, "point count field")
fieldList[index] = field
sRs = polyProvider.crs()
check = QFile(self.shapefileName)
Expand Down
46 changes: 46 additions & 0 deletions src/sextante/ftools/RandomSelection.py
@@ -0,0 +1,46 @@
from sextante.core.GeoAlgorithm import GeoAlgorithm
import os.path
from PyQt4 import QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from sextante.parameters.ParameterVector import ParameterVector
from sextante.core.QGisLayers import QGisLayers
from sextante.outputs.OutputVector import OutputVector
from sextante.parameters.ParameterSelection import ParameterSelection
from sextante.parameters.ParameterNumber import ParameterNumber
import random

class RandomSelection(GeoAlgorithm):

INPUT = "INPUT"
OUTPUT = "OUTPUT"
METHOD = "METHOD"
NUMBER = "NUMBER"
PERCENTAGE = "PERCENTAGE"

def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/icons/random_selection.png")

def processAlgorithm(self, progress):
filename = self.getParameterValue(RandomSelection.INPUT)
layer = QGisLayers.getObjectFromUri(filename)
method = self.getParameterValue(self.METHOD)
if method == 0:
value = int(self.getParameterValue(self.NUMBER))
else:
value = self.getParameterValue(self.PERCENTAGE)
value = int(round((value / 100.0000), 4) * layer.featureCount())
selran = random.sample(xrange(0, layer.featureCount()), value)
layer.setSelectedFeatures(selran)
self.setOutputValue(self.OUTPUT, filename)


def defineCharacteristics(self):
self.name = "Random selection"
self.group = "Research tools"
self.addParameter(ParameterVector(RandomSelection.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterSelection(RandomSelection.METHOD, "Method", ["Number of selected features", "Percentage of selected features"]))
self.addParameter(ParameterNumber(RandomSelection.NUMBER, "Number of selected features", 1, None, 10))
self.addParameter(ParameterNumber(RandomSelection.PERCENTAGE, "Percentage of selected features", 0, 100, 50))
self.addOutput(OutputVector(RandomSelection.OUTPUT, "Selection", True))
78 changes: 78 additions & 0 deletions src/sextante/ftools/RandomSelectionWithinSubsets.py
@@ -0,0 +1,78 @@
from sextante.core.GeoAlgorithm import GeoAlgorithm
import os.path
from PyQt4 import QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from sextante.parameters.ParameterVector import ParameterVector
from sextante.core.QGisLayers import QGisLayers
from sextante.outputs.OutputVector import OutputVector
from sextante.parameters.ParameterSelection import ParameterSelection
from sextante.parameters.ParameterNumber import ParameterNumber
import random
from sextante.parameters.ParameterTableField import ParameterTableField
from sextante.ftools import ftools_utils

class RandomSelectionWithinSubsets(GeoAlgorithm):

INPUT = "INPUT"
OUTPUT = "OUTPUT"
METHOD = "METHOD"
NUMBER = "NUMBER"
PERCENTAGE = "PERCENTAGE"
FIELD = "FIELD"

def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/icons/random_selection.png")

def processAlgorithm(self, progress):
method = self.getParameterValue(self.METHOD)
field = self.getParameterValue(RandomSelectionWithinSubsets.FIELD)
filename = self.getParameterValue(RandomSelectionWithinSubsets.INPUT)
vlayer = QGisLayers.getObjectFromUri(filename)
vlayer.removeSelection(True)
vprovider = vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select(allAttrs)
index = vprovider.fieldNameIndex(field)
unique = ftools_utils.getUniqueValues(vprovider, int(index))
inFeat = QgsFeature()
selran = []
nFeat = vprovider.featureCount() * len(unique)
nElement = 0
if not len(unique) == vlayer.featureCount():
for i in unique:
vprovider.rewind()
FIDs= []
while vprovider.nextFeature(inFeat):
atMap = inFeat.attributeMap()
if atMap[index] == QVariant(i):
FID = inFeat.id()
FIDs.append(FID)
nElement += 1
progress.setPercentage(nElement/nFeat * 100)
if method == 0:
value = int(self.getParameterValue(self.NUMBER))
else:
value = self.getParameterValue(self.PERCENTAGE)
value = int(round((value / 100.0000) * len(FIDs), 0))
if value >= len(FIDs):
selFeat = FIDs
else:
selFeat = random.sample(FIDs, value)
selran.extend(selFeat)
vlayer.setSelectedFeatures(selran)
else:
vlayer.setSelectedFeatures(range(0, vlayer.featureCount()))
self.setOutputValue(RandomSelectionWithinSubsets.OUTPUT, filename)


def defineCharacteristics(self):
self.name = "Random selection within subsets"
self.group = "Research tools"
self.addParameter(ParameterVector(RandomSelectionWithinSubsets.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterTableField(RandomSelectionWithinSubsets.FIELD, "ID Field", RandomSelectionWithinSubsets.INPUT))
self.addParameter(ParameterSelection(RandomSelectionWithinSubsets.METHOD, "Method", ["Number of selected features", "Percentage of selected features"]))
self.addParameter(ParameterNumber(RandomSelectionWithinSubsets.NUMBER, "Number of selected features", 1, None, 10))
self.addParameter(ParameterNumber(RandomSelectionWithinSubsets.PERCENTAGE, "Percentage of selected features", 0, 100, 50))
self.addOutput(OutputVector(RandomSelectionWithinSubsets.OUTPUT, "Selection", True))

0 comments on commit e4554d5

Please sign in to comment.