QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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
19
21
22
23void QgsTransformAlgorithm::initParameters( const QVariantMap & )
24{
25 addParameter( new QgsProcessingParameterCrs( QStringLiteral( "TARGET_CRS" ), QObject::tr( "Target CRS" ), QStringLiteral( "EPSG:4326" ) ) );
26
27 std::unique_ptr< QgsProcessingParameterCoordinateOperation > crsOpParam = std::make_unique< QgsProcessingParameterCoordinateOperation >( QStringLiteral( "OPERATION" ), QObject::tr( "Coordinate operation" ),
28 QVariant(), QStringLiteral( "INPUT" ), QStringLiteral( "TARGET_CRS" ), QVariant(), QVariant(), true );
29 crsOpParam->setFlags( crsOpParam->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
30 addParameter( crsOpParam.release() );
31}
32
34{
35 return mDestCrs;
36}
37
38QString QgsTransformAlgorithm::outputName() const
39{
40 return QObject::tr( "Reprojected" );
41}
42
43QgsProcessingFeatureSource::Flag QgsTransformAlgorithm::sourceFlags() const
44{
46}
47
48QString QgsTransformAlgorithm::name() const
49{
50 return QStringLiteral( "reprojectlayer" );
51}
52
53QString QgsTransformAlgorithm::displayName() const
54{
55 return QObject::tr( "Reproject layer" );
56}
57
58QStringList QgsTransformAlgorithm::tags() const
59{
60 return QObject::tr( "transform,reprojection,crs,srs,warp" ).split( ',' );
61}
62
63QString QgsTransformAlgorithm::group() const
64{
65 return QObject::tr( "Vector general" );
66}
67
68QString QgsTransformAlgorithm::groupId() const
69{
70 return QStringLiteral( "vectorgeneral" );
71}
72
73QString QgsTransformAlgorithm::shortHelpString() const
74{
75 return QObject::tr( "This algorithm reprojects a vector layer. It creates a new layer with the same features "
76 "as the input one, but with geometries reprojected to a new CRS.\n\n"
77 "Attributes are not modified by this algorithm." );
78}
79
80QgsTransformAlgorithm *QgsTransformAlgorithm::createInstance() const
81{
82 return new QgsTransformAlgorithm();
83}
84
85bool QgsTransformAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
86{
87 prepareSource( parameters, context );
88 mDestCrs = parameterAsCrs( parameters, QStringLiteral( "TARGET_CRS" ), context );
89 mTransformContext = context.transformContext();
90 mCoordOp = parameterAsString( parameters, QStringLiteral( "OPERATION" ), context );
91 return true;
92}
93
94QgsFeatureList QgsTransformAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback *feedback )
95{
96 QgsFeature feature = f;
97 if ( !mCreatedTransform )
98 {
99 mCreatedTransform = true;
100 if ( !mCoordOp.isEmpty() )
101 mTransformContext.addCoordinateOperation( sourceCrs(), mDestCrs, mCoordOp, false );
102 mTransform = QgsCoordinateTransform( sourceCrs(), mDestCrs, mTransformContext );
103
104 mTransform.disableFallbackOperationHandler( true );
105 }
106
107 if ( feature.hasGeometry() )
108 {
109 QgsGeometry g = feature.geometry();
110 try
111 {
112 if ( g.transform( mTransform ) == Qgis::GeometryOperationResult::Success )
113 {
114 feature.setGeometry( g );
115 }
116 else
117 {
118 feature.clearGeometry();
119 }
120
121 if ( !mWarnedAboutFallbackTransform && mTransform.fallbackOperationOccurred() )
122 {
123 feedback->reportError( QObject::tr( "An alternative, ballpark-only transform was used when transforming coordinates for one or more features. "
124 "(Possibly an incorrect choice of operation was made for transformations between these reference systems - check "
125 "that the selected operation is valid for the full extent of the input layer.)" ) );
126 mWarnedAboutFallbackTransform = true; // only warn once to avoid flooding the log
127 }
128 }
129 catch ( QgsCsException & )
130 {
131 if ( feedback )
132 feedback->reportError( QObject::tr( "Encountered a transform error when reprojecting feature with id %1." ).arg( f.id() ) );
133 feature.clearGeometry();
134 }
135 }
136 return QgsFeatureList() << feature;
137}
138
140
141
142
@ Success
Operation succeeded.
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:66
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
QgsGeometry geometry
Definition: qgsfeature.h:67
void clearGeometry()
Removes any geometry associated with the feature.
Definition: qgsfeature.cpp:184
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:233
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:170
Q_GADGET QgsFeatureId id
Definition: qgsfeature.h:64
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:164
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false) SIP_THROW(QgsCsException)
Transforms this geometry as described by the coordinate transform ct.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Base class for providing feedback from a processing algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A coordinate reference system parameter for processing algorithms.
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:922
const QgsCoordinateReferenceSystem & outputCrs