Skip to content
Permalink
Browse files

Port points along line to new API

  • Loading branch information
nyalldawson committed Jul 28, 2017
1 parent becf690 commit 2a6f51218ab3bfd92478829f2d0fa20478e96b98
@@ -31,12 +31,17 @@
from qgis.PyQt.QtCore import QVariant
from qgis.PyQt.QtGui import QIcon

from qgis.core import QgsFeature, QgsFeatureSink, QgsWkbTypes, QgsField, QgsProcessingUtils
from qgis.core import (QgsFeature,
QgsFeatureSink,
QgsWkbTypes,
QgsField,
QgsProcessing,
QgsProcessingUtils,
QgsProcessingParameterNumber,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterVector, ParameterNumber
from processing.core.outputs import OutputVector
from processing.tools import dataobjects

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]

@@ -62,16 +67,16 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(ParameterVector(self.INPUT,
self.tr('Input layer'),
[dataobjects.TYPE_VECTOR_POLYGON, dataobjects.TYPE_VECTOR_LINE]))
self.addParameter(ParameterNumber(self.DISTANCE,
self.tr('Distance'), default=1.0))
self.addParameter(ParameterNumber(self.START_OFFSET,
self.tr('Start offset'), default=0.0))
self.addParameter(ParameterNumber(self.END_OFFSET,
self.tr('End offset'), default=0.0))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Points'), datatype=[dataobjects.TYPE_VECTOR_POINT]))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Input layer'), [QgsProcessing.TypeVectorPolygon, QgsProcessing.TypeVectorLine]))
self.addParameter(QgsProcessingParameterNumber(self.DISTANCE,
self.tr('Distance'), minValue=0.0, defaultValue=1.0))
self.addParameter(QgsProcessingParameterNumber(self.START_OFFSET,
self.tr('Start offset'), minValue=0.0, defaultValue=0.0))
self.addParameter(QgsProcessingParameterNumber(self.END_OFFSET,
self.tr('End offset'), minValue=0.0, defaultValue=0.0))

self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Points'), QgsProcessing.TypeVectorPoint))

def name(self):
return 'pointsalonglines'
@@ -80,23 +85,27 @@ def displayName(self):
return self.tr('Points along lines')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
distance = self.getParameterValue(self.DISTANCE)
start_offset = self.getParameterValue(self.START_OFFSET)
end_offset = self.getParameterValue(self.END_OFFSET)
source = self.parameterAsSource(parameters, self.INPUT, context)
distance = self.parameterAsDouble(parameters, self.DISTANCE, context)
start_offset = self.parameterAsDouble(parameters, self.START_OFFSET, context)
end_offset = self.parameterAsDouble(parameters, self.END_OFFSET, context)

fields = layer.fields()
fields = source.fields()
fields.append(QgsField('distance', QVariant.Double))
fields.append(QgsField('angle', QVariant.Double))

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, QgsWkbTypes.Point, layer.crs(), context)
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
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

input_geometry = input_feature.geometry()
if not input_geometry:
writer.addFeature(input_feature, QgsFeatureSink.FastInsert)
sink.addFeature(input_feature, QgsFeatureSink.FastInsert)
else:
if input_geometry.type == QgsWkbTypes.PolygonGeometry:
length = input_geometry.geometry().perimeter()
@@ -114,10 +123,10 @@ def processAlgorithm(self, parameters, context, feedback):
attrs.append(current_distance)
attrs.append(angle)
output_feature.setAttributes(attrs)
writer.addFeature(output_feature, QgsFeatureSink.FastInsert)
sink.addFeature(output_feature, QgsFeatureSink.FastInsert)

current_distance += distance

feedback.setProgress(int(current * total))

del writer
return {self.OUTPUT: dest_id}
@@ -84,6 +84,7 @@
from .Orthogonalize import Orthogonalize
from .PointDistance import PointDistance
from .PointOnSurface import PointOnSurface
from .PointsAlongGeometry import PointsAlongGeometry
from .PointsInPolygon import PointsInPolygon
from .PointsLayerFromTable import PointsLayerFromTable
from .PoleOfInaccessibility import PoleOfInaccessibility
@@ -162,7 +163,6 @@
# from .DefineProjection import DefineProjection
# from .RectanglesOvalsDiamondsVariable import RectanglesOvalsDiamondsVariable
# from .RectanglesOvalsDiamondsFixed import RectanglesOvalsDiamondsFixed
# from .PointsAlongGeometry import PointsAlongGeometry
# from .Relief import Relief
# from .IdwInterpolation import IdwInterpolation
# from .TinInterpolation import TinInterpolation
@@ -208,7 +208,6 @@ def getAlgs(self):
# DefineProjection(),
# RectanglesOvalsDiamondsVariable(),
# RectanglesOvalsDiamondsFixed(),
# PointsAlongGeometry(),
# Relief(),
# IdwInterpolation(), TinInterpolation(),
# RasterCalculator(),
@@ -259,6 +258,7 @@ def getAlgs(self):
Orthogonalize(),
PointDistance(),
PointOnSurface(),
PointsAlongGeometry(),
PointsInPolygon(),
PointsLayerFromTable(),
PoleOfInaccessibility(),
@@ -2338,19 +2338,19 @@ tests:
name: expected/count_points_weighted.gml
type: vector

# - algorithm: qgis:pointsalonglines
# name: standard points alog lines
# params:
# DISTANCE: 1.0
# END_OFFSET: 0.0
# INPUT:
# name: lines.gml
# type: vector
# START_OFFSET: 0.0
# results:
# OUTPUT:
# name: expected/points_alog_lines.gml
# type: vector
- algorithm: qgis:pointsalonglines
name: standard points alog lines
params:
DISTANCE: 1.0
END_OFFSET: 0.0
INPUT:
name: lines.gml
type: vector
START_OFFSET: 0.0
results:
OUTPUT:
name: expected/points_alog_lines.gml
type: vector

- algorithm: qgis:meancoordinates
name: standard mean coordinates

0 comments on commit 2a6f512

Please sign in to comment.
You can’t perform that action at this time.