-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3393 from nyalldawson/processing_translate
[FEATURE][processing] New algorithm for translating (moving) points
- Loading branch information
Showing
6 changed files
with
175 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
*************************************************************************** | ||
Translate.py | ||
-------------- | ||
Date : August 2016 | ||
Copyright : (C) 2016 by Nyall Dawson | ||
Email : nyall dot dawson at gmail dot com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
*************************************************************************** | ||
""" | ||
|
||
__author__ = 'Nyall Dawson' | ||
__date__ = 'August 2016' | ||
__copyright__ = '(C) 2016, Nyall Dawson' | ||
|
||
# This will get replaced with a git SHA1 when you do a git archive323 | ||
|
||
__revision__ = '$Format:%H$' | ||
|
||
import os | ||
|
||
from qgis.core import QgsGeometry, QgsWkbTypes | ||
|
||
from qgis.PyQt.QtGui import QIcon | ||
|
||
from processing.core.GeoAlgorithm import GeoAlgorithm | ||
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException | ||
from processing.core.parameters import ParameterVector, ParameterSelection, ParameterNumber | ||
from processing.core.outputs import OutputVector | ||
from processing.tools import dataobjects, vector | ||
|
||
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0] | ||
|
||
|
||
class Translate(GeoAlgorithm): | ||
|
||
INPUT_LAYER = 'INPUT_LAYER' | ||
OUTPUT_LAYER = 'OUTPUT_LAYER' | ||
DELTA_X = 'DELTA_X' | ||
DELTA_Y = 'DELTA_Y' | ||
|
||
def defineCharacteristics(self): | ||
self.name, self.i18n_name = self.trAlgorithm('Translate geometry') | ||
self.group, self.i18n_group = self.trAlgorithm('Vector geometry tools') | ||
|
||
self.addParameter(ParameterVector(self.INPUT_LAYER, | ||
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_ANY])) | ||
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 processAlgorithm(self, progress): | ||
layer = dataobjects.getObjectFromUri( | ||
self.getParameterValue(self.INPUT_LAYER)) | ||
|
||
writer = self.getOutputFromName( | ||
self.OUTPUT_LAYER).getVectorWriter( | ||
layer.fields().toList(), | ||
layer.wkbType(), | ||
layer.crs()) | ||
|
||
delta_x = self.getParameterValue(self.DELTA_X) | ||
delta_y = self.getParameterValue(self.DELTA_Y) | ||
|
||
features = vector.features(layer) | ||
total = 100.0 / len(features) | ||
|
||
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) | ||
progress.setPercentage(int(current * total)) | ||
|
||
del writer |
15 changes: 15 additions & 0 deletions
15
python/plugins/processing/tests/testdata/expected/lines_translated.gfs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<GMLFeatureClassList> | ||
<GMLFeatureClass> | ||
<Name>lines_translated</Name> | ||
<ElementPath>lines_translated</ElementPath> | ||
<GeometryType>2</GeometryType> | ||
<SRSName>EPSG:4326</SRSName> | ||
<DatasetSpecificInfo> | ||
<FeatureCount>7</FeatureCount> | ||
<ExtentXMin>-0.90000</ExtentXMin> | ||
<ExtentXMax>11.10000</ExtentXMax> | ||
<ExtentYMin>-3.20000</ExtentYMin> | ||
<ExtentYMax>4.80000</ExtentYMax> | ||
</DatasetSpecificInfo> | ||
</GMLFeatureClass> | ||
</GMLFeatureClassList> |
48 changes: 48 additions & 0 deletions
48
python/plugins/processing/tests/testdata/expected/lines_translated.gml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<ogr:FeatureCollection | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="" | ||
xmlns:ogr="http://ogr.maptools.org/" | ||
xmlns:gml="http://www.opengis.net/gml"> | ||
<gml:boundedBy> | ||
<gml:Box> | ||
<gml:coord><gml:X>-0.9</gml:X><gml:Y>-3.2</gml:Y></gml:coord> | ||
<gml:coord><gml:X>11.1</gml:X><gml:Y>4.8</gml:Y></gml:coord> | ||
</gml:Box> | ||
</gml:boundedBy> | ||
|
||
<gml:featureMember> | ||
<ogr:lines_translated fid="lines.0"> | ||
<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> | ||
</ogr:lines_translated> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:lines_translated fid="lines.1"> | ||
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>-0.9,-1.2 1.1,-1.2</gml:coordinates></gml:LineString></ogr:geometryProperty> | ||
</ogr:lines_translated> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:lines_translated fid="lines.2"> | ||
<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> | ||
</ogr:lines_translated> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:lines_translated fid="lines.3"> | ||
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>3.1,0.8 5.1,0.8</gml:coordinates></gml:LineString></ogr:geometryProperty> | ||
</ogr:lines_translated> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:lines_translated fid="lines.4"> | ||
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>7.1,-3.2 10.1,-3.2</gml:coordinates></gml:LineString></ogr:geometryProperty> | ||
</ogr:lines_translated> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:lines_translated fid="lines.5"> | ||
<ogr:geometryProperty><gml:LineString srsName="EPSG:4326"><gml:coordinates>6.1,-3.2 10.1,0.8</gml:coordinates></gml:LineString></ogr:geometryProperty> | ||
</ogr:lines_translated> | ||
</gml:featureMember> | ||
<gml:featureMember> | ||
<ogr:lines_translated fid="lines.6"> | ||
</ogr:lines_translated> | ||
</gml:featureMember> | ||
</ogr:FeatureCollection> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters