Skip to content
Permalink
Browse files

QgsOgrLayerItem(): avoid useless dataset opening

  • Loading branch information
rouault committed Nov 12, 2017
1 parent 75ea372 commit 8cefb45d6d8a9cfa8b880c9c59d4bd36651f6c2d
Showing with 13 additions and 9 deletions.
  1. +13 −9 src/providers/ogr/qgsogrdataitems.cpp
@@ -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
@@ -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();

0 comments on commit 8cefb45

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