QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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(
61 "This algorithm projects point geometries by a specified distance and bearing (azimuth), creating a new point layer with the projected points.\n\n"
62 "The distance is specified in layer units, and the bearing in degrees clockwise from North."
63 );
64}
65
66QString QgsProjectPointCartesianAlgorithm::shortDescription() const
67{
68 return QObject::tr( "Creates a point layer with geometries projected by a specified distance and bearing (azimuth)." );
69}
70
71QList<int> QgsProjectPointCartesianAlgorithm::inputLayerTypes() const
72{
73 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPoint );
74}
75
76Qgis::ProcessingSourceType QgsProjectPointCartesianAlgorithm::outputLayerType() const
77{
79}
80
81QgsProjectPointCartesianAlgorithm *QgsProjectPointCartesianAlgorithm::createInstance() const
82{
83 return new QgsProjectPointCartesianAlgorithm();
84}
85
86void QgsProjectPointCartesianAlgorithm::initParameters( const QVariantMap & )
87{
88 auto bearing = std::make_unique<QgsProcessingParameterNumber>( u"BEARING"_s, QObject::tr( "Bearing (degrees from North)" ), Qgis::ProcessingNumberParameterType::Double, 0, false );
89 bearing->setIsDynamic( true );
90 bearing->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Bearing"_s, QObject::tr( "Bearing (degrees from North)" ), QgsPropertyDefinition::Double ) );
91 bearing->setDynamicLayerParameterName( u"INPUT"_s );
92 addParameter( bearing.release() );
93
94 auto distance = std::make_unique<QgsProcessingParameterDistance>( u"DISTANCE"_s, QObject::tr( "Distance" ), 1, u"INPUT"_s, false );
95 distance->setIsDynamic( true );
96 distance->setDynamicPropertyDefinition( QgsPropertyDefinition( u"Distance"_s, QObject::tr( "Projection distance" ), QgsPropertyDefinition::Double ) );
97 distance->setDynamicLayerParameterName( u"INPUT"_s );
98 addParameter( distance.release() );
99}
100
101Qgis::ProcessingFeatureSourceFlags QgsProjectPointCartesianAlgorithm::sourceFlags() const
102{
104}
105
106bool QgsProjectPointCartesianAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
107{
108 mBearing = parameterAsDouble( parameters, u"BEARING"_s, context );
109 mDynamicBearing = QgsProcessingParameters::isDynamic( parameters, u"BEARING"_s );
110 if ( mDynamicBearing )
111 mBearingProperty = parameters.value( u"BEARING"_s ).value<QgsProperty>();
112
113 mDistance = parameterAsDouble( parameters, u"DISTANCE"_s, context );
114 mDynamicDistance = QgsProcessingParameters::isDynamic( parameters, u"DISTANCE"_s );
115 if ( mDynamicDistance )
116 mDistanceProperty = parameters.value( u"DISTANCE"_s ).value<QgsProperty>();
117
118 return true;
119}
120
121QgsFeatureList QgsProjectPointCartesianAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
122{
123 QgsFeature f = feature;
125 {
126 double distance = mDistance;
127 if ( mDynamicDistance )
128 distance = mDistanceProperty.valueAsDouble( context.expressionContext(), distance );
129 double bearing = mBearing;
130 if ( mDynamicBearing )
131 bearing = mBearingProperty.valueAsDouble( context.expressionContext(), bearing );
132
133 const QgsGeometry g = f.geometry();
135 {
136 const QgsMultiPoint *mp = static_cast<const QgsMultiPoint *>( g.constGet() );
137 auto result = std::make_unique<QgsMultiPoint>();
138 result->reserve( mp->numGeometries() );
139 for ( int i = 0; i < mp->numGeometries(); ++i )
140 {
141 const QgsPoint *p = mp->pointN( i );
142 result->addGeometry( p->project( distance, bearing ).clone() );
143 }
144 f.setGeometry( QgsGeometry( std::move( result ) ) );
145 }
146 else
147 {
148 const QgsPoint *p = static_cast<const QgsPoint *>( g.constGet() );
149 const QgsPoint result = p->project( distance, bearing );
150 f.setGeometry( QgsGeometry( result.clone() ) );
151 }
152 }
153
154 return QgsFeatureList() << f;
155}
156
ProcessingSourceType
Processing data source types.
Definition qgis.h:3645
@ VectorPoint
Vector point layers.
Definition qgis.h:3648
@ Point
Points.
Definition qgis.h:380
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3828
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3839
@ Double
Double/float values.
Definition qgis.h:3921
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:138
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:734
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:56
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