Skip to content
Permalink
Browse files

Simplify QgsGeometryEngine code

  • Loading branch information
m-kuhn committed Aug 13, 2017
1 parent e3787ef commit b51a5f7b6f5e692441b3311ec81ee05f23e7bf3a
@@ -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

@@ -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
@@ -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
@@ -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;

/**
@@ -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,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
@@ -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;

0 comments on commit b51a5f7

Please sign in to comment.
You can’t perform that action at this time.