Skip to content
Permalink
Browse files

Replace more duplicate code with QgsProcessingUtils.combineFields

  • Loading branch information
nyalldawson committed Sep 22, 2017
1 parent a191c77 commit 10d6ef06e83242fc8d3e8245831ce37b896d89ed
Showing with 13 additions and 56 deletions.
  1. +11 −14 python/plugins/processing/algs/qgis/ExportGeometryInfo.py
  2. +2 −42 python/plugins/processing/tools/vector.py
@@ -32,9 +32,11 @@

from qgis.core import (QgsCoordinateTransform,
QgsField,
QgsFields,
QgsWkbTypes,
QgsFeatureSink,
QgsDistanceArea,
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterEnum,
QgsProcessingParameterFeatureSink)
@@ -89,28 +91,23 @@ def processAlgorithm(self, parameters, context, feedback):
wkb_type = source.wkbType()
fields = source.fields()

new_fields = QgsFields()
if QgsWkbTypes.geometryType(wkb_type) == QgsWkbTypes.PolygonGeometry:
areaName = vector.createUniqueFieldName('area', fields)
fields.append(QgsField(areaName, QVariant.Double))
perimeterName = vector.createUniqueFieldName('perimeter', fields)
fields.append(QgsField(perimeterName, QVariant.Double))
new_fields.append(QgsField('area', QVariant.Double))
new_fields.append(QgsField('perimeter', QVariant.Double))
elif QgsWkbTypes.geometryType(wkb_type) == QgsWkbTypes.LineGeometry:
lengthName = vector.createUniqueFieldName('length', fields)
fields.append(QgsField(lengthName, QVariant.Double))
new_fields.append(QgsField('length', QVariant.Double))
else:
xName = vector.createUniqueFieldName('xcoord', fields)
fields.append(QgsField(xName, QVariant.Double))
yName = vector.createUniqueFieldName('ycoord', fields)
fields.append(QgsField(yName, QVariant.Double))
new_fields.append(QgsField('xcoord', QVariant.Double))
new_fields.append(QgsField('ycoord', QVariant.Double))
if QgsWkbTypes.hasZ(source.wkbType()):
self.export_z = True
zName = vector.createUniqueFieldName('zcoord', fields)
fields.append(QgsField(zName, QVariant.Double))
new_fields.append(QgsField('zcoord', QVariant.Double))
if QgsWkbTypes.hasM(source.wkbType()):
self.export_m = True
zName = vector.createUniqueFieldName('mvalue', fields)
fields.append(QgsField(zName, QVariant.Double))
new_fields.append(QgsField('mvalue', QVariant.Double))

fields = QgsProcessingUtils.combineFields(fields, new_fields)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, wkb_type, source.sourceCrs())

@@ -27,13 +27,8 @@

import csv

from qgis.PyQt.QtCore import QVariant
from qgis.core import (QgsFields,
QgsField,
QgsGeometry,
QgsWkbTypes,
QgsFeatureRequest,
QgsPointXY)
from qgis.core import (QgsWkbTypes,
QgsFeatureRequest)


def resolveFieldIndex(source, attr):
@@ -95,41 +90,6 @@ def values(source, *attributes):
return ret


def createUniqueFieldName(fieldName, fieldList):
def nextname(name):
num = 1
while True:
returnname = '{name}_{num}'.format(name=name[:8], num=num)
yield returnname
num += 1

def found(name):
return any(f.name() == name for f in fieldList)

shortName = fieldName[:10]

if not fieldList:
return shortName

if not found(shortName):
return shortName

for newname in nextname(shortName):
if not found(newname):
return newname


def findOrCreateField(layer, fieldList, fieldName, fieldLen=24, fieldPrec=15):
idx = layer.fields().lookupField(fieldName)
if idx == -1:
fn = createUniqueFieldName(fieldName, fieldList)
field = QgsField(fn, QVariant.Double, '', fieldLen, fieldPrec)
idx = len(fieldList)
fieldList.append(field)

return (idx, fieldList)


def extractPoints(geom):
points = []
if geom.type() == QgsWkbTypes.PointGeometry:

0 comments on commit 10d6ef0

Please sign in to comment.
You can’t perform that action at this time.