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$'
|
|
|
|
|
2013-04-11 11:53:19 +04:00
|
|
|
from PyQt4.QtCore import *
|
|
|
|
|
|
|
|
from qgis.core import *
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
from sextante.core.GeoAlgorithm import GeoAlgorithm
|
2013-04-11 11:53:19 +04:00
|
|
|
from sextante.core.QGisLayers import QGisLayers
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
from sextante.parameters.ParameterVector import ParameterVector
|
|
|
|
from sextante.parameters.ParameterString import ParameterString
|
2013-04-11 11:53:19 +04:00
|
|
|
from sextante.parameters.ParameterNumber import ParameterNumber
|
|
|
|
from sextante.parameters.ParameterSelection import ParameterSelection
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-04-11 11:53:19 +04:00
|
|
|
from sextante.outputs.OutputVector import OutputVector
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
class FieldsCalculator(GeoAlgorithm):
|
|
|
|
|
|
|
|
INPUT_LAYER = "INPUT_LAYER"
|
|
|
|
FIELD_NAME = "FIELD_NAME"
|
2013-04-11 11:53:19 +04:00
|
|
|
FIELD_TYPE = "FIELD_TYPE"
|
|
|
|
FIELD_LENGTH = "FIELD_LENGTH"
|
|
|
|
FIELD_PRECISION = "FIELD_PRECISION"
|
2012-09-15 18:25:25 +03:00
|
|
|
FORMULA = "FORMULA"
|
2013-01-01 23:52:00 +01:00
|
|
|
OUTPUT_LAYER = "OUTPUT_LAYER"
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-04-11 11:53:19 +04:00
|
|
|
TYPE_NAMES = ["Integer", "Float", "String"]
|
|
|
|
TYPES = [QVariant.Int, QVariant.Double, QVariant.String]
|
|
|
|
|
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"))
|
2013-04-11 11:53:19 +04:00
|
|
|
self.addParameter(ParameterSelection(self.FIELD_TYPE, "Field type", self.TYPE_NAMES))
|
|
|
|
self.addParameter(ParameterNumber(self.FIELD_LENGTH, "Field lenght", 1, 255, 10))
|
|
|
|
self.addParameter(ParameterNumber(self.FIELD_PRECISION, "Field precision", 0, 10, 0))
|
2012-09-15 18:25:25 +03:00
|
|
|
self.addParameter(ParameterString(self.FORMULA, "Formula"))
|
|
|
|
self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer"))
|
|
|
|
|
|
|
|
def processAlgorithm(self, progress):
|
2013-04-11 11:53:19 +04:00
|
|
|
fieldName = self.getParameterValue(self.FIELD_NAME)
|
|
|
|
fieldType = self.getParameterValue(self.FIELD_TYPE)
|
|
|
|
fieldLength = self.getParameterValue(self.FIELD_LENGTH)
|
|
|
|
fieldPrecision = self.getParameterValue(self.FIELD_PRECISION)
|
2012-09-15 18:25:25 +03:00
|
|
|
formula = self.getParameterValue(self.FORMULA)
|
|
|
|
output = self.getOutputFromName(self.OUTPUT_LAYER)
|
2013-04-11 11:53:19 +04:00
|
|
|
|
|
|
|
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
|
|
|
|
provider = layer.dataProvider()
|
|
|
|
fields = provider.fields()
|
|
|
|
fields.append(QgsField(fieldName, self.TYPES[fieldType], "", fieldLength, fieldPrecision))
|
|
|
|
writer = output.getVectorWriter(fields, provider.geometryType(), layer.crs())
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
outFeat = QgsFeature()
|
|
|
|
inGeom = QgsGeometry()
|
2013-04-11 11:53:19 +04:00
|
|
|
nFeat = provider.featureCount()
|
2012-09-15 18:25:25 +03:00
|
|
|
nElement = 0
|
2013-04-11 11:53:19 +04:00
|
|
|
features = QGisLayers.features(layer)
|
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:
|
2013-04-11 11:53:19 +04:00
|
|
|
expression = expression.replace(unicode(fields[k].name()), unicode(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)
|
2013-03-26 14:15:12 +01:00
|
|
|
attrs = inFeat.attributes()
|
2013-03-26 00:57:27 +01:00
|
|
|
attrs.append(QVariant(result))
|
|
|
|
outFeat.setAttributes(attrs)
|
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
|