28using namespace Qt::StringLiterals;
32QString QgsConcaveHullAlgorithm::name()
const
34 return u
"concavehull"_s;
37QString QgsConcaveHullAlgorithm::displayName()
const
39 return QObject::tr(
"Concave hull (by layer)" );
42QStringList QgsConcaveHullAlgorithm::tags()
const
44 return QObject::tr(
"concave,hull,bounds,bounding" ).split(
',' );
47QString QgsConcaveHullAlgorithm::group()
const
49 return QObject::tr(
"Vector geometry" );
52QString QgsConcaveHullAlgorithm::groupId()
const
54 return u
"vectorgeometry"_s;
57QString QgsConcaveHullAlgorithm::shortHelpString()
const
59 return QObject::tr(
"This algorithm computes the concave hull covering all features from an input point layer." )
61 + QObject::tr(
"See the 'Concave hull (by feature)' algorithm for a concave hull calculation which covers individual features from a layer." );
64QString QgsConcaveHullAlgorithm::shortDescription()
const
66 return QObject::tr(
"Computes the concave hull of all features from an input point layer." );
69QgsConcaveHullAlgorithm *QgsConcaveHullAlgorithm::createInstance()
const
71 return new QgsConcaveHullAlgorithm();
74void QgsConcaveHullAlgorithm::initAlgorithm(
const QVariantMap & )
79 addParameter(
new QgsProcessingParameterBoolean( u
"NO_MULTIGEOMETRY"_s, QObject::tr(
"Split multipart geometry into singleparts" ),
false ) );
85 mSource.reset( parameterAsSource( parameters, u
"INPUT"_s, context ) );
89 if ( mSource->featureCount() < 3 )
92 mStep = mSource->featureCount() > 0 ? 50.0 /
static_cast<double>( mSource->featureCount() ) : 1;
94 mPercentage = parameterAsDouble( parameters, u
"ALPHA"_s, context );
95 mAllowHoles = parameterAsBool( parameters, u
"HOLES"_s, context );
96 mSplitMultipart = parameterAsBool( parameters, u
"NO_MULTIGEOMETRY"_s, context );
103 QGS_MARK_ALGORITHM_SOURCE
106 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest,
QgsFields(),
Qgis::WkbType::Polygon, mSource->sourceCrs() ) );
110#if GEOS_VERSION_NUM < GEOS_COMPUTE_VERSION( 3, 11, 0 )
111 concaveHullQgis( sink, parameters, context, feedback );
113 concaveHullGeos( sink, parameters, feedback );
120 outputs.insert( u
"OUTPUT"_s, dest );
124void QgsConcaveHullAlgorithm::concaveHullGeos( std::unique_ptr<QgsFeatureSink> &sink,
const QVariantMap ¶meters,
QgsProcessingFeedback *feedback )
137 feedback->
setProgress(
static_cast<double>( i ) * mStep );
146 for (
auto pit = mp.const_parts_begin(); pit != mp.const_parts_end(); ++pit )
163 if ( mSplitMultipart && concaveHull.
isMultipart() )
166 mStep = collection.length() > 0 ? 50.0 /
static_cast<double>( collection.length() ) : 1;
167 for (
int i = 0; i < collection.length(); i++ )
177 geom = collection[i].removeInteriorRings();
206 feedback->
setProgressText( QObject::tr(
"Creating Delaunay triangles…" ) );
207 multiStepFeedback.setCurrentStep( 1 );
210 params[
"INPUT"] = parameters[
"INPUT"];
211 params[
"TOLERANCE"] = 0.0;
212 params[
"ADD_ATTRIBUTES"] =
false;
217 throw QgsProcessingException( QObject::tr(
"Failed to compute concave hull: Delaunay triangulation algorithm not found!" ) );
220 std::unique_ptr<QgsProcessingAlgorithm>
algorithm;
222 QVariantMap results =
algorithm->run( params, context, &multiStepFeedback );
235 feedback->
setProgressText( QObject::tr(
"Computing edges max length…" ) );
236 multiStepFeedback.setCurrentStep( 2 );
238 QVector<double> length;
239 QMap<long, double> edges;
250 multiStepFeedback.setProgress(
static_cast<double>( i ) * step );
256 for (
int j = 0; j < points.size() - 1; j++ )
258 length << std::sqrt( points.at( j ).sqrDist( points.at( j + 1 ) ) );
260 QVector<double> vec = length.mid( length.size() - 3, -1 );
261 edges[f.
id()] = *std::max_element( vec.constBegin(), vec.constEnd() );
263 const double maxLength = *std::max_element( length.constBegin(), length.constEnd() );
266 multiStepFeedback.setCurrentStep( 3 );
268 step = edges.size() > 0 ? 100.0 /
static_cast<double>( edges.size() ) : 1;
270 QMap<long, double>::iterator edgesIt = edges.begin();
271 while ( edgesIt != edges.end() )
276 if ( edgesIt.value() > mPercentage * maxLength )
278 toDelete << edgesIt.key();
283 multiStepFeedback.setProgress(
static_cast<double>( i ) * step );
287 feedback->
setProgressText( QObject::tr(
"Dissolving Delaunay triangles…" ) );
288 multiStepFeedback.setCurrentStep( 4 );
290 params[
"INPUT"] = layer->
source();
295 throw QgsProcessingException( QObject::tr(
"Failed to compute concave hull: Dissolve algorithm not found!" ) );
298 results =
algorithm->run( params, context, &multiStepFeedback );
313 multiStepFeedback.setCurrentStep( 5 );
315 if ( mSplitMultipart && concaveHull.
isMultipart() )
318 step = collection.length() > 0 ? 50.0 /
static_cast<double>( collection.length() ) : 1;
319 for (
int i = 0; i < collection.length(); i++ )
329 geom = collection[i].removeInteriorRings();
336 multiStepFeedback.setProgress( i * step );
350 multiStepFeedback.setProgress( 100 );
@ VectorPoint
Vector point layers.
@ VectorPolygon
Vector polygon layers.
@ SkipGeometryValidityChecks
Invalid geometry checks should always be skipped. This flag can be useful for algorithms which always...
@ Double
Double/float values.
Abstract base class for all geometries.
Qgis::WkbType wkbType() const
Returns the WKB type of the geometry.
static QgsProcessingRegistry * processingRegistry()
Returns the application's processing registry, used for managing processing providers,...
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
bool hasGeometry() const
Returns true if the feature has an associated geometry.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
QgsGeometry concaveHull(double targetPercent, bool allowHoles=false, QgsFeedback *feedback=nullptr) const
Returns a possibly concave polygon that contains all the points in the geometry.
QString lastError() const
Returns an error string referring to the last error encountered either when this geometry was created...
QgsPolygonXY asPolygon() const
Returns the contents of the geometry as a polygon.
QVector< QgsGeometry > asGeometryCollection() const
Returns contents of the geometry as a list of geometries.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
Qgis::GeometryOperationResult addPartV2(const QVector< QgsPointXY > &points, Qgis::WkbType wkbType=Qgis::WkbType::Unknown)
Adds a new part to a the geometry.
QgsGeometry removeInteriorRings(double minimumAllowedArea=-1) const
Removes the interior rings from a (multi)polygon geometry.
QString source() const
Returns the source for the layer.
Multi point geometry collection.
Abstract base class for processing algorithms.
QgsProcessingAlgorithm * create(const QVariantMap &configuration=QVariantMap()) const
Creates a copy of the algorithm, ready for execution.
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.
void featureSinkFinalized(const QString &output)
Reports that a feature sink has been finalized.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
virtual void setProgressText(const QString &text)
Sets a progress report text string.
Processing feedback object for multi-step operations.
A boolean parameter for processing algorithms.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A numeric parameter for processing algorithms.
const QgsProcessingAlgorithm * algorithmById(const QString &id) const
Finds an algorithm by its ID.
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true, QgsProcessingUtils::LayerHint typeHint=QgsProcessingUtils::LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags=QgsProcessing::LayerOptionsFlags())
Interprets a string as a map layer within the supplied context.
static const QString TEMPORARY_OUTPUT
Constant used to indicate that a Processing algorithm output should be a temporary layer/file.
virtual bool deleteFeatures(const QgsFeatureIds &id)
Deletes one or more features from the provider.
Represents a vector layer which manages a vector based dataset.
long long featureCount(const QString &legendKey) const
Number of features rendered with specified legend key.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const final
Queries the layer for features specified in request.
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.
static Q_INVOKABLE bool isMultiType(Qgis::WkbType type)
Returns true if the WKB type is a multi type.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QSet< QgsFeatureId > QgsFeatureIds