Skip to content

Commit 102188a

Browse files
committed
Port PointOnSurface QGIS algorithm
1 parent aec6a79 commit 102188a

File tree

3 files changed

+52
-53
lines changed

3 files changed

+52
-53
lines changed

python/plugins/processing/algs/qgis/PointOnSurface.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@
2727

2828
import os
2929

30-
from qgis.core import QgsWkbTypes, QgsFeatureSink, QgsProcessingUtils
30+
from qgis.core import (QgsWkbTypes,
31+
QgsFeatureSink,
32+
QgsProcessing,
33+
QgsProcessingParameterFeatureSource,
34+
QgsProcessingParameterFeatureSink)
3135

3236
from qgis.PyQt.QtGui import QIcon
3337

3438
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
35-
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
36-
from processing.core.parameters import ParameterVector
37-
from processing.core.outputs import OutputVector
3839
from processing.tools import dataobjects
3940

4041
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
@@ -55,9 +56,9 @@ def __init__(self):
5556
super().__init__()
5657

5758
def initAlgorithm(self, config=None):
58-
self.addParameter(ParameterVector(self.INPUT_LAYER,
59-
self.tr('Input layer')))
60-
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Point'), datatype=[dataobjects.TYPE_VECTOR_POINT]))
59+
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT_LAYER,
60+
self.tr('Input layer')))
61+
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT_LAYER, self.tr('Point'), QgsProcessing.TypeVectorPoint))
6162

6263
def name(self):
6364
return 'pointonsurface'
@@ -66,26 +67,24 @@ def displayName(self):
6667
return self.tr('Point on surface')
6768

6869
def processAlgorithm(self, parameters, context, feedback):
69-
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)
70+
source = self.parameterAsSource(parameters, self.INPUT_LAYER, context)
7071

71-
writer = self.getOutputFromName(
72-
self.OUTPUT_LAYER).getVectorWriter(layer.fields(), QgsWkbTypes.Point, layer.crs(), context)
72+
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT_LAYER, context, source.fields(), QgsWkbTypes.Point, source.sourceCrs())
7373

74-
features = QgsProcessingUtils.getFeatures(layer, context)
75-
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
74+
features = source.getFeatures()
75+
total = 100.0 / source.featureCount() if source.featureCount() else 0
7676

7777
for current, input_feature in enumerate(features):
78+
if feedback.isCanceled():
79+
break
80+
7881
output_feature = input_feature
7982
input_geometry = input_feature.geometry()
8083
if input_geometry:
8184
output_geometry = input_geometry.pointOnSurface()
82-
if not output_geometry:
83-
raise GeoAlgorithmExecutionException(
84-
self.tr('Error calculating point on surface'))
85-
8685
output_feature.setGeometry(output_geometry)
8786

88-
writer.addFeature(output_feature, QgsFeatureSink.FastInsert)
87+
sink.addFeature(output_feature, QgsFeatureSink.FastInsert)
8988
feedback.setProgress(int(current * total))
9089

91-
del writer
90+
return {self.OUTPUT_LAYER: dest_id}

python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
from .Merge import Merge
7272
from .NearestNeighbourAnalysis import NearestNeighbourAnalysis
7373
from .PointDistance import PointDistance
74+
from .PointOnSurface import PointOnSurface
7475
from .PointsInPolygon import PointsInPolygon
7576
from .PointsLayerFromTable import PointsLayerFromTable
7677
from .PolygonsToLines import PolygonsToLines
@@ -144,7 +145,6 @@
144145
# from .RectanglesOvalsDiamondsVariable import RectanglesOvalsDiamondsVariable
145146
# from .RectanglesOvalsDiamondsFixed import RectanglesOvalsDiamondsFixed
146147
# from .MergeLines import MergeLines
147-
# from .PointOnSurface import PointOnSurface
148148
# from .OffsetLine import OffsetLine
149149
# from .Translate import Translate
150150
# from .SingleSidedBuffer import SingleSidedBuffer
@@ -215,7 +215,6 @@ def getAlgs(self):
215215
# ReverseLineDirection(), SpatialIndex(), DefineProjection(),
216216
# RectanglesOvalsDiamondsVariable(),
217217
# RectanglesOvalsDiamondsFixed(), MergeLines(),
218-
# PointOnSurface(),
219218
# OffsetLine(), Translate(),
220219
# SingleSidedBuffer(), PointsAlongGeometry(),
221220
# Slope(), Ruggedness(), Hillshade(),
@@ -264,6 +263,7 @@ def getAlgs(self):
264263
Merge(),
265264
NearestNeighbourAnalysis(),
266265
PointDistance(),
266+
PointOnSurface(),
267267
PointsInPolygon(),
268268
PointsLayerFromTable(),
269269
PolygonsToLines(),

python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -522,39 +522,39 @@ tests:
522522
name: expected/multiline_boundary.gml
523523
type: vector
524524

525-
# - algorithm: qgis:pointonsurface
526-
# name: Point on polygon surface
527-
# params:
528-
# INPUT_LAYER:
529-
# name: polys.gml
530-
# type: vector
531-
# results:
532-
# OUTPUT_LAYER:
533-
# name: expected/point_on_poly.gml
534-
# type: vector
535-
#
536-
# - algorithm: qgis:pointonsurface
537-
# name: Point on multipoint surface
538-
# params:
539-
# INPUT_LAYER:
540-
# name: multipoints.gml
541-
# type: vector
542-
# results:
543-
# OUTPUT_LAYER:
544-
# name: expected/point_on_multipoint.gml
545-
# type: vector
546-
#
547-
# - algorithm: qgis:pointonsurface
548-
# name: Point on line surface
549-
# params:
550-
# INPUT_LAYER:
551-
# name: lines.gml
552-
# type: vector
553-
# results:
554-
# OUTPUT_LAYER:
555-
# name: expected/point_on_line.gml
556-
# type: vector
557-
#
525+
- algorithm: qgis:pointonsurface
526+
name: Point on polygon surface
527+
params:
528+
INPUT_LAYER:
529+
name: polys.gml
530+
type: vector
531+
results:
532+
OUTPUT_LAYER:
533+
name: expected/point_on_poly.gml
534+
type: vector
535+
536+
- algorithm: qgis:pointonsurface
537+
name: Point on multipoint surface
538+
params:
539+
INPUT_LAYER:
540+
name: multipoints.gml
541+
type: vector
542+
results:
543+
OUTPUT_LAYER:
544+
name: expected/point_on_multipoint.gml
545+
type: vector
546+
547+
- algorithm: qgis:pointonsurface
548+
name: Point on line surface
549+
params:
550+
INPUT_LAYER:
551+
name: lines.gml
552+
type: vector
553+
results:
554+
OUTPUT_LAYER:
555+
name: expected/point_on_line.gml
556+
type: vector
557+
558558
# - algorithm: qgis:offsetline
559559
# name: Offset line positive
560560
# params:

0 commit comments

Comments
 (0)