Skip to content

Commit 0f268bf

Browse files
committedAug 14, 2016
[FEATURE][processing] New algorithm for translating (moving) points
Allows geometries to be shifted by a x/y displacement
1 parent a064c0a commit 0f268bf

File tree

6 files changed

+175
-1
lines changed

6 files changed

+175
-1
lines changed
 

‎python/plugins/processing/algs/help/qgis.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,9 @@ qgis:symmetricaldifference: >
452452
qgis:texttofloat: >
453453
This algorithm modifies the type of a given attribute in a vector layer, converting a text attribute containing numeric strings into a numeric attribute.
454454

455+
qgis:translate: >
456+
This algorithm moves the geometries within a layer, by offsetting them with a specified x and y displacement.
457+
455458
qgis:union: >
456459
This algorithm creates a layer containing all the features from both input layers. In the case of polygon layers, separate features are created for overlapping and non-overlapping features. The attribute table of the union layer contains attribute values from the respective input layer for non-overlapping features, and attribute values from both input layers for overlapping features.
457460

‎python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
from .PointOnSurface import PointOnSurface
153153
from .OffsetLine import OffsetLine
154154
from .PolygonCentroids import PolygonCentroids
155+
from .Translate import Translate
155156

156157
pluginPath = os.path.normpath(os.path.join(
157158
os.path.split(os.path.dirname(__file__))[0], os.pardir))
@@ -205,7 +206,8 @@ def __init__(self):
205206
RectanglesOvalsDiamondsVariable(),
206207
RectanglesOvalsDiamondsFixed(), MergeLines(),
207208
BoundingBox(), Boundary(), PointOnSurface(),
208-
OffsetLine(), PolygonCentroids()
209+
OffsetLine(), PolygonCentroids(),
210+
Translate()
209211
]
210212

