Skip to content
Permalink
Browse files

Add a bit of typesafety to ogr extent handling

  • Loading branch information
m-kuhn committed Feb 2, 2016
1 parent 29a72b5 commit 9fb78193d65cd5d24e5308db409117cee1b6a34e
Showing with 19 additions and 37 deletions.
  1. +18 −36 src/providers/ogr/qgsogrprovider.cpp
  2. +1 −1 src/providers/ogr/qgsogrprovider.h
@@ -272,7 +272,7 @@ QgsVectorLayerImport::ImportError QgsOgrProvider::createEmptyLayer(
QgsOgrProvider::QgsOgrProvider( QString const & uri )
: QgsVectorDataProvider( uri )
, ogrDataSource( nullptr )
, extent_( nullptr )
, mExtent( nullptr )
, ogrLayer( nullptr )
, ogrOrigLayer( nullptr )
, mLayerIndex( 0 )
@@ -456,11 +456,7 @@ bool QgsOgrProvider::setSubsetString( const QString& theSQL, bool updateFeatureC
loadFields();
QgsDebugMsg( "Done checking validity" );

if ( extent_ )
{
free( extent_ );
extent_ = nullptr;
}
updateExtents();

emit dataChanged();

@@ -846,26 +842,24 @@ unsigned char * QgsOgrProvider::getGeometryPointer( OGRFeatureH fet )

QgsRectangle QgsOgrProvider::extent()
{
if ( !extent_ )
if ( !mExtent )
{
extent_ = calloc( sizeof( OGREnvelope ), 1 );
mExtent = new OGREnvelope();

// get the extent_ (envelope) of the layer
QgsDebugMsg( "Starting get extent" );

// TODO: This can be expensive, do we really need it!
if ( ogrLayer == ogrOrigLayer )
{
OGR_L_GetExtent( ogrLayer, ( OGREnvelope * ) extent_, true );
OGR_L_GetExtent( ogrLayer, mExtent, true );
}
else
{
OGREnvelope *bb = static_cast<OGREnvelope*>( extent_ );

bb->MinX = std::numeric_limits<double>::max();
bb->MinY = std::numeric_limits<double>::max();
bb->MaxX = -std::numeric_limits<double>::max();
bb->MaxY = -std::numeric_limits<double>::max();
mExtent->MinX = std::numeric_limits<double>::max();
mExtent->MinY = std::numeric_limits<double>::max();
mExtent->MaxX = -std::numeric_limits<double>::max();
mExtent->MaxY = -std::numeric_limits<double>::max();

OGRFeatureH f;

@@ -878,10 +872,10 @@ QgsRectangle QgsOgrProvider::extent()
OGREnvelope env;
OGR_G_GetEnvelope( g, &env );

if ( env.MinX < bb->MinX ) bb->MinX = env.MinX;
if ( env.MinY < bb->MinY ) bb->MinY = env.MinY;
if ( env.MaxX > bb->MaxX ) bb->MaxX = env.MaxX;
if ( env.MaxY > bb->MaxY ) bb->MaxY = env.MaxY;
mExtent->MinX = qMin( mExtent->MinX, env.MinX );
mExtent->MinY = qMin( mExtent->MinY, env.MinY );
mExtent->MaxX = qMin( mExtent->MaxX, env.MaxX );
mExtent->MaxY = qMin( mExtent->MaxY, env.MaxY );
}

OGR_F_Destroy( f );
@@ -892,18 +886,14 @@ QgsRectangle QgsOgrProvider::extent()
QgsDebugMsg( "Finished get extent" );
}

OGREnvelope *ext = static_cast<OGREnvelope *>( extent_ );
mExtentRect.set( ext->MinX, ext->MinY, ext->MaxX, ext->MaxY );
mExtentRect.set( mExtent->MinX, mExtent->MinY, mExtent->MaxX, mExtent->MaxY );
return mExtentRect;
}

void QgsOgrProvider::updateExtents()
{
if ( extent_ )
{
free( extent_ );
extent_ = nullptr;
}
delete mExtent;
mExtent = nullptr;
}

size_t QgsOgrProvider::layerCount() const
@@ -1409,11 +1399,7 @@ bool QgsOgrProvider::deleteFeatures( const QgsFeatureIds & id )

clearMinMaxCache();

if ( extent_ )
{
free( extent_ );
extent_ = nullptr;
}
updateExtents();

return returnvalue;
}
@@ -2846,11 +2832,7 @@ void QgsOgrProvider::close()
}
ogrDataSource = nullptr;

if ( extent_ )
{
free( extent_ );
extent_ = nullptr;
}
updateExtents();

QgsOgrConnPool::unrefS( mFilePath );
}
@@ -295,7 +295,7 @@ class QgsOgrProvider : public QgsVectorDataProvider
OGRwkbGeometryType ogrWkbGeometryTypeFromName( const QString& typeName ) const;
QgsFields mAttributeFields;
OGRDataSourceH ogrDataSource;
void *extent_;
OGREnvelope* mExtent;

/** This member variable receives the same value as extent_
in the method QgsOgrProvider::extent(). The purpose is to prevent a memory leak*/

0 comments on commit 9fb7819

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