QGIS API Documentation 4.1.0-Master (376402f9aeb)
Loading...
Searching...
No Matches
qgsalgorithmapproximatemedialaxis.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmapproximatemedialaxis.cpp
3 ---------------------
4 begin : September 2025
5 copyright : (C) 2025 by Jean Felder
6 email : jean dot felder at oslandia 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
18
20
21#include "qgsexception.h"
22
23#include <QString>
24
25using namespace Qt::StringLiterals;
26
27#ifdef WITH_SFCGAL
28#include "qgssfcgalgeometry.h"
29#endif
30
32
33QString QgsApproximateMedialAxisAlgorithm::name() const
34{
35 return u"approximatemedialaxis"_s;
36}
37
38QString QgsApproximateMedialAxisAlgorithm::displayName() const
39{
40 return QObject::tr( "Approximate medial axis" );
41}
42
43QStringList QgsApproximateMedialAxisAlgorithm::tags() const
44{
45 return QObject::tr( "medial,axis,create,lines,straight,skeleton" ).split( ',' );
46}
47
48QString QgsApproximateMedialAxisAlgorithm::group() const
49{
50 return QObject::tr( "Vector geometry" );
51}
52
53QString QgsApproximateMedialAxisAlgorithm::groupId() const
54{
55 return u"vectorgeometry"_s;
56}
57
58QString QgsApproximateMedialAxisAlgorithm::shortHelpString() const
59{
60 return QObject::tr(
61 "The Approximate Medial Axis algorithm generates a simplified skeleton of a shape by approximating its medial axis. \n\n"
62 "The output is a collection of lines that follow the central structure of the shape. The result is a thin, stable set "
63 "of curves that capture the main topology while ignoring noise.\n\n"
64 "This algorithm ignores the Z dimensions. If the geometry is 3D, the approximate medial axis will be calculated from "
65 "its 2D projection.\n\n"
66 "The option \"Extend end points to the polygon boundary\" extends the medial axis so that its endpoints reach "
67 "the boundary of the input polygon. This option is only available with SFCGAL version 2.3 or higher."
68 );
69}
70
71QString QgsApproximateMedialAxisAlgorithm::shortDescription() const
72{
73 return QObject::tr( "Returns an approximate medial axis for a polygon layer input based on its straight skeleton." );
74}
75
76QgsApproximateMedialAxisAlgorithm *QgsApproximateMedialAxisAlgorithm::createInstance() const
77{
78 return new QgsApproximateMedialAxisAlgorithm();
79}
80
81QgsFields QgsApproximateMedialAxisAlgorithm::outputFields( const QgsFields &inputFields ) const
82{
83 QgsFields newFields;
84 newFields.append( QgsField( u"length"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
85 return QgsProcessingUtils::combineFields( inputFields, newFields );
86}
87
88QList<int> QgsApproximateMedialAxisAlgorithm::inputLayerTypes() const
89{
90 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
91}
92
93QString QgsApproximateMedialAxisAlgorithm::outputName() const
94{
95 return QObject::tr( "Medial axis" );
96}
97
98Qgis::ProcessingSourceType QgsApproximateMedialAxisAlgorithm::outputLayerType() const
99{
101}
102
103Qgis::WkbType QgsApproximateMedialAxisAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
104{
105 Q_UNUSED( inputWkbType )
107}
108
109void QgsApproximateMedialAxisAlgorithm::initParameters( const QVariantMap & )
110{
111 addParameter( new QgsProcessingParameterBoolean( u"EXTEND_TO_EDGES"_s, QObject::tr( "Extend endpoints to the polygon boundary" ), false ) );
112}
113
114bool QgsApproximateMedialAxisAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
115{
116 Q_UNUSED( parameters )
117 Q_UNUSED( context )
118 Q_UNUSED( feedback )
119
120#ifdef WITH_SFCGAL
121 mExtendToEdges = parameterAsBool( parameters, "EXTEND_TO_EDGES", context );
122
123#if SFCGAL_VERSION_NUM < SFCGAL_MAKE_VERSION( 2, 3, 0 )
124 if ( mExtendToEdges )
125 {
126 throw QgsProcessingException( QObject::tr( "The \"extend to the polygon boundary\" option requires a QGIS build based on SFCGAL 2.3 or later." ) );
127 }
128#endif
129
130 return true;
131#else
132 throw QgsProcessingException( QObject::tr( "This processing algorithm requires a QGIS installation with SFCGAL support enabled. Please use a version of QGIS that includes SFCGAL." ) );
133#endif
134}
135
136QgsFeatureList QgsApproximateMedialAxisAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
137{
138 Q_UNUSED( context )
139
140#ifdef WITH_SFCGAL
141 QgsFeature modifiedFeature = feature;
142 if ( modifiedFeature.hasGeometry() )
143 {
144 QgsGeometry outputGeometry;
145 QgsSfcgalGeometry inputSfcgalGeometry;
146 try
147 {
148 inputSfcgalGeometry = QgsSfcgalGeometry( modifiedFeature.geometry() );
149 }
150 catch ( const QgsSfcgalException &exception )
151 {
152 feedback->reportError( QObject::tr( "Cannot calculate approximate medial axis for feature %1: %2" ).arg( feature.id() ).arg( exception.what() ) );
153 modifiedFeature.clearGeometry();
154 }
155
156 if ( !inputSfcgalGeometry.isEmpty() )
157 {
158 try
159 {
160 std::unique_ptr<QgsSfcgalGeometry> outputSfcgalGeometry = inputSfcgalGeometry.approximateMedialAxis( mExtendToEdges );
161 outputGeometry = QgsGeometry( outputSfcgalGeometry->asQgisGeometry() );
162 modifiedFeature.setGeometry( outputGeometry );
163 }
164 catch ( const QgsSfcgalException &medialAxisException )
165 {
166 feedback->reportError( QObject::tr( "Cannot calculate approximate medial axis for feature %1: %2" ).arg( feature.id() ).arg( medialAxisException.what() ) );
167 }
168 }
169
170 if ( !outputGeometry.isNull() )
171 {
172 QgsAttributes attrs = modifiedFeature.attributes();
173 attrs << outputGeometry.constGet()->length();
174 modifiedFeature.setAttributes( attrs );
175 }
176 else
177 {
178 QgsAttributes attrs = modifiedFeature.attributes();
179 attrs << QVariant();
180 modifiedFeature.setAttributes( attrs );
181 }
182 }
183 return QgsFeatureList() << modifiedFeature;
184#else
185 Q_UNUSED( feature )
186 Q_UNUSED( feedback )
187 throw QgsProcessingException( QObject::tr( "This processing algorithm requires a QGIS installation with SFCGAL support enabled. Please use a version of QGIS that includes SFCGAL." ) );
188#endif
189}
190
ProcessingSourceType
Processing data source types.
Definition qgis.h:3712
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3717
@ VectorLine
Vector line layers.
Definition qgis.h:3716
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
@ MultiLineString
MultiLineString.
Definition qgis.h:301
virtual double length() const
Returns the planar, 2-dimensional length of the geometry.
A vector of attributes.
QString what() const
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
QgsFeatureId id
Definition qgsfeature.h:68
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:75
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.
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.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A boolean 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).
Custom exception class for SfCGAL related operations.
QList< QgsFeature > QgsFeatureList