Skip to content

Commit

Permalink
add overload methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lbartoletti authored and m-kuhn committed Sep 8, 2017
1 parent 6ae929e commit 629f46d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
16 changes: 16 additions & 0 deletions python/core/geometry/qgsgeometry.sip
Original file line number Diff line number Diff line change
Expand Up @@ -577,12 +577,28 @@ Returns true if WKB of the geometry is of WKBMulti* type
:rtype: QgsGeometry
%End

QgsGeometry orientedMinimumBoundingBox( ) const;
%Docstring
Returns the oriented minimum bounding box for the geometry, which is the smallest (by area)
rotated rectangle which fully encompasses the geometry.
.. versionadded:: 3.0
:rtype: QgsGeometry
%End

QgsGeometry minimalEnclosingCircle( QgsPointXY &center /Out/, double &radius /Out/, unsigned int segments = 36 ) const;
%Docstring
Returns the minimal enclosing circle for the geometry.
\param center Center of the minimal enclosing circle returneds
\param radius Radius of the minimal enclosing circle returned
.. seealso:: QgsEllipse.toPolygon()
.. versionadded:: 3.0
:rtype: QgsGeometry
%End

QgsGeometry minimalEnclosingCircle( unsigned int segments = 36 ) const;
%Docstring
Returns the minimal enclosing circle for the geometry.
.. seealso:: QgsEllipse.toPolygon()
.. versionadded:: 3.0
:rtype: QgsGeometry
%End
Expand Down
8 changes: 3 additions & 5 deletions src/core/expression/qgsexpressionfunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2536,24 +2536,22 @@ static QVariant fcnConvexHull( const QVariantList &values, const QgsExpressionCo
return result;
}


static QVariant fcnMinimalCircle( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent )
{
QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
QgsPointXY center;
double radius;
unsigned int segments = 36;
if ( values.length() == 2 )
segments = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );
QgsGeometry geom = fGeom.minimalEnclosingCircle( center, radius, segments );
QgsGeometry geom = fGeom.minimalEnclosingCircle( segments );
QVariant result = !geom.isNull() ? QVariant::fromValue( geom ) : QVariant();
return result;
}

static QVariant fcnOrientedBBox( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent )
{
QgsGeometry fGeom = QgsExpressionUtils::getGeometry( values.at( 0 ), parent );
double area, angle, width, height;
QgsGeometry geom = fGeom.orientedMinimumBoundingBox( area, angle, width, height );
QgsGeometry geom = fGeom.orientedMinimumBoundingBox( );
QVariant result = !geom.isNull() ? QVariant::fromValue( geom ) : QVariant();
return result;
}
Expand Down
14 changes: 14 additions & 0 deletions src/core/geometry/qgsgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,12 @@ QgsGeometry QgsGeometry::orientedMinimumBoundingBox( double &area, double &angle
return minBounds;
}

QgsGeometry QgsGeometry::orientedMinimumBoundingBox() const
{
double area, angle, width, height;
return orientedMinimumBoundingBox( area, angle, width, height );
}

static QgsCircle __recMinimalEnclosingCircle( QgsMultiPoint points, QgsMultiPoint boundary )
{
auto l_boundary = boundary.length();
Expand Down Expand Up @@ -1084,6 +1090,14 @@ QgsGeometry QgsGeometry::minimalEnclosingCircle( QgsPointXY &center, double &rad

}

QgsGeometry QgsGeometry::minimalEnclosingCircle( unsigned int segments ) const
{
QgsPointXY center;
double radius;
return minimalEnclosingCircle( center, radius, segments );

}

QgsGeometry QgsGeometry::orthogonalize( double tolerance, int maxIterations, double angleThreshold ) const
{
QgsInternalGeometryEngine engine( *this );
Expand Down
14 changes: 14 additions & 0 deletions src/core/geometry/qgsgeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,13 @@ class CORE_EXPORT QgsGeometry
*/
QgsGeometry orientedMinimumBoundingBox( double &area SIP_OUT, double &angle SIP_OUT, double &width SIP_OUT, double &height SIP_OUT ) const;

/**
* Returns the oriented minimum bounding box for the geometry, which is the smallest (by area)
* rotated rectangle which fully encompasses the geometry.
* \since QGIS 3.0
*/
QgsGeometry orientedMinimumBoundingBox( ) const;

/**
* Returns the minimal enclosing circle for the geometry.
* \param center Center of the minimal enclosing circle returneds
Expand All @@ -608,6 +615,13 @@ class CORE_EXPORT QgsGeometry
*/
QgsGeometry minimalEnclosingCircle( QgsPointXY &center SIP_OUT, double &radius SIP_OUT, unsigned int segments = 36 ) const;

/**
* Returns the minimal enclosing circle for the geometry.
* \param segments Number of segments used to segment geometry. \see QgsEllipse::toPolygon()
* \since QGIS 3.0
*/
QgsGeometry minimalEnclosingCircle( unsigned int segments = 36 ) const;

/**
* Attempts to orthogonalize a line or polygon geometry by shifting vertices to make the geometries
* angles either right angles or flat lines. This is an iterative algorithm which will loop until
Expand Down
7 changes: 2 additions & 5 deletions tests/src/core/testqgsexpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2303,12 +2303,9 @@ class TestQgsExpression: public QObject
QTest::newRow( "bounds" ) << "bounds( $geometry )" << geom << false << true << QgsGeometry::fromRect( geom.boundingBox() );

geom = QgsGeometry::fromPolygon( polygon );
double bbox_area, bbox_angle, bbox_width, bbox_height;
QTest::newRow( "oriented_bbox" ) << "oriented_bbox( $geometry )" << geom << false << true << geom.orientedMinimumBoundingBox( bbox_area, bbox_angle, bbox_width, bbox_height );
QTest::newRow( "oriented_bbox" ) << "oriented_bbox( $geometry )" << geom << false << true << geom.orientedMinimumBoundingBox( );
geom = QgsGeometry::fromPolygon( polygon );
QgsPointXY circ_center;
double circ_radius;
QTest::newRow( "minimal_circle" ) << "minimal_circle( $geometry )" << geom << false << true << geom.minimalEnclosingCircle( circ_center, circ_radius );
QTest::newRow( "minimal_circle" ) << "minimal_circle( $geometry )" << geom << false << true << geom.minimalEnclosingCircle( );

geom = QgsGeometry::fromPolygon( polygon );
QTest::newRow( "translate" ) << "translate( $geometry, 1, 2)" << geom << false << true << QgsGeometry::fromWkt( QStringLiteral( "POLYGON ((1 2,11 12,11 2,1 2))" ) );
Expand Down

0 comments on commit 629f46d

Please sign in to comment.