Skip to content
Permalink
Browse files

[processing] fixed 'add geometric properties' algorithm when output f…

…ields already exist in layer

fixes #13010
  • Loading branch information
volaya committed Jun 26, 2015
1 parent 768485d commit 8a541823e57aed73486dff32e344c01e334f7d67
Showing with 16 additions and 20 deletions.
  1. +16 −20 python/plugins/processing/algs/qgis/ExportGeometryInfo.py
@@ -25,7 +25,8 @@

__revision__ = '$Format:%H$'

from qgis.core import QGis, QgsProject, QgsCoordinateTransform, QgsFeature, QgsGeometry
from qgis.core import QGis, QgsProject, QgsCoordinateTransform, QgsFeature, QgsGeometry, QgsField
from PyQt4.QtCore import QVariant
from qgis.utils import iface
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
@@ -59,29 +60,24 @@ def processAlgorithm(self, progress):
method = self.getParameterValue(self.METHOD)

geometryType = layer.geometryType()

idx1 = -1
idx2 = -1
fields = layer.pendingFields()

if geometryType == QGis.Polygon:
(idx1, fields) = vector.findOrCreateField(layer, fields, 'area',
21, 6)
(idx2, fields) = vector.findOrCreateField(layer, fields,
'perimeter', 21, 6)
areaName = vector.createUniqueFieldName('area', fields)
fields.append(QgsField(areaName, QVariant.Double))
perimeterName = vector.createUniqueFieldName('perimeter', fields)
fields.append(QgsField(perimeterName, QVariant.Double))
elif geometryType == QGis.Line:
(idx1, fields) = vector.findOrCreateField(layer, fields, 'length',
21, 6)
idx2 = idx1
lengthName = vector.createUniqueFieldName('length', fields)
fields.append(QgsField(lengthName, QVariant.Double))
else:
(idx1, fields) = vector.findOrCreateField(layer, fields, 'xcoord',
21, 6)
(idx2, fields) = vector.findOrCreateField(layer, fields, 'ycoord',
21, 6)
xName = vector.createUniqueFieldName('xcoord', fields)
fields.append(QgsField(xName, QVariant.Double))
yName = vector.createUniqueFieldName('ycoord', fields)
fields.append(QgsField(yName, QVariant.Double))

writer = self.getOutputFromName(
self.OUTPUT).getVectorWriter(fields.toList(),
layer.dataProvider().geometryType(), layer.crs())
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
fields.toList(), layer.dataProvider().geometryType(), layer.crs())

ellips = None
crs = None
@@ -120,9 +116,9 @@ def processAlgorithm(self, progress):

outFeat.setGeometry(inGeom)
attrs = f.attributes()
attrs.insert(idx1, attr1)
attrs.append(attr1)
if attr2 is not None:
attrs.insert(idx2, attr2)
attrs.append(attr2)
outFeat.setAttributes(attrs)
writer.addFeature(outFeat)

0 comments on commit 8a54182

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