mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-13 00:03:09 -04:00
[processing] port QGIS native field calculator
This commit is contained in:
parent
f1551df3a0
commit
2fc5c01c7d
@ -2,5 +2,6 @@ FILE(GLOB PY_FILES *.py)
|
||||
|
||||
ADD_SUBDIRECTORY(mmqgisx)
|
||||
ADD_SUBDIRECTORY(ftools)
|
||||
ADD_SUBDIRECTORY(ui)
|
||||
|
||||
PLUGIN_INSTALL(processing ./algs ${PY_FILES})
|
||||
|
@ -27,18 +27,24 @@ __revision__ = '$Format:%H$'
|
||||
|
||||
from PyQt4.QtCore import *
|
||||
from qgis.core import *
|
||||
from processing import interface
|
||||
from processing.core.GeoAlgorithm import GeoAlgorithm
|
||||
from processing.core.GeoAlgorithmExecutionException import \
|
||||
GeoAlgorithmExecutionException
|
||||
from processing.parameters.ParameterVector import ParameterVector
|
||||
from processing.parameters.ParameterString import ParameterString
|
||||
from processing.parameters.ParameterNumber import ParameterNumber
|
||||
from processing.parameters.ParameterBoolean import ParameterBoolean
|
||||
from processing.parameters.ParameterSelection import ParameterSelection
|
||||
from processing.outputs.OutputVector import OutputVector
|
||||
from processing.tools import dataobjects, vector
|
||||
|
||||
from processing.algs.ui.FieldsCalculatorDialog import FieldsCalculatorDialog
|
||||
|
||||
class FieldsCalculator(GeoAlgorithm):
|
||||
|
||||
INPUT_LAYER = 'INPUT_LAYER'
|
||||
NEW_FIELD = 'NEW_FIELD'
|
||||
FIELD_NAME = 'FIELD_NAME'
|
||||
FIELD_TYPE = 'FIELD_TYPE'
|
||||
FIELD_LENGTH = 'FIELD_LENGTH'
|
||||
@ -46,8 +52,8 @@ class FieldsCalculator(GeoAlgorithm):
|
||||
FORMULA = 'FORMULA'
|
||||
OUTPUT_LAYER = 'OUTPUT_LAYER'
|
||||
|
||||
TYPE_NAMES = ['Float', 'Integer', 'String']
|
||||
TYPES = [QVariant.Double, QVariant.Int, QVariant.String]
|
||||
TYPE_NAMES = ['Float', 'Integer', 'String', 'Date']
|
||||
TYPES = [QVariant.Double, QVariant.Int, QVariant.String, QVariant.Date]
|
||||
|
||||
def defineCharacteristics(self):
|
||||
self.name = 'Field calculator'
|
||||
@ -61,58 +67,95 @@ class FieldsCalculator(GeoAlgorithm):
|
||||
self.addParameter(ParameterNumber(self.FIELD_LENGTH, 'Field length',
|
||||
1, 255, 10))
|
||||
self.addParameter(ParameterNumber(self.FIELD_PRECISION,
|
||||
'Field precision', 0, 10, 5))
|
||||
'Field precision', 0, 15, 3))
|
||||
self.addParameter(ParameterBoolean(self.NEW_FIELD,
|
||||
'Create new field', True))
|
||||
self.addParameter(ParameterString(self.FORMULA, 'Formula'))
|
||||
self.addOutput(OutputVector(self.OUTPUT_LAYER, 'Output layer'))
|
||||
|
||||
def processAlgorithm(self, progress):
|
||||
fieldName = self.getParameterValue(self.FIELD_NAME)
|
||||
fieldType = self.getParameterValue(self.FIELD_TYPE)
|
||||
fieldLength = self.getParameterValue(self.FIELD_LENGTH)
|
||||
fieldPrecision = self.getParameterValue(self.FIELD_PRECISION)
|
||||
formula = self.getParameterValue(self.FORMULA)
|
||||
output = self.getOutputFromName(self.OUTPUT_LAYER)
|
||||
|
||||
layer = dataobjects.getObjectFromUri(
|
||||
self.getParameterValue(self.INPUT_LAYER))
|
||||
fieldName = self.getParameterValue(self.FIELD_NAME)
|
||||
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)
|
||||
formula = self.getParameterValue(self.FORMULA)
|
||||
|
||||
output = self.getOutputFromName(self.OUTPUT_LAYER)
|
||||
|
||||
provider = layer.dataProvider()
|
||||
fields = provider.fields()
|
||||
fields.append(QgsField(fieldName, self.TYPES[fieldType], '',
|
||||
fieldLength, fieldPrecision))
|
||||
fields = layer.pendingFields()
|
||||
if newField:
|
||||
fields.append(QgsField(fieldName, fieldType, '', width, precision))
|
||||
|
||||
writer = output.getVectorWriter(fields, provider.geometryType(),
|
||||
layer.crs())
|
||||
layer.crs())
|
||||
|
||||
outFeat = QgsFeature()
|
||||
inGeom = QgsGeometry()
|
||||
nFeat = provider.featureCount()
|
||||
nElement = 0
|
||||
exp = QgsExpression(formula)
|
||||
|
||||
da = QgsDistanceArea()
|
||||
da.setSourceCrs(layer.crs().srsid())
|
||||
canvas = interface.iface.mapCanvas()
|
||||
da.setEllipsoidalMode(canvas.mapRenderer().hasCrsTransformEnabled())
|
||||
da.setEllipsoid(QgsProject.instance().readEntry('Measure',
|
||||
'/Ellipsoid',
|
||||
GEO_NONE)[0])
|
||||
exp.setGeomCalculator(da)
|
||||
|
||||
if not exp.prepare(layer.pendingFields()):
|
||||
raise GeoAlgorithmExecutionException(
|
||||
'Evaluation error: ' + exp.evalErrorString())
|
||||
|
||||
outFeature = QgsFeature()
|
||||
outFeature.initAttributes(len(fields))
|
||||
outFeature.setFields(fields)
|
||||
|
||||
error = ''
|
||||
calculationSuccess = True
|
||||
|
||||
current = 0
|
||||
features = vector.features(layer)
|
||||
total = 100.0 / len(features)
|
||||
|
||||
fieldnames = [field.name() for field in provider.fields()]
|
||||
fieldnames.sort(key=len, reverse=False)
|
||||
fieldidx = [fieldnames.index(field.name()) for field in
|
||||
provider.fields()]
|
||||
print fieldidx
|
||||
for inFeat in features:
|
||||
progress.setPercentage(int(100 * nElement / nFeat))
|
||||
attrs = inFeat.attributes()
|
||||
expression = formula
|
||||
for idx in fieldidx:
|
||||
expression = expression.replace(unicode(fields[idx].name()),
|
||||
unicode(attrs[idx]))
|
||||
try:
|
||||
result = eval(expression)
|
||||
except Exception:
|
||||
result = None
|
||||
nElement += 1
|
||||
inGeom = inFeat.geometry()
|
||||
outFeat.setGeometry(inGeom)
|
||||
attrs = inFeat.attributes()
|
||||
attrs.append(result)
|
||||
outFeat.setAttributes(attrs)
|
||||
writer.addFeature(outFeat)
|
||||
rownum = 1
|
||||
for f in features:
|
||||
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)
|
||||
|
||||
current += 1
|
||||
progress.setPercentage(int(current * total))
|
||||
del writer
|
||||
|
||||
if not calculationSuccess:
|
||||
raise GeoAlgorithmExecutionException(
|
||||
'An error occured while evaluating the calculation '
|
||||
'string:\n' + error)
|
||||
|
||||
|
||||
def checkParameterValuesBeforeExecuting(self):
|
||||
# TODO check that formula is correct and fields exist
|
||||
pass
|
||||
newField = self.getParameterValue(self.NEW_FIELD)
|
||||
fieldName = self.getParameterValue(self.FIELD_NAME)
|
||||
if newField and len(fieldName) == 0:
|
||||
raise GeoAlgorithmExecutionException('Field name is not set. '
|
||||
'Please enter a field name')
|
||||
|
||||
outputName = self.getOutputValue(self.OUTPUT_LAYER)
|
||||
if outputName == '':
|
||||
raise GeoAlgorithmExecutionException('Output is not set. '
|
||||
'Please specify valid filename')
|
||||
|
||||
|
||||
def getCustomParametersDialog(self):
|
||||
return FieldsCalculatorDialog(self)
|
||||
|
6
python/plugins/processing/algs/ui/CMakeLists.txt
Normal file
6
python/plugins/processing/algs/ui/CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
FILE(GLOB PY_FILES *.py)
|
||||
|
||||
FILE(GLOB UI_FILES *.ui)
|
||||
PYQT4_WRAP_UI(PYUI_FILES ${UI_FILES})
|
||||
|
||||
PLUGIN_INSTALL(processing ./algs/ui ${PY_FILES} ${PYUI_FILES})
|
270
python/plugins/processing/algs/ui/DlgFieldsCalculator.ui
Normal file
270
python/plugins/processing/algs/ui/DlgFieldsCalculator.ui
Normal file
@ -0,0 +1,270 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>FieldsCalculator</class>
|
||||
<widget class="QDialog" name="FieldsCalculator">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>681</width>
|
||||
<height>681</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Field calculator</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="2" column="0">
|
||||
<widget class="QGroupBox" name="mNewFieldGroupBox">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="title">
|
||||
<string>Create a new field</string>
|
||||
</property>
|
||||
<property name="flat">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="checkable">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="checked">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<layout class="QGridLayout">
|
||||
<property name="sizeConstraint">
|
||||
<enum>QLayout::SetMinimumSize</enum>
|
||||
</property>
|
||||
<property name="leftMargin">
|
||||
<number>3</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>3</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>3</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="verticalSpacing">
|
||||
<number>3</number>
|
||||
</property>
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="mFieldNameLabel">
|
||||
<property name="text">
|
||||
<string>Output field name</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>mOutputFieldNameLineEdit</cstring>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1" colspan="3">
|
||||
<widget class="QLineEdit" name="mOutputFieldNameLineEdit"/>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLabel" name="mOutputFieldTypeLabel">
|
||||
<property name="text">
|
||||
<string>Output field type</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>mOutputFieldTypeComboBox</cstring>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1" colspan="3">
|
||||
<widget class="QComboBox" name="mOutputFieldTypeComboBox"/>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QLabel" name="mOutputFieldWidthLabel">
|
||||
<property name="text">
|
||||
<string>Output field width</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>mOutputFieldWidthSpinBox</cstring>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="1">
|
||||
<widget class="QSpinBox" name="mOutputFieldWidthSpinBox">
|
||||
<property name="toolTip">
|
||||
<string>Width of complete output. For example 123,456 means 6 as field width.</string>
|
||||
</property>
|
||||
<property name="minimum">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<property name="value">
|
||||
<number>15</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="2">
|
||||
<widget class="QLabel" name="mOutputFieldPrecisionLabel">
|
||||
<property name="text">
|
||||
<string>Precision</string>
|
||||
</property>
|
||||
<property name="buddy">
|
||||
<cstring>mOutputFieldPrecisionSpinBox</cstring>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="3">
|
||||
<widget class="QSpinBox" name="mOutputFieldPrecisionSpinBox">
|
||||
<property name="value">
|
||||
<number>2</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="5" column="0" colspan="2">
|
||||
<widget class="QDialogButtonBox" name="mButtonBox">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
|
||||
<horstretch>3</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="standardButtons">
|
||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="0" colspan="2">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>Input layer</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QComboBox" name="cmbInputLayer">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item row="2" column="1">
|
||||
<widget class="QGroupBox" name="mUpdateExistingGroupBox">
|
||||
<property name="title">
|
||||
<string>Update existing field</string>
|
||||
</property>
|
||||
<property name="flat">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="checkable">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="checked">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QComboBox" name="mExistingFieldComboBox"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="0" colspan="2">
|
||||
<widget class="QgsExpressionBuilderWidget" name="builder" native="true">
|
||||
<property name="autoFillBackground">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0" colspan="2">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||
<item>
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="text">
|
||||
<string>Output file</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLineEdit" name="leOutputFile"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QToolButton" name="btnBrowse">
|
||||
<property name="text">
|
||||
<string>...</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item row="4" column="0" colspan="2">
|
||||
<widget class="QProgressBar" name="progressBar">
|
||||
<property name="value">
|
||||
<number>0</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
<class>QgsExpressionBuilderWidget</class>
|
||||
<extends>QWidget</extends>
|
||||
<header>qgis.gui</header>
|
||||
<container>1</container>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<tabstops>
|
||||
<tabstop>mOutputFieldNameLineEdit</tabstop>
|
||||
<tabstop>mOutputFieldTypeComboBox</tabstop>
|
||||
<tabstop>mOutputFieldWidthSpinBox</tabstop>
|
||||
<tabstop>mOutputFieldPrecisionSpinBox</tabstop>
|
||||
<tabstop>mButtonBox</tabstop>
|
||||
</tabstops>
|
||||
<resources/>
|
||||
<connections>
|
||||
<connection>
|
||||
<sender>mButtonBox</sender>
|
||||
<signal>accepted()</signal>
|
||||
<receiver>FieldsCalculator</receiver>
|
||||
<slot>accept()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>679</x>
|
||||
<y>559</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>157</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>mButtonBox</sender>
|
||||
<signal>rejected()</signal>
|
||||
<receiver>FieldsCalculator</receiver>
|
||||
<slot>reject()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>679</x>
|
||||
<y>559</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>286</x>
|
||||
<y>274</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
</ui>
|
228
python/plugins/processing/algs/ui/FieldsCalculatorDialog.py
Normal file
228
python/plugins/processing/algs/ui/FieldsCalculatorDialog.py
Normal file
@ -0,0 +1,228 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
***************************************************************************
|
||||
FieldsCalculatorDialog.py
|
||||
---------------------
|
||||
Date : October 2013
|
||||
Copyright : (C) 2013 by Alexander Bruy
|
||||
Email : alexander dot bruy 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__ = 'Alexander Bruy'
|
||||
__date__ = 'October 2013'
|
||||
__copyright__ = '(C) 2013, Alexander Bruy'
|
||||
|
||||
# This will get replaced with a git SHA1 when you do a git archive
|
||||
|
||||
__revision__ = '$Format:%H$'
|
||||
|
||||
import os
|
||||
import re
|
||||
|
||||
from PyQt4.QtCore import *
|
||||
from PyQt4.QtGui import *
|
||||
from qgis.core import *
|
||||
from qgis.gui import *
|
||||
|
||||
from processing.core.ProcessingConfig import ProcessingConfig
|
||||
from processing.core.ProcessingLog import ProcessingLog
|
||||
from processing.core.GeoAlgorithmExecutionException import \
|
||||
GeoAlgorithmExecutionException
|
||||
from processing.gui.UnthreadedAlgorithmExecutor import \
|
||||
UnthreadedAlgorithmExecutor
|
||||
from processing.gui.Postprocessing import Postprocessing
|
||||
from processing.tools import dataobjects
|
||||
|
||||
from ui_DlgFieldsCalculator import Ui_FieldsCalculator
|
||||
|
||||
|
||||
class FieldsCalculatorDialog(QDialog, Ui_FieldsCalculator):
|
||||
def __init__(self, alg):
|
||||
QDialog.__init__(self)
|
||||
self.setupUi( self )
|
||||
|
||||
self.executed = False
|
||||
self.alg = alg
|
||||
self.layer = None
|
||||
|
||||
self.cmbInputLayer.currentIndexChanged.connect(self.updateLayer)
|
||||
self.btnBrowse.clicked.connect(self.selectFile)
|
||||
self.mNewFieldGroupBox.toggled.connect(self.toggleExistingGroup)
|
||||
self.mUpdateExistingGroupBox.toggled.connect(self.toggleNewGroup)
|
||||
self.mOutputFieldTypeComboBox.currentIndexChanged.connect(
|
||||
self.setupSpinboxes)
|
||||
|
||||
# Default values for field width and precision
|
||||
self.mOutputFieldWidthSpinBox.setValue(10)
|
||||
self.mOutputFieldPrecisionSpinBox.setValue(3)
|
||||
|
||||
# Output is a shapefile, so limit maximum field name length
|
||||
self.mOutputFieldNameLineEdit.setMaxLength(10)
|
||||
|
||||
self.manageGui()
|
||||
|
||||
def manageGui(self):
|
||||
self.mOutputFieldTypeComboBox.blockSignals(True)
|
||||
for t in self.alg.TYPE_NAMES:
|
||||
self.mOutputFieldTypeComboBox.addItem(t)
|
||||
self.mOutputFieldTypeComboBox.blockSignals(False)
|
||||
|
||||
self.cmbInputLayer.blockSignals(True)
|
||||
layers = dataobjects.getVectorLayers()
|
||||
for layer in layers:
|
||||
self.cmbInputLayer.addItem(layer.name())
|
||||
self.cmbInputLayer.blockSignals(False)
|
||||
|
||||
self.updateLayer()
|
||||
|
||||
def updateLayer(self):
|
||||
self.layer = dataobjects.getObject(self.cmbInputLayer.currentText())
|
||||
|
||||
self.builder.setLayer(self.layer)
|
||||
self.builder.loadFieldNames()
|
||||
|
||||
self.populateFields()
|
||||
#populateOutputFieldTypes()
|
||||
|
||||
def setupSpinboxes(self, index):
|
||||
if index != 0:
|
||||
self.mOutputFieldPrecisionSpinBox.setEnabled(False)
|
||||
else:
|
||||
self.mOutputFieldPrecisionSpinBox.setEnabled(True)
|
||||
|
||||
if index == 0:
|
||||
self.mOutputFieldWidthSpinBox.setRange(1, 20)
|
||||
self.mOutputFieldWidthSpinBox.setValue(10)
|
||||
self.mOutputFieldPrecisionSpinBox.setRange(0, 15)
|
||||
self.mOutputFieldPrecisionSpinBox.setValue(3)
|
||||
elif index == 1:
|
||||
self.mOutputFieldWidthSpinBox.setRange(1, 10)
|
||||
self.mOutputFieldWidthSpinBox.setValue(10)
|
||||
elif index == 2:
|
||||
self.mOutputFieldWidthSpinBox.setRange(1, 255)
|
||||
self.mOutputFieldWidthSpinBox.setValue(80)
|
||||
else:
|
||||
self.mOutputFieldWidthSpinBox.setEnabled(False)
|
||||
self.mOutputFieldPrecisionSpinBox.setEnabled(False)
|
||||
|
||||
def selectFile(self):
|
||||
output = self.alg.getOutputFromName('OUTPUT_LAYER')
|
||||
fileFilter = output.getFileFilter(self.alg)
|
||||
|
||||
settings = QSettings()
|
||||
if settings.contains('/Processing/LastOutputPath'):
|
||||
path = settings.value('/Processing/LastOutputPath')
|
||||
else:
|
||||
path = ProcessingConfig.getSetting(ProcessingConfig.OUTPUT_FOLDER)
|
||||
lastEncoding = settings.value('/Processing/encoding', 'System')
|
||||
fileDialog = QgsEncodingFileDialog(self,
|
||||
self.tr('Save file'),
|
||||
path,
|
||||
fileFilter,
|
||||
lastEncoding)
|
||||
fileDialog.setFileMode(QFileDialog.AnyFile)
|
||||
fileDialog.setAcceptMode(QFileDialog.AcceptSave)
|
||||
fileDialog.setConfirmOverwrite(True)
|
||||
if fileDialog.exec_() == QDialog.Accepted:
|
||||
files = fileDialog.selectedFiles()
|
||||
encoding = unicode(fileDialog.encoding())
|
||||
output.encoding = encoding
|
||||
filename = unicode(files[0])
|
||||
selectedFileFilter = unicode(fileDialog.selectedNameFilter())
|
||||
if not filename.lower().endswith(
|
||||
tuple(re.findall("\*(\.[a-z]{1,5})", fileFilter))):
|
||||
ext = re.search("\*(\.[a-z]{1,5})", selectedFileFilter)
|
||||
if ext:
|
||||
filename = filename + ext.group(1)
|
||||
self.leOutputFile.setText(filename)
|
||||
settings.setValue('/Processing/LastOutputPath',
|
||||
os.path.dirname(filename))
|
||||
settings.setValue('/Processing/encoding', encoding)
|
||||
|
||||
def toggleExistingGroup(self, toggled):
|
||||
self.mUpdateExistingGroupBox.setChecked(not toggled)
|
||||
|
||||
def toggleNewGroup(self, toggled):
|
||||
self.mNewFieldGroupBox.setChecked(not toggled)
|
||||
|
||||
def populateFields(self):
|
||||
if self.layer is None:
|
||||
return
|
||||
|
||||
fields = self.layer.pendingFields()
|
||||
for f in fields:
|
||||
self.mExistingFieldComboBox.addItem(f.name())
|
||||
|
||||
def setParamValues(self):
|
||||
if self.mUpdateExistingGroupBox.isChecked():
|
||||
fieldName = self.mExistingFieldComboBox.currentText()
|
||||
else:
|
||||
fieldName = self.mOutputFieldNameLineEdit.text()
|
||||
|
||||
layer = dataobjects.getObjectFromName(self.cmbInputLayer.currentText())
|
||||
|
||||
self.alg.setParameterValue('INPUT_LAYER', layer)
|
||||
self.alg.setParameterValue('FIELD_NAME', fieldName)
|
||||
self.alg.setParameterValue('FIELD_TYPE',
|
||||
self.mOutputFieldTypeComboBox.currentIndex())
|
||||
self.alg.setParameterValue('FIELD_LENGTH',
|
||||
self.mOutputFieldWidthSpinBox.value())
|
||||
self.alg.setParameterValue('FIELD_PRECISION',
|
||||
self.mOutputFieldPrecisionSpinBox.value())
|
||||
self.alg.setParameterValue('NEW_FIELD',
|
||||
self.mNewFieldGroupBox.isChecked())
|
||||
self.alg.setParameterValue('FORMULA', self.builder.expressionText())
|
||||
self.alg.setOutputValue('OUTPUT_LAYER',
|
||||
self.leOutputFile.text())
|
||||
return True
|
||||
|
||||
def accept(self):
|
||||
keepOpen = ProcessingConfig.getSetting(
|
||||
ProcessingConfig.KEEP_DIALOG_OPEN)
|
||||
try:
|
||||
if self.setParamValues():
|
||||
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
|
||||
ProcessingLog.addToLog(ProcessingLog.LOG_ALGORITHM,
|
||||
self.alg.getAsCommand())
|
||||
ret = UnthreadedAlgorithmExecutor.runalg(self.alg, self)
|
||||
QApplication.restoreOverrideCursor()
|
||||
if ret:
|
||||
Postprocessing.handleAlgorithmResults(self.alg,
|
||||
self,
|
||||
not keepOpen)
|
||||
self.executed = True
|
||||
QDialog.reject(self)
|
||||
else:
|
||||
QMessageBox.critical(self,
|
||||
self.tr('Unable to execute algorithm'),
|
||||
self.tr('Wrong or missing parameter '
|
||||
'values'))
|
||||
return
|
||||
except GeoAlgorithmExecutionException, e:
|
||||
QApplication.restoreOverrideCursor()
|
||||
QMessageBox.critical(self, "Error",e.msg)
|
||||
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, e.msg)
|
||||
self.executed = False
|
||||
QDialog.reject(self)
|
||||
|
||||
def reject(self):
|
||||
self.executed = False
|
||||
QDialog.reject(self)
|
||||
|
||||
def setPercentage(self, i):
|
||||
self.progressBar.setValue(i)
|
||||
|
||||
def setText(self, text):
|
||||
pass
|
||||
|
||||
def error(self, text):
|
||||
ProcessingLog.addToLog(ProcessingLog.LOG_ERROR, text)
|
0
python/plugins/processing/algs/ui/__init__.py
Normal file
0
python/plugins/processing/algs/ui/__init__.py
Normal file
148
python/plugins/processing/algs/ui/ui_DlgFieldsCalculator.py
Normal file
148
python/plugins/processing/algs/ui/ui_DlgFieldsCalculator.py
Normal file
@ -0,0 +1,148 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Form implementation generated from reading ui file 'python/plugins/processing/algs/ui/DlgFieldsCalculator.ui'
|
||||
#
|
||||
# Created: Thu Oct 24 16:06:48 2013
|
||||
# by: PyQt4 UI code generator 4.9.1
|
||||
#
|
||||
# WARNING! All changes made in this file will be lost!
|
||||
|
||||
from PyQt4 import QtCore, QtGui
|
||||
|
||||
try:
|
||||
_fromUtf8 = QtCore.QString.fromUtf8
|
||||
except AttributeError:
|
||||
_fromUtf8 = lambda s: s
|
||||
|
||||
class Ui_FieldsCalculator(object):
|
||||
def setupUi(self, FieldsCalculator):
|
||||
FieldsCalculator.setObjectName(_fromUtf8("FieldsCalculator"))
|
||||
FieldsCalculator.resize(681, 681)
|
||||
self.gridLayout = QtGui.QGridLayout(FieldsCalculator)
|
||||
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
|
||||
self.mNewFieldGroupBox = QtGui.QGroupBox(FieldsCalculator)
|
||||
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
|
||||
sizePolicy.setHorizontalStretch(0)
|
||||
sizePolicy.setVerticalStretch(0)
|
||||
sizePolicy.setHeightForWidth(self.mNewFieldGroupBox.sizePolicy().hasHeightForWidth())
|
||||
self.mNewFieldGroupBox.setSizePolicy(sizePolicy)
|
||||
self.mNewFieldGroupBox.setFlat(True)
|
||||
self.mNewFieldGroupBox.setCheckable(True)
|
||||
self.mNewFieldGroupBox.setChecked(True)
|
||||
self.mNewFieldGroupBox.setObjectName(_fromUtf8("mNewFieldGroupBox"))
|
||||
self.gridlayout = QtGui.QGridLayout(self.mNewFieldGroupBox)
|
||||
self.gridlayout.setSizeConstraint(QtGui.QLayout.SetMinimumSize)
|
||||
self.gridlayout.setContentsMargins(3, 3, 3, 0)
|
||||
self.gridlayout.setVerticalSpacing(3)
|
||||
self.gridlayout.setObjectName(_fromUtf8("gridlayout"))
|
||||
self.mFieldNameLabel = QtGui.QLabel(self.mNewFieldGroupBox)
|
||||
self.mFieldNameLabel.setObjectName(_fromUtf8("mFieldNameLabel"))
|
||||
self.gridlayout.addWidget(self.mFieldNameLabel, 0, 0, 1, 1)
|
||||
self.mOutputFieldNameLineEdit = QtGui.QLineEdit(self.mNewFieldGroupBox)
|
||||
self.mOutputFieldNameLineEdit.setObjectName(_fromUtf8("mOutputFieldNameLineEdit"))
|
||||
self.gridlayout.addWidget(self.mOutputFieldNameLineEdit, 0, 1, 1, 3)
|
||||
self.mOutputFieldTypeLabel = QtGui.QLabel(self.mNewFieldGroupBox)
|
||||
self.mOutputFieldTypeLabel.setObjectName(_fromUtf8("mOutputFieldTypeLabel"))
|
||||
self.gridlayout.addWidget(self.mOutputFieldTypeLabel, 1, 0, 1, 1)
|
||||
self.mOutputFieldTypeComboBox = QtGui.QComboBox(self.mNewFieldGroupBox)
|
||||
self.mOutputFieldTypeComboBox.setObjectName(_fromUtf8("mOutputFieldTypeComboBox"))
|
||||
self.gridlayout.addWidget(self.mOutputFieldTypeComboBox, 1, 1, 1, 3)
|
||||
self.mOutputFieldWidthLabel = QtGui.QLabel(self.mNewFieldGroupBox)
|
||||
self.mOutputFieldWidthLabel.setObjectName(_fromUtf8("mOutputFieldWidthLabel"))
|
||||
self.gridlayout.addWidget(self.mOutputFieldWidthLabel, 2, 0, 1, 1)
|
||||
self.mOutputFieldWidthSpinBox = QtGui.QSpinBox(self.mNewFieldGroupBox)
|
||||
self.mOutputFieldWidthSpinBox.setMinimum(0)
|
||||
self.mOutputFieldWidthSpinBox.setProperty("value", 15)
|
||||
self.mOutputFieldWidthSpinBox.setObjectName(_fromUtf8("mOutputFieldWidthSpinBox"))
|
||||
self.gridlayout.addWidget(self.mOutputFieldWidthSpinBox, 2, 1, 1, 1)
|
||||
self.mOutputFieldPrecisionLabel = QtGui.QLabel(self.mNewFieldGroupBox)
|
||||
self.mOutputFieldPrecisionLabel.setObjectName(_fromUtf8("mOutputFieldPrecisionLabel"))
|
||||
self.gridlayout.addWidget(self.mOutputFieldPrecisionLabel, 2, 2, 1, 1)
|
||||
self.mOutputFieldPrecisionSpinBox = QtGui.QSpinBox(self.mNewFieldGroupBox)
|
||||
self.mOutputFieldPrecisionSpinBox.setProperty("value", 2)
|
||||
self.mOutputFieldPrecisionSpinBox.setObjectName(_fromUtf8("mOutputFieldPrecisionSpinBox"))
|
||||
self.gridlayout.addWidget(self.mOutputFieldPrecisionSpinBox, 2, 3, 1, 1)
|
||||
self.gridLayout.addWidget(self.mNewFieldGroupBox, 2, 0, 1, 1)
|
||||
self.mButtonBox = QtGui.QDialogButtonBox(FieldsCalculator)
|
||||
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Fixed)
|
||||
sizePolicy.setHorizontalStretch(3)
|
||||
sizePolicy.setVerticalStretch(0)
|
||||
sizePolicy.setHeightForWidth(self.mButtonBox.sizePolicy().hasHeightForWidth())
|
||||
self.mButtonBox.setSizePolicy(sizePolicy)
|
||||
self.mButtonBox.setOrientation(QtCore.Qt.Horizontal)
|
||||
self.mButtonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
|
||||
self.mButtonBox.setObjectName(_fromUtf8("mButtonBox"))
|
||||
self.gridLayout.addWidget(self.mButtonBox, 5, 0, 1, 2)
|
||||
self.horizontalLayout = QtGui.QHBoxLayout()
|
||||
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
|
||||
self.label = QtGui.QLabel(FieldsCalculator)
|
||||
self.label.setObjectName(_fromUtf8("label"))
|
||||
self.horizontalLayout.addWidget(self.label)
|
||||
self.cmbInputLayer = QtGui.QComboBox(FieldsCalculator)
|
||||
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Fixed)
|
||||
sizePolicy.setHorizontalStretch(0)
|
||||
sizePolicy.setVerticalStretch(0)
|
||||
sizePolicy.setHeightForWidth(self.cmbInputLayer.sizePolicy().hasHeightForWidth())
|
||||
self.cmbInputLayer.setSizePolicy(sizePolicy)
|
||||
self.cmbInputLayer.setObjectName(_fromUtf8("cmbInputLayer"))
|
||||
self.horizontalLayout.addWidget(self.cmbInputLayer)
|
||||
self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 2)
|
||||
self.mUpdateExistingGroupBox = QtGui.QGroupBox(FieldsCalculator)
|
||||
self.mUpdateExistingGroupBox.setFlat(True)
|
||||
self.mUpdateExistingGroupBox.setCheckable(True)
|
||||
self.mUpdateExistingGroupBox.setChecked(False)
|
||||
self.mUpdateExistingGroupBox.setObjectName(_fromUtf8("mUpdateExistingGroupBox"))
|
||||
self.verticalLayout = QtGui.QVBoxLayout(self.mUpdateExistingGroupBox)
|
||||
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
|
||||
self.mExistingFieldComboBox = QtGui.QComboBox(self.mUpdateExistingGroupBox)
|
||||
self.mExistingFieldComboBox.setObjectName(_fromUtf8("mExistingFieldComboBox"))
|
||||
self.verticalLayout.addWidget(self.mExistingFieldComboBox)
|
||||
self.gridLayout.addWidget(self.mUpdateExistingGroupBox, 2, 1, 1, 1)
|
||||
self.builder = QgsExpressionBuilderWidget(FieldsCalculator)
|
||||
self.builder.setAutoFillBackground(False)
|
||||
self.builder.setObjectName(_fromUtf8("builder"))
|
||||
self.gridLayout.addWidget(self.builder, 3, 0, 1, 2)
|
||||
self.horizontalLayout_2 = QtGui.QHBoxLayout()
|
||||
self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
|
||||
self.label_2 = QtGui.QLabel(FieldsCalculator)
|
||||
self.label_2.setObjectName(_fromUtf8("label_2"))
|
||||
self.horizontalLayout_2.addWidget(self.label_2)
|
||||
self.leOutputFile = QtGui.QLineEdit(FieldsCalculator)
|
||||
self.leOutputFile.setObjectName(_fromUtf8("leOutputFile"))
|
||||
self.horizontalLayout_2.addWidget(self.leOutputFile)
|
||||
self.btnBrowse = QtGui.QToolButton(FieldsCalculator)
|
||||
self.btnBrowse.setObjectName(_fromUtf8("btnBrowse"))
|
||||
self.horizontalLayout_2.addWidget(self.btnBrowse)
|
||||
self.gridLayout.addLayout(self.horizontalLayout_2, 1, 0, 1, 2)
|
||||
self.progressBar = QtGui.QProgressBar(FieldsCalculator)
|
||||
self.progressBar.setProperty("value", 0)
|
||||
self.progressBar.setObjectName(_fromUtf8("progressBar"))
|
||||
self.gridLayout.addWidget(self.progressBar, 4, 0, 1, 2)
|
||||
self.mFieldNameLabel.setBuddy(self.mOutputFieldNameLineEdit)
|
||||
self.mOutputFieldTypeLabel.setBuddy(self.mOutputFieldTypeComboBox)
|
||||
self.mOutputFieldWidthLabel.setBuddy(self.mOutputFieldWidthSpinBox)
|
||||
self.mOutputFieldPrecisionLabel.setBuddy(self.mOutputFieldPrecisionSpinBox)
|
||||
|
||||
self.retranslateUi(FieldsCalculator)
|
||||
QtCore.QObject.connect(self.mButtonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), FieldsCalculator.accept)
|
||||
QtCore.QObject.connect(self.mButtonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), FieldsCalculator.reject)
|
||||
QtCore.QMetaObject.connectSlotsByName(FieldsCalculator)
|
||||
FieldsCalculator.setTabOrder(self.mOutputFieldNameLineEdit, self.mOutputFieldTypeComboBox)
|
||||
FieldsCalculator.setTabOrder(self.mOutputFieldTypeComboBox, self.mOutputFieldWidthSpinBox)
|
||||
FieldsCalculator.setTabOrder(self.mOutputFieldWidthSpinBox, self.mOutputFieldPrecisionSpinBox)
|
||||
FieldsCalculator.setTabOrder(self.mOutputFieldPrecisionSpinBox, self.mButtonBox)
|
||||
|
||||
def retranslateUi(self, FieldsCalculator):
|
||||
FieldsCalculator.setWindowTitle(QtGui.QApplication.translate("FieldsCalculator", "Field calculator", None, QtGui.QApplication.UnicodeUTF8))
|
||||
self.mNewFieldGroupBox.setTitle(QtGui.QApplication.translate("FieldsCalculator", "Create a new field", None, QtGui.QApplication.UnicodeUTF8))
|
||||
self.mFieldNameLabel.setText(QtGui.QApplication.translate("FieldsCalculator", "Output field name", None, QtGui.QApplication.UnicodeUTF8))
|
||||
self.mOutputFieldTypeLabel.setText(QtGui.QApplication.translate("FieldsCalculator", "Output field type", None, QtGui.QApplication.UnicodeUTF8))
|
||||
self.mOutputFieldWidthLabel.setText(QtGui.QApplication.translate("FieldsCalculator", "Output field width", None, QtGui.QApplication.UnicodeUTF8))
|
||||
self.mOutputFieldWidthSpinBox.setToolTip(QtGui.QApplication.translate("FieldsCalculator", "Width of complete output. For example 123,456 means 6 as field width.", None, QtGui.QApplication.UnicodeUTF8))
|
||||
self.mOutputFieldPrecisionLabel.setText(QtGui.QApplication.translate("FieldsCalculator", "Precision", None, QtGui.QApplication.UnicodeUTF8))
|
||||
self.label.setText(QtGui.QApplication.translate("FieldsCalculator", "Input layer", None, QtGui.QApplication.UnicodeUTF8))
|
||||
self.mUpdateExistingGroupBox.setTitle(QtGui.QApplication.translate("FieldsCalculator", "Update existing field", None, QtGui.QApplication.UnicodeUTF8))
|
||||
self.label_2.setText(QtGui.QApplication.translate("FieldsCalculator", "Output file", None, QtGui.QApplication.UnicodeUTF8))
|
||||
self.btnBrowse.setText(QtGui.QApplication.translate("FieldsCalculator", "...", None, QtGui.QApplication.UnicodeUTF8))
|
||||
|
||||
from qgis.gui import QgsExpressionBuilderWidget
|
Loading…
x
Reference in New Issue
Block a user