QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
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
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
25
26
27void QgsTransformAlgorithm::initParameters( const QVariantMap & )
28{
29 addParameter( new QgsProcessingParameterCrs( u"TARGET_CRS"_s, QObject::tr( "Target CRS" ), u"EPSG:4326"_s ) );
30
31 // Convert curves to straight segments
32 auto convertCurvesParam = std::make_unique<QgsProcessingParameterBoolean>( u"CONVERT_CURVED_GEOMETRIES"_s, QObject::tr( "Convert curved geometries to straight segments" ), false );
33 convertCurvesParam->setHelp( QObject::tr( "If checked, curved geometries will be converted to straight segments. Otherwise, they will be kept as curves. This can fix distortion issues." ) );
34 addParameter( convertCurvesParam.release() );
35
36 // Transform Z
37 auto transformZParam = std::make_unique<QgsProcessingParameterBoolean>( u"TRANSFORM_Z"_s, QObject::tr( "Also transform Z coordinates" ), false );
38 transformZParam->setHelp(
39 QObject::tr(
40 "If checked, the z coordinates will also be transformed. This requires that the z coordinates in the geometries represent height relative to the vertical datum of the source CRS (generally "
41 "ellipsoidal heights) and are expressed in its vertical units (generally meters). If unchecked, then z coordinates will not be changed by the transform."
42 )
43 );
44 addParameter( transformZParam.release() );
45
46 // Optional coordinate operation
47 auto crsOpParam
48 = std::make_unique<QgsProcessingParameterCoordinateOperation>( u"OPERATION"_s, QObject::tr( "Coordinate operation" ), QVariant(), u"INPUT"_s, u"TARGET_CRS"_s, QVariant(), QVariant(), true );
49 crsOpParam->setFlags( crsOpParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
50 addParameter( crsOpParam.release() );
51}
52
53QgsCoordinateReferenceSystem QgsTransformAlgorithm::outputCrs( const QgsCoordinateReferenceSystem & ) const
54{
55 return mDestCrs;
56}
57
58QString QgsTransformAlgorithm::outputName() const
59{
60 return QObject::tr( "Reprojected" );
61}
62
63Qgis::ProcessingFeatureSourceFlags QgsTransformAlgorithm::sourceFlags() const
64{
66}
67
68QString QgsTransformAlgorithm::name() const
69{
70 return u"reprojectlayer"_s;
71}
72
73QString QgsTransformAlgorithm::displayName() const
74{
75 return QObject::tr( "Reproject layer" );
76}
77
78QStringList QgsTransformAlgorithm::tags() const
79{
80 return QObject::tr( "transform,reprojection,crs,srs,warp" ).split( ',' );
81}
82
83QString QgsTransformAlgorithm::group() const
84{
85 return QObject::tr( "Vector general" );
86}
87
88QString QgsTransformAlgorithm::groupId() const
89{
90 return u"vectorgeneral"_s;
91}
92
93QString QgsTransformAlgorithm::shortHelpString() const
94{
95 return QObject::tr(
96 "This algorithm reprojects a vector layer. It creates a new layer with the same features "
97 "as the input one, but with geometries reprojected to a new CRS.\n\n"
98 "Attributes are not modified by this algorithm."
99 );
100}
101
102QString QgsTransformAlgorithm::shortDescription() const
103{
104 return QObject::tr( "Creates a vector layer with geometries transformed to a new CRS." );
105}
106
107QgsTransformAlgorithm *QgsTransformAlgorithm::createInstance() const
108{
109 return new QgsTransformAlgorithm();
110}
111
112bool QgsTransformAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
113{
114 prepareSource( parameters, context );
115 mDestCrs = parameterAsCrs( parameters, u"TARGET_CRS"_s, context );
116 mTransformContext = context.transformContext();
117 mConvertCurveToSegments = parameterAsBoolean( parameters, u"CONVERT_CURVED_GEOMETRIES"_s, context );
118 mTransformZ = parameterAsBoolean( parameters, u"TRANSFORM_Z"_s, context );
119 mCoordOp = parameterAsString( parameters, u"OPERATION"_s, context );
120 return true;
121}
122
123QgsFeatureList QgsTransformAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback *feedback )
124{
125 QgsFeature feature = f;
126 if ( !mCreatedTransform )
127 {
128 mCreatedTransform = true;
129 if ( !mCoordOp.isEmpty() )
130 mTransformContext.addCoordinateOperation( sourceCrs(), mDestCrs, mCoordOp, false );
131 mTransform = QgsCoordinateTransform( sourceCrs(), mDestCrs, mTransformContext );
132
133 mTransform.disableFallbackOperationHandler( true );
134 }
135
136 if ( feature.hasGeometry() )
137 {
138 QgsGeometry g = feature.geometry();
139
140 if ( !mTransform.isShortCircuited() && mConvertCurveToSegments )
141 {
142 // convert to straight segments to avoid issues with distorted curves
144 }
145 try
146 {
148 {
149 feature.setGeometry( g );
150 }
151 else
152 {
153 feature.clearGeometry();
154 }
155
156 if ( !mWarnedAboutFallbackTransform && mTransform.fallbackOperationOccurred() )
157 {
158 feedback->reportError(
159 QObject::tr(
160 "An alternative, ballpark-only transform was used when transforming coordinates for one or more features. "
161 "(Possibly an incorrect choice of operation was made for transformations between these reference systems - check "
162 "that the selected operation is valid for the full extent of the input layer.)"
163 )
164 );
165 mWarnedAboutFallbackTransform = true; // only warn once to avoid flooding the log
166 }
167 }
168 catch ( QgsCsException & )
169 {
170 if ( feedback )
171 feedback->reportError( QObject::tr( "Encountered a transform error when reprojecting feature with id %1." ).arg( f.id() ) );
172 feature.clearGeometry();
173 }
174 }
175 return QgsFeatureList() << feature;
176}
177
@ Success
Operation succeeded.
Definition qgis.h:2122
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3828
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3880
@ Forward
Forward transform (from source to destination).
Definition qgis.h:2765
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3839
Represents a coordinate reference system (CRS).
Handles coordinate transforms between two coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsFeatureId id
Definition qgsfeature.h:68
QgsGeometry geometry
Definition qgsfeature.h:71
void clearGeometry()
Removes any geometry associated with the feature.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
A geometry is the spatial representation of a feature.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
void convertToStraightSegment(double tolerance=M_PI/180., QgsAbstractGeometry::SegmentationToleranceType toleranceType=QgsAbstractGeometry::MaximumAngle)
Converts the geometry to straight line segments, if it is a curved geometry type.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
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.
QList< QgsFeature > QgsFeatureList