Skip to content

Commit 47cb75d

Browse files
committed
fix relative path support for gdal layers (fixes #12823)
1 parent 205daae commit 47cb75d

File tree

1 file changed

+46
-25
lines changed

1 file changed

+46
-25
lines changed

src/core/qgsmaplayer.cpp

+46-25
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@
4949

5050
QgsMapLayer::QgsMapLayer( QgsMapLayer::LayerType type,
5151
QString lyrname,
52-
QString source ) :
53-
mValid( false ), // assume the layer is invalid
54-
mDataSource( source ),
55-
mLayerOrigName( lyrname ), // store the original name
56-
mID( "" ),
57-
mLayerType( type ),
58-
mBlendMode( QPainter::CompositionMode_SourceOver ) // Default to normal blending
52+
QString source )
53+
: mValid( false ) // assume the layer is invalid
54+
, mDataSource( source )
55+
, mLayerOrigName( lyrname ) // store the original name
56+
, mID( "" )
57+
, mLayerType( type )
58+
, mBlendMode( QPainter::CompositionMode_SourceOver ) // Default to normal blending
5959
, mLegend( 0 )
6060
, mStyleManager( new QgsMapLayerStyleManager( this ) )
6161
{
@@ -292,33 +292,54 @@ bool QgsMapLayer::readLayerXML( const QDomElement& layerElement )
292292
}
293293
// <<< BACKWARD COMPATIBILITY < 1.9
294294
}
295-
else if ( provider == "gdal" )
295+
else if ( provider == "gdal" && mDataSource.startsWith( "NETCDF:" ) )
296296
{
297-
QStringList theURIParts = mDataSource.split( ":" );
298-
if ( theURIParts[0] == "NETCDF" )
297+
// NETCDF:filename:variable
298+
// filename can be quoted with " as it can contain colons
299+
QRegExp r( "NETCDF:(.+):([^:]+)" );
300+
if ( r.exactMatch( mDataSource ) )
299301
{
300-
QString src = theURIParts[1];
301-
src.replace( "\"", "" );
302-
src = QgsProject::instance()->readPath( src );
303-
theURIParts[1] = "\"" + src + "\"";
302+
QString filename = r.cap( 1 );
303+
if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
304+
filename = filename.mid( 1, filename.length() - 2 );
305+
mDataSource = "NETCDF:\"" + QgsProject::instance()->readPath( filename ) + "\":" + r.cap( 2 );
304306
}
305-
else if ( theURIParts[0] == "HDF4_SDS" )
306-
{
307-
theURIParts[2] = QgsProject::instance()->readPath( theURIParts[2] );
308-
}
309-
else if ( theURIParts[0] == "HDF5" )
307+
}
308+
else if ( provider == "gdal" && mDataSource.startsWith( "HDF4_SDS:" ) )
309+
{
310+
// HDF4_SDS:subdataset_type:file_name:subdataset_index
311+
// filename can be quoted with " as it can contain colons
312+
QRegExp r( "HDF4_SDS:([^:]+):(.+):([^:]+)" );
313+
if ( r.exactMatch( mDataSource ) )
310314
{
311-
theURIParts[1] = QgsProject::instance()->readPath( theURIParts[1] );
315+
QString filename = r.cap( 2 );
316+
if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
317+
filename = filename.mid( 1, filename.length() - 2 );
318+
mDataSource = "HDF4_SDS:" + r.cap( 1 ) + ":\"" + QgsProject::instance()->readPath( filename ) + "\":" + r.cap( 3 );
312319
}
313-
else if ( theURIParts[0] == "NITF_IM" )
320+
}
321+
else if ( provider == "gdal" && mDataSource.startsWith( "HDF5:" ) )
322+
{
323+
// HDF5:file_name:subdataset
324+
// filename can be quoted with " as it can contain colons
325+
QRegExp r( "HDF5:(.+):([^:]+)" );
326+
if ( r.exactMatch( mDataSource ) )
314327
{
315-
theURIParts[2] = QgsProject::instance()->readPath( theURIParts[2] );
328+
QString filename = r.cap( 1 );
329+
if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
330+
filename = filename.mid( 1, filename.length() - 2 );
331+
mDataSource = "HDF5:\"" + QgsProject::instance()->readPath( filename ) + "\":" + r.cap( 2 );
316332
}
317-
else if ( theURIParts[0] == "RADARSAT_2_CALIB" )
333+
}
334+
else if ( provider == "gdal" && mDataSource.contains( QRegExp( "^(NITF_IM|RADARSAT_2_CALIB):" ) ) )
335+
{
336+
// NITF_IM:0:filename
337+
// RADARSAT_2_CALIB:?:filename
338+
QRegExp r( "([^:]+):([^:]+):(.+)" );
339+
if ( r.exactMatch( mDataSource ) )
318340
{
319-
theURIParts[2] = QgsProject::instance()->readPath( theURIParts[2] );
341+
mDataSource = r.cap( 1 ) + ":" + r.cap( 2 ) + ":" + QgsProject::instance()->readPath( r.cap( 3 ) );
320342
}
321-
mDataSource = theURIParts.join( ":" );
322343
}
323344
else
324345
{

0 commit comments

Comments
 (0)