24using namespace Qt::StringLiterals;
28QString QgsMinimumBoundingGeometryAlgorithm::name()
const
30 return u
"minimumboundinggeometry"_s;
33QString QgsMinimumBoundingGeometryAlgorithm::displayName()
const
35 return QObject::tr(
"Minimum bounding geometry" );
38QStringList QgsMinimumBoundingGeometryAlgorithm::tags()
const
40 return QObject::tr(
"bounding,box,bounds,envelope,minimum,oriented,rectangle,enclosing,circle,convex,hull,generalization" ).split(
',' );
43QString QgsMinimumBoundingGeometryAlgorithm::group()
const
45 return QObject::tr(
"Vector geometry" );
48QString QgsMinimumBoundingGeometryAlgorithm::groupId()
const
50 return u
"vectorgeometry"_s;
53QString QgsMinimumBoundingGeometryAlgorithm::shortHelpString()
const
56 "This algorithm creates geometries which enclose the features from an input layer.\n\n"
57 "Numerous enclosing geometry types are supported, including bounding "
58 "boxes (envelopes), oriented rectangles, circles and convex hulls.\n\n"
59 "Optionally, the features can be grouped by a field. If set, this "
60 "causes the output layer to contain one feature per grouped value with "
61 "a minimal geometry covering just the features with matching values."
65QString QgsMinimumBoundingGeometryAlgorithm::shortDescription()
const
67 return QObject::tr(
"Creates geometries which enclose the features from an input layer." );
70QgsMinimumBoundingGeometryAlgorithm *QgsMinimumBoundingGeometryAlgorithm::createInstance()
const
72 return new QgsMinimumBoundingGeometryAlgorithm();
75void QgsMinimumBoundingGeometryAlgorithm::initAlgorithm(
const QVariantMap & )
82 QStringList geometryTypes
83 = QStringList() << QObject::tr(
"Envelope (Bounding Box)" ) << QObject::tr(
"Minimum Oriented Rectangle" ) << QObject::tr(
"Minimum Enclosing Circle" ) << QObject::tr(
"Convex Hull" );
91 QGS_MARK_ALGORITHM_SOURCE
93 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
99 const QString fieldName = parameterAsString( parameters, u
"FIELD"_s, context );
100 const int geometryType = parameterAsEnum( parameters, u
"TYPE"_s, context );
101 const bool useField = !fieldName.isEmpty();
106 fields.
append(
QgsField( u
"id"_s, QMetaType::Type::Int, QString(), 20 ) );
111 fieldIndex = source->fields().lookupField( fieldName );
112 if ( fieldIndex >= 0 )
114 fields.
append( source->fields().at( fieldIndex ) );
118 if ( geometryType == 0 )
121 fields.
append(
QgsField( u
"width"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
122 fields.
append(
QgsField( u
"height"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
123 fields.
append(
QgsField( u
"area"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
124 fields.
append(
QgsField( u
"perimeter"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
126 else if ( geometryType == 1 )
129 fields.
append(
QgsField( u
"width"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
130 fields.
append(
QgsField( u
"height"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
131 fields.
append(
QgsField( u
"angle"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
132 fields.
append(
QgsField( u
"area"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
133 fields.
append(
QgsField( u
"perimeter"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
135 else if ( geometryType == 2 )
138 fields.
append(
QgsField( u
"radius"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
139 fields.
append(
QgsField( u
"area"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
141 else if ( geometryType == 3 )
144 fields.
append(
QgsField( u
"area"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
145 fields.
append(
QgsField( u
"perimeter"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
149 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, fields,
Qgis::WkbType::Polygon, source->sourceCrs() ) );
155 if ( fieldIndex >= 0 )
157 QHash<QVariant, QVector<QgsGeometry>> geometryHash;
158 QHash<QVariant, QgsRectangle> boundsHash;
160 double step = source->featureCount() > 0 ? 50 / source->featureCount() : 1;
173 QVariant fieldValue = f.
attribute( fieldIndex );
174 if ( geometryType == 0 )
176 auto boundsHashIt = boundsHash.find( fieldValue );
177 if ( boundsHashIt == boundsHash.end() )
188 auto geometryHashIt = geometryHash.find( fieldValue );
189 if ( geometryHashIt == geometryHash.end() )
191 geometryHash.insert( fieldValue, QVector<QgsGeometry>() << f.
geometry() );
195 geometryHashIt.value().append( f.
geometry() );
204 if ( geometryType == 0 )
206 step = boundsHash.size() > 0 ? 50 / boundsHash.size() : 1;
207 for (
auto it = boundsHash.constBegin(); it != boundsHash.constEnd(); ++it )
227 step = geometryHash.size() > 0 ? 50 / geometryHash.size() : 1;
228 for (
auto it = geometryHash.constBegin(); it != geometryHash.constEnd(); ++it )
234 QgsFeature feature = createFeature( feedback, i, geometryType, it.value(), it.key() );
246 double step = source->featureCount() > 0 ? 80 / source->featureCount() : 1;
249 QVector<QgsGeometry> geometryQueue;
250 geometryQueue.reserve( source->featureCount() );
263 if ( geometryType == 0 )
279 if ( geometryType == 0 )
286 feature = createFeature( feedback, 0, geometryType, geometryQueue );
299 results.insert( u
"OUTPUT"_s, dest );
303QgsFeature QgsMinimumBoundingGeometryAlgorithm::createFeature(
QgsProcessingFeedback *feedback,
const int featureId,
const int featureType, QVector<QgsGeometry> geometries, QVariant classField )
307 if ( classField.isValid() )
312 auto multiPoint = std::make_unique<QgsMultiPoint>();
314 for (
auto &g : geometries )
319 for (
auto it = g.constGet()->vertices_begin(); it != g.constGet()->vertices_end(); ++it )
324 multiPoint->addGeometry( ( *it ).clone() );
330 if ( featureType == 0 )
337 else if ( featureType == 1 )
340 double area,
angle, width, height;
342 attrs << width << height <<
angle << area << 2 * width + 2 * height;
344 else if ( featureType == 2 )
350 attrs << radius << M_PI * radius * radius;
352 else if ( featureType == 3 )
@ VectorAnyGeometry
Any vector layer with geometry.
@ VectorPolygon
Vector polygon layers.
virtual double perimeter() const
Returns the planar, 2-dimensional perimeter of the geometry.
virtual double area() const
Returns the planar, 2-dimensional area of the geometry.
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...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
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.
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.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
QgsGeometry orientedMinimumBoundingBox(double &area, double &angle, double &width, double &height) const
Returns the oriented minimum bounding box for the geometry, which is the smallest (by area) rotated r...
QgsGeometry convexHull() const
Returns the smallest convex polygon that contains all the points in the geometry.
QgsGeometry minimalEnclosingCircle(QgsPointXY ¢er, double &radius, unsigned int segments=36) const
Returns the minimal enclosing circle for the geometry.
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
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 featureAddedToSink(const QString &output)
Reports that a feature was added to the the sink associated with the specified algorithm output.
void featureSinkFinalized(const QString &output)
Reports that a feature sink has been finalized.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
A rectangle specified with double values.
void combineExtentWith(const QgsRectangle &rect)
Expands the rectangle so that it covers both the original rectangle and the given rectangle.
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored).