|
26 | 26 | __revision__ = '$Format:%H$'
|
27 | 27 |
|
28 | 28 | from qgis.PyQt.QtCore import QVariant
|
29 |
| -from qgis.core import (QgsApplication, |
30 |
| - QgsField, |
31 |
| - QgsFeatureSink, |
32 |
| - QgsProcessingUtils) |
33 |
| -from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm |
34 |
| -from processing.core.parameters import ParameterVector |
35 |
| -from processing.core.parameters import ParameterTableField |
36 |
| -from processing.core.outputs import OutputVector |
37 |
| - |
38 |
| - |
39 |
| -class TextToFloat(QgisAlgorithm): |
40 |
| - INPUT = 'INPUT' |
| 29 | +from qgis.core import (QgsField, |
| 30 | + QgsProcessingParameterField) |
| 31 | +from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm |
| 32 | + |
| 33 | + |
| 34 | +class TextToFloat(QgisFeatureBasedAlgorithm): |
| 35 | + |
41 | 36 | FIELD = 'FIELD'
|
42 |
| - OUTPUT = 'OUTPUT' |
43 | 37 |
|
44 | 38 | def group(self):
|
45 | 39 | return self.tr('Vector table tools')
|
46 | 40 |
|
47 | 41 | def __init__(self):
|
48 | 42 | super().__init__()
|
| 43 | + self.field_name = None |
| 44 | + self.field_idx = -1 |
49 | 45 |
|
50 |
| - def initAlgorithm(self, config=None): |
51 |
| - self.addParameter(ParameterVector(self.INPUT, |
52 |
| - self.tr('Input Layer'))) |
53 |
| - self.addParameter(ParameterTableField(self.FIELD, |
54 |
| - self.tr('Text attribute to convert to float'), |
55 |
| - self.INPUT, ParameterTableField.DATA_TYPE_STRING)) |
56 |
| - self.addOutput(OutputVector(self.OUTPUT, self.tr('Float from text'))) |
| 46 | + def initParameters(self, config=None): |
| 47 | + self.addParameter(QgsProcessingParameterField(self.FIELD, |
| 48 | + self.tr('Text attribute to convert to float'), |
| 49 | + parentLayerParameterName='INPUT', |
| 50 | + type=QgsProcessingParameterField.String |
| 51 | + )) |
57 | 52 |
|
58 | 53 | def name(self):
|
59 | 54 | return 'texttofloat'
|
60 | 55 |
|
61 | 56 | def displayName(self):
|
62 | 57 | return self.tr('Text to float')
|
63 | 58 |
|
64 |
| - def processAlgorithm(self, parameters, context, feedback): |
65 |
| - layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context) |
66 |
| - fieldName = self.getParameterValue(self.FIELD) |
67 |
| - idx = layer.fields().lookupField(fieldName) |
68 |
| - |
69 |
| - fields = layer.fields() |
70 |
| - fields[idx] = QgsField(fieldName, QVariant.Double, '', 24, 15) |
71 |
| - |
72 |
| - writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, layer.wkbType(), layer.crs(), context) |
73 |
| - |
74 |
| - features = QgsProcessingUtils.getFeatures(layer, context) |
75 |
| - |
76 |
| - total = 100.0 / layer.featureCount() if layer.featureCount() else 0 |
77 |
| - for current, f in enumerate(features): |
78 |
| - value = f[idx] |
79 |
| - try: |
80 |
| - if '%' in value: |
81 |
| - f[idx] = float(value.replace('%', '')) / 100.0 |
82 |
| - else: |
83 |
| - f[idx] = float(value) |
84 |
| - except: |
85 |
| - f[idx] = None |
86 |
| - |
87 |
| - writer.addFeature(f, QgsFeatureSink.FastInsert) |
88 |
| - feedback.setProgress(int(current * total)) |
89 |
| - |
90 |
| - del writer |
| 59 | + def outputName(self): |
| 60 | + return self.tr('Float from text') |
| 61 | + |
| 62 | + def outputFields(self, inputFields): |
| 63 | + self.field_idx = inputFields.lookupField(self.field_name) |
| 64 | + if self.field_idx >= 0: |
| 65 | + inputFields[self.field_idx] = QgsField(self.field_name, QVariant.Double, '', 24, 15) |
| 66 | + return inputFields |
| 67 | + |
| 68 | + def prepareAlgorithm(self, parameters, context, feedback): |
| 69 | + self.field_name = self.parameterAsString(parameters, self.FIELD, context) |
| 70 | + return True |
| 71 | + |
| 72 | + def processFeature(self, feature, feedback): |
| 73 | + value = feature[self.field_idx] |
| 74 | + try: |
| 75 | + if '%' in value: |
| 76 | + feature[self.field_idx] = float(value.replace('%', '')) / 100.0 |
| 77 | + else: |
| 78 | + feature[self.field_idx] = float(value) |
| 79 | + except: |
| 80 | + feature[self.field_idx] = None |
| 81 | + return feature |
0 commit comments