QGIS API Documentation 3.99.0-Master (09f76ad7019)
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( QObject::tr( "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 ellipsoidal heights) and are expressed in its vertical units (generally meters). If unchecked, then z coordinates will not be changed by the transform." ) );
39 addParameter( transformZParam.release() );
40
41 // Optional coordinate operation
42 auto crsOpParam = std::make_unique<QgsProcessingParameterCoordinateOperation>( u"OPERATION"_s, QObject::tr( "Coordinate operation" ), QVariant(), u"INPUT"_s, u"TARGET_CRS"_s, QVariant(), QVariant(), true );
43 crsOpParam->setFlags( crsOpParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
44 addParameter( crsOpParam.release() );
45}
46
47QgsCoordinateReferenceSystem QgsTransformAlgorithm::outputCrs( const QgsCoordinateReferenceSystem & ) const
48{
49 return mDestCrs;
50}
51
52QString QgsTransformAlgorithm::outputName() const
53{
54 return QObject::tr( "Reprojected" );
55}
56
57Qgis::ProcessingFeatureSourceFlags QgsTransformAlgorithm::sourceFlags() const
58{
60}
61
62QString QgsTransformAlgorithm::name() const
63{
64 return u"reprojectlayer"_s;
65}
66
67QString QgsTransformAlgorithm::displayName() const
68{
69 return QObject::tr( "Reproject layer" );
70}
71
72QStringList QgsTransformAlgorithm::tags() const
73{
74 return QObject::tr( "transform,reprojection,crs,srs,warp" ).split( ',' );
75}
76
77QString QgsTransformAlgorithm::group() const
78{
79 return QObject::tr( "Vector general" );
80}
81
82QString QgsTransformAlgorithm::groupId() const
83{
84 return u"vectorgeneral"_s;
85}
86
87QString QgsTransformAlgorithm::shortHelpString() const
88{
89 return QObject::tr( "This algorithm reprojects a vector layer. It creates a new layer with the same features "
90 "as the input one, but with geometries reprojected to a new CRS.\n\n"
91 "Attributes are not modified by this algorithm." );
92}
93
94QString QgsTransformAlgorithm::shortDescription() const
95{
96 return QObject::tr( "Creates a vector layer with geometries transformed to a new CRS." );
97}
98
99QgsTransformAlgorithm *QgsTransformAlgorithm::createInstance() const
100{
101 return new QgsTransformAlgorithm();
102}
103
104bool QgsTransformAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
105{
106 prepareSource( parameters, context );
107 mDestCrs = parameterAsCrs( parameters, u"TARGET_CRS"_s, context );
108 mTransformContext = context.transformContext();
109 mConvertCurveToSegments = parameterAsBoolean( parameters, u"CONVERT_CURVED_GEOMETRIES"_s, context );
110 mTransformZ = parameterAsBoolean( parameters, u"TRANSFORM_Z"_s, context );
111 mCoordOp = parameterAsString( parameters, u"OPERATION"_s, context );
112 return true;
113}
114
115QgsFeatureList QgsTransformAlgorithm::processFeature( const QgsFeature &f, QgsProcessingContext &, QgsProcessingFeedback *feedback )
116{
117 QgsFeature feature = f;
118 if ( !mCreatedTransform )
119 {
120 mCreatedTransform = true;
121 if ( !mCoordOp.isEmpty() )
122 mTransformContext.addCoordinateOperation( sourceCrs(), mDestCrs, mCoordOp, false );
123 mTransform = QgsCoordinateTransform( sourceCrs(), mDestCrs, mTransformContext );
124
125 mTransform.disableFallbackOperationHandler( true );
126 }
127
128 if ( feature.hasGeometry() )
129 {
130 QgsGeometry g = feature.geometry();
131
132 if ( !mTransform.isShortCircuited() && mConvertCurveToSegments )
133 {
134 // convert to straight segments to avoid issues with distorted curves
136 }
137 try
138 {
140 {
141 feature.setGeometry( g );
142 }
143 else
144 {
145 feature.clearGeometry();
146 }
147
148 if ( !mWarnedAboutFallbackTransform && mTransform.fallbackOperationOccurred() )
149 {
150 feedback->reportError( QObject::tr( "An alternative, ballpark-only transform was used when transforming coordinates for one or more features. "
151 "(Possibly an incorrect choice of operation was made for transformations between these reference systems - check "
152 "that the selected operation is valid for the full extent of the input layer.)" ) );
153 mWarnedAboutFallbackTransform = true; // only warn once to avoid flooding the log
154 }
155 }
156 catch ( QgsCsException & )
157 {
158 if ( feedback )
159 feedback->reportError( QObject::tr( "Encountered a transform error when reprojecting feature with id %1." ).arg( f.id() ) );
160 feature.clearGeometry();
161 }
162 }
163 return QgsFeatureList() << feature;
164}
165
@ Success
Operation succeeded.
Definition qgis.h:2101
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3782
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3834
@ Forward
Forward transform (from source to destination).
Definition qgis.h:2730
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3793
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