Skip to content

Commit

Permalink
More use of geos::unique_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 19, 2017
1 parent 0d6df12 commit bb522b3
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 51 deletions.
20 changes: 7 additions & 13 deletions src/analysis/vector/qgszonalstatistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "qgsrasterlayer.h"
#include "qgsrasterblock.h"
#include "qgslogger.h"
#include "qgsgeos.h"

#include <QFile>

Expand Down Expand Up @@ -394,40 +395,38 @@ void QgsZonalStatistics::statisticsFromMiddlePointTest( const QgsGeometry &poly,
cellCenterY = rasterBBox.yMaximum() - pixelOffsetY * cellSizeY - cellSizeY / 2;
stats.reset();

GEOSGeometry *polyGeos = poly.exportToGeos();
geos::unique_ptr polyGeos( poly.exportToGeos() );
if ( !polyGeos )
{
return;
}

GEOSContextHandle_t geosctxt = QgsGeometry::getGEOSHandler();
const GEOSPreparedGeometry *polyGeosPrepared = GEOSPrepare_r( geosctxt, polyGeos );
geos::prepared_unique_ptr polyGeosPrepared( GEOSPrepare_r( geosctxt, polyGeos.get() ) );
if ( !polyGeosPrepared )
{
GEOSGeom_destroy_r( geosctxt, polyGeos );
return;
}

GEOSCoordSequence *cellCenterCoords = nullptr;
GEOSGeometry *currentCellCenter = nullptr;
geos::unique_ptr currentCellCenter;

QgsRectangle featureBBox = poly.boundingBox().intersect( &rasterBBox );
QgsRectangle intersectBBox = rasterBBox.intersect( &featureBBox );

QgsRasterBlock *block = mRasterProvider->block( mRasterBand, intersectBBox, nCellsX, nCellsY );
std::unique_ptr< QgsRasterBlock > block( mRasterProvider->block( mRasterBand, intersectBBox, nCellsX, nCellsY ) );
for ( int i = 0; i < nCellsY; ++i )
{
cellCenterX = rasterBBox.xMinimum() + pixelOffsetX * cellSizeX + cellSizeX / 2;
for ( int j = 0; j < nCellsX; ++j )
{
if ( validPixel( block->value( i, j ) ) )
{
GEOSGeom_destroy_r( geosctxt, currentCellCenter );
cellCenterCoords = GEOSCoordSeq_create_r( geosctxt, 1, 2 );
GEOSCoordSeq_setX_r( geosctxt, cellCenterCoords, 0, cellCenterX );
GEOSCoordSeq_setY_r( geosctxt, cellCenterCoords, 0, cellCenterY );
currentCellCenter = GEOSGeom_createPoint_r( geosctxt, cellCenterCoords );
if ( GEOSPreparedContains_r( geosctxt, polyGeosPrepared, currentCellCenter ) )
currentCellCenter.reset( GEOSGeom_createPoint_r( geosctxt, cellCenterCoords ) );
if ( GEOSPreparedContains_r( geosctxt, polyGeosPrepared.get(), currentCellCenter.get() ) )
{
stats.addValue( block->value( i, j ) );
}
Expand All @@ -436,11 +435,6 @@ void QgsZonalStatistics::statisticsFromMiddlePointTest( const QgsGeometry &poly,
}
cellCenterY -= cellSizeY;
}

GEOSGeom_destroy_r( geosctxt, currentCellCenter );
GEOSPreparedGeom_destroy_r( geosctxt, polyGeosPrepared );
GEOSGeom_destroy_r( geosctxt, polyGeos );
delete block;
}

void QgsZonalStatistics::statisticsFromPreciseIntersection( const QgsGeometry &poly, int pixelOffsetX,
Expand Down
8 changes: 4 additions & 4 deletions src/app/qgsmaptooloffsetcurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "qgssnappingconfig.h"
#include "qgssettings.h"
#include "qgisapp.h"
#include "qgsgeos.h"

#include <QGraphicsProxyWidget>
#include <QMouseEvent>
Expand Down Expand Up @@ -367,16 +368,15 @@ void QgsMapToolOffsetCurve::setOffsetForRubberBand( double offset )
}

QgsGeometry geomCopy( mOriginalGeometry );
GEOSGeometry *geosGeom = geomCopy.exportToGeos();
geos::unique_ptr geosGeom( geomCopy.exportToGeos() );
if ( geosGeom )
{
QgsSettings s;
int joinStyle = s.value( QStringLiteral( "/qgis/digitizing/offset_join_style" ), 0 ).toInt();
int quadSegments = s.value( QStringLiteral( "/qgis/digitizing/offset_quad_seg" ), 8 ).toInt();
double miterLimit = s.value( QStringLiteral( "/qgis/digitizing/offset_miter_limit" ), 5.0 ).toDouble();

GEOSGeometry *offsetGeom = GEOSOffsetCurve_r( QgsGeometry::getGEOSHandler(), geosGeom, offset, quadSegments, joinStyle, miterLimit );
GEOSGeom_destroy_r( QgsGeometry::getGEOSHandler(), geosGeom );
geos::unique_ptr offsetGeom( GEOSOffsetCurve_r( QgsGeometry::getGEOSHandler(), geosGeom.get(), offset, quadSegments, joinStyle, miterLimit ) );
if ( !offsetGeom )
{
deleteRubberBandAndGeometry();
Expand All @@ -392,7 +392,7 @@ void QgsMapToolOffsetCurve::setOffsetForRubberBand( double offset )

if ( offsetGeom )
{
mModifiedGeometry.fromGeos( offsetGeom );
mModifiedGeometry.fromGeos( offsetGeom.release() );
mRubberBand->setToGeometry( mModifiedGeometry, sourceLayer );
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/geometry/qgsgeos.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace geos
* Destroys the GEOS geometry \a geom, using the static QGIS
* geos context.
*/
struct GeosDeleter
struct CORE_EXPORT GeosDeleter
{

/**
Expand Down
10 changes: 4 additions & 6 deletions src/core/pal/feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1684,20 +1684,18 @@ bool FeaturePart::mergeWithFeaturePart( FeaturePart *other )
GEOSGeometry *g1 = GEOSGeom_clone_r( ctxt, mGeos );
GEOSGeometry *g2 = GEOSGeom_clone_r( ctxt, other->mGeos );
GEOSGeometry *geoms[2] = { g1, g2 };
GEOSGeometry *g = GEOSGeom_createCollection_r( ctxt, GEOS_MULTILINESTRING, geoms, 2 );
GEOSGeometry *gTmp = GEOSLineMerge_r( ctxt, g );
GEOSGeom_destroy_r( ctxt, g );
geos::unique_ptr g( GEOSGeom_createCollection_r( ctxt, GEOS_MULTILINESTRING, geoms, 2 ) );
geos::unique_ptr gTmp( GEOSLineMerge_r( ctxt, g.get() ) );

if ( GEOSGeomTypeId_r( ctxt, gTmp ) != GEOS_LINESTRING )
if ( GEOSGeomTypeId_r( ctxt, gTmp.get() ) != GEOS_LINESTRING )
{
// sometimes it's not possible to merge lines (e.g. they don't touch at endpoints)
GEOSGeom_destroy_r( ctxt, gTmp );
return false;
}
invalidateGeos();

// set up new geometry
mGeos = gTmp;
mGeos = gTmp.release();
mOwnsGeom = true;

deleteCoords();
Expand Down
5 changes: 2 additions & 3 deletions src/core/pal/geomfunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,8 @@ bool GeomFunction::containsCandidate( const GEOSPreparedGeometry *geom, double x

try
{
GEOSGeometry *bboxGeos = GEOSGeom_createLinearRing_r( geosctxt, coord );
bool result = ( GEOSPreparedContainsProperly_r( geosctxt, geom, bboxGeos ) == 1 );
GEOSGeom_destroy_r( geosctxt, bboxGeos );
geos::unique_ptr bboxGeos( GEOSGeom_createLinearRing_r( geosctxt, coord ) );
bool result = ( GEOSPreparedContainsProperly_r( geosctxt, geom, bboxGeos.get() ) == 1 );
return result;
}
catch ( GEOSException &e )
Expand Down
21 changes: 8 additions & 13 deletions src/core/pal/pointset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,8 @@ bool PointSet::containsPoint( double x, double y ) const
GEOSCoordSequence *seq = GEOSCoordSeq_create_r( geosctxt, 1, 2 );
GEOSCoordSeq_setX_r( geosctxt, seq, 0, x );
GEOSCoordSeq_setY_r( geosctxt, seq, 0, y );
GEOSGeometry *point = GEOSGeom_createPoint_r( geosctxt, seq );
bool result = ( GEOSPreparedContainsProperly_r( geosctxt, preparedGeom(), point ) == 1 );
GEOSGeom_destroy_r( geosctxt, point );
geos::unique_ptr point( GEOSGeom_createPoint_r( geosctxt, seq ) );
bool result = ( GEOSPreparedContainsProperly_r( geosctxt, preparedGeom(), point.get() ) == 1 );

return result;
}
Expand Down Expand Up @@ -706,7 +705,7 @@ double PointSet::minDistanceToPoint( double px, double py, double *rx, double *r
GEOSCoordSequence *coord = GEOSCoordSeq_create_r( geosctxt, 1, 2 );
GEOSCoordSeq_setX_r( geosctxt, coord, 0, px );
GEOSCoordSeq_setY_r( geosctxt, coord, 0, py );
GEOSGeometry *geosPt = GEOSGeom_createPoint_r( geosctxt, coord );
geos::unique_ptr geosPt( GEOSGeom_createPoint_r( geosctxt, coord ) );

int type = GEOSGeomTypeId_r( geosctxt, mGeos );
const GEOSGeometry *extRing = nullptr;
Expand All @@ -719,13 +718,12 @@ 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 );
GEOSCoordSequence *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 );
GEOSGeom_destroy_r( geosctxt, geosPt );

if ( rx )
*rx = nx;
Expand All @@ -752,29 +750,26 @@ void PointSet::getCentroid( double &px, double &py, bool forceInside ) const
try
{
GEOSContextHandle_t geosctxt = geosContext();
GEOSGeometry *centroidGeom = GEOSGetCentroid_r( geosctxt, mGeos );
geos::unique_ptr centroidGeom( GEOSGetCentroid_r( geosctxt, mGeos ) );
if ( centroidGeom )
{
const GEOSCoordSequence *coordSeq = GEOSGeom_getCoordSeq_r( geosctxt, centroidGeom );
const GEOSCoordSequence *coordSeq = GEOSGeom_getCoordSeq_r( geosctxt, centroidGeom.get() );
GEOSCoordSeq_getX_r( geosctxt, coordSeq, 0, &px );
GEOSCoordSeq_getY_r( geosctxt, coordSeq, 0, &py );
}

// check if centroid inside in polygon
if ( forceInside && !containsPoint( px, py ) )
{
GEOSGeometry *pointGeom = GEOSPointOnSurface_r( geosctxt, mGeos );
geos::unique_ptr pointGeom( GEOSPointOnSurface_r( geosctxt, mGeos ) );

if ( pointGeom )
{
const GEOSCoordSequence *coordSeq = GEOSGeom_getCoordSeq_r( geosctxt, pointGeom );
const GEOSCoordSequence *coordSeq = GEOSGeom_getCoordSeq_r( geosctxt, pointGeom.get() );
GEOSCoordSeq_getX_r( geosctxt, coordSeq, 0, &px );
GEOSCoordSeq_getY_r( geosctxt, coordSeq, 0, &py );
GEOSGeom_destroy_r( geosctxt, pointGeom );
}
}

GEOSGeom_destroy_r( geosctxt, centroidGeom );
}
catch ( GEOSException &e )
{
Expand Down
6 changes: 3 additions & 3 deletions src/core/qgsgeometryvalidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ email : jef at norbit dot de
#include "qgsgeometryvalidator.h"
#include "qgsgeometry.h"
#include "qgslogger.h"
#include "qgsgeos.h"

QgsGeometryValidator::QgsGeometryValidator( const QgsGeometry &geometry, QList<QgsGeometry::Error> *errors, QgsGeometry::ValidationMethod method )
: mGeometry( geometry )
Expand Down Expand Up @@ -217,7 +218,7 @@ void QgsGeometryValidator::run()
case QgsGeometry::ValidatorGeos:
{
char *r = nullptr;
GEOSGeometry *g0 = mGeometry.exportToGeos();
geos::unique_ptr g0( mGeometry.exportToGeos() );
GEOSContextHandle_t handle = QgsGeometry::getGEOSHandler();
if ( !g0 )
{
Expand All @@ -226,8 +227,7 @@ void QgsGeometryValidator::run()
else
{
GEOSGeometry *g1 = nullptr;
char res = GEOSisValidDetail_r( handle, g0, GEOSVALID_ALLOW_SELFTOUCHING_RING_FORMING_HOLE, &r, &g1 );
GEOSGeom_destroy_r( handle, g0 );
char res = GEOSisValidDetail_r( handle, g0.get(), GEOSVALID_ALLOW_SELFTOUCHING_RING_FORMING_HOLE, &r, &g1 );
if ( res != 1 )
{
if ( g1 )
Expand Down
13 changes: 5 additions & 8 deletions src/core/qgstracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,17 +526,14 @@ bool QgsTracer::initGraph()
{
t2a.start();
// GEOSNode_r may throw an exception
GEOSGeometry *allGeomGeos = allGeom.exportToGeos();
GEOSGeometry *allNoded = GEOSNode_r( QgsGeometry::getGEOSHandler(), allGeomGeos );
GEOSGeom_destroy_r( QgsGeometry::getGEOSHandler(), allGeomGeos );
geos::unique_ptr allGeomGeos( allGeom.exportToGeos() );
geos::unique_ptr allNoded( GEOSNode_r( QgsGeometry::getGEOSHandler(), allGeomGeos.get() ) );
timeNodingCall = t2a.elapsed();

QgsGeometry *noded = new QgsGeometry;
noded->fromGeos( allNoded );
QgsGeometry noded;
noded.fromGeos( allNoded.release() );

mpl = noded->asMultiPolyline();

delete noded;
mpl = noded.asMultiPolyline();
}
catch ( GEOSException &e )
{
Expand Down

0 comments on commit bb522b3

Please sign in to comment.