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