Skip to content
Permalink
Browse files

Implement scoped pointers for gdal handled objects

  • Loading branch information
nyalldawson committed Oct 23, 2017
1 parent a89dfde commit 4f4681aeb6ac5a0add56d34168fc83cc2e5b7a18

Large diffs are not rendered by default.

@@ -21,6 +21,7 @@
#include <ogr_api.h>

#include "qgsabstractgeometry.h"
#include "qgsogrutils.h"

class QgsCompoundCurve;
class QgsQgsCoordinateReferenceSystem;
@@ -186,7 +187,7 @@ class QgsDwgImporter : public DRW_Interface

bool createFeature( OGRLayerH layer, OGRFeatureH f, const QgsAbstractGeometry &g ) const;

OGRDataSourceH mDs;
gdal::ogr_datasource_unique_ptr mDs;
QString mDatabase;
bool mInTransaction;
int mSplineSegs;
@@ -21,7 +21,7 @@
#include "qgsmessagelog.h"
#include "qgsnetworkaccessmanager.h"
#include "qgswkbptr.h"

#include "qgsogrutils.h"
#include <QBuffer>
#include <QList>
#include <QNetworkRequest>
@@ -875,20 +875,19 @@ void QgsGmlStreamingParser::endElement( const XML_Char *el )
mParseModeStack.pop();
if ( mFoundUnhandledGeometryElement )
{
OGRGeometryH hGeom = OGR_G_CreateFromGML( mGeometryString.c_str() );
gdal::ogr_geometry_unique_ptr hGeom( OGR_G_CreateFromGML( mGeometryString.c_str() ) );
if ( hGeom )
{
const int wkbSize = OGR_G_WkbSize( hGeom );
const int wkbSize = OGR_G_WkbSize( hGeom.get() );
unsigned char *pabyBuffer = new unsigned char[ wkbSize ];
OGR_G_ExportToIsoWkb( hGeom, wkbNDR, pabyBuffer );
OGR_G_ExportToIsoWkb( hGeom.get(), wkbNDR, pabyBuffer );
QgsGeometry g;
g.fromWkb( pabyBuffer, wkbSize );
if ( mInvertAxisOrientation )
{
g.transform( QTransform( 0, 1, 1, 0, 0, 0 ) );
}
mCurrentFeature->setGeometry( g );
OGR_G_DestroyGeometry( hGeom );
}
}
mGeometryString.clear();
@@ -28,6 +28,29 @@
#define OGR_F_IsFieldSetAndNotNull OGR_F_IsFieldSet
#endif



void gdal::OGRDataSourceDeleter::operator()( void *source )
{
OGR_DS_Destroy( source );
}


void gdal::OGRGeometryDeleter::operator()( void *geometry )
{
OGR_G_DestroyGeometry( geometry );
}

void gdal::OGRFldDeleter::operator()( void *definition )
{
OGR_Fld_Destroy( definition );
}

void gdal::OGRFeatureDeleter::operator()( void *feature )
{
OGR_F_Destroy( feature );
}

