Skip to content

Commit

Permalink
Replace more duplicate code with QgsProcessingUtils.combineFields
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 22, 2017
1 parent a191c77 commit 10d6ef0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 56 deletions.
25 changes: 11 additions & 14 deletions python/plugins/processing/algs/qgis/ExportGeometryInfo.py
Expand Up @@ -32,9 +32,11 @@

from qgis.core import (QgsCoordinateTransform,
QgsField,
QgsFields,
QgsWkbTypes,
QgsFeatureSink,
QgsDistanceArea,
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterEnum,
QgsProcessingParameterFeatureSink)
Expand Down Expand Up @@ -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())

Expand Down
44 changes: 2 additions & 42 deletions python/plugins/processing/tools/vector.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 10d6ef0

Please sign in to comment.