QGIS API Documentation  3.2.0-Bonn (bc43194)
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,reprojection,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 *feedback )
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  try
95  {
96  if ( g.transform( mTransform ) == 0 )
97  {
98  feature.setGeometry( g );
99  }
100  else
101  {
102  feature.clearGeometry();
103  }
104  }
105  catch ( QgsCsException & )
106  {
107  if ( feedback )
108  feedback->reportError( QObject::tr( "Encountered a transform error when reprojecting feature with id %1." ).arg( f.id() ) );
109  feature.clearGeometry();
110  }
111  }
112  return QgsFeatureList() << feature;
113 }
114 
116 
117 
118 
QgsFeatureId id
Definition: qgsfeature.h:71
Base class for providing feedback from a processing algorithm.
OperationResult transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection direction=QgsCoordinateTransform::ForwardTransform, bool transformZ=false) SIP_THROW(QgsCsException)
Transforms this geometry as described by the coordinate transform ct.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:104
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:543
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.
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.
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
Contains information about the context in which a processing algorithm is executed.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.