Skip to content
Permalink
Browse files

[processing] Fix Geometry algorithm always produces multi-geometry

Also added code to handle GeometryCollections
  • Loading branch information
alexbruy committed Feb 7, 2017
1 parent 5a5c1bf commit 7df04ac7e77e3470f9fb61936b8cf92dafe1a856
Showing with 18 additions and 2 deletions.
  1. +18 −2 python/plugins/processing/algs/qgis/FixGeometry.py
@@ -25,6 +25,8 @@

__revision__ = '$Format:%H$'

from qgis.core import QgsWkbTypes

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector
@@ -42,7 +44,8 @@ def defineCharacteristics(self):
self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools')

self.addParameter(ParameterVector(self.INPUT,
self.tr('Input Layer')))
self.tr('Input Layer'),
[dataobjects.TYPE_VECTOR_POLYGON, dataobjects.TYPE_VECTOR_LINE]))
self.addOutput(OutputVector(self.OUTPUT,
self.tr('Layer with fixed geometries')))

@@ -53,7 +56,7 @@ def processAlgorithm(self, feedback):
writer = self.getOutputFromName(
self.OUTPUT).getVectorWriter(
layer.fields(),
layer.wkbType(),
QgsWkbTypes.multiType(layer.wkbType()),
layer.crs())

features = vector.features(layer)
@@ -68,6 +71,19 @@ def processAlgorithm(self, feedback):
if not outputGeometry:
ProcessingLog.addToLog(ProcessingLog.LOG_WARNING,
'makeValid failed for feature {}'.format(inputFeature.id()))

if outputGeometry.wkbType() == QgsWkbTypes.Unknown or QgsWkbTypes.flatType(outputGeometry.geometry().wkbType()) == QgsWkbTypes.GeometryCollection:
tmpGeometries = outputGeometry.asGeometryCollection()
for g in tmpGeometries:
if g.type() == inputFeature.geometry().type():
try:
outputFeature.setGeometry(QgsGeometry(g))
writer.addFeature(outputFeature)
except:
pass
feedback.setProgress(int(current * total))
continue

outputFeature.setGeometry(outputGeometry)

writer.addFeature(outputFeature)

0 comments on commit 7df04ac

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