Skip to content

Commit

Permalink
Finished adapting sextante python algorithms to new vector API
Browse files Browse the repository at this point in the history
Some code cleaning
  • Loading branch information
volaya committed Feb 4, 2013
1 parent 2c980d2 commit 7dce617
Show file tree
Hide file tree
Showing 12 changed files with 382 additions and 2,154 deletions.
4 changes: 2 additions & 2 deletions python/plugins/sextante/algs/Explode.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ def processAlgorithm(self, progress):
fields = vprovider.fields()
writer = output.getVectorWriter(fields, QGis.WKBLineString, vprovider.crs() )
outFeat = QgsFeature()
inGeom = QgsGeometry()
nFeat = vprovider.featureCount()
inGeom = QgsGeometry()
nElement = 0
features = QGisLayers.features(layer)
nFeat = len(features)
for feature in features:
nElement += 1
progress.setPercentage((nElement*100)/nFeat)
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/sextante/algs/JoinAttributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class JoinAttributes(GeoAlgorithm):
TABLE_FIELD_2 = "TABLE_FIELD_2"

def defineCharacteristics(self):
self.name = "Joint Attributes table"
self.name = "Join attributes table"
self.group = "Vector general tools"
self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_ANY, False))
self.addParameter(ParameterVector(self.INPUT_LAYER_2, "Input layer 2", ParameterVector.VECTOR_TYPE_ANY, False))
Expand Down
86 changes: 86 additions & 0 deletions python/plugins/sextante/algs/PointsLayerFromTable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
PointsLayerFromTable.py
---------------------
Date : January 2013
Copyright : (C) 2013 by Victor Olaya
Email : volayaf at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
from sextante.parameters.ParameterTable import ParameterTable
from sextante.parameters.ParameterTableField import ParameterTableField
from sextante.core.SextanteLog import SextanteLog

__author__ = 'Victor Olaya'
__date__ = 'August 2013'
__copyright__ = '(C) 2013, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from sextante.core.GeoAlgorithm import GeoAlgorithm
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from sextante.core.QGisLayers import QGisLayers
from sextante.outputs.OutputVector import OutputVector

class PointsLayerFromTable(GeoAlgorithm):

INPUT = "INPUT"
XFIELD = "XFIELD"
YFIELD = "YFIELD"
OUTPUT = "OUTPUT"

#===========================================================================
# def getIcon(self):
# return QtGui.QIcon(os.path.dirname(__file__) + "/../images/toolbox.png")
#===========================================================================

def processAlgorithm(self, progress):
source = self.getParameterValue(self.INPUT)
vlayer = QGisLayers.getObjectFromUri(source)
output = self.getOutputFromName(self.OUTPUT)
vprovider = vlayer.dataProvider()
fields = vprovider.fields()
writer = output.getVectorWriter(fields, QGis.WKBPoint, self.crs)
xfieldindex = vlayer.fieldNameIndex(self.getParameterValue(self.XFIELD))
yfieldindex = vlayer.fieldNameIndex(self.getParameterValue(self.YFIELD))

outFeat = QgsFeature()
nElement = 0
features = QGisLayers.features(vlayer)
nFeat = len(features)
for feature in features:
nElement += 1
progress.setPercentage((nElement*100)/nFeat)
attrs = feature.attributes()
try:
x = float(attrs[xfieldindex].toString())
y = float(attrs[yfieldindex].toString())
except:
continue
pt = QgsPoint(x, y)
outFeat.setGeometry(QgsGeometry.fromPoint(pt))
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)

del writer


def defineCharacteristics(self):
self.name = "Points layer from table"
self.group = "Vector creation tools"
self.addParameter(ParameterTable(self.INPUT, "Input layer"))
self.addParameter(ParameterTableField(self.XFIELD, "X field", self.INPUT, ParameterTableField.DATA_TYPE_NUMBER))
self.addParameter(ParameterTableField(self.YFIELD, "Y field", self.INPUT, ParameterTableField.DATA_TYPE_NUMBER))
self.addOutput(OutputVector(self.OUTPUT, "Output layer"))

