2014-10-02 19:02:09 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
2014-10-03 10:29:54 +03:00
|
|
|
TextToFloat.py
|
2014-10-02 19:02:09 +03:00
|
|
|
---------------------
|
|
|
|
Date : May 2010
|
|
|
|
Copyright : (C) 2010 by Michael Minn
|
|
|
|
Email : pyqgis at michaelminn 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__ = 'Michael Minn'
|
|
|
|
__date__ = 'May 2010'
|
|
|
|
__copyright__ = '(C) 2010, Michael Minn'
|
|
|
|
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtCore import QVariant
|
2017-03-29 10:42:42 +10:00
|
|
|
from qgis.core import (QgsApplication,
|
2017-04-25 17:53:32 +10:00
|
|
|
QgsField,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-04-25 17:53:32 +10:00
|
|
|
QgsProcessingUtils)
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2014-10-02 19:02:09 +03:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterTableField
|
|
|
|
from processing.core.outputs import OutputVector
|
|
|
|
|
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class TextToFloat(QgisAlgorithm):
|
2014-10-02 19:02:09 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
FIELD = 'FIELD'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector table tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Input Layer')))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterTableField(self.FIELD,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Text attribute to convert to float'),
|
|
|
|
self.INPUT, ParameterTableField.DATA_TYPE_STRING))
|
2015-05-22 16:39:20 +02:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Float from text')))
|
2014-10-02 19:02:09 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'texttofloat'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Text to float')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-05-02 13:40:49 +10:00
|
|
|
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
|
2014-10-02 19:02:09 +03:00
|
|
|
fieldName = self.getParameterValue(self.FIELD)
|
2016-09-22 12:09:13 +02:00
|
|
|
idx = layer.fields().lookupField(fieldName)
|
2014-10-02 19:02:09 +03:00
|
|
|
|
2016-08-04 07:37:22 +10:00
|
|
|
fields = layer.fields()
|
2014-10-02 19:02:09 +03:00
|
|
|
fields[idx] = QgsField(fieldName, QVariant.Double, '', 24, 15)
|
|
|
|
|
2017-04-26 14:33:53 +10:00
|
|
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, layer.wkbType(), layer.crs(), context)
|
2014-10-02 19:02:09 +03:00
|
|
|
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(layer, context)
|
2014-10-02 19:02:09 +03:00
|
|
|
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, f in enumerate(features):
|
2014-10-02 19:02:09 +03:00
|
|
|
value = f[idx]
|
|
|
|
try:
|
|
|
|
if '%' in value:
|
|
|
|
f[idx] = float(value.replace('%', '')) / 100.0
|
|
|
|
else:
|
|
|
|
f[idx] = float(value)
|
|
|
|
except:
|
|
|
|
f[idx] = None
|
|
|
|
|
2017-06-23 14:34:38 +10:00
|
|
|
writer.addFeature(f, QgsFeatureSink.FastInsert)
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2014-10-02 19:02:09 +03:00
|
|
|
|
|
|
|
del writer
|