Return std::unique_ptrs where possible

When a class isn't exposed to Python, we should return a
std::unique_ptr whenever a returned pointer value is owned
by the caller.
This commit is contained in:
Nyall Dawson 2017-06-20 14:09:34 +10:00
parent e32589599a
commit d81a1a3b5f
12 changed files with 113 additions and 122 deletions

View File

@ -782,7 +782,7 @@ bool QgsAuthManager::registerCoreAuthMethods()
mAuthMethods.clear();
Q_FOREACH ( const QString &authMethodKey, QgsAuthMethodRegistry::instance()->authMethodList() )
{
mAuthMethods.insert( authMethodKey, QgsAuthMethodRegistry::instance()->authMethod( authMethodKey ) );
mAuthMethods.insert( authMethodKey, QgsAuthMethodRegistry::instance()->authMethod( authMethodKey ).release() );
}
return !mAuthMethods.isEmpty();

View File

@ -260,7 +260,7 @@ void QgsAuthMethodRegistry::setLibraryDirectory( const QDir &path )
// typedef for the QgsDataProvider class factory
typedef QgsAuthMethod *classFactoryFunction_t();
QgsAuthMethod *QgsAuthMethodRegistry::authMethod( const QString &authMethodKey )
std::unique_ptr<QgsAuthMethod> QgsAuthMethodRegistry::authMethod( const QString &authMethodKey )
{
// load the plugin
QString lib = library( authMethodKey );
@ -299,7 +299,7 @@ QgsAuthMethod *QgsAuthMethodRegistry::authMethod( const QString &authMethodKey )
return nullptr;
}
QgsAuthMethod *authMethod = classFactory();
std::unique_ptr< QgsAuthMethod > authMethod( classFactory() );
if ( !authMethod )
{
QgsMessageLog::logMessage( QObject::tr( "Unable to instantiate the auth method plugin %1" ).arg( lib ) );
@ -342,9 +342,9 @@ QFunctionPointer QgsAuthMethodRegistry::function( QString const &authMethodKey,
}
}
QLibrary *QgsAuthMethodRegistry::authMethodLibrary( const QString &authMethodKey ) const
std::unique_ptr<QLibrary> QgsAuthMethodRegistry::authMethodLibrary( const QString &authMethodKey ) const
{
QLibrary *myLib = new QLibrary( library( authMethodKey ) );
std::unique_ptr< QLibrary > myLib( new QLibrary( library( authMethodKey ) ) );
QgsDebugMsg( "Library name is " + myLib->fileName() );
@ -352,9 +352,6 @@ QLibrary *QgsAuthMethodRegistry::authMethodLibrary( const QString &authMethodKey
return myLib;
QgsDebugMsg( "Cannot load library: " + myLib->errorString() );
delete myLib;
return nullptr;
}

View File

@ -23,6 +23,7 @@
#include <QLibrary>
#include <QMap>
#include <QString>
#include <memory>
#include "qgis_core.h"
@ -67,7 +68,7 @@ class CORE_EXPORT QgsAuthMethodRegistry
\param authMethodKey identificator of the auth method
\returns instance of auth method or nullptr on error
*/
QgsAuthMethod *authMethod( const QString &authMethodKey );
std::unique_ptr< QgsAuthMethod > authMethod( const QString &authMethodKey );
/** Return the auth method capabilities
\param authMethodKey identificator of the auth method
@ -89,7 +90,7 @@ class CORE_EXPORT QgsAuthMethodRegistry
const QString &functionName );
//! Return the library object associated with an auth method key
QLibrary *authMethodLibrary( const QString &authMethodKey ) const;
std::unique_ptr< QLibrary > authMethodLibrary( const QString &authMethodKey ) const;
//! Return list of available auth methods by their keys
QStringList authMethodList() const;

View File

@ -134,70 +134,70 @@ bool QgsGeometry::isNull() const
QgsGeometry QgsGeometry::fromWkt( const QString &wkt )
{
QgsAbstractGeometry *geom = QgsGeometryFactory::geomFromWkt( wkt );
std::unique_ptr< QgsAbstractGeometry > geom = QgsGeometryFactory::geomFromWkt( wkt );
if ( !geom )
{
return QgsGeometry();
}
return QgsGeometry( geom );
return QgsGeometry( geom.release() );
}
QgsGeometry QgsGeometry::fromPoint( const QgsPointXY &point )
{
QgsAbstractGeometry *geom = QgsGeometryFactory::fromPoint( point );
std::unique_ptr< QgsAbstractGeometry > geom( QgsGeometryFactory::fromPoint( point ) );
if ( geom )
{
return QgsGeometry( geom );
return QgsGeometry( geom.release() );
}
return QgsGeometry();
}
QgsGeometry QgsGeometry::fromPolyline( const QgsPolyline &polyline )
{
QgsAbstractGeometry *geom = QgsGeometryFactory::fromPolyline( polyline );
std::unique_ptr< QgsAbstractGeometry > geom = QgsGeometryFactory::fromPolyline( polyline );
if ( geom )
{
return QgsGeometry( geom );
return QgsGeometry( geom.release() );
}
return QgsGeometry();
}
QgsGeometry QgsGeometry::fromPolygon( const QgsPolygon &polygon )
{
QgsAbstractGeometry *geom = QgsGeometryFactory::fromPolygon( polygon );
std::unique_ptr< QgsAbstractGeometry > geom = QgsGeometryFactory::fromPolygon( polygon );
if ( geom )
{
return QgsGeometry( geom );
return QgsGeometry( geom.release() );
}
return QgsGeometry();
}
QgsGeometry QgsGeometry::fromMultiPoint( const QgsMultiPoint &multipoint )
{
QgsAbstractGeometry *geom = QgsGeometryFactory::fromMultiPoint( multipoint );
std::unique_ptr< QgsAbstractGeometry > geom = QgsGeometryFactory::fromMultiPoint( multipoint );
if ( geom )
{
return QgsGeometry( geom );
return QgsGeometry( geom.release() );
}
return QgsGeometry();
}
QgsGeometry QgsGeometry::fromMultiPolyline( const QgsMultiPolyline &multiline )
{
QgsAbstractGeometry *geom = QgsGeometryFactory::fromMultiPolyline( multiline );
std::unique_ptr< QgsAbstractGeometry > geom = QgsGeometryFactory::fromMultiPolyline( multiline );
if ( geom )
{
return QgsGeometry( geom );
return QgsGeometry( geom.release() );
}
return QgsGeometry();
}
QgsGeometry QgsGeometry::fromMultiPolygon( const QgsMultiPolygon &multipoly )
{
QgsAbstractGeometry *geom = QgsGeometryFactory::fromMultiPolygon( multipoly );
std::unique_ptr< QgsAbstractGeometry > geom = QgsGeometryFactory::fromMultiPolygon( multipoly );
if ( geom )
{
return QgsGeometry( geom );
return QgsGeometry( geom.release() );
}
return QgsGeometry();
}
@ -246,7 +246,7 @@ void QgsGeometry::fromWkb( unsigned char *wkb, int length )
delete d->geometry;
}
QgsConstWkbPtr ptr( wkb, length );
d->geometry = QgsGeometryFactory::geomFromWkb( ptr );
d->geometry = QgsGeometryFactory::geomFromWkb( ptr ).release();
delete [] wkb;
}
@ -259,7 +259,7 @@ void QgsGeometry::fromWkb( const QByteArray &wkb )
delete d->geometry;
}
QgsConstWkbPtr ptr( wkb );
d->geometry = QgsGeometryFactory::geomFromWkb( ptr );
d->geometry = QgsGeometryFactory::geomFromWkb( ptr ).release();
}
GEOSGeometry *QgsGeometry::exportToGeos( double precision ) const
@ -1155,8 +1155,8 @@ bool QgsGeometry::convertToMultiType()
return true;
}
QgsGeometryCollection *multiGeom = qgsgeometry_cast<QgsGeometryCollection *>
( QgsGeometryFactory::geomFromWkbType( QgsWkbTypes::multiType( d->geometry->wkbType() ) ) );
std::unique_ptr< QgsAbstractGeometry >geom = QgsGeometryFactory::geomFromWkbType( QgsWkbTypes::multiType( d->geometry->wkbType() ) );
QgsGeometryCollection *multiGeom = qgsgeometry_cast<QgsGeometryCollection *>( geom.get() );
if ( !multiGeom )
{
return false;
@ -1164,7 +1164,7 @@ bool QgsGeometry::convertToMultiType()
detach( true );
multiGeom->addGeometry( d->geometry );
d->geometry = multiGeom;
d->geometry = geom.release();
return true;
}
@ -2026,11 +2026,11 @@ int QgsGeometry::avoidIntersections( const QList<QgsVectorLayer *> &avoidInterse
return 1;
}
QgsAbstractGeometry *diffGeom = QgsGeometryEditUtils::avoidIntersections( *( d->geometry ), avoidIntersectionsLayers, ignoreFeatures );
std::unique_ptr< QgsAbstractGeometry > diffGeom = QgsGeometryEditUtils::avoidIntersections( *( d->geometry ), avoidIntersectionsLayers, ignoreFeatures );
if ( diffGeom )
{
detach( false );
d->geometry = diffGeom;
d->geometry = diffGeom.release();
}
return 0;
}

View File

@ -219,10 +219,10 @@ bool QgsGeometryCollection::fromWkb( QgsConstWkbPtr &wkbPtr )
mGeometries.clear();
for ( int i = 0; i < nGeometries; ++i )
{
QgsAbstractGeometry *geom = QgsGeometryFactory::geomFromWkb( wkbPtr ); // also updates wkbPtr
std::unique_ptr< QgsAbstractGeometry > geom( QgsGeometryFactory::geomFromWkb( wkbPtr ) ); // also updates wkbPtr
if ( geom )
{
if ( !addGeometry( geom ) )
if ( !addGeometry( geom.release() ) )
{
qDeleteAll( mGeometries );
mGeometries = geometryListBackup;
@ -597,11 +597,10 @@ bool QgsGeometryCollection::hasCurvedSegments() const
QgsAbstractGeometry *QgsGeometryCollection::segmentize( double tolerance, SegmentationToleranceType toleranceType ) const
{
QgsAbstractGeometry *geom = QgsGeometryFactory::geomFromWkbType( mWkbType );
QgsGeometryCollection *geomCollection = qgsgeometry_cast<QgsGeometryCollection *>( geom );
std::unique_ptr< QgsAbstractGeometry > geom( QgsGeometryFactory::geomFromWkbType( mWkbType ) );
QgsGeometryCollection *geomCollection = qgsgeometry_cast<QgsGeometryCollection *>( geom.get() );
if ( !geomCollection )
{
delete geom;
return clone();
}
@ -610,7 +609,7 @@ QgsAbstractGeometry *QgsGeometryCollection::segmentize( double tolerance, Segmen
{
geomCollection->addGeometry( ( *geomIt )->segmentize( tolerance, toleranceType ) );
}
return geomCollection;
return geom.release();
}
double QgsGeometryCollection::vertexAngle( QgsVertexId vertex ) const

View File

@ -221,7 +221,7 @@ bool QgsGeometryEditUtils::deletePart( QgsAbstractGeometry *geom, int partNum )
return c->removeGeometry( partNum );
}
QgsAbstractGeometry *QgsGeometryEditUtils::avoidIntersections( const QgsAbstractGeometry &geom,
std::unique_ptr<QgsAbstractGeometry> QgsGeometryEditUtils::avoidIntersections( const QgsAbstractGeometry &geom,
const QList<QgsVectorLayer *> &avoidIntersectionsLayers,
QHash<QgsVectorLayer *, QSet<QgsFeatureId> > ignoreFeatures )
{
@ -281,7 +281,7 @@ QgsAbstractGeometry *QgsGeometryEditUtils::avoidIntersections( const QgsAbstract
return nullptr;
}
QgsAbstractGeometry *diffGeom = geomEngine->difference( combinedGeometries );
std::unique_ptr< QgsAbstractGeometry > diffGeom( geomEngine->difference( combinedGeometries ) );
delete combinedGeometries;
return diffGeom;

View File

@ -26,6 +26,7 @@ class QgsVectorLayer;
#include "qgsfeature.h"
#include "qgsgeometry.h"
#include <QMap>
#include <memory>
/** \ingroup core
* \class QgsGeometryEditUtils
@ -69,7 +70,7 @@ class QgsGeometryEditUtils
* \param avoidIntersectionsLayers list of layers to check for intersections
* \param ignoreFeatures map of layer to feature id of features to ignore
*/
static QgsAbstractGeometry *avoidIntersections( const QgsAbstractGeometry &geom,
static std::unique_ptr< QgsAbstractGeometry > avoidIntersections( const QgsAbstractGeometry &geom,
const QList<QgsVectorLayer *> &avoidIntersectionsLayers,
QHash<QgsVectorLayer *, QSet<QgsFeatureId> > ignoreFeatures = ( QHash<QgsVectorLayer *, QSet<QgsFeatureId> >() ) );
};

View File

@ -30,7 +30,7 @@
#include "qgswkbtypes.h"
#include "qgslogger.h"
QgsAbstractGeometry *QgsGeometryFactory::geomFromWkb( QgsConstWkbPtr &wkbPtr )
std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::geomFromWkb( QgsConstWkbPtr &wkbPtr )
{
if ( !wkbPtr )
return nullptr;
@ -49,9 +49,7 @@ QgsAbstractGeometry *QgsGeometryFactory::geomFromWkb( QgsConstWkbPtr &wkbPtr )
}
wkbPtr -= 1 + sizeof( int );
QgsAbstractGeometry *geom = nullptr;
geom = geomFromWkbType( type );
std::unique_ptr< QgsAbstractGeometry > geom = geomFromWkbType( type );
if ( geom )
{
@ -63,86 +61,83 @@ QgsAbstractGeometry *QgsGeometryFactory::geomFromWkb( QgsConstWkbPtr &wkbPtr )
{
Q_UNUSED( e );
QgsDebugMsg( "WKB exception: " + e.what() );
delete geom;
geom = nullptr;
}
}
return geom;
}
QgsAbstractGeometry *QgsGeometryFactory::geomFromWkt( const QString &text )
std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::geomFromWkt( const QString &text )
{
QString trimmed = text.trimmed();
QgsAbstractGeometry *geom = nullptr;
std::unique_ptr< QgsAbstractGeometry> geom;
if ( trimmed.startsWith( QLatin1String( "Point" ), Qt::CaseInsensitive ) )
{
geom = new QgsPoint();
geom.reset( new QgsPoint() );
}
else if ( trimmed.startsWith( QLatin1String( "LineString" ), Qt::CaseInsensitive ) )
{
geom = new QgsLineString();
geom.reset( new QgsLineString() );
}
else if ( trimmed.startsWith( QLatin1String( "CircularString" ), Qt::CaseInsensitive ) )
{
geom = new QgsCircularString();
geom.reset( new QgsCircularString() );
}
else if ( trimmed.startsWith( QLatin1String( "CompoundCurve" ), Qt::CaseInsensitive ) )
{
geom = new QgsCompoundCurve();
geom.reset( new QgsCompoundCurve() );
}
else if ( trimmed.startsWith( QLatin1String( "Polygon" ), Qt::CaseInsensitive ) )
{
geom = new QgsPolygonV2();
geom.reset( new QgsPolygonV2() );
}
else if ( trimmed.startsWith( QLatin1String( "CurvePolygon" ), Qt::CaseInsensitive ) )
{
geom = new QgsCurvePolygon();
geom.reset( new QgsCurvePolygon() );
}
else if ( trimmed.startsWith( QLatin1String( "MultiPoint" ), Qt::CaseInsensitive ) )
{
geom = new QgsMultiPointV2();
geom.reset( new QgsMultiPointV2() );
}
else if ( trimmed.startsWith( QLatin1String( "MultiCurve" ), Qt::CaseInsensitive ) )
{
geom = new QgsMultiCurve();
geom.reset( new QgsMultiCurve() );
}
else if ( trimmed.startsWith( QLatin1String( "MultiLineString" ), Qt::CaseInsensitive ) )
{
geom = new QgsMultiLineString();
geom.reset( new QgsMultiLineString() );
}
else if ( trimmed.startsWith( QLatin1String( "MultiSurface" ), Qt::CaseInsensitive ) )
{
geom = new QgsMultiSurface();
geom.reset( new QgsMultiSurface() );
}
else if ( trimmed.startsWith( QLatin1String( "MultiPolygon" ), Qt::CaseInsensitive ) )
{
geom = new QgsMultiPolygonV2();
geom.reset( new QgsMultiPolygonV2() );
}
else if ( trimmed.startsWith( QLatin1String( "GeometryCollection" ), Qt::CaseInsensitive ) )
{
geom = new QgsGeometryCollection();
geom.reset( new QgsGeometryCollection() );
}
if ( geom )
{
if ( !geom->fromWkt( text ) )
{
delete geom;
return nullptr;
}
}
return geom;
}
QgsAbstractGeometry *QgsGeometryFactory::fromPoint( const QgsPointXY &point )
std::unique_ptr< QgsAbstractGeometry > QgsGeometryFactory::fromPoint( const QgsPointXY &point )
{
return new QgsPoint( point.x(), point.y() );
return std::unique_ptr< QgsAbstractGeometry >( new QgsPoint( point.x(), point.y() ) );
}
QgsAbstractGeometry *QgsGeometryFactory::fromMultiPoint( const QgsMultiPoint &multipoint )
std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::fromMultiPoint( const QgsMultiPoint &multipoint )
{
QgsMultiPointV2 *mp = new QgsMultiPointV2();
std::unique_ptr< QgsMultiPointV2 > mp( new QgsMultiPointV2() );
QgsMultiPoint::const_iterator ptIt = multipoint.constBegin();
for ( ; ptIt != multipoint.constEnd(); ++ptIt )
{
@ -152,55 +147,55 @@ QgsAbstractGeometry *QgsGeometryFactory::fromMultiPoint( const QgsMultiPoint &mu
return mp;
}
QgsAbstractGeometry *QgsGeometryFactory::fromPolyline( const QgsPolyline &polyline )
std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::fromPolyline( const QgsPolyline &polyline )
{
return linestringFromPolyline( polyline );
}
QgsAbstractGeometry *QgsGeometryFactory::fromMultiPolyline( const QgsMultiPolyline &multiline )
std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::fromMultiPolyline( const QgsMultiPolyline &multiline )
{
QgsMultiLineString *mLine = new QgsMultiLineString();
std::unique_ptr< QgsMultiLineString > mLine( new QgsMultiLineString() );
for ( int i = 0; i < multiline.size(); ++i )
{
mLine->addGeometry( fromPolyline( multiline.at( i ) ) );
mLine->addGeometry( fromPolyline( multiline.at( i ) ).release() );
}
return mLine;
}
QgsAbstractGeometry *QgsGeometryFactory::fromPolygon( const QgsPolygon &polygon )
std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::fromPolygon( const QgsPolygon &polygon )
{
QgsPolygonV2 *poly = new QgsPolygonV2();
std::unique_ptr< QgsPolygonV2 > poly( new QgsPolygonV2() );
QList<QgsCurve *> holes;
for ( int i = 0; i < polygon.size(); ++i )
{
QgsLineString *l = linestringFromPolyline( polygon.at( i ) );
std::unique_ptr< QgsLineString > l = linestringFromPolyline( polygon.at( i ) );
l->close();
if ( i == 0 )
{
poly->setExteriorRing( l );
poly->setExteriorRing( l.release() );
}
else
{
holes.push_back( l );
holes.push_back( l.release() );
}
}
poly->setInteriorRings( holes );
return poly;
}
QgsAbstractGeometry *QgsGeometryFactory::fromMultiPolygon( const QgsMultiPolygon &multipoly )
std::unique_ptr< QgsAbstractGeometry > QgsGeometryFactory::fromMultiPolygon( const QgsMultiPolygon &multipoly )
{
QgsMultiPolygonV2 *mp = new QgsMultiPolygonV2();
std::unique_ptr< QgsMultiPolygonV2 > mp( new QgsMultiPolygonV2() );
for ( int i = 0; i < multipoly.size(); ++i )
{
mp->addGeometry( fromPolygon( multipoly.at( i ) ) );
mp->addGeometry( fromPolygon( multipoly.at( i ) ).release() );
}
return mp;
}
QgsAbstractGeometry *QgsGeometryFactory::fromRect( const QgsRectangle &rect )
std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::fromRect( const QgsRectangle &rect )
{
QgsPolyline ring;
ring.append( QgsPointXY( rect.xMinimum(), rect.yMinimum() ) );
@ -215,7 +210,7 @@ QgsAbstractGeometry *QgsGeometryFactory::fromRect( const QgsRectangle &rect )
return fromPolygon( polygon );
}
QgsLineString *QgsGeometryFactory::linestringFromPolyline( const QgsPolyline &polyline )
std::unique_ptr<QgsLineString> QgsGeometryFactory::linestringFromPolyline( const QgsPolyline &polyline )
{
QVector< double > x;
x.reserve( polyline.size() );
@ -227,39 +222,39 @@ QgsLineString *QgsGeometryFactory::linestringFromPolyline( const QgsPolyline &po
x << it->x();
y << it->y();
}
QgsLineString *line = new QgsLineString( x, y );
std::unique_ptr< QgsLineString > line( new QgsLineString( x, y ) );
return line;
}
QgsAbstractGeometry *QgsGeometryFactory::geomFromWkbType( QgsWkbTypes::Type t )
std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::geomFromWkbType( QgsWkbTypes::Type t )
{
QgsWkbTypes::Type type = QgsWkbTypes::flatType( t );
switch ( type )
{
case QgsWkbTypes::Point:
return new QgsPoint();
return std::unique_ptr<QgsAbstractGeometry>( new QgsPoint() );
case QgsWkbTypes::LineString:
return new QgsLineString();
return std::unique_ptr<QgsAbstractGeometry>( new QgsLineString() );
case QgsWkbTypes::CircularString:
return new QgsCircularString();
return std::unique_ptr<QgsAbstractGeometry>( new QgsCircularString() );
case QgsWkbTypes::CompoundCurve:
return new QgsCompoundCurve();
return std::unique_ptr<QgsAbstractGeometry>( new QgsCompoundCurve() );
case QgsWkbTypes::Polygon:
return new QgsPolygonV2();
return std::unique_ptr<QgsAbstractGeometry>( new QgsPolygonV2() );
case QgsWkbTypes::CurvePolygon:
return new QgsCurvePolygon();
return std::unique_ptr<QgsAbstractGeometry>( new QgsCurvePolygon() );
case QgsWkbTypes::MultiLineString:
return new QgsMultiLineString();
return std::unique_ptr<QgsAbstractGeometry>( new QgsMultiLineString() );
case QgsWkbTypes::MultiPolygon:
return new QgsMultiPolygonV2();
return std::unique_ptr<QgsAbstractGeometry>( new QgsMultiPolygonV2() );
case QgsWkbTypes::MultiPoint:
return new QgsMultiPointV2();
return std::unique_ptr<QgsAbstractGeometry>( new QgsMultiPointV2() );
case QgsWkbTypes::MultiCurve:
return new QgsMultiCurve();
return std::unique_ptr<QgsAbstractGeometry>( new QgsMultiCurve() );
case QgsWkbTypes::MultiSurface:
return new QgsMultiSurface();
return std::unique_ptr<QgsAbstractGeometry>( new QgsMultiSurface() );
case QgsWkbTypes::GeometryCollection:
return new QgsGeometryCollection();
return std::unique_ptr<QgsAbstractGeometry>( new QgsGeometryCollection() );
default:
return nullptr;
}

View File

@ -51,28 +51,28 @@ class CORE_EXPORT QgsGeometryFactory
/** Construct geometry from a WKB string.
* Updates position of the passed WKB pointer.
*/
static QgsAbstractGeometry *geomFromWkb( QgsConstWkbPtr &wkb );
static std::unique_ptr< QgsAbstractGeometry > geomFromWkb( QgsConstWkbPtr &wkb );
/** Construct geometry from a WKT string.
*/
static QgsAbstractGeometry *geomFromWkt( const QString &text );
static std::unique_ptr< QgsAbstractGeometry > geomFromWkt( const QString &text );
//! Construct geometry from a point
static QgsAbstractGeometry *fromPoint( const QgsPointXY &point );
static std::unique_ptr< QgsAbstractGeometry > fromPoint( const QgsPointXY &point );
//! Construct geometry from a multipoint
static QgsAbstractGeometry *fromMultiPoint( const QgsMultiPoint &multipoint );
static std::unique_ptr< QgsAbstractGeometry > fromMultiPoint( const QgsMultiPoint &multipoint );
//! Construct geometry from a polyline
static QgsAbstractGeometry *fromPolyline( const QgsPolyline &polyline );
static std::unique_ptr< QgsAbstractGeometry > fromPolyline( const QgsPolyline &polyline );
//! Construct geometry from a multipolyline
static QgsAbstractGeometry *fromMultiPolyline( const QgsMultiPolyline &multiline );
static std::unique_ptr< QgsAbstractGeometry > fromMultiPolyline( const QgsMultiPolyline &multiline );
//! Construct geometry from a polygon
static QgsAbstractGeometry *fromPolygon( const QgsPolygon &polygon );
static std::unique_ptr< QgsAbstractGeometry > fromPolygon( const QgsPolygon &polygon );
//! Construct geometry from a multipolygon
static QgsAbstractGeometry *fromMultiPolygon( const QgsMultiPolygon &multipoly );
static std::unique_ptr< QgsAbstractGeometry > fromMultiPolygon( const QgsMultiPolygon &multipoly );
//! Construct geometry from a rectangle
static QgsAbstractGeometry *fromRect( const QgsRectangle &rect );
static std::unique_ptr< QgsAbstractGeometry > fromRect( const QgsRectangle &rect );
//! Return empty geometry from wkb type
static QgsAbstractGeometry *geomFromWkbType( QgsWkbTypes::Type t );
static std::unique_ptr< QgsAbstractGeometry > geomFromWkbType( QgsWkbTypes::Type t );
/**
* Returns a new geometry collection matching a specified WKB \a type. For instance, if
@ -81,7 +81,7 @@ class CORE_EXPORT QgsGeometryFactory
static std::unique_ptr< QgsGeometryCollection > createCollectionOfType( QgsWkbTypes::Type type );
private:
static QgsLineString *linestringFromPolyline( const QgsPolyline &polyline );
static std::unique_ptr< QgsLineString > linestringFromPolyline( const QgsPolyline &polyline );
};
#endif // QGSGEOMETRYFACTORY_H

View File

@ -722,7 +722,7 @@ QgsGeometry QgsVectorDataProvider::convertToProviderType( const QgsGeometry &geo
return QgsGeometry();
}
QgsAbstractGeometry *outputGeom = nullptr;
std::unique_ptr< QgsAbstractGeometry > outputGeom;
//convert compoundcurve to circularstring (possible if compoundcurve consists of one circular string)
if ( QgsWkbTypes::flatType( providerGeomType ) == QgsWkbTypes::CircularString )
@ -735,7 +735,7 @@ QgsGeometry QgsVectorDataProvider::convertToProviderType( const QgsGeometry &geo
const QgsCircularString *circularString = qgsgeometry_cast<const QgsCircularString *>( compoundCurve->curveAt( 0 ) );
if ( circularString )
{
outputGeom = circularString->clone();
outputGeom.reset( circularString->clone() );
}
}
}
@ -745,7 +745,7 @@ QgsGeometry QgsVectorDataProvider::convertToProviderType( const QgsGeometry &geo
if ( QgsWkbTypes::isMultiType( providerGeomType ) && !QgsWkbTypes::isMultiType( geometry->wkbType() ) )
{
outputGeom = QgsGeometryFactory::geomFromWkbType( providerGeomType );
QgsGeometryCollection *geomCollection = qgsgeometry_cast<QgsGeometryCollection *>( outputGeom );
QgsGeometryCollection *geomCollection = qgsgeometry_cast<QgsGeometryCollection *>( outputGeom.get() );
if ( geomCollection )
{
geomCollection->addGeometry( geometry->clone() );
@ -758,8 +758,7 @@ QgsGeometry QgsVectorDataProvider::convertToProviderType( const QgsGeometry &geo
QgsAbstractGeometry *curveGeom = outputGeom ? outputGeom->toCurveType() : geometry->toCurveType();
if ( curveGeom )
{
delete outputGeom;
outputGeom = curveGeom;
outputGeom.reset( curveGeom );
}
}
@ -770,8 +769,7 @@ QgsGeometry QgsVectorDataProvider::convertToProviderType( const QgsGeometry &geo
segmentizedGeom = outputGeom ? outputGeom->segmentize() : geometry->segmentize();
if ( segmentizedGeom )
{
delete outputGeom;
outputGeom = segmentizedGeom;
outputGeom.reset( segmentizedGeom );
}
}
@ -780,7 +778,7 @@ QgsGeometry QgsVectorDataProvider::convertToProviderType( const QgsGeometry &geo
{
if ( !outputGeom )
{
outputGeom = geometry->clone();
outputGeom.reset( geometry->clone() );
}
outputGeom->addZValue();
}
@ -788,14 +786,14 @@ QgsGeometry QgsVectorDataProvider::convertToProviderType( const QgsGeometry &geo
{
if ( !outputGeom )
{
outputGeom = geometry->clone();
outputGeom.reset( geometry->clone() );
}
outputGeom->addMValue();
}
if ( outputGeom )
{
return QgsGeometry( outputGeom );
return QgsGeometry( outputGeom.release() );
}
return QgsGeometry();
}

View File

@ -218,7 +218,7 @@ void TestQgsDistanceArea::regression13601()
QgsDistanceArea calc;
calc.setEllipsoid( QStringLiteral( "NONE" ) );
calc.setSourceCrs( QgsCoordinateReferenceSystem::fromSrsId( 1108L ) );
QgsGeometry geom( QgsGeometryFactory::geomFromWkt( QStringLiteral( "Polygon ((252000 1389000, 265000 1389000, 265000 1385000, 252000 1385000, 252000 1389000))" ) ) );
QgsGeometry geom( QgsGeometryFactory::geomFromWkt( QStringLiteral( "Polygon ((252000 1389000, 265000 1389000, 265000 1385000, 252000 1385000, 252000 1389000))" ) ).release() );
QGSCOMPARENEAR( calc.measureArea( geom ), 52000000, 0.0001 );
}
@ -230,21 +230,21 @@ void TestQgsDistanceArea::collections()
myDa.setEllipsoid( QStringLiteral( "WGS84" ) );
//collection of lines, should be sum of line length
QgsGeometry lines( QgsGeometryFactory::geomFromWkt( QStringLiteral( "GeometryCollection( LineString(0 36.53, 5.76 -48.16), LineString(0 25.54, 24.20 36.70) )" ) ) );
QgsGeometry lines( QgsGeometryFactory::geomFromWkt( QStringLiteral( "GeometryCollection( LineString(0 36.53, 5.76 -48.16), LineString(0 25.54, 24.20 36.70) )" ) ).release() );
double result = myDa.measureLength( lines );
QGSCOMPARENEAR( result, 12006159, 1 );
result = myDa.measureArea( lines );
QVERIFY( qgsDoubleNear( result, 0 ) );
//collection of polygons
QgsGeometry polys( QgsGeometryFactory::geomFromWkt( QStringLiteral( "GeometryCollection( Polygon((0 36.53, 5.76 -48.16, 0 25.54, 0 36.53)), Polygon((10 20, 15 20, 15 10, 10 20)) )" ) ) );
QgsGeometry polys( QgsGeometryFactory::geomFromWkt( QStringLiteral( "GeometryCollection( Polygon((0 36.53, 5.76 -48.16, 0 25.54, 0 36.53)), Polygon((10 20, 15 20, 15 10, 10 20)) )" ) ).release() );
result = myDa.measureArea( polys );
QGSCOMPARENEAR( result, 670434859475LL, 1 );
result = myDa.measureLength( polys );
QVERIFY( qgsDoubleNear( result, 0 ) );
//mixed collection
QgsGeometry mixed( QgsGeometryFactory::geomFromWkt( QStringLiteral( "GeometryCollection( LineString(0 36.53, 5.76 -48.16), LineString(0 25.54, 24.20 36.70), Polygon((0 36.53, 5.76 -48.16, 0 25.54, 0 36.53)), Polygon((10 20, 15 20, 15 10, 10 20)) )" ) ) );
QgsGeometry mixed( QgsGeometryFactory::geomFromWkt( QStringLiteral( "GeometryCollection( LineString(0 36.53, 5.76 -48.16), LineString(0 25.54, 24.20 36.70), Polygon((0 36.53, 5.76 -48.16, 0 25.54, 0 36.53)), Polygon((10 20, 15 20, 15 10, 10 20)) )" ) ).release() );
//measure area specifically
result = myDa.measureArea( mixed );
QGSCOMPARENEAR( result, 670434859475LL, 1 );
@ -375,7 +375,7 @@ void TestQgsDistanceArea::regression14675()
QgsDistanceArea calc;
calc.setEllipsoid( QStringLiteral( "GRS80" ) );
calc.setSourceCrs( QgsCoordinateReferenceSystem::fromSrsId( 145L ) );
QgsGeometry geom( QgsGeometryFactory::geomFromWkt( QStringLiteral( "Polygon ((917593.5791854317067191 6833700.00807378999888897, 917596.43389983859378844 6833700.67099479306489229, 917599.53056440979707986 6833700.78673478215932846, 917593.5791854317067191 6833700.00807378999888897))" ) ) );
QgsGeometry geom( QgsGeometryFactory::geomFromWkt( QStringLiteral( "Polygon ((917593.5791854317067191 6833700.00807378999888897, 917596.43389983859378844 6833700.67099479306489229, 917599.53056440979707986 6833700.78673478215932846, 917593.5791854317067191 6833700.00807378999888897))" ) ).release() );
//lots of tolerance here - the formulas get quite unstable with small areas due to division by very small floats
QGSCOMPARENEAR( calc.measureArea( geom ), 0.833010, 0.03 );
}

View File

@ -5293,7 +5293,7 @@ void TestQgsGeometry::wkbInOut()
void TestQgsGeometry::segmentizeCircularString()
{
QString wkt( QStringLiteral( "CIRCULARSTRING( 0 0, 0.5 0.5, 2 0 )" ) );
QgsCircularString *circularString = dynamic_cast<QgsCircularString *>( QgsGeometryFactory::geomFromWkt( wkt ) );
QgsCircularString *circularString = dynamic_cast<QgsCircularString *>( QgsGeometryFactory::geomFromWkt( wkt ).release() );
QVERIFY( circularString );
QgsLineString *lineString = circularString->curveToLine();
QVERIFY( lineString );
@ -5311,11 +5311,11 @@ void TestQgsGeometry::directionNeutralSegmentation()
{
//Tests, if segmentation of a circularstring is the same in both directions
QString CWCircularStringWkt( QStringLiteral( "CIRCULARSTRING( 0 0, 0.5 0.5, 0.83 7.33 )" ) );
QgsCircularString *CWCircularString = static_cast<QgsCircularString *>( QgsGeometryFactory::geomFromWkt( CWCircularStringWkt ) );
QgsCircularString *CWCircularString = static_cast<QgsCircularString *>( QgsGeometryFactory::geomFromWkt( CWCircularStringWkt ).release() );
QgsLineString *CWLineString = CWCircularString->curveToLine();
QString CCWCircularStringWkt( QStringLiteral( "CIRCULARSTRING( 0.83 7.33, 0.5 0.5, 0 0 )" ) );
QgsCircularString *CCWCircularString = static_cast<QgsCircularString *>( QgsGeometryFactory::geomFromWkt( CCWCircularStringWkt ) );
QgsCircularString *CCWCircularString = static_cast<QgsCircularString *>( QgsGeometryFactory::geomFromWkt( CCWCircularStringWkt ).release() );
QgsLineString *CCWLineString = CCWCircularString->curveToLine();
QgsLineString *reversedCCWLineString = CCWLineString->reversed();