QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
qgsalgorithmdelaunaytriangulation.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmdelaunaytriangulation.cpp
3 ---------------------
4 begin : July 2023
5 copyright : (C) 2023 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 "qgsattributes.h"
21#include "qgsmultipoint.h"
22#include "qgsspatialindex.h"
23
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
29
30QString QgsDelaunayTriangulationAlgorithm::name() const
31{
32 return u"delaunaytriangulation"_s;
33}
34
35QString QgsDelaunayTriangulationAlgorithm::displayName() const
36{
37 return QObject::tr( "Delaunay triangulation" );
38}
39
40QStringList QgsDelaunayTriangulationAlgorithm::tags() const
41{
42 return QObject::tr( "delaunay,triangulation,polygons,voronoi" ).split( ',' );
43}
44
45QString QgsDelaunayTriangulationAlgorithm::group() const
46{
47 return QObject::tr( "Vector geometry" );
48}
49
50QString QgsDelaunayTriangulationAlgorithm::groupId() const
51{
52 return u"vectorgeometry"_s;
53}
54
55QString QgsDelaunayTriangulationAlgorithm::shortHelpString() const
56{
57 return QObject::tr( "This algorithm creates a polygon layer with the Delaunay triangulation corresponding to a points layer." );
58}
59
60QString QgsDelaunayTriangulationAlgorithm::shortDescription() const
61{
62 return QObject::tr( "Creates a polygon layer with the Delaunay triangulation corresponding to a points layer." );
63}
64
65QgsDelaunayTriangulationAlgorithm *QgsDelaunayTriangulationAlgorithm::createInstance() const
66{
67 return new QgsDelaunayTriangulationAlgorithm();
68}
69
70void QgsDelaunayTriangulationAlgorithm::initAlgorithm( const QVariantMap & )
71{
72 addParameter( new QgsProcessingParameterFeatureSource( u"INPUT"_s, QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPoint ) ) );
73 auto toleranceParam = std::make_unique<QgsProcessingParameterNumber>( u"TOLERANCE"_s, QObject::tr( "Tolerance" ), Qgis::ProcessingNumberParameterType::Double, 0, true, 0 );
74 toleranceParam->setHelp( QObject::tr( "Specifies an optional snapping tolerance which can be used to improve the robustness of the triangulation" ) );
75 addParameter( toleranceParam.release() );
76 addParameter( new QgsProcessingParameterBoolean( u"ADD_ATTRIBUTES"_s, QObject::tr( "Add point IDs to output" ), true ) );
77 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Delaunay triangulation" ), Qgis::ProcessingSourceType::VectorPolygon ) );
78}
79
80QVariantMap QgsDelaunayTriangulationAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
81{
82 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u"INPUT"_s, context ) );
83 if ( !source )
84 throw QgsProcessingException( invalidSourceError( parameters, u"INPUT"_s ) );
85
86 if ( source->featureCount() < 3 )
87 throw QgsProcessingException( QObject::tr( "Input layer should contain at least 3 points." ) );
88
89 const double tolerance = parameterAsDouble( parameters, u"TOLERANCE"_s, context );
90 const bool addAttributes = parameterAsBool( parameters, u"ADD_ATTRIBUTES"_s, context );
91
92 QgsFields fields;
93 if ( addAttributes )
94 {
95 fields.append( QgsField( u"POINTA"_s, QMetaType::Type::LongLong ) );
96 fields.append( QgsField( u"POINTB"_s, QMetaType::Type::LongLong ) );
97 fields.append( QgsField( u"POINTC"_s, QMetaType::Type::LongLong ) );
98 }
99 else
100 {
101 fields.append( QgsField( u"id"_s, QMetaType::Type::LongLong ) );
102 }
103
104 QString dest;
105 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, fields, Qgis::WkbType::Polygon, source->sourceCrs() ) );
106 if ( !sink )
107 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
108
110
111 QgsGeometry allPoints;
112
113 long long i = 0;
114 const double step = source->featureCount() > 0 ? 50.0 / source->featureCount() : 1;
115
116 const QgsSpatialIndex index( it, [&]( const QgsFeature &f ) -> bool {
117 i++;
118 if ( feedback->isCanceled() )
119 return false;
120
121 feedback->setProgress( i * step );
122
123 if ( !f.hasGeometry() )
124 return true;
125
126 const QgsAbstractGeometry *geom = f.geometry().constGet();
127 if ( QgsWkbTypes::isMultiType( geom->wkbType() ) )
128 {
130 for ( auto pit = mp.const_parts_begin(); pit != mp.const_parts_end(); ++pit )
131 {
133 }
134 }
135 else
136 {
138 }
139
141
142 const QgsGeometry triangulation = allPoints.delaunayTriangulation( tolerance );
143
144 if ( !triangulation.isEmpty() )
145 {
146 const QVector<QgsGeometry> collection = triangulation.asGeometryCollection();
147 for ( int i = 0; i < collection.length(); i++ )
148 {
149 if ( feedback->isCanceled() )
150 {
151 break;
152 }
153 QgsFeature f;
154 f.setFields( fields );
155 f.setGeometry( collection[i] );
156 if ( addAttributes )
157 {
158 const QList<QgsFeatureId> nearest = index.nearestNeighbor( collection[i], 3 );
159 QgsAttributes attrs;
160 for ( int j = 0; j < 3; j++ )
161 {
162 attrs << nearest.at( j );
163 }
164 f.setAttributes( attrs );
165 }
166 else
167 {
168 f.setAttributes( QgsAttributes() << i );
169 }
170
171 if ( !sink->addFeature( f, QgsFeatureSink::FastInsert ) )
172 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
173 feedback->setProgress( 50 + i * step );
174 }
175 }
176
177 sink->finalize();
178
179 QVariantMap outputs;
180 outputs.insert( u"OUTPUT"_s, dest );
181 return outputs;
182}
183
@ VectorPoint
Vector point layers.
Definition qgis.h:3605
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3607
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
Definition qgis.h:3782
@ Point
Point.
Definition qgis.h:282
@ Polygon
Polygon.
Definition qgis.h:284
@ Double
Double/float values.
Definition qgis.h:3875
Abstract base class for all geometries.
Qgis::WkbType wkbType() const
Returns the WKB type of the geometry.
A vector of attributes.
Wrapper for iterator of features from vector data provider or vector layer.
Wraps a request for features to a vector layer (or directly its vector data provider).
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
void setFields(const QgsFields &fields, bool initAttributes=false)
Assigns a field map with the feature to allow attribute access by attribute name.
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.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:55
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:63
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
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:76
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::GeometryOperationResult addPartV2(const QVector< QgsPointXY > &points, Qgis::WkbType wkbType=Qgis::WkbType::Unknown)
Adds a new part to a the geometry.
QgsGeometry delaunayTriangulation(double tolerance=0.0, bool edgesOnly=false) const
Returns the Delaunay triangulation for the vertices of the geometry.
Multi point geometry collection.
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 boolean parameter for processing algorithms.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A spatial index for QgsFeature objects.
@ FlagStoreFeatureGeometries
Indicates that the spatial index should also store feature geometries. This requires more memory,...
static Q_INVOKABLE bool isMultiType(Qgis::WkbType type)
Returns true if the WKB type is a multi type.
T qgsgeometry_cast(QgsAbstractGeometry *geom)