QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 = std::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 = std::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  const 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 = std::make_unique< QgsMultiPoint >();
126  result->reserve( mp->numGeometries() );
127  for ( int i = 0; i < mp->numGeometries(); ++i )
128  {
129  const QgsPoint *p = mp->pointN( i );
130  result->addGeometry( p->project( distance, bearing ).clone() );
131  }
132  f.setGeometry( QgsGeometry( std::move( result ) ) );
133  }
134  else
135  {
136  const QgsPoint *p = static_cast< const QgsPoint * >( g.constGet() );
137  const QgsPoint result = p->project( distance, bearing );
138  f.setGeometry( QgsGeometry( result.clone() ) );
139  }
140  }
141 
142  return QgsFeatureList() << f;
143 }
144 
146 
QgsProcessingParameterNumber::Double
@ Double
Double/float values.
Definition: qgsprocessingparameters.h:2187
QgsProperty
A store for object properties.
Definition: qgsproperty.h:230
QgsProcessingParameters::isDynamic
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: qgsprocessingparameters.cpp:114
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:48
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
qgsmultipoint.h
QgsFeature::geometry
QgsGeometry geometry
Definition: qgsfeature.h:71
QgsPropertyDefinition::Double
@ Double
Double value (including negative values)
Definition: qgsproperty.h:57
QgsProcessingFeatureSource::FlagSkipGeometryValidityChecks
@ FlagSkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition: qgsprocessingutils.h:584
QgsProcessing::TypeVectorPoint
@ TypeVectorPoint
Vector point layers.
Definition: qgsprocessing.h:49
qgsalgorithmprojectpointcartesian.h
QgsPoint::project
QgsPoint project(double distance, double azimuth, double inclination=90.0) const SIP_HOLDGIL
Returns a new point which corresponds to this point projected by a specified distance with specified ...
Definition: qgspoint.cpp:735
QgsGeometryCollection::numGeometries
int numGeometries() const SIP_HOLDGIL
Returns the number of geometries within the collection.
Definition: qgsgeometrycollection.h:58
QgsPoint::clone
QgsPoint * clone() const override
Clones the geometry by performing a deep copy.
Definition: qgspoint.cpp:104
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:170
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
QgsPropertyDefinition
Definition for a property.
Definition: qgsproperty.h:46
QgsGeometry::constGet
const QgsAbstractGeometry * constGet() const SIP_HOLDGIL
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
Definition: qgsgeometry.cpp:136
QgsMultiPoint
Multi point geometry collection.
Definition: qgsmultipoint.h:29
QgsMultiPoint::pointN
QgsPoint * pointN(int index)
Returns the point with the specified index.
Definition: qgsmultipoint.cpp:32
QgsWkbTypes::PointGeometry
@ PointGeometry
Definition: qgswkbtypes.h:142
QgsProcessingFeatureSource::Flag
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Definition: qgsprocessingutils.h:582
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsFeature::hasGeometry
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:230
QgsProcessingContext::expressionContext
QgsExpressionContext & expressionContext()
Returns the expression context.
Definition: qgsprocessingcontext.h:149
QgsProperty::valueAsDouble
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.
Definition: qgsproperty.cpp:690
QgsWkbTypes::geometryType
static GeometryType geometryType(Type type) SIP_HOLDGIL
Returns the geometry type for a WKB type, e.g., both MultiPolygon and CurvePolygon would have a Polyg...
Definition: qgswkbtypes.h:968
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsWkbTypes::isMultiType
static bool isMultiType(Type type) SIP_HOLDGIL
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:862
QgsProcessing::SourceType
SourceType
Data source types enum.
Definition: qgsprocessing.h:45
QgsGeometry::wkbType
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
Definition: qgsgeometry.cpp:357