[processing] Make translate algorithm native

And add option to translate z/m values
This commit is contained in:
Nyall Dawson 2017-11-24 16:18:13 +10:00
parent dd223d9b08
commit ff900c0b05
13 changed files with 326 additions and 80 deletions

View File

@ -143,7 +143,6 @@ from .SymmetricalDifference import SymmetricalDifference
from .TextToFloat import TextToFloat
from .TinInterpolation import TinInterpolation
from .TopoColors import TopoColor
from .Translate import Translate
from .TruncateTable import TruncateTable
from .Union import Union
from .UniqueValues import UniqueValues
@ -268,7 +267,6 @@ class QGISAlgorithmProvider(QgsProcessingProvider):
TextToFloat(),
TinInterpolation(),
TopoColor(),
Translate(),
TruncateTable(),
Union(),
UniqueValues(),

View File

@ -1,76 +0,0 @@
# -*- 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$'
from qgis.core import (QgsProcessingException,
QgsProcessingParameterNumber)
from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm
class Translate(QgisFeatureBasedAlgorithm):
DELTA_X = 'DELTA_X'
DELTA_Y = 'DELTA_Y'
def group(self):
return self.tr('Vector geometry')
def __init__(self):
super().__init__()
self.delta_x = 0
self.delta_y = 0
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 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

View File

@ -0,0 +1,32 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>buffer_polys</Name>
<ElementPath>buffer_polys</ElementPath>
<!--POLYGON-->
<GeometryType>3</GeometryType>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>6</FeatureCount>
<ExtentXMin>-1.50000</ExtentXMin>
<ExtentXMax>10.50000</ExtentXMax>
<ExtentYMin>-3.50000</ExtentYMin>
<ExtentYMax>6.50000</ExtentYMax>
</DatasetSpecificInfo>
<PropertyDefn>
<Name>name</Name>
<ElementPath>name</ElementPath>
<Type>String</Type>
<Width>5</Width>
</PropertyDefn>
<PropertyDefn>
<Name>intval</Name>
<ElementPath>intval</ElementPath>
<Type>Integer</Type>
</PropertyDefn>
<PropertyDefn>
<Name>floatval</Name>
<ElementPath>floatval</ElementPath>
<Type>Real</Type>
</PropertyDefn>
</GMLFeatureClass>
</GMLFeatureClassList>

View File

@ -0,0 +1,16 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>gridify_lines</Name>
<ElementPath>gridify_lines</ElementPath>
<!--LINESTRING-->
<GeometryType>2</GeometryType>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>7</FeatureCount>
<ExtentXMin>-2.00000</ExtentXMin>
<ExtentXMax>12.00000</ExtentXMax>
<ExtentYMin>-4.00000</ExtentYMin>
<ExtentYMax>6.00000</ExtentYMax>
</DatasetSpecificInfo>
</GMLFeatureClass>
</GMLFeatureClassList>

View File

@ -0,0 +1,32 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>gridify_polys</Name>
<ElementPath>gridify_polys</ElementPath>
<!--POLYGON-->
<GeometryType>3</GeometryType>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>6</FeatureCount>
<ExtentXMin>-2.00000</ExtentXMin>
<ExtentXMax>10.00000</ExtentXMax>
<ExtentYMin>-4.00000</ExtentYMin>
<ExtentYMax>6.00000</ExtentYMax>
</DatasetSpecificInfo>
<PropertyDefn>
<Name>name</Name>
<ElementPath>name</ElementPath>
<Type>String</Type>
<Width>5</Width>
</PropertyDefn>
<PropertyDefn>
<Name>intval</Name>
<ElementPath>intval</ElementPath>
<Type>Integer</Type>
</PropertyDefn>
<PropertyDefn>
<Name>floatval</Name>
<ElementPath>floatval</ElementPath>
<Type>Real</Type>
</PropertyDefn>
</GMLFeatureClass>
</GMLFeatureClassList>

View File

