QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgsalgorithmextractspecificvertices.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmextractspecificvertices.cpp
3 --------------------------
4 begin : November 2019
5 copyright : (C) 2019 by Alexander Bruy
6 email : alexander dot bruy 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 "qgsabstractgeometry.h"
21#include "qgsgeometryutils.h"
22
24
25QString QgsExtractSpecificVerticesAlgorithm::name() const
26{
27 return QStringLiteral( "extractspecificvertices" );
28}
29
30QString QgsExtractSpecificVerticesAlgorithm::displayName() const
31{
32 return QObject::tr( "Extract specific vertices" );
33}
34
35QStringList QgsExtractSpecificVerticesAlgorithm::tags() const
36{
37 return QObject::tr( "points,vertex,nodes" ).split( ',' );
38}
39
40QString QgsExtractSpecificVerticesAlgorithm::group() const
41{
42 return QObject::tr( "Vector geometry" );
43}
44
45QString QgsExtractSpecificVerticesAlgorithm::groupId() const
46{
47 return QStringLiteral( "vectorgeometry" );
48}
49
50QString QgsExtractSpecificVerticesAlgorithm::shortHelpString() const
51{
52 return QObject::tr( "This algorithm takes a vector layer and generates a point layer with points "
53 "representing specific vertices in the input geometries. For instance, this algorithm "
54 "can be used to extract the first or last vertices in the geometry. The attributes associated "
55 "to each point are the same ones associated to the feature that the point belongs to." )
56 + QStringLiteral( "\n\n" ) + QObject::tr( "The vertex indices parameter accepts a comma separated string specifying the indices of the "
57 "vertices to extract. The first vertex corresponds to an index of 0, the second vertex has an "
58 "index of 1, etc. Negative indices can be used to find vertices at the end of the geometry, "
59 "e.g., an index of -1 corresponds to the last vertex, -2 corresponds to the second last vertex, etc." )
60 + QStringLiteral( "\n\n" ) + QObject::tr( "Additional fields are added to the points indicating the specific vertex position (e.g., 0, -1, etc), "
61 "the original vertex index, the vertex’s part and its index within the part (as well as its ring for "
62 "polygons), distance along the original geometry and bisector angle of vertex for the original geometry." );
63}
64
65QString QgsExtractSpecificVerticesAlgorithm::shortDescription() const
66{
67 return QObject::tr( "Generates a point layer with points representing specific vertices in the input geometries." );
68}
69
70Qgis::ProcessingAlgorithmDocumentationFlags QgsExtractSpecificVerticesAlgorithm::documentationFlags() const
71{
73}
74
75QString QgsExtractSpecificVerticesAlgorithm::outputName() const
76{
77 return QObject::tr( "Vertices" );
78}
79
80QgsExtractSpecificVerticesAlgorithm *QgsExtractSpecificVerticesAlgorithm::createInstance() const
81{
82 return new QgsExtractSpecificVerticesAlgorithm();
83}
84
85Qgis::ProcessingSourceType QgsExtractSpecificVerticesAlgorithm::outputLayerType() const
86{
88}
89
90QgsFields QgsExtractSpecificVerticesAlgorithm::outputFields( const QgsFields &inputFields ) const
91{
92 QgsFields newFields;
93 newFields.append( QgsField( QStringLiteral( "vertex_pos" ), QMetaType::Type::Int ) );
94 newFields.append( QgsField( QStringLiteral( "vertex_index" ), QMetaType::Type::Int ) );
95 newFields.append( QgsField( QStringLiteral( "vertex_part" ), QMetaType::Type::Int ) );
96 if ( mGeometryType == Qgis::GeometryType::Polygon )
97 {
98 newFields.append( QgsField( QStringLiteral( "vertex_part_ring" ), QMetaType::Type::Int ) );
99 }
100 newFields.append( QgsField( QStringLiteral( "vertex_part_index" ), QMetaType::Type::Int ) );
101 newFields.append( QgsField( QStringLiteral( "distance" ), QMetaType::Type::Double ) );
102 newFields.append( QgsField( QStringLiteral( "angle" ), QMetaType::Type::Double ) );
103
104 return QgsProcessingUtils::combineFields( inputFields, newFields );
105}
106
107Qgis::WkbType QgsExtractSpecificVerticesAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
108{
109 Qgis::WkbType outputWkbType = Qgis::WkbType::Point;
110 if ( QgsWkbTypes::hasM( inputWkbType ) )
111 {
112 outputWkbType = QgsWkbTypes::addM( outputWkbType );
113 }
114 if ( QgsWkbTypes::hasZ( inputWkbType ) )
115 {
116 outputWkbType = QgsWkbTypes::addZ( outputWkbType );
117 }
118
119 return outputWkbType;
120}
121
122Qgis::ProcessingFeatureSourceFlags QgsExtractSpecificVerticesAlgorithm::sourceFlags() const
123{
125}
126
127QgsFeatureSink::SinkFlags QgsExtractSpecificVerticesAlgorithm::sinkFlags() const
128{
130}
131
132void QgsExtractSpecificVerticesAlgorithm::initParameters( const QVariantMap & )
133{
134 addParameter( new QgsProcessingParameterString( QStringLiteral( "VERTICES" ), QObject::tr( "Vertex indices" ), QStringLiteral( "0" ) ) );
135}
136
137bool QgsExtractSpecificVerticesAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
138{
139 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral( "INPUT" ), context ) );
140 if ( !source )
141 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
142
143 mGeometryType = QgsWkbTypes::geometryType( source->wkbType() );
144
145 const QString verticesString = parameterAsString( parameters, QStringLiteral( "VERTICES" ), context );
146 const QStringList verticesList = verticesString.split( ',', Qt::SkipEmptyParts );
147 for ( const QString &vertex : verticesList )
148 {
149 bool ok = false;
150 const int i = vertex.toInt( &ok );
151 if ( ok )
152 {
153 mIndices << i;
154 }
155 else
156 {
157 throw QgsProcessingException( QObject::tr( "'%1' is not a valid vertex index" ).arg( vertex ) );
158 }
159 }
160
161 return true;
162}
163
164QgsFeatureList QgsExtractSpecificVerticesAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &, QgsProcessingFeedback * )
165{
166 QgsFeatureList outputFeatures;
167
168 QgsFeature f = feature;
169 const QgsGeometry inputGeom = f.geometry();
170 if ( inputGeom.isEmpty() )
171 {
172 QgsAttributes attrs = f.attributes();
173 attrs << QVariant()
174 << QVariant()
175 << QVariant();
176 if ( mGeometryType == Qgis::GeometryType::Polygon )
177 {
178 attrs << QVariant();
179 }
180 attrs << QVariant()
181 << QVariant()
182 << QVariant();
183
184 f.clearGeometry();
185 f.setAttributes( attrs );
186 outputFeatures << f;
187 }
188 else
189 {
190 int vertexIndex;
191 const int totalVertices = inputGeom.constGet()->nCoordinates();
192 for ( const int vertex : mIndices )
193 {
194 if ( vertex < 0 )
195 {
196 vertexIndex = totalVertices + vertex;
197 }
198 else
199 {
200 vertexIndex = vertex;
201 }
202
203 if ( vertexIndex < 0 || vertexIndex >= totalVertices )
204 continue;
205
206 QgsVertexId vertexId;
207 inputGeom.vertexIdFromVertexNr( vertexIndex, vertexId );
208
209 const double distance = inputGeom.distanceToVertex( vertexIndex );
210 const double angle = inputGeom.angleAtVertex( vertexIndex ) * 180 / M_PI;
211
212 QgsFeature outFeature = QgsFeature();
213 QgsAttributes attrs = f.attributes();
214 attrs << vertex
215 << vertexIndex
216 << vertexId.part;
217 if ( mGeometryType == Qgis::GeometryType::Polygon )
218 {
219 attrs << vertexId.ring;
220 }
221 attrs << vertexId.vertex
222 << distance
223 << angle;
224
225 outFeature.setAttributes( attrs );
226 const QgsPoint point = inputGeom.vertexAt( vertexIndex );
227 outFeature.setGeometry( QgsGeometry( point.clone() ) );
228 outputFeatures << outFeature;
229 }
230 }
231
232 return outputFeatures;
233}
234
ProcessingSourceType
Processing data source types.
Definition qgis.h:3399
@ VectorPoint
Vector point layers.
@ Polygon
Polygons.
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3496
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3588
virtual int nCoordinates() const
Returns the number of nodes contained in the geometry.
A vector of attributes.
QFlags< SinkFlag > SinkFlags
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsAttributes attributes
Definition qgsfeature.h:67
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
QgsGeometry geometry
Definition qgsfeature.h:69
void clearGeometry()
Removes any geometry associated with the feature.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:70
A geometry is the spatial representation of a feature.
bool vertexIdFromVertexNr(int number, QgsVertexId &id) const
Calculates the vertex ID from a vertex number.
QgsPoint vertexAt(int atVertex) const
Returns coordinates of a vertex.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
double distanceToVertex(int vertex) const
Returns the distance along this geometry from its first vertex to the specified vertex.
double angleAtVertex(int vertex) const
Returns the bisector angle for this geometry at the specified vertex.
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:105
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A string parameter for processing algorithms.
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix=QString())
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
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 Qgis::WkbType addM(Qgis::WkbType type)
Adds the m dimension to a WKB type and returns the new type.
static Qgis::WkbType addZ(Qgis::WkbType type)
Adds the z dimension to a WKB type and returns the new type.
static bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
QList< QgsFeature > QgsFeatureList
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:30
int vertex
Vertex number.
Definition qgsvertexid.h:94
int part
Part number.
Definition qgsvertexid.h:88
int ring
Ring number.
Definition qgsvertexid.h:91