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