Skip to content

Commit 95be6d1

Browse files
committed
Restore text to float algorithm
And add test
1 parent ab70e05 commit 95be6d1

File tree

7 files changed

+233
-49
lines changed

7 files changed

+233
-49
lines changed

python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
from .SpatialiteExecuteSQL import SpatialiteExecuteSQL
111111
from .SumLines import SumLines
112112
from .SymmetricalDifference import SymmetricalDifference
113+
from .TextToFloat import TextToFloat
113114
from .Translate import Translate
114115
from .Union import Union
115116
from .UniqueValues import UniqueValues
@@ -127,7 +128,6 @@
127128
# from .SelectByLocation import SelectByLocation
128129
# from .SpatialJoin import SpatialJoin
129130
# from .DeleteDuplicateGeometries import DeleteDuplicateGeometries
130-
# from .TextToFloat import TextToFloat
131131
# from .GridLine import GridLine
132132
# from .Gridify import Gridify
133133
# from .HubDistancePoints import HubDistancePoints
@@ -192,7 +192,7 @@ def getAlgs(self):
192192
# SelectByLocation(),
193193
# ExtractByLocation(),
194194
# SpatialJoin(),
195-
# DeleteDuplicateGeometries(), TextToFloat(),
195+
# DeleteDuplicateGeometries(),
196196
# GridLine(), Gridify(), HubDistancePoints(),
197197
# HubDistanceLines(), HubLines(),
198198
# GeometryConvert(), FieldsCalculator(),
@@ -295,6 +295,7 @@ def getAlgs(self):
295295
SpatialiteExecuteSQL(),
296296
SumLines(),
297297
SymmetricalDifference(),
298+
TextToFloat(),
298299
Translate(),
299300
Union(),
300301
UniqueValues(),

python/plugins/processing/algs/qgis/TextToFloat.py

+38-47
Original file line numberDiff line numberDiff line change
@@ -26,65 +26,56 @@
2626
__revision__ = '$Format:%H$'
2727

2828
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+
4136
FIELD = 'FIELD'
42-
OUTPUT = 'OUTPUT'
4337

4438
def group(self):
4539
return self.tr('Vector table tools')
4640

4741
def __init__(self):
4842
super().__init__()
43+
self.field_name = None
44+
self.field_idx = -1
4945

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+
))
5752

5853
def name(self):
5954
return 'texttofloat'
6055

