Skip to content

Commit e2e8c87

Browse files
committed
also handle writing of relative paths (followup 47cb75d; fixes #12823)
1 parent 598563c commit e2e8c87

File tree

1 file changed

+123
-80
lines changed

1 file changed

+123
-80
lines changed

src/core/qgsmaplayer.cpp

+123-80
Original file line numberDiff line numberDiff line change
@@ -292,58 +292,69 @@ bool QgsMapLayer::readLayerXML( const QDomElement& layerElement )
292292
}
293293
// <<< BACKWARD COMPATIBILITY < 1.9
294294
}
295-
else if ( provider == "gdal" && mDataSource.startsWith( "NETCDF:" ) )
296-
{
297-
// NETCDF:filename:variable
298-
// filename can be quoted with " as it can contain colons
299-
QRegExp r( "NETCDF:(.+):([^:]+)" );
300-
if ( r.exactMatch( mDataSource ) )
301-
{
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 );
306-
}
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 ) )
314-
{
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 );
319-
}
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 ) )
327-
{
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 );
332-
}
333-
}
334-
else if ( provider == "gdal" && mDataSource.contains( QRegExp( "^(NITF_IM|RADARSAT_2_CALIB):" ) ) )
295+
else
335296
{
336-
// NITF_IM:0:filename
337-
// RADARSAT_2_CALIB:?:filename
338-
QRegExp r( "([^:]+):([^:]+):(.+)" );
339-
if ( r.exactMatch( mDataSource ) )
297+
bool handled = false;
298+
299+
if ( provider == "gdal" )
340300
{
341-
mDataSource = r.cap( 1 ) + ":" + r.cap( 2 ) + ":" + QgsProject::instance()->readPath( r.cap( 3 ) );
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+
}
342354
}
343-
}
344-
else
345-
{
346-
mDataSource = QgsProject::instance()->readPath( mDataSource );
355+
356+
if ( !handled )
357+
mDataSource = QgsProject::instance()->readPath( mDataSource );
347358
}
348359

349360
// Set the CRS from project file, asking the user if necessary.
@@ -538,42 +549,74 @@ bool QgsMapLayer::writeLayerXML( QDomElement& layerElement, QDomDocument& docume
538549
urlDest.setQueryItems( urlSource.queryItems() );
539550
src = QString::fromAscii( urlDest.toEncoded() );
540551
}
541-
else if ( !vlayer )
552+
else
542553
{
543-
QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( this );
544-
// Update path for subdataset
545-
if ( rlayer && rlayer->providerType() == "gdal" )
554+
bool handled = false;
555+
556+
if ( !vlayer )
546557
{
547-
QStringList theURIParts = src.split( ":" );
548-
if ( theURIParts[0] == "NETCDF" )
549-
{
550-
src = theURIParts[1];
551-
src.replace( "\"", "" );
552-
src = QgsProject::instance()->writePath( src, relativeBasePath );
553-
theURIParts[1] = "\"" + src + "\"";
554-
}
555-
else if ( theURIParts[0] == "HDF4_SDS" )
556-
{
557-
theURIParts[2] = QgsProject::instance()->writePath( theURIParts[2], relativeBasePath );
558-
}
559-
else if ( theURIParts[0] == "HDF5" )
558+
QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( this );
559+
// Update path for subdataset
560+
if ( rlayer && rlayer->providerType() == "gdal" )
560561
{
561-
theURIParts[1] = QgsProject::instance()->writePath( theURIParts[1], relativeBasePath );
562-
}
563-
else if ( theURIParts[0] == "NITF_IM" )
564-
{
565-
theURIParts[2] = QgsProject::instance()->writePath( theURIParts[2], relativeBasePath );
566-
}
567-
else if ( theURIParts[0] == "RADARSAT_2_CALIB" )
568-
{
569-
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+
}
570615
}
571-
src = theURIParts.join( ":" );
572616
}
573-
}
574-
else
575-
{
576-
src = QgsProject::instance()->writePath( src, relativeBasePath );
617+
618+
if ( !handled )
619+
src = QgsProject::instance()->writePath( src, relativeBasePath );
577620
}
578621

579622
QDomText dataSourceText = document.createTextNode( src );

0 commit comments

Comments
 (0)