Skip to content

Commit

Permalink
Port PointOnSurface QGIS algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Jul 16, 2017
1 parent aec6a79 commit 102188a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 53 deletions.
35 changes: 17 additions & 18 deletions python/plugins/processing/algs/qgis/PointOnSurface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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'
Expand All @@ -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}
4 changes: 2 additions & 2 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -215,7 +215,6 @@ def getAlgs(self):
# ReverseLineDirection(), SpatialIndex(), DefineProjection(),
# RectanglesOvalsDiamondsVariable(),
# RectanglesOvalsDiamondsFixed(), MergeLines(),
# PointOnSurface(),
# OffsetLine(), Translate(),
# SingleSidedBuffer(), PointsAlongGeometry(),
# Slope(), Ruggedness(), Hillshade(),
Expand Down Expand Up @@ -264,6 +263,7 @@ def getAlgs(self):
Merge(),
NearestNeighbourAnalysis(),
PointDistance(),
PointOnSurface(),
PointsInPolygon(),
PointsLayerFromTable(),
PolygonsToLines(),
Expand Down
66 changes: 33 additions & 33 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 102188a

Please sign in to comment.