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