Port singlepart to multipart to new API, fix disabled test

This commit is contained in:
Nyall Dawson 2017-07-28 12:42:40 +10:00
parent 2a6f51218a
commit c5cb3df15b
4 changed files with 50 additions and 44 deletions

View File

@ -111,6 +111,7 @@ from .ShortestPathLayerToPoint import ShortestPathLayerToPoint
from .ShortestPathPointToLayer import ShortestPathPointToLayer from .ShortestPathPointToLayer import ShortestPathPointToLayer
from .ShortestPathPointToPoint import ShortestPathPointToPoint from .ShortestPathPointToPoint import ShortestPathPointToPoint
from .SimplifyGeometries import SimplifyGeometries from .SimplifyGeometries import SimplifyGeometries
from .SinglePartsToMultiparts import SinglePartsToMultiparts
from .SingleSidedBuffer import SingleSidedBuffer from .SingleSidedBuffer import SingleSidedBuffer
from .Slope import Slope from .Slope import Slope
from .Smooth import Smooth from .Smooth import Smooth
@ -130,7 +131,6 @@ from .VoronoiPolygons import VoronoiPolygons
from .ZonalStatistics import ZonalStatistics from .ZonalStatistics import ZonalStatistics
# from .ExtractByLocation import ExtractByLocation # from .ExtractByLocation import ExtractByLocation
# from .SinglePartsToMultiparts import SinglePartsToMultiparts
# from .ConvexHull import ConvexHull # from .ConvexHull import ConvexHull
# from .FixedDistanceBuffer import FixedDistanceBuffer # from .FixedDistanceBuffer import FixedDistanceBuffer
# from .VariableDistanceBuffer import VariableDistanceBuffer # from .VariableDistanceBuffer import VariableDistanceBuffer
@ -185,7 +185,6 @@ class QGISAlgorithmProvider(QgsProcessingProvider):
def getAlgs(self): def getAlgs(self):
# algs = [ # algs = [
# SinglePartsToMultiparts(),
# ConvexHull(), FixedDistanceBuffer(), # ConvexHull(), FixedDistanceBuffer(),
# VariableDistanceBuffer(), # VariableDistanceBuffer(),
# RandomSelection(), RandomSelectionWithinSubsets(), # RandomSelection(), RandomSelectionWithinSubsets(),
@ -285,6 +284,7 @@ class QGISAlgorithmProvider(QgsProcessingProvider):
ShortestPathPointToLayer(), ShortestPathPointToLayer(),
ShortestPathPointToPoint(), ShortestPathPointToPoint(),
SimplifyGeometries(), SimplifyGeometries(),
SinglePartsToMultiparts(),
SingleSidedBuffer(), SingleSidedBuffer(),
Slope(), Slope(),
Smooth(), Smooth(),

View File

