Skip to content

Commit 10d6ef0

Browse files
committed
Replace more duplicate code with QgsProcessingUtils.combineFields
1 parent a191c77 commit 10d6ef0

File tree

2 files changed

+13
-56
lines changed

2 files changed

+13
-56
lines changed

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

+11-14
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
3232

3333
from qgis.core import (QgsCoordinateTransform,
3434
QgsField,
35+
QgsFields,
3536
QgsWkbTypes,
3637
QgsFeatureSink,
3738
QgsDistanceArea,
39+
QgsProcessingUtils,
3840
QgsProcessingParameterFeatureSource,
3941
QgsProcessingParameterEnum,
4042
QgsProcessingParameterFeatureSink)
@@ -89,28 +91,23 @@ def processAlgorithm(self, parameters, context, feedback):
8991
wkb_type = source.wkbType()
9092
fields = source.fields()
9193

94+
new_fields = QgsFields()
9295
if QgsWkbTypes.geometryType(wkb_type) == QgsWkbTypes.PolygonGeometry:
93-
areaName = vector.createUniqueFieldName('area', fields)
94-
fields.append(QgsField(areaName, QVariant.Double))
95-
perimeterName = vector.createUniqueFieldName('perimeter', fields)
96-
fields.append(QgsField(perimeterName, QVariant.Double))
96+
new_fields.append(QgsField('area', QVariant.Double))
97+
new_fields.append(QgsField('perimeter', QVariant.Double))
9798
elif QgsWkbTypes.geometryType(wkb_type) == QgsWkbTypes.LineGeometry:
98-
lengthName = vector.createUniqueFieldName('length', fields)
99-
fields.append(QgsField(lengthName, QVariant.Double))
99+
new_fields.append(QgsField('length', QVariant.Double))
100100
else:
101-
xName = vector.createUniqueFieldName('xcoord', fields)
102-
fields.append(QgsField(xName, QVariant.Double))
103-
yName = vector.createUniqueFieldName('ycoord', fields)
104-
fields.append(QgsField(yName, QVariant.Double))
101+
new_fields.append(QgsField('xcoord', QVariant.Double))
102+
new_fields.append(QgsField('ycoord', QVariant.Double))
105103
if QgsWkbTypes.hasZ(source.wkbType()):
106104
self.export_z = True
107-
zName = vector.createUniqueFieldName('zcoord', fields)
108-
fields.append(QgsField(zName, QVariant.Double))
105+
new_fields.append(QgsField('zcoord', QVariant.Double))
109106
if QgsWkbTypes.hasM(source.wkbType()):
110107
self.export_m = True
111-
zName = vector.createUniqueFieldName('mvalue', fields)
112-
fields.append(QgsField(zName, QVariant.Double))
108+
new_fields.append(QgsField('mvalue', QVariant.Double))
113109

110+
fields = QgsProcessingUtils.combineFields(fields, new_fields)
114111
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
115112
fields, wkb_type, source.sourceCrs())
116113

python/plugins/processing/tools/vector.py

+2-42
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,8 @@
2727

2828
import csv
2929

30-
from qgis.PyQt.QtCore import QVariant
31-
from qgis.core import (QgsFields,
32-
QgsField,
33-
QgsGeometry,
34-
QgsWkbTypes,
35-
QgsFeatureRequest,
36-
QgsPointXY)
30+
from qgis.core import (QgsWkbTypes,
31+
QgsFeatureRequest)
3732

3833

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

9792

98-
def createUniqueFieldName(fieldName, fieldList):
99-
def nextname(name):
100-
num = 1
101-
while True:
102-
returnname = '{name}_{num}'.format(name=name[:8], num=num)
103-
yield returnname
104-
num += 1
105-
106-
def found(name):
107-
return any(f.name() == name for f in fieldList)
108-
109-
shortName = fieldName[:10]
110-
111-
if not fieldList:
112-
return shortName
113-
114-
if not found(shortName):
115-
return shortName
116-
117-
for newname in nextname(shortName):
118-
if not found(newname):
119-
return newname
120-
121-
122-
def findOrCreateField(layer, fieldList, fieldName, fieldLen=24, fieldPrec=15):
123-
idx = layer.fields().lookupField(fieldName)
124-
if idx == -1:
125-
fn = createUniqueFieldName(fieldName, fieldList)
126-
field = QgsField(fn, QVariant.Double, '', fieldLen, fieldPrec)
127-
idx = len(fieldList)
128-
fieldList.append(field)
129-
130-
return (idx, fieldList)
131-
132-
13393
def extractPoints(geom):
13494
points = []
13595
if geom.type() == QgsWkbTypes.PointGeometry:

0 commit comments

Comments
 (0)