Skip to content

Commit 8cefb45

Browse files
committedNov 12, 2017
QgsOgrLayerItem(): avoid useless dataset opening
1 parent 75ea372 commit 8cefb45

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed
 

‎src/providers/ogr/qgsogrdataitems.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,17 @@ QgsOgrLayerItem::QgsOgrLayerItem( QgsDataItem *parent,
5353
mToolTip = uri;
5454
setState( Populated ); // children are not expected
5555

56-
OGRRegisterAll();
57-
gdal::dataset_unique_ptr hDataSource( GDALOpenEx( mPath.toUtf8().constData(), GDAL_OF_VECTOR, nullptr, nullptr, nullptr ) );
58-
59-
if ( hDataSource )
56+
if ( mPath.toLower().endsWith( QLatin1String( ".shp" ) ) )
6057
{
61-
GDALDriverH hDriver = GDALGetDatasetDriver( hDataSource.get() );
62-
QString driverName = GDALGetDriverShortName( hDriver );
63-
64-
if ( driverName == QLatin1String( "ESRI Shapefile" ) )
58+
if ( OGRGetDriverCount() == 0 )
59+
{
60+
OGRRegisterAll();
61+
}
62+
gdal::dataset_unique_ptr hDataSource( GDALOpenEx( mPath.toUtf8().constData(), GDAL_OF_VECTOR | GDAL_OF_UPDATE, nullptr, nullptr, nullptr ) );
63+
if ( hDataSource )
64+
{
6565
mCapabilities |= SetCrs;
66+
}
6667

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

642643
// test that file is valid with OGR
643-
OGRRegisterAll();
644+
if ( OGRGetDriverCount() == 0 )
645+
{
646+
OGRRegisterAll();
647+
}
644648
// do not print errors, but write to debug
645649
CPLPushErrorHandler( CPLQuietErrorHandler );
646650
CPLErrorReset();

0 commit comments

Comments
 (0)
Please sign in to comment.