Skip to content

Commit

Permalink
Update multiparts to singleparts to new processing API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 14, 2017
1 parent 4dcf8d8 commit 4768025
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
37 changes: 24 additions & 13 deletions python/plugins/processing/algs/qgis/MultipartToSingleparts.py
Expand Up @@ -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
Expand All @@ -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'
Expand All @@ -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}
7 changes: 4 additions & 3 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -271,7 +271,8 @@ def getAlgs(self):
ExtentFromLayer(),
ExtractByExpression(),
GridPolygon(),
Merge()
Merge(),
MultipartToSingleparts()
]

if hasPlotly:
Expand Down
22 changes: 11 additions & 11 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Expand Up @@ -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:
Expand Down

0 comments on commit 4768025

Please sign in to comment.