Skip to content
Permalink
Browse files

Port extend lines to new API

  • Loading branch information
nyalldawson committed Jul 20, 2017
1 parent 1cac3bb commit c0669d4fd24a1a2d64270285dd3ba5eef70c8326
@@ -25,20 +25,13 @@

__revision__ = '$Format:%H$'

from qgis.core import (QgsApplication,
QgsFeatureSink,
QgsProcessingUtils)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector, ParameterNumber
from processing.core.outputs import OutputVector
from processing.tools import dataobjects
from qgis.core import (QgsProcessingParameterNumber,
QgsProcessingException)
from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm


class ExtendLines(QgisAlgorithm):
class ExtendLines(QgisFeatureBasedAlgorithm):

INPUT_LAYER = 'INPUT_LAYER'
OUTPUT_LAYER = 'OUTPUT_LAYER'
START_DISTANCE = 'START_DISTANCE'
END_DISTANCE = 'END_DISTANCE'

@@ -47,47 +40,37 @@ def group(self):

def __init__(self):
super().__init__()
self.start_distance = None
self.end_distance = None

def initAlgorithm(self, config=None):
self.addParameter(ParameterVector(self.INPUT_LAYER,
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_LINE]))
self.addParameter(ParameterNumber(self.START_DISTANCE,
self.tr('Start distance'), default=0.0))
self.addParameter(ParameterNumber(self.END_DISTANCE,
self.tr('End distance'), default=0.0))

self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Extended lines')))
def initParameters(self, config=None):
self.addParameter(QgsProcessingParameterNumber(self.START_DISTANCE,
self.tr('Start distance'), defaultValue=0.0))
self.addParameter(QgsProcessingParameterNumber(self.END_DISTANCE,
self.tr('End distance'), defaultValue=0.0))

def name(self):
return 'extendlines'

def displayName(self):
return self.tr('Extend lines')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)

writer = self.getOutputFromName(
self.OUTPUT_LAYER).getVectorWriter(layer.fields(), layer.wkbType(), layer.crs(), context)

start_distance = self.getParameterValue(self.START_DISTANCE)
end_distance = self.getParameterValue(self.END_DISTANCE)

features = QgsProcessingUtils.getFeatures(layer, context)
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
def outputName(self):
return self.tr('Extended')

for current, input_feature in enumerate(features):
output_feature = input_feature
input_geometry = input_feature.geometry()
if input_geometry:
output_geometry = input_geometry.extendLine(start_distance, end_distance)
if not output_geometry:
raise GeoAlgorithmExecutionException(
self.tr('Error calculating extended line'))
def prepareAlgorithm(self, parameters, context, feedback):
self.start_distance = self.parameterAsDouble(parameters, self.START_DISTANCE, context)
self.end_distance = self.parameterAsDouble(parameters, self.END_DISTANCE, context)
return True

output_feature.setGeometry(output_geometry)
def processFeature(self, feature, feedback):
input_geometry = feature.geometry()
if input_geometry:
output_geometry = input_geometry.extendLine(self.start_distance, self.end_distance)
if not output_geometry:
raise QgsProcessingException(
self.tr('Error calculating extended line'))

writer.addFeature(output_feature, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))
feature.setGeometry(output_geometry)

del writer
return feature
@@ -57,6 +57,7 @@
from .Difference import Difference
from .DropGeometry import DropGeometry
from .DropMZValues import DropMZValues
from .ExtendLines import ExtendLines
from .ExtentFromLayer import ExtentFromLayer
from .ExtractNodes import ExtractNodes
from .FixGeometry import FixGeometry
@@ -162,7 +163,6 @@
# from .Relief import Relief
# from .IdwInterpolation import IdwInterpolation
# from .TinInterpolation import TinInterpolation
# from .ExtendLines import ExtendLines
# from .ExtractSpecificNodes import ExtractSpecificNodes
# from .GeometryByExpression import GeometryByExpression
# from .RasterCalculator import RasterCalculator
@@ -217,11 +217,10 @@ def getAlgs(self):
# SpatialIndex(), DefineProjection(),
# RectanglesOvalsDiamondsVariable(),
# RectanglesOvalsDiamondsFixed(), MergeLines(),
#
# PointsAlongGeometry(),
# Relief(),
# IdwInterpolation(), TinInterpolation(),
# ExtendLines(), ExtractSpecificNodes(),
# ExtractSpecificNodes(),
# GeometryByExpression(),
# RasterCalculator(),
# ShortestPathPointToPoint(), ShortestPathPointToLayer(),
@@ -247,6 +246,7 @@ def getAlgs(self):
Difference(),
DropGeometry(),
DropMZValues(),
ExtendLines(),
ExtentFromLayer(),
ExtractNodes(),
FixGeometry(),
@@ -1346,7 +1346,7 @@ tests:
OUTPUT:
name: expected/remove_null_polys.gml
type: vector
#

- algorithm: native:extractbyexpression
name: Extract by Expression
params:
@@ -1359,34 +1359,34 @@ tests:
name: expected/extract_expression.gml
type: vector

# - algorithm: qgis:extendlines
# name: Extend lines
# params:
# END_DISTANCE: 0.2
# INPUT_LAYER:
# name: lines.gml
# type: vector
# START_DISTANCE: 0.1
# results:
# OUTPUT_LAYER:
# name: expected/extend_lines.gml
# type: vector
# compare:
# geometry:
# precision: 7
# - algorithm: qgis:extendlines
# name: Extend multilines
# params:
# END_DISTANCE: 0.4
# INPUT_LAYER:
# name: multilines.gml
# type: vector
# START_DISTANCE: 0.2
# results:
# OUTPUT_LAYER:
# name: expected/extend_multilines.gml
# type: vector
#
- algorithm: qgis:extendlines
name: Extend lines
params:
END_DISTANCE: 0.2
INPUT:
name: lines.gml
type: vector
START_DISTANCE: 0.1
results:
OUTPUT:
name: expected/extend_lines.gml
type: vector
compare:
geometry:
precision: 7
- algorithm: qgis:extendlines
name: Extend multilines
params:
END_DISTANCE: 0.4
INPUT:
name: multilines.gml
type: vector
START_DISTANCE: 0.2
results:
OUTPUT:
name: expected/extend_multilines.gml
type: vector

# - algorithm: qgis:extractspecificnodes
# name: Extract specific nodes lines
# params:

0 comments on commit c0669d4

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