Skip to content
Permalink
Browse files

Update multiparts to singleparts to new processing API

  • Loading branch information
nyalldawson committed Jun 14, 2017
1 parent 4dcf8d8 commit 4768025130a8fbc857ef3436fd467a36969a0728
@@ -29,7 +29,11 @@

from qgis.PyQt.QtGui import QIcon

from qgis.core import QgsWkbTypes, QgsProcessingUtils
from qgis.core import (QgsWkbTypes,
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsProcessingOutputVectorLayer)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterVector
@@ -51,8 +55,11 @@ def group(self):

def __init__(self):
super().__init__()
self.addParameter(ParameterVector(self.INPUT, self.tr('Input layer')))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Single parts')))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Input layer')))

self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Single parts')))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Single parts')))

def name(self):
return 'multiparttosingleparts'
@@ -61,28 +68,32 @@ def displayName(self):
return self.tr('Multipart to singleparts')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
geomType = QgsWkbTypes.singleType(layer.wkbType())
source = self.parameterAsSource(parameters, self.INPUT, context)
geomType = QgsWkbTypes.singleType(source.wkbType())

(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
source.fields(), geomType, source.sourceCrs())

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.fields(), geomType, layer.crs(),
context)
features = source.getFeatures()
total = 100.0 / source.featureCount()

features = QgsProcessingUtils.getFeatures(layer, context)
total = 100.0 / QgsProcessingUtils.featureCount(layer, context)
for current, f in enumerate(features):
if feedback.isCanceled():
break

input_geometry = f.geometry()
if input_geometry:
if input_geometry.isMultipart():
for g in input_geometry.asGeometryCollection():
output_feature = f
output_feature.setGeometry(g)
writer.addFeature(output_feature)
sink.addFeature(output_feature)
else:
writer.addFeature(f)
sink.addFeature(f)
else:
#input feature with null geometry
writer.addFeature(f)
sink.addFeature(f)

feedback.setProgress(int(current * total))

del writer
return {self.OUTPUT: dest_id}
@@ -66,7 +66,7 @@
# from .Delaunay import Delaunay
# from .VoronoiPolygons import VoronoiPolygons
# from .DensifyGeometries import DensifyGeometries
# from .MultipartToSingleparts import MultipartToSingleparts
from .MultipartToSingleparts import MultipartToSingleparts
# from .SimplifyGeometries import SimplifyGeometries
# from .LinesToPolygons import LinesToPolygons
# from .PolygonsToLines import PolygonsToLines
@@ -206,7 +206,7 @@ def getAlgs(self):
# ReprojectLayer(), ExportGeometryInfo(), Centroids(),
# Delaunay(), VoronoiPolygons(), SimplifyGeometries(),
# DensifyGeometries(), DensifyGeometriesInterval(),
# MultipartToSingleparts(), SinglePartsToMultiparts(),
# , SinglePartsToMultiparts(),
# PolygonsToLines(), LinesToPolygons(), ExtractNodes(),
# ConvexHull(), FixedDistanceBuffer(),
# VariableDistanceBuffer(), Dissolve(), Difference(),
@@ -271,7 +271,8 @@ def getAlgs(self):
ExtentFromLayer(),
ExtractByExpression(),
GridPolygon(),
Merge()
Merge(),
MultipartToSingleparts()
]

if hasPlotly:
@@ -389,17 +389,17 @@ tests:
# name: expected/merge_lines.gml
# type: vector
#
# - algorithm: qgis:multiparttosingleparts
# name: Multiparts to singleparts
# params:
# INPUT:
# name: multilines.gml
# type: vector
# results:
# OUTPUT:
# name: expected/multi_to_single.gml
# type: vector
#
- algorithm: qgis:multiparttosingleparts
name: Multiparts to singleparts
params:
INPUT:
name: multilines.gml
type: vector
results:
OUTPUT:
name: expected/multi_to_single.gml
type: vector

- algorithm: qgis:boundingboxes
name: Bounding boxes for lines
params:

0 comments on commit 4768025

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