6156
def displayName(self):
6257
return self.tr('Text to float')
6358

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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ogr:FeatureCollection
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://ogr.maptools.org/ text_to_float.xsd"
5+
xmlns:ogr="http://ogr.maptools.org/"
6+
xmlns:gml="http://www.opengis.net/gml">
7+
<gml:boundedBy>
8+
<gml:Box>
9+
<gml:coord><gml:X>1</gml:X><gml:Y>1</gml:Y></gml:coord>
10+
<gml:coord><gml:X>5</gml:X><gml:Y>3</gml:Y></gml:coord>
11+
</gml:Box>
12+
</gml:boundedBy>
13+
14+
<gml:featureMember>
15+
<ogr:text_to_float fid="points.0">
16+
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>1,1</gml:coordinates></gml:Point></ogr:geometryProperty>
17+
<ogr:id>1</ogr:id>
18+
<ogr:id2>2</ogr:id2>
19+
<ogr:text_float>1</ogr:text_float>
20+
</ogr:text_to_float>
21+
</gml:featureMember>
22+
<gml:featureMember>
23+
<ogr:text_to_float fid="points.1">
24+
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>3,3</gml:coordinates></gml:Point></ogr:geometryProperty>
25+
<ogr:id>2</ogr:id>
26+
<ogr:id2>1</ogr:id2>
27+
<ogr:text_float>1.1</ogr:text_float>
28+
</ogr:text_to_float>
29+
</gml:featureMember>
30+
<gml:featureMember>
31+
<ogr:text_to_float fid="points.2">
32+
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>2,2</gml:coordinates></gml:Point></ogr:geometryProperty>
33+
<ogr:id>3</ogr:id>
34+
<ogr:id2>0</ogr:id2>
35+
<ogr:text_float>5%</ogr:text_float>
36+
</ogr:text_to_float>
37+
</gml:featureMember>
38+
<gml:featureMember>
39+
<ogr:text_to_float fid="points.3">
40+
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>5,2</gml:coordinates></gml:Point></ogr:geometryProperty>
41+
<ogr:id>4</ogr:id>
42+
<ogr:id2>2</ogr:id2>
43+
<ogr:text_float>notfloat</ogr:text_float>
44+
</ogr:text_to_float>
45+
</gml:featureMember>
46+
<gml:featureMember>
47+
<ogr:text_to_float fid="points.4">
48+
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>4,1</gml:coordinates></gml:Point></ogr:geometryProperty>
49+
<ogr:id>5</ogr:id>
50+
<ogr:id2>1</ogr:id2>
51+
</ogr:text_to_float>
52+
</gml:featureMember>
53+
</ogr:FeatureCollection>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xs:schema targetNamespace="http://ogr.maptools.org/" xmlns:ogr="http://ogr.maptools.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:gml="http://www.opengis.net/gml" elementFormDefault="qualified" version="1.0">
3+
<xs:import namespace="http://www.opengis.net/gml" schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd"/>
4+
<xs:element name="FeatureCollection" type="ogr:FeatureCollectionType" substitutionGroup="gml:_FeatureCollection"/>
5+
<xs:complexType name="FeatureCollectionType">
6+
<xs:complexContent>
7+
<xs:extension base="gml:AbstractFeatureCollectionType">
8+
<xs:attribute name="lockId" type="xs:string" use="optional"/>
9+
<xs:attribute name="scope" type="xs:string" use="optional"/>
10+
</xs:extension>
11+
</xs:complexContent>
12+
</xs:complexType>
13+
<xs:element name="text_to_float" type="ogr:text_to_float_Type" substitutionGroup="gml:_Feature"/>
14+
<xs:complexType name="text_to_float_Type">
15+
<xs:complexContent>
16+
<xs:extension base="gml:AbstractFeatureType">
17+
<xs:sequence>
18+
<xs:element name="geometryProperty" type="gml:PointPropertyType" nillable="true" minOccurs="0" maxOccurs="1"/>
19+
<xs:element name="id" nillable="true" minOccurs="0" maxOccurs="1">
20+
<xs:simpleType>
21+
<xs:restriction base="xs:integer">
22+
<xs:totalDigits value="10"/>
23+
</xs:restriction>
24+
</xs:simpleType>
25+
</xs:element>
26+
<xs:element name="id2" nillable="true" minOccurs="0" maxOccurs="1">
27+
<xs:simpleType>
28+
<xs:restriction base="xs:integer">
29+
<xs:totalDigits value="10"/>
30+
</xs:restriction>
31+
</xs:simpleType>
32+
</xs:element>
33+
<xs:element name="text_float" nillable="true" minOccurs="0" maxOccurs="1">
34+
<xs:simpleType>
35+
<xs:restriction base="xs:string">
36+
<xs:maxLength value="255"/>
37+
</xs:restriction>
38+
</xs:simpleType>
39+
</xs:element>
40+
</xs:sequence>
41+
</xs:extension>
42+
</xs:complexContent>
43+
</xs:complexType>
44+
</xs:schema>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<GMLFeatureClassList>
2+
<GMLFeatureClass>
3+
<Name>text_to_float</Name>
4+
<ElementPath>text_to_float</ElementPath>
5+
<!--POINT-->
6+
<GeometryType>1</GeometryType>
7+
<SRSName>EPSG:4326</SRSName>
8+
<DatasetSpecificInfo>
9+
<FeatureCount>5</FeatureCount>
10+
<ExtentXMin>1.00000</ExtentXMin>
11+
<ExtentXMax>5.00000</ExtentXMax>
12+
<ExtentYMin>1.00000</ExtentYMin>
13+
<ExtentYMax>3.00000</ExtentYMax>
14+
</DatasetSpecificInfo>
15+
<PropertyDefn>
16+
<Name>id</Name>
17+
<ElementPath>id</ElementPath>
18+
<Type>Integer</Type>
19+
</PropertyDefn>
20+
<PropertyDefn>
21+
<Name>id2</Name>
22+
<ElementPath>id2</ElementPath>
23+
<Type>Integer</Type>
24+
</PropertyDefn>
25+
<PropertyDefn>
26+
<Name>text_float</Name>
27+
<ElementPath>text_float</ElementPath>
28+
<Type>Real</Type>
29+
</PropertyDefn>
30+
</GMLFeatureClass>
31+
</GMLFeatureClassList>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ogr:FeatureCollection
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation=""
5+
xmlns:ogr="http://ogr.maptools.org/"
6+
xmlns:gml="http://www.opengis.net/gml">
7+
<gml:boundedBy>
8+
<gml:Box>
9+
<gml:coord><gml:X>1</gml:X><gml:Y>1</gml:Y></gml:coord>
10+
<gml:coord><gml:X>5</gml:X><gml:Y>3</gml:Y></gml:coord>
11+
</gml:Box>
12+
</gml:boundedBy>
13+
14+
<gml:featureMember>
15+
<ogr:text_to_float fid="points.0">
16+
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>1,1</gml:coordinates></gml:Point></ogr:geometryProperty>
17+
<ogr:id>1</ogr:id>
18+
<ogr:id2>2</ogr:id2>
19+
<ogr:text_float>1.000000000000000</ogr:text_float>
20+
</ogr:text_to_float>
21+
</gml:featureMember>
22+
<gml:featureMember>
23+
<ogr:text_to_float fid="points.1">
24+
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>3,3</gml:coordinates></gml:Point></ogr:geometryProperty>
25+
<ogr:id>2</ogr:id>
26+
<ogr:id2>1</ogr:id2>
27+
<ogr:text_float>1.100000000000000</ogr:text_float>
28+
</ogr:text_to_float>
29+
</gml:featureMember>
30+
<gml:featureMember>
31+
<ogr:text_to_float fid="points.2">
32+
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>2,2</gml:coordinates></gml:Point></ogr:geometryProperty>
33+
<ogr:id>3</ogr:id>
34+
<ogr:id2>0</ogr:id2>
35+
<ogr:text_float>0.050000000000000</ogr:text_float>
36+
</ogr:text_to_float>
37+
</gml:featureMember>
38+
<gml:featureMember>
39+
<ogr:text_to_float fid="points.3">
40+
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>5,2</gml:coordinates></gml:Point></ogr:geometryProperty>
41+
<ogr:id>4</ogr:id>
42+
<ogr:id2>2</ogr:id2>
43+
</ogr:text_to_float>
44+
</gml:featureMember>
45+
<gml:featureMember>
46+
<ogr:text_to_float fid="points.4">
47+
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>4,1</gml:coordinates></gml:Point></ogr:geometryProperty>
48+
<ogr:id>5</ogr:id>
49+
<ogr:id2>1</ogr:id2>
50+
</ogr:text_to_float>
51+
</gml:featureMember>
52+
</ogr:FeatureCollection>

python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,18 @@ tests:
11071107
name: expected/add_geometry_pointz.gml
11081108
type: vector
11091109

1110+
- algorithm: qgis:texttofloat
1111+
name: Text to float
1112+
params:
1113+
FIELD: 'text_float'
1114+
INPUT:
1115+
name: custom/text_to_float.gml
1116+
type: vector
1117+
results:
1118+
OUTPUT:
1119+
name: expected/text_to_float.gml
1120+
type: vector
1121+
11101122
- algorithm: qgis:countpointsinpolygon
11111123
name: Count points in polygon
11121124
params:

0 commit comments

Comments
 (0)