13 changes: 5 additions & 8 deletions python/plugins/sextante/algs/QGISAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
* *
***************************************************************************
"""
from sextante.algs.CreateConstantRaster import CreateConstantRaster

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
Expand Down Expand Up @@ -65,8 +63,7 @@
mmqgisx_geometry_convert_algorithm,
mmqgisx_grid_algorithm, mmqgisx_gridify_algorithm,
mmqgisx_hub_distance_algorithm, mmqgisx_hub_lines_algorithm,
mmqgisx_label_point_algorithm, mmqgisx_merge_algorithm,
mmqgisx_select_algorithm, mmqgisx_sort_algorithm,
mmqgisx_merge_algorithm, mmqgisx_select_algorithm,
mmqgisx_text_to_float_algorithm)
from sextante.algs.EquivalentNumField import EquivalentNumField
from sextante.core.AlgorithmProvider import AlgorithmProvider
Expand All @@ -79,6 +76,8 @@
from sextante.algs.AutoincrementalField import AutoincrementalField
from sextante.algs.FieldPyculator import FieldsPyculator
from sextante.algs.JoinAttributes import JoinAttributes
from sextante.algs.CreateConstantRaster import CreateConstantRaster
from sextante.algs.PointsLayerFromTable import PointsLayerFromTable

class QGISAlgorithmProvider(AlgorithmProvider):

Expand All @@ -89,7 +88,7 @@ def __init__(self):
#FTOOLS
SumLines(), PointsInPolygon(), PointsInPolygonWeighted(), PointsInPolygonUnique(),
BasicStatisticsStrings(), BasicStatisticsNumbers(), NearestNeighbourAnalysis(),
MeanCoords(), LinesIntersection(), UniqueValues(), PointDistance(),
MeanCoords(), LinesIntersection(), UniqueValues(), PointDistance(), PointsLayerFromTable(),
# data management
ReprojectLayer(),
# geometry
Expand All @@ -116,10 +115,8 @@ def __init__(self):
mmqgisx_gridify_algorithm(),
mmqgisx_hub_distance_algorithm(),
mmqgisx_hub_lines_algorithm(),
mmqgisx_label_point_algorithm(),
mmqgisx_merge_algorithm(),
mmqgisx_select_algorithm(),
mmqgisx_sort_algorithm(),
mmqgisx_select_algorithm(),
mmqgisx_text_to_float_algorithm(),
#raster
CreateConstantRaster()]
Expand Down
34 changes: 13 additions & 21 deletions python/plugins/sextante/algs/ftools/ExportGeometryInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,13 @@
__revision__ = '$Format:%H$'

from PyQt4.QtCore import *

from qgis.core import *

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.QGisLayers import QGisLayers

from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterSelection import ParameterSelection

from sextante.outputs.OutputVector import OutputVector

from sextante.algs.ftools import FToolsUtils as utils

class ExportGeometryInfo(GeoAlgorithm):

INPUT = "INPUT"
Expand Down Expand Up @@ -72,19 +66,16 @@ def processAlgorithm(self, progress):

layer.select(layer.pendingAllAttributesList())

idx1 = -1
idx2 = -1
fields = layer.pendingFields()
fields = provider.fields()

if geometryType == QGis.Polygon:
idx1, fields = utils.findOrCreateField(layer, fields, "area", 21, 6)
idx2, fields = utils.findOrCreateField(layer, fields, "perimeter", 21, 6)
fields.append(QgsField(QString("area"), QVariant.Double))
fields.append(QgsField(QString("perimeter"), QVariant.Double))
elif geometryType == QGis.Line:
idx1, fields = utils.findOrCreateField(layer, fields, "length", 21, 6)
idx2 = idx1
fields.append(QgsField(QString("length"), QVariant.Double))
else:
idx1, fields = utils.findOrCreateField(layer, fields, "xcoord", 21, 6)
idx2, fields = utils.findOrCreateField(layer, fields, "ycoord", 21, 6)
fields.append(QgsField(QString("xcoords"), QVariant.Double))
fields.append(QgsField(QString("ycoords"), QVariant.Double))

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields,
provider.geometryType(), provider.crs())
Expand Down Expand Up @@ -121,11 +112,12 @@ def processAlgorithm(self, progress):
(attr1, attr2) = self.simpleMeasure(inGeom, method, ellips, crs)

outFeat.setGeometry(inGeom)
atMap = inFeat.attributeMap()
outFeat.setAttributeMap(atMap)
outFeat.addAttribute(idx1, QVariant(attr1))
outFeat.addAttribute(idx2, QVariant(attr2))
writer.addFeature( outFeat )
atMap = inFeat.attributes()
atMap.append(QVariant(attr1))
if attr2 is not None:
atMap.append(QVariant(attr2))
outFeat.setAttributes(atMap)
writer.addFeature(outFeat)

current += 1
progress.setPercentage(int(current * total))
Expand Down Expand Up @@ -153,7 +145,7 @@ def simpleMeasure(self, geom, method, ellips, crs):
if geom.type() == QGis.Polygon:
attr2 = self.perimMeasure(geom, measure)
else:
attr2 = attr1
attr2 = None

return (attr1, attr2)

Expand Down
2 changes: 0 additions & 2 deletions python/plugins/sextante/algs/ftools/ExtentFromLayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* *
***************************************************************************
"""
from PyQt4 import QtGui

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
Expand Down Expand Up @@ -120,7 +119,6 @@ def featureExtent(self, layer, writer, progress):
feat = QgsFeature()

provider = layer.dataProvider()
provider.select()
features = QGisLayers.features(layer)
total = 100.0 / float(len(features))
for inFeat in features:
Expand Down
Loading

0 comments on commit 7dce617

Please sign in to comment.