24QString QgsMinimumBoundingGeometryAlgorithm::name()
const
26 return QStringLiteral(
"minimumboundinggeometry" );
29QString QgsMinimumBoundingGeometryAlgorithm::displayName()
const
31 return QObject::tr(
"Minimum bounding geometry" );
34QStringList QgsMinimumBoundingGeometryAlgorithm::tags()
const
36 return QObject::tr(
"bounding,box,bounds,envelope,minimum,oriented,rectangle,enclosing,circle,convex,hull,generalization" ).split(
',' );
39QString QgsMinimumBoundingGeometryAlgorithm::group()
const
41 return QObject::tr(
"Vector geometry" );
44QString QgsMinimumBoundingGeometryAlgorithm::groupId()
const
46 return QStringLiteral(
"vectorgeometry" );
49QString QgsMinimumBoundingGeometryAlgorithm::shortHelpString()
const
51 return QObject::tr(
"This algorithm creates geometries which enclose the features from an input layer.\n\n"
52 "Numerous enclosing geometry types are supported, including bounding "
53 "boxes (envelopes), oriented rectangles, circles and convex hulls.\n\n"
54 "Optionally, the features can be grouped by a field. If set, this "
55 "causes the output layer to contain one feature per grouped value with "
56 "a minimal geometry covering just the features with matching values." );
59QString QgsMinimumBoundingGeometryAlgorithm::shortDescription()
const
61 return QObject::tr(
"Creates geometries which enclose the features from an input layer." );
64QgsMinimumBoundingGeometryAlgorithm *QgsMinimumBoundingGeometryAlgorithm::createInstance()
const
66 return new QgsMinimumBoundingGeometryAlgorithm();
69void QgsMinimumBoundingGeometryAlgorithm::initAlgorithm(
const QVariantMap & )
74 QStringList geometryTypes = QStringList() << QObject::tr(
"Envelope (Bounding Box)" )
75 << QObject::tr(
"Minimum Oriented Rectangle" )
76 << QObject::tr(
"Minimum Enclosing Circle" )
77 << QObject::tr(
"Convex Hull" );
85 std::unique_ptr<QgsProcessingFeatureSource> source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
91 const QString fieldName = parameterAsString( parameters, QStringLiteral(
"FIELD" ), context );
92 const int geometryType = parameterAsEnum( parameters, QStringLiteral(
"TYPE" ), context );
93 const bool useField = !fieldName.isEmpty();
98 fields.
append(
QgsField( QStringLiteral(
"id" ), QMetaType::Type::Int, QString(), 20 ) );
103 fieldIndex = source->fields().lookupField( fieldName );
104 if ( fieldIndex >= 0 )
106 fields.
append( source->fields().at( fieldIndex ) );
110 if ( geometryType == 0 )
113 fields.
append(
QgsField( QStringLiteral(
"width" ), QMetaType::Type::Double, QString(), 20, 6 ) );
114 fields.
append(
QgsField( QStringLiteral(
"height" ), QMetaType::Type::Double, QString(), 20, 6 ) );
115 fields.
append(
QgsField( QStringLiteral(
"area" ), QMetaType::Type::Double, QString(), 20, 6 ) );
116 fields.
append(
QgsField( QStringLiteral(
"perimeter" ), QMetaType::Type::Double, QString(), 20, 6 ) );
118 else if ( geometryType == 1 )
121 fields.
append(
QgsField( QStringLiteral(
"width" ), QMetaType::Type::Double, QString(), 20, 6 ) );
122 fields.
append(
QgsField( QStringLiteral(
"height" ), QMetaType::Type::Double, QString(), 20, 6 ) );
123 fields.
append(
QgsField( QStringLiteral(
"angle" ), QMetaType::Type::Double, QString(), 20, 6 ) );
124 fields.
append(
QgsField( QStringLiteral(
"area" ), QMetaType::Type::Double, QString(), 20, 6 ) );
125 fields.
append(
QgsField( QStringLiteral(
"perimeter" ), QMetaType::Type::Double, QString(), 20, 6 ) );
127 else if ( geometryType == 2 )
130 fields.
append(
QgsField( QStringLiteral(
"radius" ), QMetaType::Type::Double, QString(), 20, 6 ) );
131 fields.
append(
QgsField( QStringLiteral(
"area" ), QMetaType::Type::Double, QString(), 20, 6 ) );
133 else if ( geometryType == 3 )
136 fields.
append(
QgsField( QStringLiteral(
"area" ), QMetaType::Type::Double, QString(), 20, 6 ) );
137 fields.
append(
QgsField( QStringLiteral(
"perimeter" ), QMetaType::Type::Double, QString(), 20, 6 ) );
141 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields,
Qgis::WkbType::Polygon, source->sourceCrs() ) );
147 if ( fieldIndex >= 0 )
149 QHash<QVariant, QVector<QgsGeometry>> geometryHash;
150 QHash<QVariant, QgsRectangle> boundsHash;
152 double step = source->featureCount() > 0 ? 50 / source->featureCount() : 1;
165 QVariant fieldValue = f.
attribute( fieldIndex );
166 if ( geometryType == 0 )
168 auto boundsHashIt = boundsHash.find( fieldValue );
169 if ( boundsHashIt == boundsHash.end() )
180 auto geometryHashIt = geometryHash.find( fieldValue );
181 if ( geometryHashIt == geometryHash.end() )
183 geometryHash.insert( fieldValue, QVector<QgsGeometry>() << f.
geometry() );
187 geometryHashIt.value().append( f.
geometry() );
196 if ( geometryType == 0 )
198 step = boundsHash.size() > 0 ? 50 / boundsHash.size() : 1;
199 for (
auto it = boundsHash.constBegin(); it != boundsHash.constEnd(); ++it )
217 step = geometryHash.size() > 0 ? 50 / geometryHash.size() : 1;
218 for (
auto it = geometryHash.constBegin(); it != geometryHash.constEnd(); ++it )
224 QgsFeature feature = createFeature( feedback, i, geometryType, it.value(), it.key() );
234 double step = source->featureCount() > 0 ? 80 / source->featureCount() : 1;
237 QVector<QgsGeometry> geometryQueue;
238 geometryQueue.reserve( source->featureCount() );
251 if ( geometryType == 0 )
267 if ( geometryType == 0 )
274 feature = createFeature( feedback, 0, geometryType, geometryQueue );
284 results.insert( QStringLiteral(
"OUTPUT" ), dest );
288QgsFeature QgsMinimumBoundingGeometryAlgorithm::createFeature(
QgsProcessingFeedback *feedback,
const int featureId,
const int featureType, QVector<QgsGeometry> geometries, QVariant classField )
292 if ( classField.isValid() )
297 auto multiPoint = std::make_unique<QgsMultiPoint>();
299 for (
auto &g : geometries )
304 for (
auto it = g.constGet()->vertices_begin(); it != g.constGet()->vertices_end(); ++it )
309 multiPoint->addGeometry( ( *it ).clone() );
315 if ( featureType == 0 )
322 else if ( featureType == 1 )
325 double area,
angle, width, height;
327 attrs << width << height <<
angle << area << 2 * width + 2 * height;
329 else if ( featureType == 2 )
335 attrs << radius << M_PI * radius * radius;
337 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).