Skip to content

Commit

Permalink
Resurrect Translate algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 20, 2017
1 parent d4af761 commit 4a935c1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 66 deletions.
5 changes: 3 additions & 2 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
from .SpatialiteExecuteSQL import SpatialiteExecuteSQL
from .SumLines import SumLines
from .SymmetricalDifference import SymmetricalDifference
from .Translate import Translate
from .Union import Union
from .UniqueValues import UniqueValues
from .VectorSplit import VectorSplit
Expand Down Expand Up @@ -156,7 +157,6 @@
# from .RectanglesOvalsDiamondsVariable import RectanglesOvalsDiamondsVariable
# from .RectanglesOvalsDiamondsFixed import RectanglesOvalsDiamondsFixed
# from .MergeLines import MergeLines
# from .Translate import Translate
# from .SingleSidedBuffer import SingleSidedBuffer
# from .PointsAlongGeometry import PointsAlongGeometry
# from .Relief import Relief
Expand Down Expand Up @@ -217,7 +217,7 @@ def getAlgs(self):
# SpatialIndex(), DefineProjection(),
# RectanglesOvalsDiamondsVariable(),
# RectanglesOvalsDiamondsFixed(), MergeLines(),
# Translate(),
#
# SingleSidedBuffer(), PointsAlongGeometry(),
# Relief(),
# IdwInterpolation(), TinInterpolation(),
Expand Down Expand Up @@ -292,6 +292,7 @@ def getAlgs(self):
SpatialiteExecuteSQL(),
SumLines(),
SymmetricalDifference(),
Translate(),
Union(),
UniqueValues(),
VectorSplit(),
Expand Down
81 changes: 30 additions & 51 deletions python/plugins/processing/algs/qgis/Translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,13 @@

__revision__ = '$Format:%H$'

import os
from qgis.core import (QgsProcessingException,
QgsProcessingParameterNumber)
from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm

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

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
class Translate(QgisFeatureBasedAlgorithm):


class Translate(QgisAlgorithm):

INPUT_LAYER = 'INPUT_LAYER'
OUTPUT_LAYER = 'OUTPUT_LAYER'
DELTA_X = 'DELTA_X'
DELTA_Y = 'DELTA_Y'

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

def __init__(self):
super().__init__()
self.delta_x = 0
self.delta_y = 0

def initAlgorithm(self, config=None):
self.addParameter(ParameterVector(self.INPUT_LAYER,
self.tr('Input layer')))
self.addParameter(ParameterNumber(self.DELTA_X,
self.tr('Offset distance (x-axis)'), default=1.0))
self.addParameter(ParameterNumber(self.DELTA_Y,
self.tr('Offset distance (y-axis)'), default=0.0))

self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Translated')))
def initParameters(self, config=None):
self.addParameter(QgsProcessingParameterNumber(self.DELTA_X,
self.tr('Offset distance (x-axis)'), QgsProcessingParameterNumber.Double, defaultValue=0.0))
self.addParameter(QgsProcessingParameterNumber(self.DELTA_Y,
self.tr('Offset distance (y-axis)'), QgsProcessingParameterNumber.Double, defaultValue=0.0))

def name(self):
return 'translategeometry'

def displayName(self):
return self.tr('Translate geometry')

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)

delta_x = self.getParameterValue(self.DELTA_X)
delta_y = self.getParameterValue(self.DELTA_Y)

features = QgsProcessingUtils.getFeatures(layer, context)
total = 100.0 / layer.featureCount() if layer.featureCount() else 0

for current, input_feature in enumerate(features):
output_feature = input_feature
input_geometry = input_feature.geometry()
if input_geometry:
output_geometry = input_geometry
output_geometry.translate(delta_x, delta_y)
if not output_geometry:
raise GeoAlgorithmExecutionException(
self.tr('Error translating geometry'))

output_feature.setGeometry(output_geometry)

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

del writer
def outputName(self):
return self.tr('Translated')

def prepareAlgorithm(self, parameters, context, feedback):
self.delta_x = self.parameterAsDouble(parameters, self.DELTA_X, context)
self.delta_y = self.parameterAsDouble(parameters, self.DELTA_Y, context)
return True

def processFeature(self, feature, feedback):
input_geometry = feature.geometry()
if input_geometry:
output_geometry = input_geometry
output_geometry.translate(self.delta_x, self.delta_y)
if not output_geometry:
raise QgsProcessingException(
self.tr('Error translating geometry'))

feature.setGeometry(output_geometry)
return feature
26 changes: 13 additions & 13 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -900,19 +900,19 @@ tests:
geometry:
precision: 7

# - algorithm: qgis:translategeometry
# name: Lines translated
# params:
# DELTA_X: 0.1
# DELTA_Y: -0.2
# INPUT_LAYER:
# name: lines.gml
# type: vector
# results:
# OUTPUT_LAYER:
# name: expected/lines_translated.gml
# type: vector
#
- algorithm: qgis:translategeometry
name: Lines translated
params:
DELTA_X: 0.1
DELTA_Y: -0.2
INPUT:
name: lines.gml
type: vector
results:
OUTPUT:
name: expected/lines_translated.gml
type: vector

# - algorithm: qgis:singlesidedbuffer
# name: Single sided buffer lines (left, round)
# params:
Expand Down

0 comments on commit 4a935c1

Please sign in to comment.