QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgsalgorithmprojectpointcartesian.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmprojectpointcartesian.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#include "qgsmultipoint.h"
20
22
23QString QgsProjectPointCartesianAlgorithm::name() const
24{
25 return QStringLiteral( "projectpointcartesian" );
26}
27
28QString QgsProjectPointCartesianAlgorithm::displayName() const
29{
30 return QObject::tr( "Project points (Cartesian)" );
31}
32
33QStringList QgsProjectPointCartesianAlgorithm::tags() const
34{
35 return QObject::tr( "bearing,azimuth,distance,angle" ).split( ',' );
36}
37
38QString QgsProjectPointCartesianAlgorithm::group() const
39{
40 return QObject::tr( "Vector geometry" );
41}
42
43QString QgsProjectPointCartesianAlgorithm::groupId() const
44{
45 return QStringLiteral( "vectorgeometry" );
46}
47
48QString QgsProjectPointCartesianAlgorithm::outputName() const
49{
50 return QObject::tr( "Projected" );
51}
52
53QString QgsProjectPointCartesianAlgorithm::shortHelpString() const
54{
55 return QObject::tr( "This algorithm projects point geometries by a specified distance and bearing (azimuth), creating a new point layer with the projected points.\n\n"
56 "The distance is specified in layer units, and the bearing in degrees clockwise from North." );
57}
58
59QString QgsProjectPointCartesianAlgorithm::shortDescription() const
60{
61 return QObject::tr( "Creates a point layer with geometries projected by a specified distance and bearing (azimuth)." );
62}
63
64QList<int> QgsProjectPointCartesianAlgorithm::inputLayerTypes() const
65{
66 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPoint );
67}
68
69Qgis::ProcessingSourceType QgsProjectPointCartesianAlgorithm::outputLayerType() const
70{
72}
73
74QgsProjectPointCartesianAlgorithm *QgsProjectPointCartesianAlgorithm::createInstance() const
75{
76 return new QgsProjectPointCartesianAlgorithm();
77}
78
79void QgsProjectPointCartesianAlgorithm::initParameters( const QVariantMap & )
80{
81 auto bearing = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "BEARING" ), QObject::tr( "Bearing (degrees from North)" ), Qgis::ProcessingNumberParameterType::Double, 0, false );
82 bearing->setIsDynamic( true );
83 bearing->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Bearing" ), QObject::tr( "Bearing (degrees from North)" ), QgsPropertyDefinition::Double ) );
84 bearing->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
85 addParameter( bearing.release() );
86
87 auto distance = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( "DISTANCE" ), QObject::tr( "Distance" ), 1, QStringLiteral( "INPUT" ), false );
88 distance->setIsDynamic( true );
89 distance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Distance" ), QObject::tr( "Projection distance" ), QgsPropertyDefinition::Double ) );
90 distance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
91 addParameter( distance.release() );
92}
93
94Qgis::ProcessingFeatureSourceFlags QgsProjectPointCartesianAlgorithm::sourceFlags() const
95{
97}
98
99bool QgsProjectPointCartesianAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
100{
101 mBearing = parameterAsDouble( parameters, QStringLiteral( "BEARING" ), context );
102 mDynamicBearing = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "BEARING" ) );
103 if ( mDynamicBearing )
104 mBearingProperty = parameters.value( QStringLiteral( "BEARING" ) ).value<QgsProperty>();
105
106 mDistance = parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context );
107 mDynamicDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DISTANCE" ) );
108 if ( mDynamicDistance )
109 mDistanceProperty = parameters.value( QStringLiteral( "DISTANCE" ) ).value<QgsProperty>();
110
111 return true;
112}
113
114QgsFeatureList QgsProjectPointCartesianAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
115{
116 QgsFeature f = feature;
118 {
119 double distance = mDistance;
120 if ( mDynamicDistance )
121 distance = mDistanceProperty.valueAsDouble( context.expressionContext(), distance );
122 double bearing = mBearing;
123 if ( mDynamicBearing )
124 bearing = mBearingProperty.valueAsDouble( context.expressionContext(), bearing );
125
126 const QgsGeometry g = f.geometry();
128 {
129 const QgsMultiPoint *mp = static_cast<const QgsMultiPoint *>( g.constGet() );
130 auto result = std::make_unique<QgsMultiPoint>();
131 result->reserve( mp->numGeometries() );
132 for ( int i = 0; i < mp->numGeometries(); ++i )
133 {
134 const QgsPoint *p = mp->pointN( i );
135 result->addGeometry( p->project( distance, bearing ).clone() );
136 }
137 f.setGeometry( QgsGeometry( std::move( result ) ) );
138 }
139 else
140 {
141 const QgsPoint *p = static_cast<const QgsPoint *>( g.constGet() );
142 const QgsPoint result = p->project( distance, bearing );
143 f.setGeometry( QgsGeometry( result.clone() ) );
144 }
145 }
146
147 return QgsFeatureList() << f;
148}
149
ProcessingSourceType
Processing data source types.
Definition qgis.h:3399
@ VectorPoint
Vector point layers.
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3588
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsGeometry geometry
Definition qgsfeature.h:69
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
int numGeometries() const
Returns the number of geometries within the collection.
A geometry is the spatial representation of a feature.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Qgis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
Multi point geometry collection.
QgsPoint * pointN(int index)
Returns the point with the specified index.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
QgsPoint * clone() const override
Clones the geometry by performing a deep copy.
Definition qgspoint.cpp:105
QgsPoint project(double distance, double azimuth, double inclination=90.0) const
Returns a new point which corresponds to this point projected by a specified distance with specified ...
Definition qgspoint.cpp:704
Contains information about the context in which a processing algorithm is executed.
QgsExpressionContext & expressionContext()
Returns the expression context.
Base class for providing feedback from a processing algorithm.
static bool isDynamic(const QVariantMap &parameters, const QString &name)
Returns true if the parameter with matching name is a dynamic parameter, and must be evaluated once f...
Definition for a property.
Definition qgsproperty.h:45
@ Double
Double value (including negative values)
Definition qgsproperty.h:55
A store for object properties.
double valueAsDouble(const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property and interprets it as a double.
static Qgis::GeometryType geometryType(Qgis::WkbType type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
static bool isMultiType(Qgis::WkbType type)
Returns true if the WKB type is a multi type.
QList< QgsFeature > QgsFeatureList