Skip to content

Commit b51a5f7

Browse files
committedAug 13, 2017
Simplify QgsGeometryEngine code
1 parent e3787ef commit b51a5f7

File tree

5 files changed

+58
-52
lines changed

5 files changed

+58
-52
lines changed
 

‎python/core/geometry/qgsgeometryengine.sip

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,28 @@ class QgsGeometryEngine
8585
%Docstring
8686
:rtype: QgsAbstractGeometry
8787
%End
88-
virtual bool centroid( QgsPoint &pt, QString *errorMsg = 0 ) const = 0;
88+
89+
virtual QgsPoint *centroid( QString *errorMsg = 0 ) const = 0 /Factory/;
8990
%Docstring
90-
:rtype: bool
91+
Calculates the centroid of this.
92+
May return a `None`.
93+
94+
.. versionadded:: 3.0
95+
:rtype: QgsPoint
9196
%End
92-
virtual bool pointOnSurface( QgsPoint &pt, QString *errorMsg = 0 ) const = 0;
97+
98+
virtual QgsPoint *pointOnSurface( QString *errorMsg = 0 ) const = 0 /Factory/;
9399
%Docstring
94-
:rtype: bool
100+
Calculate a point that is guaranteed to be on the surface of this.
101+
May return a `None`.
102+
103+
.. versionadded:: 3.0
104+
:rtype: QgsPoint
95105
%End
106+
96107
virtual QgsAbstractGeometry *convexHull( QString *errorMsg = 0 ) const = 0 /Factory/;
97108
%Docstring
109+
Calculate the convex hull of this.
98110
:rtype: QgsAbstractGeometry
99111
%End
100112

‎src/core/geometry/qgsgeometry.cpp

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,16 +1581,8 @@ QgsGeometry QgsGeometry::centroid() const
15811581
}
15821582

15831583
QgsGeos geos( d->geometry );
1584-
std::unique_ptr<QgsPoint> centroid( new QgsPoint() );
1585-
QString error;
1586-
bool ok = geos.centroid( *centroid.get(), &error );
1587-
if ( !ok )
1588-
{
1589-
QgsGeometry geom;
1590-
geom.d->error = error;
1591-
return geom;
1592-
}
1593-
return QgsGeometry( centroid.release() );
1584+
1585+
return QgsGeometry( geos.centroid( &d->error ) );
15941586
}
15951587

15961588
QgsGeometry QgsGeometry::pointOnSurface() const
@@ -1601,17 +1593,8 @@ QgsGeometry QgsGeometry::pointOnSurface() const
16011593
}
16021594

16031595
QgsGeos geos( d->geometry );
1604-
std::unique_ptr<QgsPoint>pt( new QgsPoint() );
16051596

1606-
QString error;
1607-
bool ok = geos.pointOnSurface( *pt.get(), &error );
1608-
if ( !ok )
1609-
{
1610-
QgsGeometry geom;
1611-
geom.d->error = error;
1612-
return geom;
1613-
}
1614-
return QgsGeometry( pt.release() );
1597+
return QgsGeometry( geos.pointOnSurface( &d->error ) );
16151598
}
16161599

16171600
QgsGeometry QgsGeometry::poleOfInaccessibility( double precision, double *distanceToBoundary ) const

‎src/core/geometry/qgsgeometryengine.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,26 @@ class CORE_EXPORT QgsGeometryEngine
7575
virtual QgsAbstractGeometry *simplify( double tolerance, QString *errorMsg = nullptr ) const = 0 SIP_FACTORY;
7676
virtual QgsAbstractGeometry *interpolate( double distance, QString *errorMsg = nullptr ) const = 0 SIP_FACTORY;
7777
virtual QgsAbstractGeometry *envelope( QString *errorMsg = nullptr ) const = 0 SIP_FACTORY;
78-
virtual bool centroid( QgsPoint &pt, QString *errorMsg = nullptr ) const = 0;
79-
virtual bool pointOnSurface( QgsPoint &pt, QString *errorMsg = nullptr ) const = 0;
78+
79+
/**
80+
* Calculates the centroid of this.
81+
* May return a `nullptr`.
82+
*
83+
* \since QGIS 3.0 the centroid is returned
84+
*/
85+
virtual QgsPoint *centroid( QString *errorMsg = nullptr ) const = 0 SIP_FACTORY;
86+
87+
/**
88+
* Calculate a point that is guaranteed to be on the surface of this.
89+
* May return a `nullptr`.
90+
*
91+
* \since QGIS 3.0 the centroid is returned
92+
*/
93+
virtual QgsPoint *pointOnSurface( QString *errorMsg = nullptr ) const = 0 SIP_FACTORY;
94+
95+
/**
96+
* Calculate the convex hull of this.
97+
*/
8098
virtual QgsAbstractGeometry *convexHull( QString *errorMsg = nullptr ) const = 0 SIP_FACTORY;
8199

