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'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
from PyQt4.QtCore import QVariant
|
|
|
|
from qgis.core import QgsExpression, QgsFeature, QgsField, QgsDistanceArea, QgsProject, GEO_NONE
|
2014-05-20 16:30:59 +02:00
|
|
|
from qgis.utils import iface
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterString
|
|
|
|
from processing.core.parameters import ParameterNumber
|
|
|
|
from processing.core.parameters import ParameterBoolean
|
|
|
|
from processing.core.parameters import ParameterSelection
|
|
|
|
from processing.core.outputs import OutputVector
|
2014-03-26 19:25:07 +02:00
|
|
|
from processing.tools import dataobjects, vector, system
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2016-01-08 10:32:43 +02:00
|
|
|
from .ui.FieldsCalculatorDialog import FieldsCalculatorDialog
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
class FieldsCalculator(GeoAlgorithm):
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT_LAYER = 'INPUT_LAYER'
|
2013-10-23 20:13:59 +03:00
|
|
|
NEW_FIELD = 'NEW_FIELD'
|
2013-10-01 20:52:22 +03:00
|
|
|
FIELD_NAME = 'FIELD_NAME'
|
|
|
|
FIELD_TYPE = 'FIELD_TYPE'
|
|
|
|
FIELD_LENGTH = 'FIELD_LENGTH'
|
|
|
|
FIELD_PRECISION = 'FIELD_PRECISION'
|
|
|
|
FORMULA = 'FORMULA'
|
|
|
|
OUTPUT_LAYER = 'OUTPUT_LAYER'
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-23 20:13:59 +03:00
|
|
|
TYPES = [QVariant.Double, QVariant.Int, QVariant.String, QVariant.Date]
|
2013-04-11 11:53:19 +04:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
def defineCharacteristics(self):
|
2015-07-26 03:48:27 +02:00
|
|
|
self.name, self.i18n_name = self.trAlgorithm('Field calculator')
|
|
|
|
self.group, self.i18n_group = self.trAlgorithm('Vector table tools')
|
2015-08-31 16:59:11 +02:00
|
|
|
|
|
|
|
self.type_names = [self.tr('Float'),
|
|
|
|
self.tr('Integer'),
|
|
|
|
self.tr('String'),
|
|
|
|
self.tr('Date')]
|
|
|
|
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT_LAYER,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_ANY], False))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterString(self.FIELD_NAME,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Result field name')))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterSelection(self.FIELD_TYPE,
|
2015-08-31 16:59:11 +02:00
|
|
|
self.tr('Field type'), self.type_names))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterNumber(self.FIELD_LENGTH,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Field length'), 1, 255, 10))
|
2013-10-01 20:52:22 +03:00
|
|
|
self.addParameter(ParameterNumber(self.FIELD_PRECISION,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Field precision'), 0, 15, 3))
|
2013-10-23 20:13:59 +03:00
|
|
|
self.addParameter(ParameterBoolean(self.NEW_FIELD,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Create new field'), True))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterString(self.FORMULA, self.tr('Formula')))
|
2015-05-22 16:39:20 +02:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Calculated')))
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def processAlgorithm(self, progress):
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
|
2013-04-11 11:53:19 +04:00
|
|
|
fieldName = self.getParameterValue(self.FIELD_NAME)
|
2013-10-23 20:13:59 +03:00
|
|
|
fieldType = self.TYPES[self.getParameterValue(self.FIELD_TYPE)]
|
|
|
|
width = self.getParameterValue(self.FIELD_LENGTH)
|
|
|
|
precision = self.getParameterValue(self.FIELD_PRECISION)
|
|
|
|
newField = self.getParameterValue(self.NEW_FIELD)
|
2012-09-15 18:25:25 +03:00
|
|
|
formula = self.getParameterValue(self.FORMULA)
|
2013-10-23 20:13:59 +03:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
output = self.getOutputFromName(self.OUTPUT_LAYER)
|
2013-04-11 11:53:19 +04:00
|
|
|
|
2014-03-26 19:25:07 +02:00
|
|
|
if output.value == '':
|
|
|
|
ext = output.getDefaultFileExtension(self)
|
2014-11-22 11:28:19 +02:00
|
|
|
output.value = system.getTempFilenameInTempFolder(
|
|
|
|
output.name + '.' + ext)
|
2014-03-26 19:25:07 +02:00
|
|
|
|
2013-04-11 11:53:19 +04:00
|
|
|
provider = layer.dataProvider()
|
2013-10-23 20:13:59 +03:00
|
|
|
fields = layer.pendingFields()
|
|
|
|
if newField:
|
|
|
|
fields.append(QgsField(fieldName, fieldType, '', width, precision))
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
writer = output.getVectorWriter(fields, provider.geometryType(),
|
2013-10-23 20:13:59 +03:00
|
|
|
layer.crs())
|
|
|
|
|
|
|
|
exp = QgsExpression(formula)
|
|
|
|
|
|
|
|
da = QgsDistanceArea()
|
|
|
|
da.setSourceCrs(layer.crs().srsid())
|
2014-11-22 11:28:19 +02:00
|
|
|
da.setEllipsoidalMode(
|
|
|
|
iface.mapCanvas().mapSettings().hasCrsTransformEnabled())
|
|
|
|
da.setEllipsoid(QgsProject.instance().readEntry(
|
|
|
|
'Measure', '/Ellipsoid', GEO_NONE)[0])
|
2013-10-23 20:13:59 +03:00
|
|
|
exp.setGeomCalculator(da)
|
|
|
|
|
|
|
|
if not exp.prepare(layer.pendingFields()):
|
|
|
|
raise GeoAlgorithmExecutionException(
|
2015-01-15 20:41:15 +02:00
|
|
|
self.tr('Evaluation error: %s' % exp.evalErrorString()))
|
2013-04-11 11:53:19 +04:00
|
|
|
|
2013-10-23 20:13:59 +03:00
|
|
|
outFeature = QgsFeature()
|
|
|
|
outFeature.initAttributes(len(fields))
|
|
|
|
outFeature.setFields(fields)
|
|
|
|
|
|
|
|
error = ''
|
|
|
|
calculationSuccess = True
|
|
|
|
|
2013-09-12 13:19:00 +02:00
|
|
|
features = vector.features(layer)
|
2013-10-23 20:13:59 +03:00
|
|
|
total = 100.0 / len(features)
|
|
|
|
|
|
|
|
rownum = 1
|
2014-01-06 12:32:00 +02:00
|
|
|
for current, f in enumerate(features):
|
|
|
|
rownum = current + 1
|
2013-10-23 20:13:59 +03:00
|
|
|
exp.setCurrentRowNumber(rownum)
|
|
|
|
value = exp.evaluate(f)
|
|
|
|
if exp.hasEvalError():
|
|
|
|
calculationSuccess = False
|
|
|
|
error = exp.evalErrorString()
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
outFeature.setGeometry(f.geometry())
|
|
|
|
for fld in f.fields():
|
|
|
|
outFeature[fld.name()] = f[fld.name()]
|
|
|
|
outFeature[fieldName] = value
|
|
|
|
writer.addFeature(outFeature)
|
2013-08-06 15:06:35 +03:00
|
|
|
|
2013-10-23 20:13:59 +03:00
|
|
|
progress.setPercentage(int(current * total))
|
2012-09-15 18:25:25 +03:00
|
|
|
del writer
|
|
|
|
|
2013-10-23 20:13:59 +03:00
|
|
|
if not calculationSuccess:
|
|
|
|
raise GeoAlgorithmExecutionException(
|
2016-01-24 20:16:19 +01:00
|
|
|
self.tr('An error occurred while evaluating the calculation '
|
2015-01-15 20:41:15 +02:00
|
|
|
'string:\n%s' % error))
|
2013-10-23 20:13:59 +03:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
def checkParameterValuesBeforeExecuting(self):
|
2013-10-23 20:13:59 +03:00
|
|
|
newField = self.getParameterValue(self.NEW_FIELD)
|
2015-04-28 09:19:58 +02:00
|
|
|
fieldName = self.getParameterValue(self.FIELD_NAME).strip()
|
2013-10-23 20:13:59 +03:00
|
|
|
if newField and len(fieldName) == 0:
|
2015-04-28 09:19:58 +02:00
|
|
|
return self.tr('Field name is not set. Please enter a field name')
|
2013-10-23 20:13:59 +03:00
|
|
|
|
|
|
|
def getCustomParametersDialog(self):
|
|
|
|
return FieldsCalculatorDialog(self)
|