Skip to content

Commit 6ae929e

Browse files
lbartolettim-kuhn
authored andcommitted
initial commit
1 parent 8770625 commit 6ae929e

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "minimal_circle",
3+
"type": "function",
4+
"description": "Returns the minimal enclosing circle of a geometry. It represents the minimum circle that encloses all geometries within the set.",
5+
"arguments": [ {"arg":"geometry","description":"a geometry"},
6+
{"arg":"segment", "description": "optional argument for polygon segmentation. By default this value is 36"}],
7+
"examples": [ { "expression":"geom_to_wkt( minimal_circle( geom_from_wkt( 'LINESTRING(0 5, 0 -5, 2 1)' ), 4 ) )", "returns":"Polygon ((0 5, 5 -0, -0 -5, -5 0, 0 5))"},
8+
{ "expression":"geom_to_wkt( minimal_circle( geom_from_wkt( 'MULTIPOINT(1 2, 3 4, 3 2)' ), 4 ) )", "returns":"Polygon ((3 4, 3 2, 1 2, 1 4, 3 4))"}
9+
]
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "oriented_bbox",
3+
"type": "function",
4+
"description":"Returns a geometry which represents the minimal oriented bounding box of an input geometry.",
5+
"arguments": [ {"arg":"geom","description":"a geometry"} ],
6+
"examples": [ { "expression":"geom_to_wkt( oriented_bbox( geom_from_wkt( 'MULTIPOINT(1 2, 3 4, 3 2)' ) ) )", "returns":"Polygon ((1 4, 1 2, 3 2, 3 4, 1 4))"}]
7+
}
8+

src/core/expression/qgsexpressionfunction.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -2535,6 +2535,29 @@ static QVariant fcnConvexHull( const QVariantList &values, const QgsExpressionCo
25352535
QVariant result = !geom.isNull() ? QVariant::fromValue( geom ) : QVariant();
25362536
return result;
25372537
}
2538+
2539+
static QVariant fcnMinimalCircle( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent )
2540+
{
2541+
QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2542+
QgsPointXY center;
2543+
double radius;
2544+
unsigned int segments = 36;
2545+
if ( values.length() == 2 )
2546+
segments = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
2547+
QgsGeometry geom = fGeom.minimalEnclosingCircle( center, radius, segments );
2548+
QVariant result = !geom.isNull() ? QVariant::fromValue( geom ) : QVariant();
2549+
return result;
2550+
}
2551+
2552+
static QVariant fcnOrientedBBox( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent )
2553+
{
2554+
QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
2555+
double area, angle, width, height;
2556+
QgsGeometry geom = fGeom.orientedMinimumBoundingBox( area, angle, width, height );
2557+
QVariant result = !geom.isNull() ? QVariant::fromValue( geom ) : QVariant();
2558+
return result;
2559+
}
2560+
25382561
static QVariant fcnDifference( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent )
25392562
{
25402563
QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
@@ -4139,6 +4162,13 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
41394162
<< new QgsStaticExpressionFunction( QStringLiteral( "bounds_height" ), 1, fcnBoundsHeight, QStringLiteral( "GeometryGroup" ) )
41404163
<< new QgsStaticExpressionFunction( QStringLiteral( "is_closed" ), 1, fcnIsClosed, QStringLiteral( "GeometryGroup" ) )
41414164
<< new QgsStaticExpressionFunction( QStringLiteral( "convex_hull" ), 1, fcnConvexHull, QStringLiteral( "GeometryGroup" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "convexHull" ) )
4165+
<< new QgsStaticExpressionFunction( QStringLiteral( "oriented_bbox" ), QgsExpressionFunction::ParameterList()
4166+
<< QgsExpressionFunction::Parameter( QStringLiteral( "geometry" ) ),
4167+
fcnOrientedBBox, QStringLiteral( "GeometryGroup" ) )
4168+
<< new QgsStaticExpressionFunction( QStringLiteral( "minimal_circle" ), QgsExpressionFunction::ParameterList()
4169+
<< QgsExpressionFunction::Parameter( QStringLiteral( "geometry" ) )
4170+
<< QgsExpressionFunction::Parameter( QStringLiteral( "segments" ), true, 36 ),
4171+
fcnMinimalCircle, QStringLiteral( "GeometryGroup" ) )
41424172
<< new QgsStaticExpressionFunction( QStringLiteral( "difference" ), 2, fcnDifference, QStringLiteral( "GeometryGroup" ) )
41434173
<< new QgsStaticExpressionFunction( QStringLiteral( "distance" ), 2, fcnDistance, QStringLiteral( "GeometryGroup" ) )
41444174
<< new QgsStaticExpressionFunction( QStringLiteral( "hausdorff_distance" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "geometry1" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "geometry2" ) )

tests/src/core/testqgsexpression.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -2302,6 +2302,14 @@ class TestQgsExpression: public QObject
23022302
geom = QgsGeometry::fromPolygon( polygon );
23032303
QTest::newRow( "bounds" ) << "bounds( $geometry )" << geom << false << true << QgsGeometry::fromRect( geom.boundingBox() );
23042304

2305+
geom = QgsGeometry::fromPolygon( polygon );
2306+
double bbox_area, bbox_angle, bbox_width, bbox_height;
2307+
QTest::newRow( "oriented_bbox" ) << "oriented_bbox( $geometry )" << geom << false << true << geom.orientedMinimumBoundingBox( bbox_area, bbox_angle, bbox_width, bbox_height );
2308+
geom = QgsGeometry::fromPolygon( polygon );
2309+
QgsPointXY circ_center;
2310+
double circ_radius;
2311+
QTest::newRow( "minimal_circle" ) << "minimal_circle( $geometry )" << geom << false << true << geom.minimalEnclosingCircle( circ_center, circ_radius );
2312+
23052313
geom = QgsGeometry::fromPolygon( polygon );
23062314
QTest::newRow( "translate" ) << "translate( $geometry, 1, 2)" << geom << false << true << QgsGeometry::fromWkt( QStringLiteral( "POLYGON ((1 2,11 12,11 2,1 2))" ) );
23072315
geom = QgsGeometry::fromPolyline( line );

0 commit comments

Comments
 (0)