Skip to content
Permalink
Browse files
also handle writing of relative paths (followup 47cb75d; fixes #12823)
  • Loading branch information
jef-n committed May 26, 2015
1 parent 598563c commit e2e8c8708d7dd55cea7963588c2029f4d9f3c039
Showing with 123 additions and 80 deletions.
  1. +123 −80 src/core/qgsmaplayer.cpp
@@ -292,58 +292,69 @@ bool QgsMapLayer::readLayerXML( const QDomElement& layerElement )
}
// <<< BACKWARD COMPATIBILITY < 1.9
}
else if ( provider == "gdal" && mDataSource.startsWith( "NETCDF:" ) )
{
// NETCDF:filename:variable
// filename can be quoted with " as it can contain colons
QRegExp r( "NETCDF:(.+):([^:]+)" );
if ( r.exactMatch( mDataSource ) )
{
QString filename = r.cap( 1 );
if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
filename = filename.mid( 1, filename.length() - 2 );
mDataSource = "NETCDF:\"" + QgsProject::instance()->readPath( filename ) + "\":" + r.cap( 2 );
}
}
else if ( provider == "gdal" && mDataSource.startsWith( "HDF4_SDS:" ) )
{
// HDF4_SDS:subdataset_type:file_name:subdataset_index
// filename can be quoted with " as it can contain colons
QRegExp r( "HDF4_SDS:([^:]+):(.+):([^:]+)" );
if ( r.exactMatch( mDataSource ) )
{
QString filename = r.cap( 2 );
if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
filename = filename.mid( 1, filename.length() - 2 );
mDataSource = "HDF4_SDS:" + r.cap( 1 ) + ":\"" + QgsProject::instance()->readPath( filename ) + "\":" + r.cap( 3 );
}
}
else if ( provider == "gdal" && mDataSource.startsWith( "HDF5:" ) )
{
// HDF5:file_name:subdataset
// filename can be quoted with " as it can contain colons
QRegExp r( "HDF5:(.+):([^:]+)" );
if ( r.exactMatch( mDataSource ) )
{
QString filename = r.cap( 1 );
if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
filename = filename.mid( 1, filename.length() - 2 );
mDataSource = "HDF5:\"" + QgsProject::instance()->readPath( filename ) + "\":" + r.cap( 2 );
}
}
else if ( provider == "gdal" && mDataSource.contains( QRegExp( "^(NITF_IM|RADARSAT_2_CALIB):" ) ) )
else
{
// NITF_IM:0:filename
// RADARSAT_2_CALIB:?:filename
QRegExp r( "([^:]+):([^:]+):(.+)" );
if ( r.exactMatch( mDataSource ) )
bool handled = false;

if ( provider == "gdal" )
{
mDataSource = r.cap( 1 ) + ":" + r.cap( 2 ) + ":" + QgsProject::instance()->readPath( r.cap( 3 ) );
if ( mDataSource.startsWith( "NETCDF:" ) )
{
// NETCDF:filename:variable
// filename can be quoted with " as it can contain colons
QRegExp r( "NETCDF:(.+):([^:]+)" );
if ( r.exactMatch( mDataSource ) )
{
QString filename = r.cap( 1 );
if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
filename = filename.mid( 1, filename.length() - 2 );
mDataSource = "NETCDF:\"" + QgsProject::instance()->readPath( filename ) + "\":" + r.cap( 2 );
handled = true;
}
}
else if ( mDataSource.startsWith( "HDF4_SDS:" ) )
{
// HDF4_SDS:subdataset_type:file_name:subdataset_index
// filename can be quoted with " as it can contain colons
QRegExp r( "HDF4_SDS:([^:]+):(.+):([^:]+)" );
if ( r.exactMatch( mDataSource ) )
{
QString filename = r.cap( 2 );
if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
filename = filename.mid( 1, filename.length() - 2 );
mDataSource = "HDF4_SDS:" + r.cap( 1 ) + ":\"" + QgsProject::instance()->readPath( filename ) + "\":" + r.cap( 3 );
handled = true;
}
}
else if ( mDataSource.startsWith( "HDF5:" ) )
{
// HDF5:file_name:subdataset
// filename can be quoted with " as it can contain colons
QRegExp r( "HDF5:(.+):([^:]+)" );
if ( r.exactMatch( mDataSource ) )
{
QString filename = r.cap( 1 );
if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
filename = filename.mid( 1, filename.length() - 2 );
mDataSource = "HDF5:\"" + QgsProject::instance()->readPath( filename ) + "\":" + r.cap( 2 );
handled = true;
}
}
else if ( mDataSource.contains( QRegExp( "^(NITF_IM|RADARSAT_2_CALIB):" ) ) )
{
// NITF_IM:0:filename
// RADARSAT_2_CALIB:?:filename
QRegExp r( "([^:]+):([^:]+):(.+)" );
if ( r.exactMatch( mDataSource ) )
{
mDataSource = r.cap( 1 ) + ":" + r.cap( 2 ) + ":" + QgsProject::instance()->readPath( r.cap( 3 ) );
handled = true;
}
}
}
}
else
{
mDataSource = QgsProject::instance()->readPath( mDataSource );

if ( !handled )
mDataSource = QgsProject::instance()->readPath( mDataSource );
}

