From b51a5f7b6f5e692441b3311ec81ee05f23e7bf3a Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Sun, 13 Aug 2017 18:46:12 +0200 Subject: [PATCH] Simplify QgsGeometryEngine code --- python/core/geometry/qgsgeometryengine.sip | 20 ++++++++--- src/core/geometry/qgsgeometry.cpp | 23 ++---------- src/core/geometry/qgsgeometryengine.h | 22 ++++++++++-- src/core/geometry/qgsgeos.cpp | 41 +++++++++------------- src/core/geometry/qgsgeos.h | 4 +-- 5 files changed, 58 insertions(+), 52 deletions(-) diff --git a/python/core/geometry/qgsgeometryengine.sip b/python/core/geometry/qgsgeometryengine.sip index f6c15f2d99a..ad86e0552b0 100644 --- a/python/core/geometry/qgsgeometryengine.sip +++ b/python/core/geometry/qgsgeometryengine.sip @@ -85,16 +85,28 @@ class QgsGeometryEngine %Docstring :rtype: QgsAbstractGeometry %End - virtual bool centroid( QgsPoint &pt, QString *errorMsg = 0 ) const = 0; + + virtual QgsPoint *centroid( QString *errorMsg = 0 ) const = 0 /Factory/; %Docstring - :rtype: bool + Calculates the centroid of this. + May return a `None`. + +.. versionadded:: 3.0 + :rtype: QgsPoint %End - virtual bool pointOnSurface( QgsPoint &pt, QString *errorMsg = 0 ) const = 0; + + virtual QgsPoint *pointOnSurface( QString *errorMsg = 0 ) const = 0 /Factory/; %Docstring - :rtype: bool + Calculate a point that is guaranteed to be on the surface of this. + May return a `None`. + +.. versionadded:: 3.0 + :rtype: QgsPoint %End + virtual QgsAbstractGeometry *convexHull( QString *errorMsg = 0 ) const = 0 /Factory/; %Docstring + Calculate the convex hull of this. :rtype: QgsAbstractGeometry %End diff --git a/src/core/geometry/qgsgeometry.cpp b/src/core/geometry/qgsgeometry.cpp index b858da02f6c..d1a710d3660 100644 --- a/src/core/geometry/qgsgeometry.cpp +++ b/src/core/geometry/qgsgeometry.cpp @@ -1581,16 +1581,8 @@ QgsGeometry QgsGeometry::centroid() const } QgsGeos geos( d->geometry ); - std::unique_ptr centroid( new QgsPoint() ); - QString error; - bool ok = geos.centroid( *centroid.get(), &error ); - if ( !ok ) - { - QgsGeometry geom; - geom.d->error = error; - return geom; - } - return QgsGeometry( centroid.release() ); + + return QgsGeometry( geos.centroid( &d->error ) ); } QgsGeometry QgsGeometry::pointOnSurface() const @@ -1601,17 +1593,8 @@ QgsGeometry QgsGeometry::pointOnSurface() const } QgsGeos geos( d->geometry ); - std::unique_ptrpt( new QgsPoint() ); - QString error; - bool ok = geos.pointOnSurface( *pt.get(), &error ); - if ( !ok ) - { - QgsGeometry geom; - geom.d->error = error; - return geom; - } - return QgsGeometry( pt.release() ); + return QgsGeometry( geos.pointOnSurface( &d->error ) ); } QgsGeometry QgsGeometry::poleOfInaccessibility( double precision, double *distanceToBoundary ) const diff --git a/src/core/geometry/qgsgeometryengine.h b/src/core/geometry/qgsgeometryengine.h index 069755f14f4..e2f5927bf45 100644 --- a/src/core/geometry/qgsgeometryengine.h +++ b/src/core/geometry/qgsgeometryengine.h @@ -75,8 +75,26 @@ class CORE_EXPORT QgsGeometryEngine virtual QgsAbstractGeometry *simplify( double tolerance, QString *errorMsg = nullptr ) const = 0 SIP_FACTORY; virtual QgsAbstractGeometry *interpolate( double distance, QString *errorMsg = nullptr ) const = 0 SIP_FACTORY; virtual QgsAbstractGeometry *envelope( QString *errorMsg = nullptr ) const = 0 SIP_FACTORY; - virtual bool centroid( QgsPoint &pt, QString *errorMsg = nullptr ) const = 0; - virtual bool pointOnSurface( QgsPoint &pt, QString *errorMsg = nullptr ) const = 0; + + /** + * Calculates the centroid of this. + * May return a `nullptr`. + * + * \since QGIS 3.0 the centroid is returned + */ + virtual QgsPoint *centroid( QString *errorMsg = nullptr ) const = 0 SIP_FACTORY; + + /** + * Calculate a point that is guaranteed to be on the surface of this. + * May return a `nullptr`. + * + * \since QGIS 3.0 the centroid is returned + */ + virtual QgsPoint *pointOnSurface( QString *errorMsg = nullptr ) const = 0 SIP_FACTORY; + + /** + * Calculate the convex hull of this. + */ virtual QgsAbstractGeometry *convexHull( QString *errorMsg = nullptr ) const = 0 SIP_FACTORY; /** diff --git a/src/core/geometry/qgsgeos.cpp b/src/core/geometry/qgsgeos.cpp index 584a0d05ca8..19eb50cc6e9 100644 --- a/src/core/geometry/qgsgeos.cpp +++ b/src/core/geometry/qgsgeos.cpp @@ -1503,36 +1503,30 @@ QgsAbstractGeometry *QgsGeos::interpolate( double distance, QString *errorMsg ) return fromGeos( geos.get() ); } -bool QgsGeos::centroid( QgsPoint &pt, QString *errorMsg ) const +QgsPoint *QgsGeos::centroid( QString *errorMsg ) const { if ( !mGeos ) { - return false; + return nullptr; } GEOSGeomScopedPtr geos; + double x; + double y; + try { geos.reset( GEOSGetCentroid_r( geosinit.ctxt, mGeos ) ); - } - CATCH_GEOS_WITH_ERRMSG( false ); - if ( !geos ) - { - return false; - } + if ( !geos ) + return nullptr; - try - { - double x, y; GEOSGeomGetX_r( geosinit.ctxt, geos.get(), &x ); GEOSGeomGetY_r( geosinit.ctxt, geos.get(), &y ); - pt.setX( x ); - pt.setY( y ); } - CATCH_GEOS_WITH_ERRMSG( false ); + CATCH_GEOS_WITH_ERRMSG( nullptr ); - return true; + return new QgsPoint( x, y ); } QgsAbstractGeometry *QgsGeos::envelope( QString *errorMsg ) const @@ -1550,13 +1544,16 @@ QgsAbstractGeometry *QgsGeos::envelope( QString *errorMsg ) const return fromGeos( geos.get() ); } -bool QgsGeos::pointOnSurface( QgsPoint &pt, QString *errorMsg ) const +QgsPoint *QgsGeos::pointOnSurface( QString *errorMsg ) const { if ( !mGeos ) { - return false; + return nullptr; } + double x; + double y; + GEOSGeomScopedPtr geos; try { @@ -1564,19 +1561,15 @@ bool QgsGeos::pointOnSurface( QgsPoint &pt, QString *errorMsg ) const if ( !geos || GEOSisEmpty_r( geosinit.ctxt, geos.get() ) != 0 ) { - return false; + return nullptr; } - double x, y; GEOSGeomGetX_r( geosinit.ctxt, geos.get(), &x ); GEOSGeomGetY_r( geosinit.ctxt, geos.get(), &y ); - - pt.setX( x ); - pt.setY( y ); } - CATCH_GEOS_WITH_ERRMSG( false ); + CATCH_GEOS_WITH_ERRMSG( nullptr ); - return true; + return new QgsPoint( x, y ); } QgsAbstractGeometry *QgsGeos::convexHull( QString *errorMsg ) const diff --git a/src/core/geometry/qgsgeos.h b/src/core/geometry/qgsgeos.h index ef10ecf6bcf..a7bfb934f65 100644 --- a/src/core/geometry/qgsgeos.h +++ b/src/core/geometry/qgsgeos.h @@ -79,8 +79,8 @@ class CORE_EXPORT QgsGeos: public QgsGeometryEngine QgsAbstractGeometry *simplify( double tolerance, QString *errorMsg = nullptr ) const override; QgsAbstractGeometry *interpolate( double distance, QString *errorMsg = nullptr ) const override; QgsAbstractGeometry *envelope( QString *errorMsg = nullptr ) const override; - bool centroid( QgsPoint &pt, QString *errorMsg = nullptr ) const override; - bool pointOnSurface( QgsPoint &pt, QString *errorMsg = nullptr ) const override; + QgsPoint *centroid( QString *errorMsg = nullptr ) const override; + QgsPoint *pointOnSurface( QString *errorMsg = nullptr ) const override; QgsAbstractGeometry *convexHull( QString *errorMsg = nullptr ) const override; double distance( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override; bool intersects( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;