Skip to content

Commit

Permalink
[processing] Polygons to line fixes
Browse files Browse the repository at this point in the history
- Maintain Z/M values
- Handle curved geometries without segmentizing
- Retain null geometries
  • Loading branch information
nyalldawson committed Nov 14, 2016
1 parent 5d78d60 commit 29d33b4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.
1 change: 1 addition & 0 deletions python/plugins/processing/algs/qgis/LinesToPolygons.py
Expand Up @@ -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'),
Expand Down
34 changes: 8 additions & 26 deletions python/plugins/processing/algs/qgis/PolygonsToLines.py
Expand Up @@ -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]))
Expand All @@ -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 []
Expand Up @@ -37,4 +37,11 @@
<ogr:Bfloatval>-0.123</ogr:Bfloatval>
</ogr:polys_to_lines>
</gml:featureMember>
<gml:featureMember>
<ogr:polys_to_lines fid="multipolys.3">
<ogr:Bname>Test</ogr:Bname>
<ogr:Bintval>3</ogr:Bintval>
<ogr:Bfloatval>0</ogr:Bfloatval>
</ogr:polys_to_lines>
</gml:featureMember>
</ogr:FeatureCollection>

0 comments on commit 29d33b4

Please sign in to comment.