diff --git a/python/plugins/processing/algs/qgis/PointOnSurface.py b/python/plugins/processing/algs/qgis/PointOnSurface.py index 6ef30957abb5..03d7fd0d5044 100644 --- a/python/plugins/processing/algs/qgis/PointOnSurface.py +++ b/python/plugins/processing/algs/qgis/PointOnSurface.py @@ -27,14 +27,15 @@ import os -from qgis.core import QgsWkbTypes, QgsFeatureSink, QgsProcessingUtils +from qgis.core import (QgsWkbTypes, + QgsFeatureSink, + QgsProcessing, + QgsProcessingParameterFeatureSource, + QgsProcessingParameterFeatureSink) from qgis.PyQt.QtGui import QIcon from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm -from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException -from processing.core.parameters import ParameterVector -from processing.core.outputs import OutputVector from processing.tools import dataobjects pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0] @@ -55,9 +56,9 @@ def __init__(self): super().__init__() def initAlgorithm(self, config=None): - self.addParameter(ParameterVector(self.INPUT_LAYER, - self.tr('Input layer'))) - self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Point'), datatype=[dataobjects.TYPE_VECTOR_POINT])) + self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT_LAYER, + self.tr('Input layer'))) + self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT_LAYER, self.tr('Point'), QgsProcessing.TypeVectorPoint)) def name(self): return 'pointonsurface' @@ -66,26 +67,24 @@ def displayName(self): return self.tr('Point on surface') def processAlgorithm(self, parameters, context, feedback): - layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context) + source = self.parameterAsSource(parameters, self.INPUT_LAYER, context) - writer = self.getOutputFromName( - self.OUTPUT_LAYER).getVectorWriter(layer.fields(), QgsWkbTypes.Point, layer.crs(), context) + (sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT_LAYER, context, source.fields(), QgsWkbTypes.Point, source.sourceCrs()) - features = QgsProcessingUtils.getFeatures(layer, context) - total = 100.0 / layer.featureCount() if layer.featureCount() else 0 + features = source.getFeatures() + total = 100.0 / source.featureCount() if source.featureCount() else 0 for current, input_feature in enumerate(features): + if feedback.isCanceled(): + break + output_feature = input_feature input_geometry = input_feature.geometry() if input_geometry: output_geometry = input_geometry.pointOnSurface() - if not output_geometry: - raise GeoAlgorithmExecutionException( - self.tr('Error calculating point on surface')) - output_feature.setGeometry(output_geometry) - writer.addFeature(output_feature, QgsFeatureSink.FastInsert) + sink.addFeature(output_feature, QgsFeatureSink.FastInsert) feedback.setProgress(int(current * total)) - del writer + return {self.OUTPUT_LAYER: dest_id} diff --git a/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py b/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py index 18b4f0a8fcc1..567b7aa7a617 100644 --- a/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py +++ b/python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py @@ -71,6 +71,7 @@ from .Merge import Merge from .NearestNeighbourAnalysis import NearestNeighbourAnalysis from .PointDistance import PointDistance +from .PointOnSurface import PointOnSurface from .PointsInPolygon import PointsInPolygon from .PointsLayerFromTable import PointsLayerFromTable from .PolygonsToLines import PolygonsToLines @@ -144,7 +145,6 @@ # from .RectanglesOvalsDiamondsVariable import RectanglesOvalsDiamondsVariable # from .RectanglesOvalsDiamondsFixed import RectanglesOvalsDiamondsFixed # from .MergeLines import MergeLines -# from .PointOnSurface import PointOnSurface # from .OffsetLine import OffsetLine # from .Translate import Translate # from .SingleSidedBuffer import SingleSidedBuffer @@ -215,7 +215,6 @@ def getAlgs(self): # ReverseLineDirection(), SpatialIndex(), DefineProjection(), # RectanglesOvalsDiamondsVariable(), # RectanglesOvalsDiamondsFixed(), MergeLines(), - # PointOnSurface(), # OffsetLine(), Translate(), # SingleSidedBuffer(), PointsAlongGeometry(), # Slope(), Ruggedness(), Hillshade(), @@ -264,6 +263,7 @@ def getAlgs(self): Merge(), NearestNeighbourAnalysis(), PointDistance(), + PointOnSurface(), PointsInPolygon(), PointsLayerFromTable(), PolygonsToLines(), diff --git a/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml b/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml index 69a5f125dc71..6b7f7a880895 100644 --- a/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml +++ b/python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml @@ -522,39 +522,39 @@ tests: name: expected/multiline_boundary.gml type: vector -# - algorithm: qgis:pointonsurface -# name: Point on polygon surface -# params: -# INPUT_LAYER: -# name: polys.gml -# type: vector -# results: -# OUTPUT_LAYER: -# name: expected/point_on_poly.gml -# type: vector -# -# - algorithm: qgis:pointonsurface -# name: Point on multipoint surface -# params: -# INPUT_LAYER: -# name: multipoints.gml -# type: vector -# results: -# OUTPUT_LAYER: -# name: expected/point_on_multipoint.gml -# type: vector -# -# - algorithm: qgis:pointonsurface -# name: Point on line surface -# params: -# INPUT_LAYER: -# name: lines.gml -# type: vector -# results: -# OUTPUT_LAYER: -# name: expected/point_on_line.gml -# type: vector -# + - algorithm: qgis:pointonsurface + name: Point on polygon surface + params: + INPUT_LAYER: + name: polys.gml + type: vector + results: + OUTPUT_LAYER: + name: expected/point_on_poly.gml + type: vector + + - algorithm: qgis:pointonsurface + name: Point on multipoint surface + params: + INPUT_LAYER: + name: multipoints.gml + type: vector + results: + OUTPUT_LAYER: + name: expected/point_on_multipoint.gml + type: vector + + - algorithm: qgis:pointonsurface + name: Point on line surface + params: + INPUT_LAYER: + name: lines.gml + type: vector + results: + OUTPUT_LAYER: + name: expected/point_on_line.gml + type: vector + # - algorithm: qgis:offsetline # name: Offset line positive # params: