QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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."
66 );
67}
68
69QString QgsApproximateMedialAxisAlgorithm::shortDescription() const
70{
71 return QObject::tr( "Returns an approximate medial axis for a polygon layer input based on its straight skeleton." );
72}
73
74QgsApproximateMedialAxisAlgorithm *QgsApproximateMedialAxisAlgorithm::createInstance() const
75{
76 return new QgsApproximateMedialAxisAlgorithm();
77}
78
79QgsFields QgsApproximateMedialAxisAlgorithm::outputFields( const QgsFields &inputFields ) const
80{
81 QgsFields newFields;
82 newFields.append( QgsField( u"length"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
83 return QgsProcessingUtils::combineFields( inputFields, newFields );
84}
85
86QList<int> QgsApproximateMedialAxisAlgorithm::inputLayerTypes() const
87{
88 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
89}
90
91QString QgsApproximateMedialAxisAlgorithm::outputName() const
92{
93 return QObject::tr( "Medial axis" );
94}
95
96Qgis::ProcessingSourceType QgsApproximateMedialAxisAlgorithm::outputLayerType() const
97{
99}
100
101Qgis::WkbType QgsApproximateMedialAxisAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
102{
103 Q_UNUSED( inputWkbType )
105}
106
107bool QgsApproximateMedialAxisAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
108{
109 Q_UNUSED( parameters )
110 Q_UNUSED( context )
111 Q_UNUSED( feedback )
112
113#ifdef WITH_SFCGAL
114 return true;
115#else
116 throw QgsProcessingException( QObject::tr( "This processing algorithm requires a QGIS installation with SFCGAL support enabled. Please use a version of QGIS that includes SFCGAL." ) );
117#endif
118}
119
120QgsFeatureList QgsApproximateMedialAxisAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
121{
122 Q_UNUSED( context )
123
124#ifdef WITH_SFCGAL
125 QgsFeature modifiedFeature = feature;
126 if ( modifiedFeature.hasGeometry() )
127 {
128 QgsGeometry outputGeometry;
129 QgsSfcgalGeometry inputSfcgalGeometry;
130 try
131 {
132 inputSfcgalGeometry = QgsSfcgalGeometry( modifiedFeature.geometry() );
133 }
134 catch ( const QgsSfcgalException &exception )
135 {
136 feedback->reportError( QObject::tr( "Cannot calculate approximate medial axis for feature %1: %2" ).arg( feature.id() ).arg( exception.what() ) );
137 modifiedFeature.clearGeometry();
138 }
139
140 if ( !inputSfcgalGeometry.isEmpty() )
141 {
142 try
143 {
144 std::unique_ptr<QgsSfcgalGeometry> outputSfcgalGeometry = inputSfcgalGeometry.approximateMedialAxis();
145 outputGeometry = QgsGeometry( outputSfcgalGeometry->asQgisGeometry() );
146 modifiedFeature.setGeometry( outputGeometry );
147 }
148 catch ( const QgsSfcgalException &medialAxisException )
149 {
150 feedback->reportError( QObject::tr( "Cannot calculate approximate medial axis for feature %1: %2" ).arg( feature.id() ).arg( medialAxisException.what() ) );
151 }
152 }
153
154 if ( !outputGeometry.isNull() )
155 {
156 QgsAttributes attrs = modifiedFeature.attributes();
157 attrs << outputGeometry.constGet()->length();
158 modifiedFeature.setAttributes( attrs );
159 }
160 else
161 {
162 QgsAttributes attrs = modifiedFeature.attributes();
163 attrs << QVariant();
164 modifiedFeature.setAttributes( attrs );
165 }
166 }
167 return QgsFeatureList() << modifiedFeature;
168#else
169 Q_UNUSED( feature )
170 Q_UNUSED( feedback )
171 throw QgsProcessingException( QObject::tr( "This processing algorithm requires a QGIS installation with SFCGAL support enabled. Please use a version of QGIS that includes SFCGAL." ) );
172#endif
173}
174
ProcessingSourceType
Processing data source types.
Definition qgis.h:3645
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3650
@ VectorLine
Vector line layers.
Definition qgis.h:3649
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.
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