2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
FieldsCalculator.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
from sextante.core.GeoAlgorithm import GeoAlgorithm
|
|
|
|
from sextante.outputs.OutputVector import OutputVector
|
|
|
|
from sextante.parameters.ParameterVector import ParameterVector
|
|
|
|
from qgis.core import *
|
|
|
|
from PyQt4.QtCore import *
|
|
|
|
from PyQt4.QtGui import *
|
|
|
|
from sextante.parameters.ParameterString import ParameterString
|
|
|
|
from sextante.core.QGisLayers import QGisLayers
|
|
|
|
|
|
|
|
|
|
|
|
class FieldsCalculator(GeoAlgorithm):
|
|
|
|
|
|
|
|
INPUT_LAYER = "INPUT_LAYER"
|
|
|
|
FIELD_NAME = "FIELD_NAME"
|
|
|
|
FORMULA = "FORMULA"
|
2013-01-01 23:52:00 +01:00
|
|
|
OUTPUT_LAYER = "OUTPUT_LAYER"
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2012-12-20 00:16:05 +01:00
|
|
|
#===========================================================================
|
|
|
|
# def getIcon(self):
|
|
|
|
# return QtGui.QIcon(os.path.dirname(__file__) + "/../images/qgis.png")
|
|
|
|
#===========================================================================
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def defineCharacteristics(self):
|
|
|
|
self.name = "Field calculator"
|
2012-12-20 00:16:05 +01:00
|
|
|
self.group = "Vector table tools"
|
2012-09-15 18:25:25 +03:00
|
|
|
self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_ANY, False))
|
|
|
|
self.addParameter(ParameterString(self.FIELD_NAME, "Result field name"))
|
|
|
|
self.addParameter(ParameterString(self.FORMULA, "Formula"))
|
|
|
|
self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer"))
|
|
|
|
|
|
|
|
def processAlgorithm(self, progress):
|
|
|
|
fieldname = self.getParameterValue(self.FIELD_NAME)
|
|
|
|
formula = self.getParameterValue(self.FORMULA)
|
|
|
|
output = self.getOutputFromName(self.OUTPUT_LAYER)
|
|
|
|
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
|
|
|
|
vprovider = vlayer.dataProvider()
|
|
|
|
fields = vprovider.fields()
|
2013-02-04 00:14:39 +01:00
|
|
|
fields.append(QgsField(fieldname, QVariant.Double))
|
2013-02-24 16:40:01 +01:00
|
|
|
writer = output.getVectorWriter(fields, vprovider.geometryType(), vlayer.crs())
|
2012-09-15 18:25:25 +03:00
|
|
|
outFeat = QgsFeature()
|
|
|
|
inGeom = QgsGeometry()
|
|
|
|
nFeat = vprovider.featureCount()
|
|
|
|
nElement = 0
|
2013-01-12 23:36:00 +01:00
|
|
|
features = QGisLayers.features(vlayer)
|
2012-12-24 00:03:30 +01:00
|
|
|
for inFeat in features:
|
2013-01-01 23:52:00 +01:00
|
|
|
progress.setPercentage(int((100 * nElement) / nFeat))
|
2013-02-04 00:14:39 +01:00
|
|
|
attrs = inFeat.attributes()
|
2012-09-15 18:25:25 +03:00
|
|
|
expression = formula
|
2013-02-04 00:14:39 +01:00
|
|
|
k = 0
|
|
|
|
for attr in attrs:
|
2012-09-15 18:25:25 +03:00
|
|
|
expression = expression.replace(str(fields[k].name()), str(attr.toString()))
|
2013-02-04 00:14:39 +01:00
|
|
|
k += 1
|
2012-09-15 18:25:25 +03:00
|
|
|
try:
|
|
|
|
result = eval(expression)
|
|
|
|
except Exception:
|
2013-01-26 23:55:14 +01:00
|
|
|
result = None
|
|
|
|
#raise GeoAlgorithmExecutionException("Problem evaluation formula: Wrong field values or formula")
|
2012-09-15 18:25:25 +03:00
|
|
|
nElement += 1
|
|
|
|
inGeom = inFeat.geometry()
|
2013-01-01 23:52:00 +01:00
|
|
|
outFeat.setGeometry(inGeom)
|
2012-09-15 18:25:25 +03:00
|
|
|
atMap = inFeat.attributeMap()
|
2013-02-04 00:14:39 +01:00
|
|
|
atMap.append(QVariant(result))
|
2013-02-07 01:09:39 +01:00
|
|
|
outFeat.setAttributeMap(atMap)
|
2013-01-01 23:52:00 +01:00
|
|
|
writer.addFeature(outFeat)
|
2012-09-15 18:25:25 +03:00
|
|
|
del writer
|
|
|
|
|
|
|
|
|
|
|
|
def checkParameterValuesBeforeExecuting(self):
|
|
|
|
##TODO check that formula is correct and fields exist
|
|
|
|
pass
|
|
|
|
|