QGIS API Documentation  3.6.0-Noosa (5873452)
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 
23 QString QgsProjectPointCartesianAlgorithm::name() const
24 {
25  return QStringLiteral( "projectpointcartesian" );
26 }
27 
28 QString QgsProjectPointCartesianAlgorithm::displayName() const
29 {
30  return QObject::tr( "Project points (Cartesian)" );
31 }
32 
33 QStringList QgsProjectPointCartesianAlgorithm::tags() const
34 {
35  return QObject::tr( "bearing,azimuth,distance,angle" ).split( ',' );
36 }
37 
38 QString QgsProjectPointCartesianAlgorithm::group() const
39 {
40  return QObject::tr( "Vector geometry" );
41 }
42 
43 QString QgsProjectPointCartesianAlgorithm::groupId() const
44 {
45  return QStringLiteral( "vectorgeometry" );
46 }
47 
48 QString QgsProjectPointCartesianAlgorithm::outputName() const
49 {
50  return QObject::tr( "Projected" );
51 }
52 
53 QString 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 
59 QList<int> QgsProjectPointCartesianAlgorithm::inputLayerTypes() const
60 {
61  return QList<int>() << QgsProcessing::TypeVectorPoint;
62 }
63 
64 QgsProcessing::SourceType QgsProjectPointCartesianAlgorithm::outputLayerType() const
65 {
67 }
68 
69 QgsProjectPointCartesianAlgorithm *QgsProjectPointCartesianAlgorithm::createInstance() const
70 {
71  return new QgsProjectPointCartesianAlgorithm();
72 }
73 
74 void QgsProjectPointCartesianAlgorithm::initParameters( const QVariantMap & )
75 {
76  std::unique_ptr< QgsProcessingParameterNumber > bearing = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "BEARING" ), QObject::tr( "Bearing (degrees from North)" ), QgsProcessingParameterNumber::Double, 0, false );
77  bearing->setIsDynamic( true );
78  bearing->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Bearing" ), QObject::tr( "Bearing (degrees from North)" ), QgsPropertyDefinition::Double ) );
79  bearing->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
80  addParameter( bearing.release() );
81 
82  std::unique_ptr< QgsProcessingParameterDistance > distance = qgis::make_unique< QgsProcessingParameterDistance >( QStringLiteral( "DISTANCE" ), QObject::tr( "Distance" ), 1, QStringLiteral( "INPUT" ), false );
83  distance->setIsDynamic( true );
84  distance->setDynamicPropertyDefinition( QgsPropertyDefinition( QStringLiteral( "Distance" ), QObject::tr( "Projection distance" ), QgsPropertyDefinition::Double ) );
85  distance->setDynamicLayerParameterName( QStringLiteral( "INPUT" ) );
86  addParameter( distance.release() );
87 }
88 
89 QgsProcessingFeatureSource::Flag QgsProjectPointCartesianAlgorithm::sourceFlags() const
90 {
92 }
93 
94 bool QgsProjectPointCartesianAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
95 {
96  mBearing = parameterAsDouble( parameters, QStringLiteral( "BEARING" ), context );
97  mDynamicBearing = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "BEARING" ) );
98  if ( mDynamicBearing )
99  mBearingProperty = parameters.value( QStringLiteral( "BEARING" ) ).value< QgsProperty >();
100 
101  mDistance = parameterAsDouble( parameters, QStringLiteral( "DISTANCE" ), context );
102  mDynamicDistance = QgsProcessingParameters::isDynamic( parameters, QStringLiteral( "DISTANCE" ) );
103  if ( mDynamicDistance )
104  mDistanceProperty = parameters.value( QStringLiteral( "DISTANCE" ) ).value< QgsProperty >();
105 
106  return true;
107 }
108 
109 QgsFeatureList QgsProjectPointCartesianAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback * )
110 {
111  QgsFeature f = feature;
113  {
114  double distance = mDistance;
115  if ( mDynamicDistance )
116  distance = mDistanceProperty.valueAsDouble( context.expressionContext(), distance );
117  double bearing = mBearing;
118  if ( mDynamicBearing )
119  bearing = mBearingProperty.valueAsDouble( context.expressionContext(), bearing );
120 
121  QgsGeometry g = f.geometry();
122  if ( QgsWkbTypes::isMultiType( g.wkbType() ) )
123  {
124  const QgsMultiPoint *mp = static_cast< const QgsMultiPoint * >( g.constGet() );
125  std::unique_ptr< QgsMultiPoint > result = qgis::make_unique< QgsMultiPoint >();
126  for ( int i = 0; i < mp->numGeometries(); ++i )
127  {
128  const QgsPoint *p = static_cast< const QgsPoint * >( mp->geometryN( i ) );
129  result->addGeometry( p->project( distance, bearing ).clone() );
130  }
131  f.setGeometry( QgsGeometry( std::move( result ) ) );
132  }
133  else
134  {
135  const QgsPoint *p = static_cast< const QgsPoint * >( g.constGet() );
136  QgsPoint result = p->project( distance, bearing );
137  f.setGeometry( QgsGeometry( result.clone() ) );
138  }
139  }
140 
141  return QgsFeatureList() << f;
142 }
143 
145 
Base class for providing feedback from a processing algorithm.
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Multi point geometry collection.
Definition: qgsmultipoint.h:29
static bool isMultiType(Type type)
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:559
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:571
QgsWkbTypes::Type wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:106
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:197
static GeometryType geometryType(Type type)
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
Definition: qgswkbtypes.h:665
QgsPoint * clone() const override
Clones the geometry by performing a deep copy.
Definition: qgspoint.cpp:94
A store for object properties.
Definition: qgsproperty.h:229
QgsPoint project(double distance, double azimuth, double inclination=90.0) const
Returns a new point which correspond to this point projected by a specified distance with specified a...
Definition: qgspoint.cpp:643
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
Double value (including negative values)
Definition: qgsproperty.h:57
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition for a property.
Definition: qgsproperty.h:46
int numGeometries() const
Returns the number of geometries within the collection.
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
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.
Vector point layers.
Definition: qgsprocessing.h:48
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
SourceType
Data source types enum.
Definition: qgsprocessing.h:44
QgsGeometry geometry
Definition: qgsfeature.h:67
Contains information about the context in which a processing algorithm is executed.
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...