QGIS API Documentation  3.12.1-București (121cc00ff0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
QgsFeatureId id
Definition: qgsfeature.h:64
Base class for providing feedback from a processing algorithm.
Parameter is an advanced parameter which should be hidden from users by default.
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:122
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
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
A coordinate reference system parameter for processing algorithms.
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
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
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
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.