Skip to content

Commit

Permalink
QgsOgrLayerItem(): avoid useless dataset opening
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Nov 12, 2017
1 parent 75ea372 commit 8cefb45
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/providers/ogr/qgsogrdataitems.cpp
Expand Up @@ -53,16 +53,17 @@ QgsOgrLayerItem::QgsOgrLayerItem( QgsDataItem *parent,
mToolTip = uri;
setState( Populated ); // children are not expected

OGRRegisterAll();
gdal::dataset_unique_ptr hDataSource( GDALOpenEx( mPath.toUtf8().constData(), GDAL_OF_VECTOR, nullptr, nullptr, nullptr ) );

if ( hDataSource )
if ( mPath.toLower().endsWith( QLatin1String( ".shp" ) ) )
{
GDALDriverH hDriver = GDALGetDatasetDriver( hDataSource.get() );
QString driverName = GDALGetDriverShortName( hDriver );

if ( driverName == QLatin1String( "ESRI Shapefile" ) )
if ( OGRGetDriverCount() == 0 )
{
OGRRegisterAll();
}
gdal::dataset_unique_ptr hDataSource( GDALOpenEx( mPath.toUtf8().constData(), GDAL_OF_VECTOR | GDAL_OF_UPDATE, nullptr, nullptr, nullptr ) );
if ( hDataSource )
{
mCapabilities |= SetCrs;
}

// It it is impossible to assign a crs to an existing layer
// No OGR_L_SetSpatialRef : http://trac.osgeo.org/gdal/ticket/4032
Expand Down Expand Up @@ -640,7 +641,10 @@ QGISEXTERN QgsDataItem *dataItem( QString path, QgsDataItem *parentItem )
QgsDataItem *item = nullptr;

// test that file is valid with OGR
OGRRegisterAll();
if ( OGRGetDriverCount() == 0 )
{
OGRRegisterAll();
}
// do not print errors, but write to debug
CPLPushErrorHandler( CPLQuietErrorHandler );
CPLErrorReset();
Expand Down

0 comments on commit 8cefb45

Please sign in to comment.