QgsFeature QgsOgrUtils::readOgrFeature( OGRFeatureH ogrFet, const QgsFields &fields, QTextCodec *encoding )
{
QgsFeature feature;
@@ -297,32 +320,30 @@ QgsFeatureList QgsOgrUtils::stringToFeatureList( const QString &string, const Qg
VSIFCloseL( VSIFileFromMemBuffer( randomFileName.toUtf8().constData(), reinterpret_cast< GByte * >( ba.data() ),
static_cast< vsi_l_offset >( ba.size() ), FALSE ) );

OGRDataSourceH hDS = OGROpen( randomFileName.toUtf8().constData(), false, nullptr );
gdal::ogr_datasource_unique_ptr hDS( OGROpen( randomFileName.toUtf8().constData(), false, nullptr ) );
if ( !hDS )
{
VSIUnlink( randomFileName.toUtf8().constData() );
return features;
}

OGRLayerH ogrLayer = OGR_DS_GetLayer( hDS, 0 );
OGRLayerH ogrLayer = OGR_DS_GetLayer( hDS.get(), 0 );
if ( !ogrLayer )
{
OGR_DS_Destroy( hDS );
hDS.reset();
VSIUnlink( randomFileName.toUtf8().constData() );
return features;
}

OGRFeatureH oFeat;
while ( ( oFeat = OGR_L_GetNextFeature( ogrLayer ) ) )
gdal::ogr_feature_unique_ptr oFeat;
while ( oFeat.reset( OGR_L_GetNextFeature( ogrLayer ) ), oFeat )
{
QgsFeature feat = readOgrFeature( oFeat, fields, encoding );
QgsFeature feat = readOgrFeature( oFeat.get(), fields, encoding );
if ( feat.isValid() )
features << feat;

OGR_F_Destroy( oFeat );
}

OGR_DS_Destroy( hDS );
hDS.reset();
VSIUnlink( randomFileName.toUtf8().constData() );

return features;
@@ -341,30 +362,30 @@ QgsFields QgsOgrUtils::stringToFields( const QString &string, QTextCodec *encodi
VSIFCloseL( VSIFileFromMemBuffer( randomFileName.toUtf8().constData(), reinterpret_cast< GByte * >( ba.data() ),
static_cast< vsi_l_offset >( ba.size() ), FALSE ) );

OGRDataSourceH hDS = OGROpen( randomFileName.toUtf8().constData(), false, nullptr );
gdal::ogr_datasource_unique_ptr hDS( OGROpen( randomFileName.toUtf8().constData(), false, nullptr ) );
if ( !hDS )
{
VSIUnlink( randomFileName.toUtf8().constData() );
return fields;
}

OGRLayerH ogrLayer = OGR_DS_GetLayer( hDS, 0 );
OGRLayerH ogrLayer = OGR_DS_GetLayer( hDS.get(), 0 );
if ( !ogrLayer )
{
OGR_DS_Destroy( hDS );
hDS.reset();
VSIUnlink( randomFileName.toUtf8().constData() );
return fields;
}

OGRFeatureH oFeat;
gdal::ogr_feature_unique_ptr oFeat;
//read in the first feature only
if ( ( oFeat = OGR_L_GetNextFeature( ogrLayer ) ) )
if ( oFeat.reset( OGR_L_GetNextFeature( ogrLayer ) ), oFeat )
{
fields = readOgrFields( oFeat, encoding );
OGR_F_Destroy( oFeat );
fields = readOgrFields( oFeat.get(), encoding );
}

OGR_DS_Destroy( hDS );
hDS.reset();
VSIUnlink( randomFileName.toUtf8().constData() );
return fields;
}

@@ -25,6 +25,82 @@
#include "cpl_conv.h"
#include "cpl_string.h"

namespace gdal
{

/**
* Destroys OGR data sources.
*/
struct OGRDataSourceDeleter
{

/**
* Destroys an OGR data \a source, using the correct gdal calls.
*/
void CORE_EXPORT operator()( void *source );

};

/**
* Destroys OGR geometries.
*/
struct OGRGeometryDeleter
{

/**
* Destroys an OGR \a geometry, using the correct gdal calls.
*/
void CORE_EXPORT operator()( void *geometry );

};

/**
* Destroys OGR field definition.
*/
struct OGRFldDeleter
{

/**
* Destroys an OGR field \a definition, using the correct gdal calls.
*/
void CORE_EXPORT operator()( void *definition );

};

/**
* Destroys OGR feature.
*/
struct OGRFeatureDeleter
{

/**
* Destroys an OGR \a feature, using the correct gdal calls.
*/
void CORE_EXPORT operator()( void *feature );

};

/**
* Scoped OGR data source.
*/
using ogr_datasource_unique_ptr = std::unique_ptr< void, OGRDataSourceDeleter>;

/**
* Scoped OGR geometry.
*/
using ogr_geometry_unique_ptr = std::unique_ptr< void, OGRGeometryDeleter>;

/**
* Scoped OGR field definition.
*/
using ogr_field_def_unique_ptr = std::unique_ptr< void, OGRFldDeleter >;

/**
* Scoped OGR feature.
*/
using ogr_feature_unique_ptr = std::unique_ptr< void, OGRFeatureDeleter >;
}

/**
* \ingroup core
* \class QgsOgrUtils

0 comments on commit 4f4681a

Please sign in to comment.
You can’t perform that action at this time.