Skip to content

Commit 5306cd8

Browse files
committedOct 3, 2014
refactor Text to float alg
1 parent 07d584d commit 5306cd8

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
 

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575

7676
from mmqgisx.DeleteColumn import DeleteColumn
7777
from mmqgisx.DeleteDuplicateGeometries import DeleteDuplicateGeometries
78+
from mmqgisx.TextToFloat import TextToFloat
7879

7980
from ConcaveHull import ConcaveHull
8081
from Polygonize import Polygonize
@@ -140,6 +141,7 @@ def __init__(self):
140141
SpatialJoin(),
141142
# ------ mmqgisx ------
142143
DeleteColumn(), DeleteDuplicateGeometries(),
144+
TextToFloat(),
143145
#mmqgisx_delete_duplicate_geometries_algorithm(),
144146
#mmqgisx_geometry_convert_algorithm(),
145147
#mmqgisx_grid_algorithm(),
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
DeleteColumn.py
6+
---------------------
7+
Date : May 2010
8+
Copyright : (C) 2010 by Michael Minn
9+
Email : pyqgis at michaelminn dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Michael Minn'
21+
__date__ = 'May 2010'
22+
__copyright__ = '(C) 2010, Michael Minn'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from PyQt4.QtCore import *
29+
from qgis.core import *
30+
from processing.core.GeoAlgorithm import GeoAlgorithm
31+
from processing.core.parameters import ParameterVector
32+
from processing.core.parameters import ParameterTableField
33+
from processing.core.outputs import OutputVector
34+
from processing.tools import dataobjects, vector
35+
36+
37+
class TextToFloat(GeoAlgorithm):
38+
INPUT = 'INPUT'
39+
FIELD = 'FIELD'
40+
OUTPUT = 'OUTPUT'
41+
42+
def defineCharacteristics(self):
43+
self.name = 'Text to float'
44+
self.group = 'Vector table tools'
45+
46+
self.addParameter(ParameterVector(
47+
self.INPUT, 'Input Layer', [ParameterVector.VECTOR_TYPE_ANY]))
48+
self.addParameter(ParameterTableField(
49+
self.FIELD, 'Text attribute to convert to float', self.INPUT,
50+
ParameterTableField.DATA_TYPE_STRING))
51+
self.addOutput(OutputVector(self.OUTPUT, 'Output'))
52+
53+
def processAlgorithm(self, progress):
54+
layer = dataobjects.getObjectFromUri(
55+
self.getParameterValue(self.INPUT))
56+
fieldName = self.getParameterValue(self.FIELD)
57+
idx = layer.fieldNameIndex(fieldName)
58+
59+
fields = layer.pendingFields()
60+
fields[idx] = QgsField(fieldName, QVariant.Double, '', 24, 15)
61+
62+
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields,
63+
layer.wkbType(), layer.crs())
64+
65+
features = vector.features(layer)
66+
67+
count = len(features)
68+
total = 100.0 / float(count)
69+
for count, f in enumerate(features):
70+
value = f[idx]
71+
try:
72+
if '%' in value:
73+
f[idx] = float(value.replace('%', '')) / 100.0
74+
else:
75+
f[idx] = float(value)
76+
except:
77+
f[idx] = None
78+
79+
writer.addFeature(f)
80+
progress.setPercentage(int(count * total))
81+
82+
del writer

0 commit comments

Comments
 (0)
Please sign in to comment.