QGIS API Documentation 3.99.0-Master (21b3aa880ba)
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#ifdef WITH_SFCGAL
24#include "qgssfcgalgeometry.h"
25#endif
26
28
29QString QgsApproximateMedialAxisAlgorithm::name() const
30{
31 return QStringLiteral( "approximatemedialaxis" );
32}
33
34QString QgsApproximateMedialAxisAlgorithm::displayName() const
35{
36 return QObject::tr( "Approximate medial axis" );
37}
38
39QStringList QgsApproximateMedialAxisAlgorithm::tags() const
40{
41 return QObject::tr( "medial,axis,create,lines,straight,skeleton" ).split( ',' );
42}
43
44QString QgsApproximateMedialAxisAlgorithm::group() const
45{
46 return QObject::tr( "Vector geometry" );
47}
48
49QString QgsApproximateMedialAxisAlgorithm::groupId() const
50{
51 return QStringLiteral( "vectorgeometry" );
52}
53
54QString QgsApproximateMedialAxisAlgorithm::shortHelpString() const
55{
56 return QObject::tr( "The Approximate Medial Axis algorithm generates a simplified skeleton of a shape by approximating its medial axis. \n\n"
57 "The output is a collection of lines that follow the central structure of the shape. The result is a thin, stable set "
58 "of curves that capture the main topology while ignoring noise.\n\n"
59 "This algorithm ignores the Z dimensions. If the geometry is 3D, the approximate medial axis will be calculated from "
60 "its 2D projection." );
61}
62
63QString QgsApproximateMedialAxisAlgorithm::shortDescription() const
64{
65 return QObject::tr( "Returns an approximate medial axis for a polygon layer input based on its straight skeleton." );
66}
67
68QgsApproximateMedialAxisAlgorithm *QgsApproximateMedialAxisAlgorithm::createInstance() const
69{
70 return new QgsApproximateMedialAxisAlgorithm();
71}
72
73QgsFields QgsApproximateMedialAxisAlgorithm::outputFields( const QgsFields &inputFields ) const
74{
75 QgsFields newFields;
76 newFields.append( QgsField( QStringLiteral( "length" ), QMetaType::Type::Double, QString(), 20, 6 ) );
77 return QgsProcessingUtils::combineFields( inputFields, newFields );
78}
79
80QList<int> QgsApproximateMedialAxisAlgorithm::inputLayerTypes() const
81{
82 return QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::VectorPolygon );
83}
84
85QString QgsApproximateMedialAxisAlgorithm::outputName() const
86{
87 return QObject::tr( "Medial axis" );
88}
89
90Qgis::ProcessingSourceType QgsApproximateMedialAxisAlgorithm::outputLayerType() const
91{
93}
94
95Qgis::WkbType QgsApproximateMedialAxisAlgorithm::outputWkbType( Qgis::WkbType inputWkbType ) const
96{
97 Q_UNUSED( inputWkbType )
99}
100
101bool QgsApproximateMedialAxisAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
102{
103 Q_UNUSED( parameters )
104 Q_UNUSED( context )
105 Q_UNUSED( feedback )
106
107#ifdef WITH_SFCGAL
108 return true;
109#else
110 throw QgsProcessingException( QObject::tr( "This processing algorithm requires a QGIS installation with SFCGAL support enabled. Please use a version of QGIS that includes SFCGAL." ) );
111#endif
112}
113
114QgsFeatureList QgsApproximateMedialAxisAlgorithm::processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
115{
116 Q_UNUSED( context )
117
118#ifdef WITH_SFCGAL
119 QgsFeature modifiedFeature = feature;
120 if ( modifiedFeature.hasGeometry() )
121 {
122 QgsGeometry outputGeometry;
123 QgsSfcgalGeometry inputSfcgalGeometry;
124 try
125 {
126 inputSfcgalGeometry = QgsSfcgalGeometry( modifiedFeature.geometry() );
127 }
128 catch ( const QgsSfcgalException &exception )
129 {
130 feedback->reportError( QObject::tr( "Cannot calculate approximate medial axis for feature %1: %2" ).arg( feature.id() ).arg( exception.what() ) );
131 modifiedFeature.clearGeometry();
132 }
133
134 if ( !inputSfcgalGeometry.isEmpty() )
135 {
136 try
137 {
138 std::unique_ptr<QgsSfcgalGeometry> outputSfcgalGeometry = inputSfcgalGeometry.approximateMedialAxis();
139 outputGeometry = QgsGeometry( outputSfcgalGeometry->asQgisGeometry() );
140 modifiedFeature.setGeometry( outputGeometry );
141 }
142 catch ( const QgsSfcgalException &medialAxisException )
143 {
144 feedback->reportError( QObject::tr( "Cannot calculate approximate medial axis for feature %1: %2" ).arg( feature.id() ).arg( medialAxisException.what() ) );
145 }
146 }
147
148 if ( !outputGeometry.isNull() )
149 {
150 QgsAttributes attrs = modifiedFeature.attributes();
151 attrs << outputGeometry.constGet()->length();
152 modifiedFeature.setAttributes( attrs );
153 }
154 else
155 {
156 QgsAttributes attrs = modifiedFeature.attributes();
157 attrs << QVariant();
158 modifiedFeature.setAttributes( attrs );
159 }
160 }
161 return QgsFeatureList() << modifiedFeature;
162#else
163 Q_UNUSED( feature )
164 Q_UNUSED( feedback )
165 throw QgsProcessingException( QObject::tr( "This processing algorithm requires a QGIS installation with SFCGAL support enabled. Please use a version of QGIS that includes SFCGAL." ) );
166#endif
167}
168
ProcessingSourceType
Processing data source types.
Definition qgis.h:3531
@ VectorPolygon
Vector polygon layers.
Definition qgis.h:3536
@ VectorLine
Vector line layers.
Definition qgis.h:3535
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:277
@ MultiLineString
MultiLineString.
Definition qgis.h:284
virtual double length() const
Returns the planar, 2-dimensional length of the geometry.
A vector of attributes.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsFeatureId id
Definition qgsfeature.h:66
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:54
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:73
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).
QList< QgsFeature > QgsFeatureList