Simplify QgsGeometryEngine code

This commit is contained in:
Matthias Kuhn 2017-08-13 18:46:12 +02:00
parent e3787effc6
commit b51a5f7b6f
5 changed files with 58 additions and 52 deletions

View File

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

View File

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

View File

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

View File

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

View File

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