QGIS API Documentation  3.6.0-Noosa (5873452)
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 
29 {
30  return mDestCrs;
31 }
32 
33 QString QgsTransformAlgorithm::outputName() const
34 {
35  return QObject::tr( "Reprojected" );
36 }
37 
38 QgsProcessingFeatureSource::Flag QgsTransformAlgorithm::sourceFlags() const
39 {
41 }
42 
43 QString QgsTransformAlgorithm::name() const
44 {
45  return QStringLiteral( "reprojectlayer" );
46 }
47 
48 QString QgsTransformAlgorithm::displayName() const
49 {
50  return QObject::tr( "Reproject layer" );
51 }
52 
53 QStringList QgsTransformAlgorithm::tags() const
54 {
55  return QObject::tr( "transform,reprojection,crs,srs,warp" ).split( ',' );
56 }
57 
58 QString QgsTransformAlgorithm::group() const
59 {
60  return QObject::tr( "Vector general" );
61 }
62 
63 QString QgsTransformAlgorithm::groupId() const
64 {
65  return QStringLiteral( "vectorgeneral" );
66 }
67 
68 QString QgsTransformAlgorithm::shortHelpString() const
69 {
70  return QObject::tr( "This algorithm reprojects a vector layer. It creates a new layer with the same features "
71  "as the input one, but with geometries reprojected to a new CRS.\n\n"
72  "Attributes are not modified by this algorithm." );
73 }
74 
75 QgsTransformAlgorithm *QgsTransformAlgorithm::createInstance() const
76 {
77  return new QgsTransformAlgorithm();
78 }
79 
80 bool QgsTransformAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
81 {
82  prepareSource( parameters, context );
83  mDestCrs = parameterAsCrs( parameters, QStringLiteral( "TARGET_CRS" ), context );
84  mTransformContext = context.project() ? context.project()->transformContext() : QgsCoordinateTransformContext();
85  return true;
86 }
87 
88 QgsFeatureList QgsTransformAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback *feedback )
89 {
90  QgsFeature feature = f;
91  if ( !mCreatedTransform )
92  {
93  mCreatedTransform = true;
94  mTransform = QgsCoordinateTransform( sourceCrs(), mDestCrs, mTransformContext );
95  }
96 
97  if ( feature.hasGeometry() )
98  {
99  QgsGeometry g = feature.geometry();
100  try
101  {
102  if ( g.transform( mTransform ) == 0 )
103  {
104  feature.setGeometry( g );
105  }
106  else
107  {
108  feature.clearGeometry();
109  }
110  }
111  catch ( QgsCsException & )
112  {
113  if ( feedback )
114  feedback->reportError( QObject::tr( "Encountered a transform error when reprojecting feature with id %1." ).arg( f.id() ) );
115  feature.clearGeometry();
116  }
117  }
118  return QgsFeatureList() << feature;
119 }
120 
122 
123 
124 
QgsFeatureId id
Definition: qgsfeature.h:64
Base class for providing feedback from a processing algorithm.
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
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:571
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:106
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
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.
A coordinate reference system parameter for processing algorithms.
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
QgsCoordinateTransformContext transformContext
Definition: qgsproject.h:96
void clearGeometry()
Removes any geometry associated with the feature.
Definition: qgsfeature.cpp:151
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.
const QgsCoordinateReferenceSystem & outputCrs
QgsGeometry geometry
Definition: qgsfeature.h:67
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.