Skip to content

Commit 11ebf21

Browse files
committed
fix relative path support for gdal layers (fixes #12823)
(merged 47cb75d and e2e8c87)
1 parent 4d0413a commit 11ebf21

File tree

1 file changed

+130
-66
lines changed

1 file changed

+130
-66
lines changed

src/core/qgsmaplayer.cpp

+130-66
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,37 +292,69 @@ bool QgsMapLayer::readLayerXML( const QDomElement& layerElement )
292292
}
293293
// <<< BACKWARD COMPATIBILITY < 1.9
294294
}
295-
else if ( provider == "gdal" )
295+
else
296296
{
297-
QStringList theURIParts = mDataSource.split( ":" );
298-
if ( theURIParts[0] == "NETCDF" )
299-
{
300-
QString src = theURIParts[1];
301-
src.replace( "\"", "" );
302-
src = QgsProject::instance()->readPath( src );
303-
theURIParts[1] = "\"" + src + "\"";
304-
}
305-
else if ( theURIParts[0] == "HDF4_SDS" )
306-
{
307-
theURIParts[2] = QgsProject::instance()->readPath( theURIParts[2] );
308-
}
309-
else if ( theURIParts[0] == "HDF5" )
310-
{
311-
theURIParts[1] = QgsProject::instance()->readPath( theURIParts[1] );
312-
}
313-
else if ( theURIParts[0] == "NITF_IM" )
314-
{
315-
theURIParts[2] = QgsProject::instance()->readPath( theURIParts[2] );
316-
}
317-
else if ( theURIParts[0] == "RADARSAT_2_CALIB" )
297+
bool handled = false;
298+
299+
if ( provider == "gdal" )
318300
{
319-
theURIParts[2] = QgsProject::instance()->readPath( theURIParts[2] );
301+
if ( mDataSource.startsWith( "NETCDF:" ) )
302+
{
303+
// NETCDF:filename:variable
304+
// filename can be quoted with " as it can contain colons
305+
QRegExp r( "NETCDF:(.+):([^:]+)" );
306+
if ( r.exactMatch( mDataSource ) )
307+
{
308+
QString filename = r.cap( 1 );
309+
if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
310+
filename = filename.mid( 1, filename.length() - 2 );
311+
mDataSource = "NETCDF:\"" + QgsProject::instance()->readPath( filename ) + "\":" + r.cap( 2 );
312+
handled = true;
313+
}
314+
}
315+
else if ( mDataSource.startsWith( "HDF4_SDS:" ) )
316+
{
317+
// HDF4_SDS:subdataset_type:file_name:subdataset_index
318+
// filename can be quoted with " as it can contain colons
319+
QRegExp r( "HDF4_SDS:([^:]+):(.+):([^:]+)" );
320+
if ( r.exactMatch( mDataSource ) )
321+
{
322+
QString filename = r.cap( 2 );
323+
if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
324+
filename = filename.mid( 1, filename.length() - 2 );
325+
mDataSource = "HDF4_SDS:" + r.cap( 1 ) + ":\"" + QgsProject::instance()->readPath( filename ) + "\":" + r.cap( 3 );
326+
handled = true;
327+
}
328+
}
329+
else if ( mDataSource.startsWith( "HDF5:" ) )
330+
{
331+
// HDF5:file_name:subdataset
332+
// filename can be quoted with " as it can contain colons
333+
QRegExp r( "HDF5:(.+):([^:]+)" );
334+
if ( r.exactMatch( mDataSource ) )
335+
{
336+
QString filename = r.cap( 1 );
337+
if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
338+
filename = filename.mid( 1, filename.length() - 2 );
339+
mDataSource = "HDF5:\"" + QgsProject::instance()->readPath( filename ) + "\":" + r.cap( 2 );
340+
handled = true;
341+
}
342+
}
343+
else if ( mDataSource.contains( QRegExp( "^(NITF_IM|RADARSAT_2_CALIB):" ) ) )
344+
{
345+
// NITF_IM:0:filename
346+
// RADARSAT_2_CALIB:?:filename
347+
QRegExp r( "([^:]+):([^:]+):(.+)" );
348+
if ( r.exactMatch( mDataSource ) )
349+
{
350+
mDataSource = r.cap( 1 ) + ":" + r.cap( 2 ) + ":" + QgsProject::instance()->readPath( r.cap( 3 ) );
351+
handled = true;
352+
}
353+
}
320354
}
321-
mDataSource = theURIParts.join( ":" );
322-
}
323-
else
324-
{
325-
mDataSource = QgsProject::instance()->readPath( mDataSource );
355+
356+
if ( !handled )
357+
mDataSource = QgsProject::instance()->readPath( mDataSource );
326358
}
327359

