QGIS API Documentation  3.0.2-Girona (307d082)
qgsalgorithmtransform.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmtransform.cpp
3  ---------------------
4  begin : April 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsalgorithmtransform.h"
19 
21 
22 
23 void QgsTransformAlgorithm::initParameters( const QVariantMap & )
24 {
25  addParameter( new QgsProcessingParameterCrs( QStringLiteral( "TARGET_CRS" ), QObject::tr( "Target CRS" ), QStringLiteral( "EPSG:4326" ) ) );
26 }
27 
28 QgsCoordinateReferenceSystem QgsTransformAlgorithm::outputCrs( const QgsCoordinateReferenceSystem & ) const
29 {
30  return mDestCrs;
31 }
32 
33 QString QgsTransformAlgorithm::outputName() const
34 {
35  return QObject::tr( "Reprojected" );
36 }
37 
38 QString QgsTransformAlgorithm::name() const
39 {
40  return QStringLiteral( "reprojectlayer" );
41 }
42 
43 QString QgsTransformAlgorithm::displayName() const
44 {
45  return QObject::tr( "Reproject layer" );
46 }
47 
48 QStringList QgsTransformAlgorithm::tags() const
49 {
50  return QObject::tr( "transform,reproject,crs,srs,warp" ).split( ',' );
51 }
52 
53 QString QgsTransformAlgorithm::group() const
54 {
55  return QObject::tr( "Vector general" );
56 }
57 
58 QString QgsTransformAlgorithm::groupId() const
59 {
60  return QStringLiteral( "vectorgeneral" );
61 }
62 
63 QString QgsTransformAlgorithm::shortHelpString() const
64 {
65  return QObject::tr( "This algorithm reprojects a vector layer. It creates a new layer with the same features "
66  "as the input one, but with geometries reprojected to a new CRS.\n\n"
67  "Attributes are not modified by this algorithm." );
68 }
69 
70 QgsTransformAlgorithm *QgsTransformAlgorithm::createInstance() const
71 {
72  return new QgsTransformAlgorithm();
73 }
74 
75 bool QgsTransformAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
76 {
77  mDestCrs = parameterAsCrs( parameters, QStringLiteral( "TARGET_CRS" ), context );
78  mTransformContext = context.project() ? context.project()->transformContext() : QgsCoordinateTransformContext();
79  return true;
80 }
81 
82 QgsFeatureList QgsTransformAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback * )
83 {
84  QgsFeature feature = f;
85  if ( !mCreatedTransform )
86  {
87  mCreatedTransform = true;
88  mTransform = QgsCoordinateTransform( sourceCrs(), mDestCrs, mTransformContext );
89  }
90 
91  if ( feature.hasGeometry() )
92  {
93  QgsGeometry g = feature.geometry();
94  if ( g.transform( mTransform ) == 0 )
95  {
96  feature.setGeometry( g );
97  }
98  else
99  {
100  feature.clearGeometry();
101  }
102  }
103  return QgsFeatureList() << feature;
104 }
105 
107 
108 
109 
Base class for providing feedback from a processing algorithm.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:190
QgsCoordinateTransformContext transformContext() const
Returns a copy of the project&#39;s coordinate transform context, which stores various information regard...
Definition: qgsproject.cpp:474
QgsProject * project() const
Returns the project in which the algorithm is being executed.
Contains information about the context in which a coordinate transform is executed.
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
A coordinate reference system parameter for processing algorithms.
OperationResult transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection direction=QgsCoordinateTransform::ForwardTransform, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
void clearGeometry()
Removes any geometry associated with the feature.
Definition: qgsfeature.cpp:144
This class represents a coordinate reference system (CRS).
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
Class for doing transforms between two map coordinate systems.
Contains information about the context in which a processing algorithm is executed.