Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Also add unique_ptrs for other geos classes
  • Loading branch information
nyalldawson committed Oct 20, 2017
1 parent b2add8c commit 8d64e2e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
42 changes: 24 additions & 18 deletions src/core/geometry/qgsgeos.cpp
Expand Up @@ -111,6 +111,16 @@ void geos::GeosDeleter::operator()( const GEOSPreparedGeometry *geom )
GEOSPreparedGeom_destroy_r( geosinit.ctxt, geom );
}

void geos::GeosDeleter::operator()( GEOSBufferParams *params )
{
GEOSBufferParams_destroy_r( geosinit.ctxt, params );
}

void geos::GeosDeleter::operator()( GEOSCoordSequence *sequence )
{
GEOSCoordSeq_destroy_r( geosinit.ctxt, sequence );
}


///@endcond

Expand Down Expand Up @@ -1851,18 +1861,17 @@ std::unique_ptr<QgsAbstractGeometry> QgsGeos::singleSidedBuffer( double distance
geos::unique_ptr geos;
try
{
GEOSBufferParams *bp = GEOSBufferParams_create_r( geosinit.ctxt );
GEOSBufferParams_setSingleSided_r( geosinit.ctxt, bp, 1 );
GEOSBufferParams_setQuadrantSegments_r( geosinit.ctxt, bp, segments );
GEOSBufferParams_setJoinStyle_r( geosinit.ctxt, bp, joinStyle );
GEOSBufferParams_setMitreLimit_r( geosinit.ctxt, bp, miterLimit ); //#spellok
geos::buffer_params_unique_ptr bp( GEOSBufferParams_create_r( geosinit.ctxt ) );
GEOSBufferParams_setSingleSided_r( geosinit.ctxt, bp.get(), 1 );
GEOSBufferParams_setQuadrantSegments_r( geosinit.ctxt, bp.get(), segments );
GEOSBufferParams_setJoinStyle_r( geosinit.ctxt, bp.get(), joinStyle );
GEOSBufferParams_setMitreLimit_r( geosinit.ctxt, bp.get(), miterLimit ); //#spellok

if ( side == 1 )
{
distance = -distance;
}
geos.reset( GEOSBufferWithParams_r( geosinit.ctxt, mGeos.get(), bp, distance ) );
GEOSBufferParams_destroy_r( geosinit.ctxt, bp );
geos.reset( GEOSBufferWithParams_r( geosinit.ctxt, mGeos.get(), bp.get(), distance ) );
}
CATCH_GEOS_WITH_ERRMSG( nullptr );
return fromGeos( geos.get() );
Expand Down Expand Up @@ -2020,11 +2029,10 @@ QgsGeometry QgsGeos::closestPoint( const QgsGeometry &other, QString *errorMsg )
double ny = 0.0;
try
{
GEOSCoordSequence *nearestCoord = GEOSNearestPoints_r( geosinit.ctxt, mGeos.get(), otherGeom.get() );
geos::coord_sequence_unique_ptr nearestCoord( GEOSNearestPoints_r( geosinit.ctxt, mGeos.get(), otherGeom.get() ) );

( void )GEOSCoordSeq_getX_r( geosinit.ctxt, nearestCoord, 0, &nx );
( void )GEOSCoordSeq_getY_r( geosinit.ctxt, nearestCoord, 0, &ny );
GEOSCoordSeq_destroy_r( geosinit.ctxt, nearestCoord );
( void )GEOSCoordSeq_getX_r( geosinit.ctxt, nearestCoord.get(), 0, &nx );
( void )GEOSCoordSeq_getY_r( geosinit.ctxt, nearestCoord.get(), 0, &ny );
}
catch ( GEOSException &e )
{
Expand Down Expand Up @@ -2057,14 +2065,12 @@ QgsGeometry QgsGeos::shortestLine( const QgsGeometry &other, QString *errorMsg )
double ny2 = 0.0;
try
{
GEOSCoordSequence *nearestCoord = GEOSNearestPoints_r( geosinit.ctxt, mGeos.get(), otherGeom.get() );

( void )GEOSCoordSeq_getX_r( geosinit.ctxt, nearestCoord, 0, &nx1 );
( void )GEOSCoordSeq_getY_r( geosinit.ctxt, nearestCoord, 0, &ny1 );
( void )GEOSCoordSeq_getX_r( geosinit.ctxt, nearestCoord, 1, &nx2 );
( void )GEOSCoordSeq_getY_r( geosinit.ctxt, nearestCoord, 1, &ny2 );
geos::coord_sequence_unique_ptr nearestCoord( GEOSNearestPoints_r( geosinit.ctxt, mGeos.get(), otherGeom.get() ) );

GEOSCoordSeq_destroy_r( geosinit.ctxt, nearestCoord );
( void )GEOSCoordSeq_getX_r( geosinit.ctxt, nearestCoord.get(), 0, &nx1 );
( void )GEOSCoordSeq_getY_r( geosinit.ctxt, nearestCoord.get(), 0, &ny1 );
( void )GEOSCoordSeq_getX_r( geosinit.ctxt, nearestCoord.get(), 1, &nx2 );
( void )GEOSCoordSeq_getY_r( geosinit.ctxt, nearestCoord.get(), 1, &ny2 );
}
catch ( GEOSException &e )
{
Expand Down
23 changes: 23 additions & 0 deletions src/core/geometry/qgsgeos.h
Expand Up @@ -54,6 +54,18 @@ namespace geos
* geos context.
*/
void CORE_EXPORT operator()( const GEOSPreparedGeometry *geom );

/**
* Destroys the GEOS buffer params \a params, using the static QGIS
* geos context.
*/
void CORE_EXPORT operator()( GEOSBufferParams *params );

/**
* Destroys the GEOS coordinate sequence \a sequence, using the static QGIS
* geos context.
*/
void CORE_EXPORT operator()( GEOSCoordSequence *sequence );
};

/**
Expand All @@ -65,6 +77,17 @@ namespace geos
* Scoped GEOS prepared geometry pointer.
*/
using prepared_unique_ptr = std::unique_ptr< const GEOSPreparedGeometry, GeosDeleter>;

/**
* Scoped GEOS buffer params pointer.
*/
using buffer_params_unique_ptr = std::unique_ptr< GEOSBufferParams, GeosDeleter>;

/**
* Scoped GEOS coordinate sequence pointer.
*/
using coord_sequence_unique_ptr = std::unique_ptr< GEOSCoordSequence, GeosDeleter>;

}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/core/pal/pointset.cpp
Expand Up @@ -718,12 +718,11 @@ double PointSet::minDistanceToPoint( double px, double py, double *rx, double *r
//for polygons, we want distance to exterior ring (not an interior point)
extRing = GEOSGetExteriorRing_r( geosctxt, mGeos );
}
GEOSCoordSequence *nearestCoord = GEOSNearestPoints_r( geosctxt, extRing, geosPt.get() );
geos::coord_sequence_unique_ptr nearestCoord( GEOSNearestPoints_r( geosctxt, extRing, geosPt.get() ) );
double nx;
double ny;
( void )GEOSCoordSeq_getX_r( geosctxt, nearestCoord, 0, &nx );
( void )GEOSCoordSeq_getY_r( geosctxt, nearestCoord, 0, &ny );
GEOSCoordSeq_destroy_r( geosctxt, nearestCoord );
( void )GEOSCoordSeq_getX_r( geosctxt, nearestCoord.get(), 0, &nx );
( void )GEOSCoordSeq_getY_r( geosctxt, nearestCoord.get(), 0, &ny );

if ( rx )
*rx = nx;
Expand Down

0 comments on commit 8d64e2e

Please sign in to comment.