Skip to content

Commit

Permalink
Port extend lines to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 20, 2017
1 parent 1cac3bb commit c0669d4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 75 deletions.
69 changes: 26 additions & 43 deletions python/plugins/processing/algs/qgis/ExtendLines.py
Expand Up @@ -25,20 +25,13 @@


__revision__ = '$Format:%H$' __revision__ = '$Format:%H$'


from qgis.core import (QgsApplication, from qgis.core import (QgsProcessingParameterNumber,
QgsFeatureSink, QgsProcessingException)
QgsProcessingUtils) from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm
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




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


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


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


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


def initAlgorithm(self, config=None): def initParameters(self, config=None):
self.addParameter(ParameterVector(self.INPUT_LAYER, self.addParameter(QgsProcessingParameterNumber(self.START_DISTANCE,
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_LINE])) self.tr('Start distance'), defaultValue=0.0))
self.addParameter(ParameterNumber(self.START_DISTANCE, self.addParameter(QgsProcessingParameterNumber(self.END_DISTANCE,
self.tr('Start distance'), default=0.0)) self.tr('End distance'), defaultValue=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 name(self): def name(self):
return 'extendlines' return 'extendlines'


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


def processAlgorithm(self, parameters, context, feedback): def outputName(self):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context) return self.tr('Extended')

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


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


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) feature.setGeometry(output_geometry)
feedback.setProgress(int(current * total))


del writer return feature
6 changes: 3 additions & 3 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Expand Up @@ -57,6 +57,7 @@
from .Difference import Difference from .Difference import Difference
from .DropGeometry import DropGeometry from .DropGeometry import DropGeometry
from .DropMZValues import DropMZValues from .DropMZValues import DropMZValues
from .ExtendLines import ExtendLines
from .ExtentFromLayer import ExtentFromLayer from .ExtentFromLayer import ExtentFromLayer
from .ExtractNodes import ExtractNodes from .ExtractNodes import ExtractNodes
from .FixGeometry import FixGeometry from .FixGeometry import FixGeometry
Expand Down Expand Up @@ -162,7 +163,6 @@
# from .Relief import Relief # from .Relief import Relief
# from .IdwInterpolation import IdwInterpolation # from .IdwInterpolation import IdwInterpolation
# from .TinInterpolation import TinInterpolation # from .TinInterpolation import TinInterpolation
# from .ExtendLines import ExtendLines
# from .ExtractSpecificNodes import ExtractSpecificNodes # from .ExtractSpecificNodes import ExtractSpecificNodes
# from .GeometryByExpression import GeometryByExpression # from .GeometryByExpression import GeometryByExpression
# from .RasterCalculator import RasterCalculator # from .RasterCalculator import RasterCalculator
Expand Down Expand Up @@ -217,11 +217,10 @@ def getAlgs(self):
# SpatialIndex(), DefineProjection(), # SpatialIndex(), DefineProjection(),
# RectanglesOvalsDiamondsVariable(), # RectanglesOvalsDiamondsVariable(),
# RectanglesOvalsDiamondsFixed(), MergeLines(), # RectanglesOvalsDiamondsFixed(), MergeLines(),
#
# PointsAlongGeometry(), # PointsAlongGeometry(),
# Relief(), # Relief(),
# IdwInterpolation(), TinInterpolation(), # IdwInterpolation(), TinInterpolation(),
# ExtendLines(), ExtractSpecificNodes(), # ExtractSpecificNodes(),
# GeometryByExpression(), # GeometryByExpression(),
# RasterCalculator(), # RasterCalculator(),
# ShortestPathPointToPoint(), ShortestPathPointToLayer(), # ShortestPathPointToPoint(), ShortestPathPointToLayer(),
Expand All @@ -247,6 +246,7 @@ def getAlgs(self):
Difference(), Difference(),
DropGeometry(), DropGeometry(),
DropMZValues(), DropMZValues(),
ExtendLines(),
ExtentFromLayer(), ExtentFromLayer(),
ExtractNodes(), ExtractNodes(),
FixGeometry(), FixGeometry(),
Expand Down
58 changes: 29 additions & 29 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Expand Up @@ -1346,7 +1346,7 @@ tests:
OUTPUT: OUTPUT:
name: expected/remove_null_polys.gml name: expected/remove_null_polys.gml
type: vector type: vector
#
- algorithm: native:extractbyexpression - algorithm: native:extractbyexpression
name: Extract by Expression name: Extract by Expression
params: params:
Expand All @@ -1359,34 +1359,34 @@ tests:
name: expected/extract_expression.gml name: expected/extract_expression.gml
type: vector type: vector


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

0 comments on commit c0669d4

Please sign in to comment.