25using namespace Qt::StringLiterals;
33QString QgsApproximateMedialAxisAlgorithm::name()
const
35 return u
"approximatemedialaxis"_s;
38QString QgsApproximateMedialAxisAlgorithm::displayName()
const
40 return QObject::tr(
"Approximate medial axis" );
43QStringList QgsApproximateMedialAxisAlgorithm::tags()
const
45 return QObject::tr(
"medial,axis,create,lines,straight,skeleton" ).split(
',' );
48QString QgsApproximateMedialAxisAlgorithm::group()
const
50 return QObject::tr(
"Vector geometry" );
53QString QgsApproximateMedialAxisAlgorithm::groupId()
const
55 return u
"vectorgeometry"_s;
58QString QgsApproximateMedialAxisAlgorithm::shortHelpString()
const
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."
71QString QgsApproximateMedialAxisAlgorithm::shortDescription()
const
73 return QObject::tr(
"Returns an approximate medial axis for a polygon layer input based on its straight skeleton." );
76QgsApproximateMedialAxisAlgorithm *QgsApproximateMedialAxisAlgorithm::createInstance()
const
78 return new QgsApproximateMedialAxisAlgorithm();
81QgsFields QgsApproximateMedialAxisAlgorithm::outputFields(
const QgsFields &inputFields )
const
84 newFields.
append(
QgsField( u
"length"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
88QList<int> QgsApproximateMedialAxisAlgorithm::inputLayerTypes()
const
93QString QgsApproximateMedialAxisAlgorithm::outputName()
const
95 return QObject::tr(
"Medial axis" );
105 Q_UNUSED( inputWkbType )
109void QgsApproximateMedialAxisAlgorithm::initParameters(
const QVariantMap & )
116 Q_UNUSED( parameters )
121 mExtendToEdges = parameterAsBool( parameters,
"EXTEND_TO_EDGES", context );
123#if SFCGAL_VERSION_NUM < SFCGAL_MAKE_VERSION( 2, 3, 0 )
124 if ( mExtendToEdges )
126 throw QgsProcessingException( QObject::tr(
"The \"extend to the polygon boundary\" option requires a QGIS build based on SFCGAL 2.3 or later." ) );
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." ) );
142 if ( modifiedFeature.hasGeometry() )
145 QgsSfcgalGeometry inputSfcgalGeometry;
148 inputSfcgalGeometry = QgsSfcgalGeometry( modifiedFeature.geometry() );
152 feedback->
reportError( QObject::tr(
"Cannot calculate approximate medial axis for feature %1: %2" ).arg( feature.
id() ).arg( exception.
what() ) );
153 modifiedFeature.clearGeometry();
156 if ( !inputSfcgalGeometry.isEmpty() )
160 std::unique_ptr<QgsSfcgalGeometry> outputSfcgalGeometry = inputSfcgalGeometry.approximateMedialAxis( mExtendToEdges );
161 outputGeometry =
QgsGeometry( outputSfcgalGeometry->asQgisGeometry() );
162 modifiedFeature.setGeometry( outputGeometry );
166 feedback->
reportError( QObject::tr(
"Cannot calculate approximate medial axis for feature %1: %2" ).arg( feature.
id() ).arg( medialAxisException.
what() ) );
170 if ( !outputGeometry.
isNull() )
174 modifiedFeature.setAttributes( attrs );
180 modifiedFeature.setAttributes( attrs );
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." ) );
ProcessingSourceType
Processing data source types.
@ VectorPolygon
Vector polygon layers.
@ VectorLine
Vector line layers.
WkbType
The WKB type describes the number of dimensions a geometry has.
@ MultiLineString
MultiLineString.
virtual double length() const
Returns the planar, 2-dimensional length of the geometry.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
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