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
55 return QObject::tr(
"This algorithm creates geometries which enclose the features from an input layer.\n\n"
56 "Numerous enclosing geometry types are supported, including bounding "
57 "boxes (envelopes), oriented rectangles, circles and convex hulls.\n\n"
58 "Optionally, the features can be grouped by a field. If set, this "
59 "causes the output layer to contain one feature per grouped value with "
60 "a minimal geometry covering just the features with matching values." );
63QString QgsMinimumBoundingGeometryAlgorithm::shortDescription()
const
65 return QObject::tr(
"Creates geometries which enclose the features from an input layer." );
68QgsMinimumBoundingGeometryAlgorithm *QgsMinimumBoundingGeometryAlgorithm::createInstance()
const
70 return new QgsMinimumBoundingGeometryAlgorithm();
73void QgsMinimumBoundingGeometryAlgorithm::initAlgorithm(
const QVariantMap & )
78 QStringList geometryTypes = QStringList() << QObject::tr(
"Envelope (Bounding Box)" )
79 << QObject::tr(
"Minimum Oriented Rectangle" )
80 << QObject::tr(
"Minimum Enclosing Circle" )
81 << QObject::tr(
"Convex Hull" );
89 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
95 const QString fieldName = parameterAsString( parameters, u
"FIELD"_s, context );
96 const int geometryType = parameterAsEnum( parameters, u
"TYPE"_s, context );
97 const bool useField = !fieldName.isEmpty();
102 fields.
append(
QgsField( u
"id"_s, QMetaType::Type::Int, QString(), 20 ) );
107 fieldIndex = source->fields().lookupField( fieldName );
108 if ( fieldIndex >= 0 )
110 fields.
append( source->fields().at( fieldIndex ) );
114 if ( geometryType == 0 )
117 fields.
append(
QgsField( u
"width"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
118 fields.
append(
QgsField( u
"height"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
119 fields.
append(
QgsField( u
"area"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
120 fields.
append(
QgsField( u
"perimeter"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
122 else if ( geometryType == 1 )
125 fields.
append(
QgsField( u
"width"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
126 fields.
append(
QgsField( u
"height"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
127 fields.
append(
QgsField( u
"angle"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
128 fields.
append(
QgsField( u
"area"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
129 fields.
append(
QgsField( u
"perimeter"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
131 else if ( geometryType == 2 )
134 fields.
append(
QgsField( u
"radius"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
135 fields.
append(
QgsField( u
"area"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
137 else if ( geometryType == 3 )
140 fields.
append(
QgsField( u
"area"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
141 fields.
append(
QgsField( u
"perimeter"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
145 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, fields,
Qgis::WkbType::Polygon, source->sourceCrs() ) );
151 if ( fieldIndex >= 0 )
153 QHash<QVariant, QVector<QgsGeometry>> geometryHash;
154 QHash<QVariant, QgsRectangle> boundsHash;
156 double step = source->featureCount() > 0 ? 50 / source->featureCount() : 1;
169 QVariant fieldValue = f.
attribute( fieldIndex );
170 if ( geometryType == 0 )
172 auto boundsHashIt = boundsHash.find( fieldValue );
173 if ( boundsHashIt == boundsHash.end() )
184 auto geometryHashIt = geometryHash.find( fieldValue );
185 if ( geometryHashIt == geometryHash.end() )
187 geometryHash.insert( fieldValue, QVector<QgsGeometry>() << f.
geometry() );
191 geometryHashIt.value().append( f.
geometry() );
200 if ( geometryType == 0 )
202 step = boundsHash.size() > 0 ? 50 / boundsHash.size() : 1;
203 for (
auto it = boundsHash.constBegin(); it != boundsHash.constEnd(); ++it )
221 step = geometryHash.size() > 0 ? 50 / geometryHash.size() : 1;
222 for (
auto it = geometryHash.constBegin(); it != geometryHash.constEnd(); ++it )
228 QgsFeature feature = createFeature( feedback, i, geometryType, it.value(), it.key() );
238 double step = source->featureCount() > 0 ? 80 / source->featureCount() : 1;
241 QVector<QgsGeometry> geometryQueue;
242 geometryQueue.reserve( source->featureCount() );
255 if ( geometryType == 0 )
271 if ( geometryType == 0 )
278 feature = createFeature( feedback, 0, geometryType, geometryQueue );
288 results.insert( u
"OUTPUT"_s, dest );
292QgsFeature QgsMinimumBoundingGeometryAlgorithm::createFeature(
QgsProcessingFeedback *feedback,
const int featureId,
const int featureType, QVector<QgsGeometry> geometries, QVariant classField )
296 if ( classField.isValid() )
301 auto multiPoint = std::make_unique<QgsMultiPoint>();
303 for (
auto &g : geometries )
308 for (
auto it = g.constGet()->vertices_begin(); it != g.constGet()->vertices_end(); ++it )
313 multiPoint->addGeometry( ( *it ).clone() );
319 if ( featureType == 0 )
326 else if ( featureType == 1 )
329 double area,
angle, width, height;
331 attrs << width << height <<
angle << area << 2 * width + 2 * height;
333 else if ( featureType == 2 )
339 attrs << radius << M_PI * radius * radius;
341 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.
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).