@ -0,0 +1,16 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>lines_bounds</Name>
<ElementPath>lines_bounds</ElementPath>
<!--POLYGON-->
<GeometryType>3</GeometryType>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>7</FeatureCount>
<ExtentXMin>-1.00000</ExtentXMin>
<ExtentXMax>11.00000</ExtentXMax>
<ExtentYMin>-3.00000</ExtentYMin>
<ExtentYMax>5.00000</ExtentYMax>
</DatasetSpecificInfo>
</GMLFeatureClass>
</GMLFeatureClassList>

View File

@ -0,0 +1,16 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>multiline_offset</Name>
<ElementPath>multiline_offset</ElementPath>
<!--MULTILINESTRING-->
<GeometryType>5</GeometryType>
<SRSName>EPSG:4326</SRSName>
<DatasetSpecificInfo>
<FeatureCount>4</FeatureCount>
<ExtentXMin>-1.00000</ExtentXMin>
<ExtentXMax>6.02404</ExtentXMax>
<ExtentYMin>0.00000</ExtentYMin>
<ExtentYMax>5.11935</ExtentYMax>
</DatasetSpecificInfo>
</GMLFeatureClass>
</GMLFeatureClassList>

View File

@ -0,0 +1,31 @@
<GMLFeatureClassList>
<GMLFeatureClass>
<Name>variable_buffer_points</Name>
<ElementPath>variable_buffer_points</ElementPath>
<!--POLYGON-->
<GeometryType>3</GeometryType>
<SRSName>EPSG:3857</SRSName>
<DatasetSpecificInfo>
<FeatureCount>9</FeatureCount>
<ExtentXMin>-50000.00000</ExtentXMin>
<ExtentXMax>990555.92635</ExtentXMax>
<ExtentYMin>-607305.25727</ExtentYMin>
<ExtentYMax>384111.17140</ExtentYMax>
</DatasetSpecificInfo>
<PropertyDefn>
<Name>id</Name>
<ElementPath>id</ElementPath>
<Type>Integer</Type>
</PropertyDefn>
<PropertyDefn>
<Name>id2</Name>
<ElementPath>id2</ElementPath>
<Type>Integer</Type>
</PropertyDefn>
<PropertyDefn>
<Name>buffer</Name>
<ElementPath>buffer</ElementPath>
<Type>Integer</Type>
</PropertyDefn>
</GMLFeatureClass>
</GMLFeatureClassList>

View File

@ -1087,7 +1087,7 @@ tests:
geometry:
precision: 7
- algorithm: qgis:translategeometry
- algorithm: native:translategeometry
name: Lines translated
params:
DELTA_X: 0.1

View File

@ -59,6 +59,7 @@ SET(QGIS_ANALYSIS_SRCS
processing/qgsalgorithmsubdivide.cpp
processing/qgsalgorithmtransect.cpp
processing/qgsalgorithmtransform.cpp
processing/qgsalgorithmtranslate.cpp
processing/qgsnativealgorithms.cpp

View File

@ -0,0 +1,114 @@
/***************************************************************************
qgsalgorithmtranslate.cpp
---------------------
begin : November 2017
copyright : (C) 2017 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. *
* *
***************************************************************************/
#include "qgsalgorithmtranslate.h"
///@cond PRIVATE
QString QgsTranslateAlgorithm::name() const
{
return QStringLiteral( "translategeometry" );
}
QString QgsTranslateAlgorithm::displayName() const
{
return QObject::tr( "Translate geometry" );
}
QStringList QgsTranslateAlgorithm::tags() const
{
return QObject::tr( "move,shift,transform,z,m,values,add" ).split( ',' );
}
QString QgsTranslateAlgorithm::group() const
{
return QObject::tr( "Vector geometry" );
}
QString QgsTranslateAlgorithm::outputName() const
{
return QObject::tr( "Translated" );
}
QString QgsTranslateAlgorithm::shortHelpString() const
{
return QObject::tr( "This algorithm moves the geometries within a layer, by offsetting them with a specified x and y displacement." )
+ QStringLiteral( "\n\n" )
+ QObject::tr( "Z and M values present in the geometry can also be translated." );
}
QgsTranslateAlgorithm *QgsTranslateAlgorithm::createInstance() const
{
return new QgsTranslateAlgorithm();
}
void QgsTranslateAlgorithm::initParameters( const QVariantMap & )
{
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "DELTA_X" ),
QObject::tr( "Offset distance (x-axis)" ), QgsProcessingParameterNumber::Double,
0.0 ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "DELTA_Y" ),
QObject::tr( "Offset distance (y-axis)" ), QgsProcessingParameterNumber::Double,
0.0 ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "DELTA_Z" ),
QObject::tr( "Offset distance (z-axis)" ), QgsProcessingParameterNumber::Double,
0.0 ) );
addParameter( new QgsProcessingParameterNumber( QStringLiteral( "DELTA_M" ),
QObject::tr( "Offset distance (m values)" ), QgsProcessingParameterNumber::Double,
0.0 ) );
}
bool QgsTranslateAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
mDeltaX = parameterAsDouble( parameters, QStringLiteral( "DELTA_X" ), context );
mDeltaY = parameterAsDouble( parameters, QStringLiteral( "DELTA_Y" ), context );
mDeltaZ = parameterAsDouble( parameters, QStringLiteral( "DELTA_Z" ), context );
mDeltaM = parameterAsDouble( parameters, QStringLiteral( "DELTA_M" ), context );
return true;
}
QgsFeature QgsTranslateAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingFeedback * )
{
QgsFeature f = feature;
if ( f.hasGeometry() )
{
QgsGeometry geometry = f.geometry();
if ( mDeltaZ != 0 && !geometry.constGet()->is3D() )
geometry.get()->addZValue( 0 );
if ( mDeltaM != 0 && !geometry.constGet()->isMeasure() )
geometry.get()->addMValue( 0 );
geometry.translate( mDeltaX, mDeltaY, mDeltaZ, mDeltaM );
f.setGeometry( geometry );
}
return f;
}
QgsWkbTypes::Type QgsTranslateAlgorithm::outputWkbType( QgsWkbTypes::Type inputWkbType ) const
{
QgsWkbTypes::Type wkb = inputWkbType;
if ( mDeltaZ != 0 )
wkb = QgsWkbTypes::addZ( wkb );
if ( mDeltaM != 0 )
wkb = QgsWkbTypes::addM( wkb );
return wkb;
}
///@endcond

View File

@ -0,0 +1,65 @@
/***************************************************************************
qgsalgorithmtranslate.h
---------------------
begin : November 2017
copyright : (C) 2017 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. *
* *
***************************************************************************/
#ifndef QGSALGORITHMTRANSLATE_H
#define QGSALGORITHMTRANSLATE_H
#define SIP_NO_FILE
#include "qgis.h"
#include "qgsprocessingalgorithm.h"
///@cond PRIVATE
/**
* Native translate algorithm.
*/
class QgsTranslateAlgorithm : public QgsProcessingFeatureBasedAlgorithm
{
public:
QgsTranslateAlgorithm() = default;
QString name() const override;
QString displayName() const override;
virtual QStringList tags() const override;
QString group() const override;
QString shortHelpString() const override;
QgsTranslateAlgorithm *createInstance() const override SIP_FACTORY;
void initParameters( const QVariantMap &configuration = QVariantMap() ) override;
protected:
QString outputName() const override;
bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QgsFeature processFeature( const QgsFeature &feature, QgsProcessingFeedback *feedback ) override;
QgsWkbTypes::Type outputWkbType( QgsWkbTypes::Type inputWkbType ) const override;
private:
double mDeltaX = 0.0;
double mDeltaY = 0.0;
double mDeltaZ = 0.0;
double mDeltaM = 0.0;
};
///@endcond PRIVATE
#endif // QGSALGORITHMTRANSLATE_H

View File

@ -56,6 +56,7 @@
#include "qgsalgorithmsubdivide.h"
#include "qgsalgorithmtransect.h"
#include "qgsalgorithmtransform.h"
#include "qgsalgorithmtranslate.h"
///@cond PRIVATE
@ -133,7 +134,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
addAlgorithm( new QgsSubdivideAlgorithm() );
addAlgorithm( new QgsTransectAlgorithm() );
addAlgorithm( new QgsTransformAlgorithm() );
addAlgorithm( new QgsTranslateAlgorithm() );
}