Skip to content
Permalink
Browse files
[processing] Polygons to line fixes
- Maintain Z/M values
- Handle curved geometries without segmentizing
- Retain null geometries
  • Loading branch information
nyalldawson committed Nov 14, 2016
1 parent 5d78d60 commit 29d33b47fe050f1004cc8a5d8474018dd84ce72d
@@ -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'),
@@ -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 []
@@ -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.