[ogr] Only retrieve layer crs once

Instead of retrieving and converting the layer crs with every
call to ::crs(), just read it once when determining the layer
properties and store for later retrieval.

Speeds up construction of OGR feature sources, which occurs
on the main thread when starting a new map render and adds
significant cost to the map render preparation.
This commit is contained in:
Nyall Dawson 2025-06-24 12:38:14 +10:00
parent cd3a1d0710
commit 5142e4995a
2 changed files with 17 additions and 11 deletions

View File

@ -830,6 +830,20 @@ void QgsOgrProvider::loadFields()
QMutexLocker locker( mutex );
mOGRGeomType = getOgrGeomType( mGDALDriverName, ogrLayer );
}
mCrs = QgsCoordinateReferenceSystem();
if ( mOGRGeomType != wkbNone )
{
if ( OGRSpatialReferenceH spatialRefSys = mOgrLayer->GetSpatialRef() )
{
mCrs = QgsOgrUtils::OGRSpatialReferenceToCrs( spatialRefSys );
}
else
{
QgsDebugMsgLevel( QStringLiteral( "no spatial reference found" ), 2 );
}
}
QgsOgrFeatureDefn &fdef = mOgrLayer->GetLayerDefn();
// Expose the OGR FID if it comes from a "real" column (typically GPKG)
@ -3866,18 +3880,9 @@ QgsCoordinateReferenceSystem QgsOgrProvider::crs() const
{
QgsCoordinateReferenceSystem srs;
if ( !mValid || ( mOGRGeomType == wkbNone ) )
return srs;
return QgsCoordinateReferenceSystem();
if ( OGRSpatialReferenceH spatialRefSys = mOgrLayer->GetSpatialRef() )
{
srs = QgsOgrUtils::OGRSpatialReferenceToCrs( spatialRefSys );
}
else
{
QgsDebugMsgLevel( QStringLiteral( "no spatial reference found" ), 2 );
}
return srs;
return mCrs;
}
QString QgsOgrProvider::dataComment() const

View File

@ -304,6 +304,7 @@ class QgsOgrProvider final: public QgsVectorDataProvider
bool mValid = false;
OGRwkbGeometryType mOGRGeomType = wkbUnknown;
QgsCoordinateReferenceSystem mCrs;
//! Whether the next call to featureCount() should refresh the feature count
mutable bool mRefreshFeatureCount = true;