211213
if hasMatplotlib:
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
Translate.py
6+
--------------
7+
Date : August 2016
8+
Copyright : (C) 2016 by Nyall Dawson
9+
Email : nyall dot dawson at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Nyall Dawson'
21+
__date__ = 'August 2016'
22+
__copyright__ = '(C) 2016, Nyall Dawson'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive323
25+
26+
__revision__ = '$Format:%H$'
27+
28+
import os
29+
30+
from qgis.core import QgsGeometry, QgsWkbTypes
31+
32+
from qgis.PyQt.QtGui import QIcon
33+
34+
from processing.core.GeoAlgorithm import GeoAlgorithm
35+
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
36+
from processing.core.parameters import ParameterVector, ParameterSelection, ParameterNumber
37+
from processing.core.outputs import OutputVector
38+
from processing.tools import dataobjects, vector
39+
40+
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
41+
42+
43+
class Translate(GeoAlgorithm):
44+
45+
INPUT_LAYER = 'INPUT_LAYER'
46+
OUTPUT_LAYER = 'OUTPUT_LAYER'
47+
DELTA_X = 'DELTA_X'
48+
DELTA_Y = 'DELTA_Y'
49+
50+
def defineCharacteristics(self):
51+
self.name, self.i18n_name = self.trAlgorithm('Translate geometry')
52+
self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools')
53+
54+
self.addParameter(ParameterVector(self.INPUT_LAYER,
55+
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_ANY]))
56+
self.addParameter(ParameterNumber(self.DELTA_X,
57+
self.tr('Offset distance (x-axis)'), default=1.0))
58+
self.addParameter(ParameterNumber(self.DELTA_Y,
59+
self.tr('Offset distance (y-axis)'), default=0.0))
60+
61+
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Translated')))
62+
63+
def processAlgorithm(self, progress):
64+
layer = dataobjects.getObjectFromUri(
65+
self.getParameterValue(self.INPUT_LAYER))
66+
67+
writer = self.getOutputFromName(
68+
self.OUTPUT_LAYER).getVectorWriter(
69+
layer.fields().toList(),
70+
layer.wkbType(),
71+
layer.crs())
72+
73+
delta_x = self.getParameterValue(self.DELTA_X)
74+
delta_y = self.getParameterValue(self.DELTA_Y)
75+
76+
features = vector.features(layer)
77+
total = 100.0 / len(features)
78+
79+
for current, input_feature in enumerate(features):
80+
output_feature = input_feature
81+
input_geometry = input_feature.geometry()
82+
if input_geometry:
83+
output_geometry = input_geometry
84+
output_geometry.translate(delta_x, delta_y)
85+
if not output_geometry:
86+
raise GeoAlgorithmExecutionException(
87+
self.tr('Error translating geometry'))
88+
89+
output_feature.setGeometry(output_geometry)
90+
91+
writer.addFeature(output_feature)
92+
progress.setPercentage(int(current * total))
93+
94+
del writer
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<GMLFeatureClassList>
2+
<GMLFeatureClass>
3+
<Name>lines_translated</Name>
4+
<ElementPath>lines_translated</ElementPath>
5+
<GeometryType>2</GeometryType>
6+
<SRSName>EPSG:4326</SRSName>
7+
<DatasetSpecificInfo>
8+
<FeatureCount>7</FeatureCount>
9+
<ExtentXMin>-0.90000</ExtentXMin>
10+
<ExtentXMax>11.10000</ExtentXMax>
11+
<ExtentYMin>-3.20000</ExtentYMin>
12+
<ExtentYMax>4.80000</ExtentYMax>
13+
</DatasetSpecificInfo>
14+
</GMLFeatureClass>
15+
</GMLFeatureClassList>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ogr:FeatureCollection
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation=""
5+
xmlns:ogr="http://ogr.maptools.org/"
6+
xmlns:gml="http://www.opengis.net/gml">
7+
<gml:boundedBy>
8+
<gml:Box>
9+
<gml:coord><gml:X>-0.9</gml:X><gml:Y>-3.2</gml:Y></gml:coord>
10+
<gml:coord><gml:X>11.1</gml:X><gml:Y>4.8</gml:Y></gml:coord>
11+
</gml:Box>
12+
</gml:boundedBy>
13+
14+
<gml:featureMember>
15+
<ogr:lines_translated fid="lines.0">
16+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>6.1,1.8 9.1,1.8 9.1,2.8 11.1,4.8</gml:coordinates></gml:LineString></ogr:geometryProperty>
17+
</ogr:lines_translated>
18+
</gml:featureMember>
19+
<gml:featureMember>
20+
<ogr:lines_translated fid="lines.1">
21+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>-0.9,-1.2 1.1,-1.2</gml:coordinates></gml:LineString></ogr:geometryProperty>
22+
</ogr:lines_translated>
23+
</gml:featureMember>
24+
<gml:featureMember>
25+
<ogr:lines_translated fid="lines.2">
26+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>2.1,-0.2 2.1,1.8 3.1,1.8 3.1,2.8</gml:coordinates></gml:LineString></ogr:geometryProperty>
27+
</ogr:lines_translated>
28+
</gml:featureMember>
29+
<gml:featureMember>
30+
<ogr:lines_translated fid="lines.3">
31+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>3.1,0.8 5.1,0.8</gml:coordinates></gml:LineString></ogr:geometryProperty>
32+
</ogr:lines_translated>
33+
</gml:featureMember>
34+
<gml:featureMember>
35+
<ogr:lines_translated fid="lines.4">
36+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>7.1,-3.2 10.1,-3.2</gml:coordinates></gml:LineString></ogr:geometryProperty>
37+
</ogr:lines_translated>
38+
</gml:featureMember>
39+
<gml:featureMember>
40+
<ogr:lines_translated fid="lines.5">
41+
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>6.1,-3.2 10.1,0.8</gml:coordinates></gml:LineString></ogr:geometryProperty>
42+
</ogr:lines_translated>
43+
</gml:featureMember>
44+
<gml:featureMember>
45+
<ogr:lines_translated fid="lines.6">
46+
</ogr:lines_translated>
47+
</gml:featureMember>
48+
</ogr:FeatureCollection>

‎python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,3 +809,15 @@ tests:
809809
name: expected/centroid_polys.gml
810810
type: vector
811811

812+
- algorithm: qgis:translategeometry
813+
name: Lines translated
814+
params:
815+
DELTA_X: 0.1
816+
DELTA_Y: -0.2
817+
INPUT_LAYER:
818+
name: lines.gml
819+
type: vector
820+
results:
821+
OUTPUT_LAYER:
822+
name: expected/lines_translated.gml
823+
type: vector

0 commit comments

Comments
 (0)
Please sign in to comment.