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 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, u
"INPUT"_s, context ) );
97 const QString fieldName = parameterAsString( parameters, u
"FIELD"_s, context );
98 const int geometryType = parameterAsEnum( parameters, u
"TYPE"_s, context );
99 const bool useField = !fieldName.isEmpty();
104 fields.
append(
QgsField( u
"id"_s, QMetaType::Type::Int, QString(), 20 ) );
109 fieldIndex = source->fields().lookupField( fieldName );
110 if ( fieldIndex >= 0 )
112 fields.
append( source->fields().at( fieldIndex ) );
116 if ( geometryType == 0 )
119 fields.
append(
QgsField( u
"width"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
120 fields.
append(
QgsField( u
"height"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
121 fields.
append(
QgsField( u
"area"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
122 fields.
append(
QgsField( u
"perimeter"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
124 else if ( geometryType == 1 )
127 fields.
append(
QgsField( u
"width"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
128 fields.
append(
QgsField( u
"height"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
129 fields.
append(
QgsField( u
"angle"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
130 fields.
append(
QgsField( u
"area"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
131 fields.
append(
QgsField( u
"perimeter"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
133 else if ( geometryType == 2 )
136 fields.
append(
QgsField( u
"radius"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
137 fields.
append(
QgsField( u
"area"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
139 else if ( geometryType == 3 )
142 fields.
append(
QgsField( u
"area"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
143 fields.
append(
QgsField( u
"perimeter"_s, QMetaType::Type::Double, QString(), 20, 6 ) );
147 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, fields,
Qgis::WkbType::Polygon, source->sourceCrs() ) );
153 if ( fieldIndex >= 0 )
155 QHash<QVariant, QVector<QgsGeometry>> geometryHash;
156 QHash<QVariant, QgsRectangle> boundsHash;
158 double step = source->featureCount() > 0 ? 50 / source->featureCount() : 1;
171 QVariant fieldValue = f.
attribute( fieldIndex );
172 if ( geometryType == 0 )
174 auto boundsHashIt = boundsHash.find( fieldValue );
175 if ( boundsHashIt == boundsHash.end() )
186 auto geometryHashIt = geometryHash.find( fieldValue );
187 if ( geometryHashIt == geometryHash.end() )
189 geometryHash.insert( fieldValue, QVector<QgsGeometry>() << f.
geometry() );
193 geometryHashIt.value().append( f.
geometry() );
202 if ( geometryType == 0 )
204 step = boundsHash.size() > 0 ? 50 / boundsHash.size() : 1;
205 for (
auto it = boundsHash.constBegin(); it != boundsHash.constEnd(); ++it )
223 step = geometryHash.size() > 0 ? 50 / geometryHash.size() : 1;
224 for (
auto it = geometryHash.constBegin(); it != geometryHash.constEnd(); ++it )
230 QgsFeature feature = createFeature( feedback, i, geometryType, it.value(), it.key() );
240 double step = source->featureCount() > 0 ? 80 / source->featureCount() : 1;
243 QVector<QgsGeometry> geometryQueue;
244 geometryQueue.reserve( source->featureCount() );
257 if ( geometryType == 0 )
273 if ( geometryType == 0 )
280 feature = createFeature( feedback, 0, geometryType, geometryQueue );
290 results.insert( u
"OUTPUT"_s, dest );
294QgsFeature QgsMinimumBoundingGeometryAlgorithm::createFeature(
QgsProcessingFeedback *feedback,
const int featureId,
const int featureType, QVector<QgsGeometry> geometries, QVariant classField )
298 if ( classField.isValid() )
303 auto multiPoint = std::make_unique<QgsMultiPoint>();
305 for (
auto &g : geometries )
310 for (
auto it = g.constGet()->vertices_begin(); it != g.constGet()->vertices_end(); ++it )
315 multiPoint->addGeometry( ( *it ).clone() );
321 if ( featureType == 0 )
328 else if ( featureType == 1 )
331 double area,
angle, width, height;
333 attrs << width << height <<
angle << area << 2 * width + 2 * height;
335 else if ( featureType == 2 )
341 attrs << radius << M_PI * radius * radius;
343 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).