@ -30,12 +30,17 @@ import os
from qgis.PyQt.QtGui import QIcon from qgis.PyQt.QtGui import QIcon
from qgis.core import QgsFeature, QgsFeatureSink, QgsGeometry, QgsWkbTypes, QgsProcessingUtils, NULL from qgis.core import (QgsFeature,
QgsFeatureSink,
QgsGeometry,
QgsWkbTypes,
QgsProcessingUtils,
NULL,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterField,
QgsProcessingParameterFeatureSink)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterTableField
from processing.core.outputs import OutputVector
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0] pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
@ -56,11 +61,12 @@ class SinglePartsToMultiparts(QgisAlgorithm):
super().__init__() super().__init__()
def initAlgorithm(self, config=None): def initAlgorithm(self, config=None):
self.addParameter(ParameterVector(self.INPUT, self.tr('Input layer'))) self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.addParameter(ParameterTableField(self.FIELD, self.tr('Input layer')))
self.tr('Unique ID field'), self.INPUT)) self.addParameter(QgsProcessingParameterField(self.FIELD,
self.tr('Unique ID field'), parentLayerParameterName=self.INPUT))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Multipart'))) self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Multipart')))
def name(self): def name(self):
return 'singlepartstomultipart' return 'singlepartstomultipart'
@ -69,31 +75,29 @@ class SinglePartsToMultiparts(QgisAlgorithm):
return self.tr('Singleparts to multipart') return self.tr('Singleparts to multipart')
def processAlgorithm(self, parameters, context, feedback): def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context) source = self.parameterAsSource(parameters, self.INPUT, context)
fieldName = self.getParameterValue(self.FIELD) field_name = self.parameterAsString(parameters, self.FIELD, context)
geomType = QgsWkbTypes.multiType(layer.wkbType()) geom_type = QgsWkbTypes.multiType(source.wkbType())
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.fields(), geomType, layer.crs(), (sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
context) source.fields(), geom_type, source.sourceCrs())
outFeat = QgsFeature() index = source.fields().lookupField(field_name)
inGeom = QgsGeometry()
index = layer.fields().lookupField(fieldName)
collection_geom = {} collection_geom = {}
collection_attrs = {} collection_attrs = {}
features = QgsProcessingUtils.getFeatures(layer, context) features = source.getFeatures()
total = 100.0 / layer.featureCount() if layer.featureCount() else 0 total = 100.0 / source.featureCount() if source.featureCount() else 0
for current, feature in enumerate(features): for current, feature in enumerate(features):
if feedback.isCanceled():
break
atMap = feature.attributes() atMap = feature.attributes()
idVar = atMap[index] idVar = atMap[index]
if idVar in [None, NULL]: if idVar in [None, NULL] or not feature.hasGeometry():
outFeat.setAttributes(atMap) sink.addFeature(feature, QgsFeatureSink.FastInsert)
outFeat.setGeometry(feature.geometry())
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total)) feedback.setProgress(int(current * total))
continue continue
@ -108,8 +112,12 @@ class SinglePartsToMultiparts(QgisAlgorithm):
feedback.setProgress(int(current * total)) feedback.setProgress(int(current * total))
for key, geoms in collection_geom.items(): for key, geoms in collection_geom.items():
outFeat.setAttributes(collection_attrs[key]) if feedback.isCanceled():
outFeat.setGeometry(QgsGeometry.collectGeometry(geoms)) break
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
del writer feature = QgsFeature()
feature.setAttributes(collection_attrs[key])
feature.setGeometry(QgsGeometry.collectGeometry(geoms))
sink.addFeature(feature, QgsFeatureSink.FastInsert)
return {self.OUTPUT: dest_id}

View File

@ -6,7 +6,7 @@
<GeometryType>6</GeometryType> <GeometryType>6</GeometryType>
<SRSName>EPSG:4326</SRSName> <SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo> <DatasetSpecificInfo>
<FeatureCount>4</FeatureCount> <FeatureCount>5</FeatureCount>
<ExtentXMin>0.21020</ExtentXMin> <ExtentXMin>0.21020</ExtentXMin>
<ExtentXMax>8.96288</ExtentXMax> <ExtentXMax>8.96288</ExtentXMax>
<ExtentYMin>-5.48232</ExtentYMin> <ExtentYMin>-5.48232</ExtentYMin>

View File

@ -2363,20 +2363,18 @@ tests:
name: expected/mean_coordinates.gml name: expected/mean_coordinates.gml
type: vector type: vector
# # Temporarily disable until we figure out why it failed after merging - algorithm: qgis:singlepartstomultipart
# # into master name: single part to multipart
# #- algorithm: qgis:singlepartstomultipart params:
# # name: single part to multipart FIELD: id
# # params: INPUT:
# # FIELD: id name: custom/single_part_poly.gml
# # INPUT: type: vector
# # name: custom/single_part_poly.gml results:
# # type: vector OUTPUT:
# # results: name: expected/single_to_multi.gml
# # OUTPUT: type: vector
# # name: expected/single_to_multi.gml
# # type: vector
#
# - algorithm: qgis:zonalstatistics # - algorithm: qgis:zonalstatistics
# name: simple zonal statistics # name: simple zonal statistics
# params: # params: