Skip to content

Commit

Permalink
Simplify QgsGeometryEngine code
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Aug 13, 2017
1 parent e3787ef commit b51a5f7
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 52 deletions.
20 changes: 16 additions & 4 deletions python/core/geometry/qgsgeometryengine.sip
Expand Up @@ -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

Expand Down
23 changes: 3 additions & 20 deletions src/core/geometry/qgsgeometry.cpp
Expand Up @@ -1581,16 +1581,8 @@ QgsGeometry QgsGeometry::centroid() const
}

QgsGeos geos( d->geometry );
std::unique_ptr<QgsPoint> 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
Expand All @@ -1601,17 +1593,8 @@ QgsGeometry QgsGeometry::pointOnSurface() const
}

QgsGeos geos( d->geometry );
std::unique_ptr<QgsPoint>pt( 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
Expand Down
22 changes: 20 additions & 2 deletions src/core/geometry/qgsgeometryengine.h
Expand Up @@ -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;

/**
Expand Down
41 changes: 17 additions & 24 deletions src/core/geometry/qgsgeos.cpp
Expand Up @@ -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
Expand All @@ -1550,33 +1544,32 @@ 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
{
geos.reset( GEOSPointOnSurface_r( geosinit.ctxt, mGeos ) );

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
Expand Down
4 changes: 2 additions & 2 deletions src/core/geometry/qgsgeos.h
Expand Up @@ -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;
Expand Down

0 comments on commit b51a5f7

Please sign in to comment.