QGIS API Documentation 3.99.0-Master (09f76ad7019)
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( "The Approximate Medial Axis algorithm generates a simplified skeleton of a shape by approximating its medial axis. \n\n"
61 "The output is a collection of lines that follow the central structure of the shape. The result is a thin, stable set "
62 "of curves that capture the main topology while ignoring noise.\n\n"
63 "This algorithm ignores the Z dimensions. If the geometry is 3D, the approximate medial axis will be calculated from "
64 "its 2D projection." );
65}
66
67QString QgsApproximateMedialAxisAlgorithm::shortDescription() const
68{
69 return QObject::tr( "Returns an approximate medial axis for a polygon layer input based on its straight skeleton." );
70}
71
72QgsApproximateMedialAxisAlgorithm *QgsApproximateMedialAxisAlgorithm::createInstance() const
73{
74 return new QgsApproximateMedialAxisAlgorithm();
75}
76
77QgsFields QgsApproximateMedialAxisAlgorithm::outputFields( const QgsFields &inputFields ) const
78{
79 QgsFields newFields;
80 newFields.append( QgsField( u"length"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
81 return QgsProcessingUtils::combineFields( inputFields, newFields );
82}
83
84QList<int> QgsApproximateMedialAxisAlgorithm::inputLayerTypes() const
85{
86 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
87}
88
89QString QgsApproximateMedialAxisAlgorithm::outputName() const
90{
91 return QObject::tr( "Medial axis" );
92}
93
94Qgis::ProcessingSourceType QgsApproximateMedialAxisAlgorithm::outputLayerType() const
95{
97}
98
99Qgis::WkbType QgsApproximateMedialAxisAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
100{
101 Q_UNUSED( inputWkbType )
103}
104
105bool QgsApproximateMedialAxisAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
106{
107 Q_UNUSED( parameters )
108 Q_UNUSED( context )
109 Q_UNUSED( feedback )
110
111#ifdef WITH_SFCGAL
112 return true;
113#else
114 throw QgsProcessingException( QObject::tr( "This processing algorithm requires a QGIS installation with SFCGAL support enabled. Please use a version of QGIS that includes SFCGAL." ) );
115#endif
116}
117
118QgsFeatureList QgsApproximateMedialAxisAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
119{
120 Q_UNUSED( context )
121
122#ifdef WITH_SFCGAL
123 QgsFeature modifiedFeature = feature;
124 if ( modifiedFeature.hasGeometry() )
125 {
126 QgsGeometry outputGeometry;
127 QgsSfcgalGeometry inputSfcgalGeometry;
128 try
129 {
130 inputSfcgalGeometry = QgsSfcgalGeometry( modifiedFeature.geometry() );
131 }
132 catch ( const QgsSfcgalException &exception )
133 {
134 feedback->reportError( QObject::tr( "Cannot calculate approximate medial axis for feature %1: %2" ).arg( feature.id() ).arg( exception.what() ) );
135 modifiedFeature.clearGeometry();
136 }
137
138 if ( !inputSfcgalGeometry.isEmpty() )
139 {
140 try
141 {
142 std::unique_ptr<QgsSfcgalGeometry> outputSfcgalGeometry = inputSfcgalGeometry.approximateMedialAxis();
143 outputGeometry = QgsGeometry( outputSfcgalGeometry->asQgisGeometry() );
144 modifiedFeature.setGeometry( outputGeometry );
145 }
146 catch ( const QgsSfcgalException &medialAxisException )
147 {
148 feedback->reportError( QObject::tr( "Cannot calculate approximate medial axis for feature %1: %2" ).arg( feature.id() ).arg( medialAxisException.what() ) );
149 }
150 }
151
152 if ( !outputGeometry.isNull() )
153 {
154 QgsAttributes attrs = modifiedFeature.attributes();
155 attrs << outputGeometry.constGet()->length();
156 modifiedFeature.setAttributes( attrs );
157 }
158 else
159 {
160 QgsAttributes attrs = modifiedFeature.attributes();
161 attrs << QVariant();
162 modifiedFeature.setAttributes( attrs );
163 }
164 }
165 return QgsFeatureList() << modifiedFeature;
166#else
167 Q_UNUSED( feature )
168 Q_UNUSED( feedback )
169 throw QgsProcessingException( QObject::tr( "This processing algorithm requires a QGIS installation with SFCGAL support enabled. Please use a version of QGIS that includes SFCGAL." ) );
170#endif
171}
172
ProcessingSourceType
Processing data source types.
Definition qgis.h:3602
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3607
@ VectorLine
Vector line layers.
Definition qgis.h:3606
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
@ MultiLineString
MultiLineString.
Definition qgis.h:287
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: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.
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.
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