QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 #if PROJ_VERSION_MAJOR>=6
28  std::unique_ptr< QgsProcessingParameterCoordinateOperation > crsOpParam = qgis::make_unique< QgsProcessingParameterCoordinateOperation >( QStringLiteral( "OPERATION" ), QObject::tr( "Coordinate operation" ),
29  QVariant(), QStringLiteral( "INPUT" ), QStringLiteral( "TARGET_CRS" ), QVariant(), QVariant(), true );
30  crsOpParam->setFlags( crsOpParam->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
31  addParameter( crsOpParam.release() );
32 #endif
33 }
34 
36 {
37  return mDestCrs;
38 }
39 
40 QString QgsTransformAlgorithm::outputName() const
41 {
42  return QObject::tr( "Reprojected" );
43 }
44 
45 QgsProcessingFeatureSource::Flag QgsTransformAlgorithm::sourceFlags() const
46 {
48 }
49 
50 QString QgsTransformAlgorithm::name() const
51 {
52  return QStringLiteral( "reprojectlayer" );
53 }
54 
55 QString QgsTransformAlgorithm::displayName() const
56 {
57  return QObject::tr( "Reproject layer" );
58 }
59 
60 QStringList QgsTransformAlgorithm::tags() const
61 {
62  return QObject::tr( "transform,reprojection,crs,srs,warp" ).split( ',' );
63 }
64 
65 QString QgsTransformAlgorithm::group() const
66 {
67  return QObject::tr( "Vector general" );
68 }
69 
70 QString QgsTransformAlgorithm::groupId() const
71 {
72  return QStringLiteral( "vectorgeneral" );
73 }
74 
75 QString QgsTransformAlgorithm::shortHelpString() const
76 {
77  return QObject::tr( "This algorithm reprojects a vector layer. It creates a new layer with the same features "
78  "as the input one, but with geometries reprojected to a new CRS.\n\n"
79  "Attributes are not modified by this algorithm." );
80 }
81 
82 QgsTransformAlgorithm *QgsTransformAlgorithm::createInstance() const
83 {
84  return new QgsTransformAlgorithm();
85 }
86 
87 bool QgsTransformAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
88 {
89  prepareSource( parameters, context );
90  mDestCrs = parameterAsCrs( parameters, QStringLiteral( "TARGET_CRS" ), context );
91  mTransformContext = context.transformContext();
92  mCoordOp = parameterAsString( parameters, QStringLiteral( "OPERATION" ), context );
93  return true;
94 }
95 
96 QgsFeatureList QgsTransformAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback *feedback )
97 {
98  QgsFeature feature = f;
99  if ( !mCreatedTransform )
100  {
101  mCreatedTransform = true;
102  if ( !mCoordOp.isEmpty() )
103  mTransformContext.addCoordinateOperation( sourceCrs(), mDestCrs, mCoordOp, false );
104  mTransform = QgsCoordinateTransform( sourceCrs(), mDestCrs, mTransformContext );
105 
106  mTransform.disableFallbackOperationHandler( true );
107  }
108 
109  if ( feature.hasGeometry() )
110  {
111  QgsGeometry g = feature.geometry();
112  try
113  {
114  if ( g.transform( mTransform ) == 0 )
115  {
116  feature.setGeometry( g );
117  }
118  else
119  {
120  feature.clearGeometry();
121  }
122 
123  if ( !mWarnedAboutFallbackTransform && mTransform.fallbackOperationOccurred() )
124  {
125  feedback->reportError( QObject::tr( "An alternative, ballpark-only transform was used when transforming coordinates for one or more features. "
126  "(Possibly an incorrect choice of operation was made for transformations between these reference systems - check "
127  "that the selected operation is valid for the full extent of the input layer.)" ) );
128  mWarnedAboutFallbackTransform = true; // only warn once to avoid flooding the log
129  }
130  }
131  catch ( QgsCsException & )
132  {
133  if ( feedback )
134  feedback->reportError( QObject::tr( "Encountered a transform error when reprojecting feature with id %1." ).arg( f.id() ) );
135  feature.clearGeometry();
136  }
137  }
138  return QgsFeatureList() << feature;
139 }
140 
142 
143 
144 
QgsGeometry::transform
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.
Definition: qgsgeometry.cpp:2836
outputCrs
const QgsCoordinateReferenceSystem & outputCrs
Definition: qgswfsgetfeature.cpp:115
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
QgsProcessingFeedback::reportError
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
Definition: qgsprocessingfeedback.cpp:39
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsProcessingParameterDefinition::FlagAdvanced
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition: qgsprocessingparameters.h:419
QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition: qgsprocessingutils.h:475
QgsFeature::clearGeometry
void clearGeometry()
Removes any geometry associated with the feature.
Definition: qgsfeature.cpp:151
QgsFeature::id
QgsFeatureId id
Definition: qgsfeature.h:68
QgsCsException
Definition: qgsexception.h:65
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:137
QgsProcessingParameterCrs
Definition: qgsprocessingparameters.h:1470
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:572
QgsProcessingContext::transformContext
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Definition: qgsprocessingcontext.h:135
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
QgsProcessingFeatureSource::Flag
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Definition: qgsprocessingutils.h:473
QgsGeometry
Definition: qgsgeometry.h:122
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
QgsFeature
Definition: qgsfeature.h:55
qgsalgorithmtransform.h
QgsCoordinateTransform
Definition: qgscoordinatetransform.h:52