328360
// Set the CRS from project file, asking the user if necessary.
@@ -517,42 +549,74 @@ bool QgsMapLayer::writeLayerXML( QDomElement& layerElement, QDomDocument& docume
517549
urlDest.setQueryItems( urlSource.queryItems() );
518550
src = QString::fromAscii( urlDest.toEncoded() );
519551
}
520-
else if ( !vlayer )
552+
else
521553
{
522-
QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( this );
523-
// Update path for subdataset
524-
if ( rlayer && rlayer->providerType() == "gdal" )
554+
bool handled = false;
555+
556+
if ( !vlayer )
525557
{
526-
QStringList theURIParts = src.split( ":" );
527-
if ( theURIParts[0] == "NETCDF" )
528-
{
529-
src = theURIParts[1];
530-
src.replace( "\"", "" );
531-
src = QgsProject::instance()->writePath( src, relativeBasePath );
532-
theURIParts[1] = "\"" + src + "\"";
533-
}
534-
else if ( theURIParts[0] == "HDF4_SDS" )
535-
{
536-
theURIParts[2] = QgsProject::instance()->writePath( theURIParts[2], relativeBasePath );
537-
}
538-
else if ( theURIParts[0] == "HDF5" )
539-
{
540-
theURIParts[1] = QgsProject::instance()->writePath( theURIParts[1], relativeBasePath );
541-
}
542-
else if ( theURIParts[0] == "NITF_IM" )
558+
QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( this );
559+
// Update path for subdataset
560+
if ( rlayer && rlayer->providerType() == "gdal" )
543561
{
544-
theURIParts[2] = QgsProject::instance()->writePath( theURIParts[2], relativeBasePath );
545-
}
546-
else if ( theURIParts[0] == "RADARSAT_2_CALIB" )
547-
{
548-
theURIParts[2] = QgsProject::instance()->writePath( theURIParts[2], relativeBasePath );
562+
if ( src.startsWith( "NETCDF:" ) )
563+
{
564+
// NETCDF:filename:variable
565+
// filename can be quoted with " as it can contain colons
566+
QRegExp r( "NETCDF:(.+):([^:]+)" );
567+
if ( r.exactMatch( src ) )
568+
{
569+
QString filename = r.cap( 1 );
570+
if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
571+
filename = filename.mid( 1, filename.length() - 2 );
572+
src = "NETCDF:\"" + QgsProject::instance()->writePath( filename, relativeBasePath ) + "\":" + r.cap( 2 );
573+
handled = true;
574+
}
575+
}
576+
else if ( src.startsWith( "HDF4_SDS:" ) )
577+
{
578+
// HDF4_SDS:subdataset_type:file_name:subdataset_index
579+
// filename can be quoted with " as it can contain colons
580+
QRegExp r( "HDF4_SDS:([^:]+):(.+):([^:]+)" );
581+
if ( r.exactMatch( src ) )
582+
{
583+
QString filename = r.cap( 2 );
584+
if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
585+
filename = filename.mid( 1, filename.length() - 2 );
586+
src = "HDF4_SDS:" + r.cap( 1 ) + ":\"" + QgsProject::instance()->writePath( filename, relativeBasePath ) + "\":" + r.cap( 3 );
587+
handled = true;
588+
}
589+
}
590+
else if ( src.startsWith( "HDF5:" ) )
591+
{
592+
// HDF5:file_name:subdataset
593+
// filename can be quoted with " as it can contain colons
594+
QRegExp r( "HDF5:(.+):([^:]+)" );
595+
if ( r.exactMatch( src ) )
596+
{
597+
QString filename = r.cap( 1 );
598+
if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
599+
filename = filename.mid( 1, filename.length() - 2 );
600+
src = "HDF5:\"" + QgsProject::instance()->writePath( filename, relativeBasePath ) + "\":" + r.cap( 2 );
601+
handled = true;
602+
}
603+
}
604+
else if ( src.contains( QRegExp( "^(NITF_IM|RADARSAT_2_CALIB):" ) ) )
605+
{
606+
// NITF_IM:0:filename
607+
// RADARSAT_2_CALIB:?:filename
608+
QRegExp r( "([^:]+):([^:]+):(.+)" );
609+
if ( r.exactMatch( src ) )
610+
{
611+
src = r.cap( 1 ) + ":" + r.cap( 2 ) + ":" + QgsProject::instance()->writePath( r.cap( 3 ), relativeBasePath );
612+
handled = true;
613+
}
614+
}
549615
}
550-
src = theURIParts.join( ":" );
551616
}
552-
}
553-
else
554-
{
555-
src = QgsProject::instance()->writePath( src, relativeBasePath );
617+
618+
if ( !handled )
619+
src = QgsProject::instance()->writePath( src, relativeBasePath );
556620
}
557621

558622
QDomText dataSourceText = document.createTextNode( src );

0 commit comments

Comments
 (0)