// Set the CRS from project file, asking the user if necessary.
@@ -538,42 +549,74 @@ bool QgsMapLayer::writeLayerXML( QDomElement& layerElement, QDomDocument& docume
urlDest.setQueryItems( urlSource.queryItems() );
src = QString::fromAscii( urlDest.toEncoded() );
}
else if ( !vlayer )
else
{
QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( this );
// Update path for subdataset
if ( rlayer && rlayer->providerType() == "gdal" )
bool handled = false;

if ( !vlayer )
{
QStringList theURIParts = src.split( ":" );
if ( theURIParts[0] == "NETCDF" )
{
src = theURIParts[1];
src.replace( "\"", "" );
src = QgsProject::instance()->writePath( src, relativeBasePath );
theURIParts[1] = "\"" + src + "\"";
}
else if ( theURIParts[0] == "HDF4_SDS" )
{
theURIParts[2] = QgsProject::instance()->writePath( theURIParts[2], relativeBasePath );
}
else if ( theURIParts[0] == "HDF5" )
QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( this );
// Update path for subdataset
if ( rlayer && rlayer->providerType() == "gdal" )
{
theURIParts[1] = QgsProject::instance()->writePath( theURIParts[1], relativeBasePath );
}
else if ( theURIParts[0] == "NITF_IM" )
{
theURIParts[2] = QgsProject::instance()->writePath( theURIParts[2], relativeBasePath );
}
else if ( theURIParts[0] == "RADARSAT_2_CALIB" )
{
theURIParts[2] = QgsProject::instance()->writePath( theURIParts[2], relativeBasePath );
if ( src.startsWith( "NETCDF:" ) )
{
// NETCDF:filename:variable
// filename can be quoted with " as it can contain colons
QRegExp r( "NETCDF:(.+):([^:]+)" );
if ( r.exactMatch( src ) )
{
QString filename = r.cap( 1 );
if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
filename = filename.mid( 1, filename.length() - 2 );
src = "NETCDF:\"" + QgsProject::instance()->writePath( filename, relativeBasePath ) + "\":" + r.cap( 2 );
handled = true;
}
}
else if ( src.startsWith( "HDF4_SDS:" ) )
{
// HDF4_SDS:subdataset_type:file_name:subdataset_index
// filename can be quoted with " as it can contain colons
QRegExp r( "HDF4_SDS:([^:]+):(.+):([^:]+)" );
if ( r.exactMatch( src ) )
{
QString filename = r.cap( 2 );
if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
filename = filename.mid( 1, filename.length() - 2 );
src = "HDF4_SDS:" + r.cap( 1 ) + ":\"" + QgsProject::instance()->writePath( filename, relativeBasePath ) + "\":" + r.cap( 3 );
handled = true;
}
}
else if ( src.startsWith( "HDF5:" ) )
{
// HDF5:file_name:subdataset
// filename can be quoted with " as it can contain colons
QRegExp r( "HDF5:(.+):([^:]+)" );
if ( r.exactMatch( src ) )
{
QString filename = r.cap( 1 );
if ( filename.startsWith( '"' ) && filename.endsWith( '"' ) )
filename = filename.mid( 1, filename.length() - 2 );
src = "HDF5:\"" + QgsProject::instance()->writePath( filename, relativeBasePath ) + "\":" + r.cap( 2 );
handled = true;
}
}
else if ( src.contains( QRegExp( "^(NITF_IM|RADARSAT_2_CALIB):" ) ) )
{
// NITF_IM:0:filename
// RADARSAT_2_CALIB:?:filename
QRegExp r( "([^:]+):([^:]+):(.+)" );
if ( r.exactMatch( src ) )
{
src = r.cap( 1 ) + ":" + r.cap( 2 ) + ":" + QgsProject::instance()->writePath( r.cap( 3 ), relativeBasePath );
handled = true;
}
}
}
src = theURIParts.join( ":" );
}
}
else
{
src = QgsProject::instance()->writePath( src, relativeBasePath );

if ( !handled )
src = QgsProject::instance()->writePath( src, relativeBasePath );
}

QDomText dataSourceText = document.createTextNode( src );

0 comments on commit e2e8c87

Please sign in to comment.