QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
20#include "qgsmultipoint.h"
21
23
24QString QgsProjectPointCartesianAlgorithm::name() const
25{
26 return QStringLiteral( "projectpointcartesian" );
27}
28
29QString QgsProjectPointCartesianAlgorithm::displayName() const
30{
31 return QObject::tr( "Project points (Cartesian)" );
32}
33
34QStringList QgsProjectPointCartesianAlgorithm::tags() const
35{
36 return QObject::tr( "bearing,azimuth,distance,angle" ).split( ',' );
37}
38
39QString QgsProjectPointCartesianAlgorithm::group() const
40{
41 return QObject::tr( "Vector geometry" );
42}
43
44QString QgsProjectPointCartesianAlgorithm::groupId() const
45{
46 return QStringLiteral( "vectorgeometry" );
47}
48
49QString QgsProjectPointCartesianAlgorithm::outputName() const
50{
51 return QObject::tr( "Projected" );
52}
53
54QString QgsProjectPointCartesianAlgorithm::shortHelpString() const
55{
56 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"
57 "The distance is specified in layer units, and the bearing in degrees clockwise from North." );
58}
59
60QString QgsProjectPointCartesianAlgorithm::shortDescription() const
61{
62 return QObject::tr( "Creates a point layer with geometries projected by a specified distance and bearing (azimuth)." );
63}
64
65QList<int> QgsProjectPointCartesianAlgorithm::inputLayerTypes() const
66{
67 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPoint );
68}
69
70Qgis::ProcessingSourceType QgsProjectPointCartesianAlgorithm::outputLayerType() const
71{
73}
74
75QgsProjectPointCartesianAlgorithm *QgsProjectPointCartesianAlgorithm::createInstance() const
76{
77 return new QgsProjectPointCartesianAlgorithm();
78}
79
80void QgsProjectPointCartesianAlgorithm::initParameters( const QVariantMap & )
81{
82 auto bearing = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral( "BEARING" ), QObject::tr( "Bearing (degrees from North)" ), Qgis::ProcessingNumberParameterType::Double, 0, false );
83 bearing->setIsDynamic( true );
84 bearing->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Bearing" ), QObject::tr( "Bearing (degrees from North)" ), QgsPropertyDefinition::Double ) );
85 bearing->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
86 addParameter( bearing.release() );
87
88 auto distance = std::make_unique<QgsProcessingParameterDistance>( QStringLiteral( "DISTANCE" ), QObject::tr( "Distance" ), 1, QStringLiteral( "INPUT" ), false );
89 distance->setIsDynamic( true );
90 distance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Distance" ), QObject::tr( "Projection distance" ), QgsPropertyDefinition::Double ) );
91 distance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
92 addParameter( distance.release() );
93}
94
95Qgis::ProcessingFeatureSourceFlags QgsProjectPointCartesianAlgorithm::sourceFlags() const
96{
98}
99
100bool QgsProjectPointCartesianAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
101{
102 mBearing = parameterAsDouble( parameters, QStringLiteral( "BEARING" ), context );
103 mDynamicBearing = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "BEARING" ) );
104 if ( mDynamicBearing )
105 mBearingProperty = parameters.value( QStringLiteral( "BEARING" ) ).value<QgsProperty>();
106
107 mDistance = parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context );
108 mDynamicDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DISTANCE" ) );
109 if ( mDynamicDistance )
110 mDistanceProperty = parameters.value( QStringLiteral( "DISTANCE" ) ).value<QgsProperty>();
111
112 return true;
113}
114
115QgsFeatureList QgsProjectPointCartesianAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
116{
117 QgsFeature f = feature;
119 {
120 double distance = mDistance;
121 if ( mDynamicDistance )
122 distance = mDistanceProperty.valueAsDouble( context.expressionContext(), distance );
123 double bearing = mBearing;
124 if ( mDynamicBearing )
125 bearing = mBearingProperty.valueAsDouble( context.expressionContext(), bearing );
126
127 const QgsGeometry g = f.geometry();
129 {
130 const QgsMultiPoint *mp = static_cast<const QgsMultiPoint *>( g.constGet() );
131 auto result = std::make_unique<QgsMultiPoint>();
132 result->reserve( mp->numGeometries() );
133 for ( int i = 0; i < mp->numGeometries(); ++i )
134 {
135 const QgsPoint *p = mp->pointN( i );
136 result->addGeometry( p->project( distance, bearing ).clone() );
137 }
138 f.setGeometry( QgsGeometry( std::move( result ) ) );
139 }
140 else
141 {
142 const QgsPoint *p = static_cast<const QgsPoint *>( g.constGet() );
143 const QgsPoint result = p->project( distance, bearing );
144 f.setGeometry( QgsGeometry( result.clone() ) );
145 }
146 }
147
148 return QgsFeatureList() << f;
149}
150
ProcessingSourceType
Processing data source types.
Definition qgis.h:3531
@ VectorPoint
Vector point layers.
Definition qgis.h:3534
@ Point
Points.
Definition qgis.h:359
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3711
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3722
@ Double
Double/float values.
Definition qgis.h:3804
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:108
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:707
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 Q_INVOKABLE bool isMultiType(Qgis::WkbType type)
Returns true if the WKB type is a multi type.
QList< QgsFeature > QgsFeatureList