From 29d33b47fe050f1004cc8a5d8474018dd84ce72d Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Tue, 15 Nov 2016 09:27:14 +1000 Subject: [PATCH] [processing] Polygons to line fixes - Maintain Z/M values - Handle curved geometries without segmentizing - Retain null geometries --- .../processing/algs/qgis/LinesToPolygons.py | 1 + .../processing/algs/qgis/PolygonsToLines.py | 34 +++++-------------- .../testdata/expected/polys_to_lines.gml | 7 ++++ 3 files changed, 16 insertions(+), 26 deletions(-) diff --git a/python/plugins/processing/algs/qgis/LinesToPolygons.py b/python/plugins/processing/algs/qgis/LinesToPolygons.py index ada0edd7fd6d..7c0331acfb3d 100644 --- a/python/plugins/processing/algs/qgis/LinesToPolygons.py +++ b/python/plugins/processing/algs/qgis/LinesToPolygons.py @@ -50,6 +50,7 @@ def getIcon(self): def defineCharacteristics(self): self.name, self.i18n_name = self.trAlgorithm('Lines to polygons') self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools') + self.tags = self.tr('line,polygon,convert') self.addParameter(ParameterVector(self.INPUT, self.tr('Input layer'), diff --git a/python/plugins/processing/algs/qgis/PolygonsToLines.py b/python/plugins/processing/algs/qgis/PolygonsToLines.py index b5472839f291..45eb45110f53 100644 --- a/python/plugins/processing/algs/qgis/PolygonsToLines.py +++ b/python/plugins/processing/algs/qgis/PolygonsToLines.py @@ -50,6 +50,7 @@ def getIcon(self): def defineCharacteristics(self): self.name, self.i18n_name = self.trAlgorithm('Polygons to lines') self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools') + self.tags = self.tr('line,polygon,convert') self.addParameter(ParameterVector(self.INPUT, self.tr('Input layer'), [dataobjects.TYPE_VECTOR_POLYGON])) @@ -62,36 +63,17 @@ def processAlgorithm(self, progress): writer = self.getOutputFromName(self.OUTPUT).getVectorWriter( layer.fields().toList(), QgsWkbTypes.LineString, layer.crs()) - outFeat = QgsFeature() - inGeom = QgsGeometry() - outGeom = QgsGeometry() - features = vector.features(layer) total = 100.0 / len(features) for current, f in enumerate(features): - inGeom = f.geometry() - attrs = f.attributes() - lineList = self.extractAsLine(inGeom) - outFeat.setAttributes(attrs) - for h in lineList: - outFeat.setGeometry(outGeom.fromPolyline(h)) - writer.addFeature(outFeat) + if f.hasGeometry(): + lines = QgsGeometry(f.geometry().geometry().boundary()).asGeometryCollection() + for line in lines: + f.setGeometry(line) + writer.addFeature(f) + else: + writer.addFeature(f) progress.setPercentage(int(current * total)) del writer - - def extractAsLine(self, geom): - multiGeom = QgsGeometry() - lines = [] - if geom and geom.type() == QgsWkbTypes.PolygonGeometry: - if geom.isMultipart(): - multiGeom = geom.asMultiPolygon() - for i in multiGeom: - lines.extend(i) - else: - multiGeom = geom.asPolygon() - lines = multiGeom - return lines - else: - return [] diff --git a/python/plugins/processing/tests/testdata/expected/polys_to_lines.gml b/python/plugins/processing/tests/testdata/expected/polys_to_lines.gml index e68960925b45..ce07984e3fe1 100644 --- a/python/plugins/processing/tests/testdata/expected/polys_to_lines.gml +++ b/python/plugins/processing/tests/testdata/expected/polys_to_lines.gml @@ -37,4 +37,11 @@ -0.123 + + + Test + 3 + 0 + +