82100
/**

‎src/core/geometry/qgsgeos.cpp

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,36 +1503,30 @@ QgsAbstractGeometry *QgsGeos::interpolate( double distance, QString *errorMsg )
15031503
return fromGeos( geos.get() );
15041504
}
15051505

1506-
bool QgsGeos::centroid( QgsPoint &pt, QString *errorMsg ) const
1506+
QgsPoint *QgsGeos::centroid( QString *errorMsg ) const
15071507
{
15081508
if ( !mGeos )
15091509
{
1510-
return false;
1510+
return nullptr;
15111511
}
15121512

15131513
GEOSGeomScopedPtr geos;
1514+
double x;
1515+
double y;
1516+
15141517
try
15151518
{
15161519
geos.reset( GEOSGetCentroid_r( geosinit.ctxt, mGeos ) );
1517-
}
1518-
CATCH_GEOS_WITH_ERRMSG( false );
15191520

1520-
if ( !geos )
1521-
{
1522-
return false;
1523-
}
1521+
if ( !geos )
1522+
return nullptr;
15241523

1525-
try
1526-
{
1527-
double x, y;
15281524
GEOSGeomGetX_r( geosinit.ctxt, geos.get(), &x );
15291525
GEOSGeomGetY_r( geosinit.ctxt, geos.get(), &y );
1530-
pt.setX( x );
1531-
pt.setY( y );
15321526
}
1533-
CATCH_GEOS_WITH_ERRMSG( false );
1527+
CATCH_GEOS_WITH_ERRMSG( nullptr );
15341528

1535-
return true;
1529+
return new QgsPoint( x, y );
15361530
}
15371531

15381532
QgsAbstractGeometry *QgsGeos::envelope( QString *errorMsg ) const
@@ -1550,33 +1544,32 @@ QgsAbstractGeometry *QgsGeos::envelope( QString *errorMsg ) const
15501544
return fromGeos( geos.get() );
15511545
}
15521546

1553-
bool QgsGeos::pointOnSurface( QgsPoint &pt, QString *errorMsg ) const
1547+
QgsPoint *QgsGeos::pointOnSurface( QString *errorMsg ) const
15541548
{
15551549
if ( !mGeos )
15561550
{
1557-
return false;
1551+
return nullptr;
15581552
}
15591553

1554+
double x;
1555+
double y;
1556+
15601557
GEOSGeomScopedPtr geos;
15611558
try
15621559
{
15631560
geos.reset( GEOSPointOnSurface_r( geosinit.ctxt, mGeos ) );
15641561

15651562
if ( !geos || GEOSisEmpty_r( geosinit.ctxt, geos.get() ) != 0 )
15661563
{
1567-
return false;
1564+
return nullptr;
15681565
}
15691566

1570-
double x, y;
15711567
GEOSGeomGetX_r( geosinit.ctxt, geos.get(), &x );
15721568
GEOSGeomGetY_r( geosinit.ctxt, geos.get(), &y );
1573-
1574-
pt.setX( x );
1575-
pt.setY( y );
15761569
}
1577-
CATCH_GEOS_WITH_ERRMSG( false );
1570+
CATCH_GEOS_WITH_ERRMSG( nullptr );
15781571

1579-
return true;
1572+
return new QgsPoint( x, y );
15801573
}
15811574

15821575
QgsAbstractGeometry *QgsGeos::convexHull( QString *errorMsg ) const

‎src/core/geometry/qgsgeos.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ class CORE_EXPORT QgsGeos: public QgsGeometryEngine
7979
QgsAbstractGeometry *simplify( double tolerance, QString *errorMsg = nullptr ) const override;
8080
QgsAbstractGeometry *interpolate( double distance, QString *errorMsg = nullptr ) const override;
8181
QgsAbstractGeometry *envelope( QString *errorMsg = nullptr ) const override;
82-
bool centroid( QgsPoint &pt, QString *errorMsg = nullptr ) const override;
83-
bool pointOnSurface( QgsPoint &pt, QString *errorMsg = nullptr ) const override;
82+
QgsPoint *centroid( QString *errorMsg = nullptr ) const override;
83+
QgsPoint *pointOnSurface( QString *errorMsg = nullptr ) const override;
8484
QgsAbstractGeometry *convexHull( QString *errorMsg = nullptr ) const override;
8585
double distance( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;
8686
bool intersects( const QgsAbstractGeometry *geom, QString *errorMsg = nullptr ) const override;

0 commit comments

Comments
 (0)
Please sign in to comment.