Skip to content

Commit

Permalink
fix relative path support for gdal layers (fixes #12823)
Browse files Browse the repository at this point in the history
(merged 47cb75d and e2e8c87)
  • Loading branch information
jef-n committed May 27, 2015
1 parent 4d0413a commit 11ebf21
Showing 1 changed file with 130 additions and 66 deletions.
196 changes: 130 additions & 66 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -49,13 +49,13 @@

QgsMapLayer::QgsMapLayer( QgsMapLayer::LayerType type,
QString lyrname,
QString source ) :
mValid( false ), // assume the layer is invalid
mDataSource( source ),
mLayerOrigName( lyrname ), // store the original name
mID( "" ),
mLayerType( type ),
mBlendMode( QPainter::CompositionMode_SourceOver ) // Default to normal blending
QString source )
: mValid( false ) // assume the layer is invalid
, mDataSource( source )
, mLayerOrigName( lyrname ) // store the original name
, mID( "" )
, mLayerType( type )
, mBlendMode( QPainter::CompositionMode_SourceOver ) // Default to normal blending
, mLegend( 0 )
, mStyleManager( new QgsMapLayerStyleManager( this ) )
{
Expand Down Expand Up @@ -292,37 +292,69 @@ bool QgsMapLayer::readLayerXML( const QDomElement& layerElement )
}
// <<< BACKWARD COMPATIBILITY < 1.9
}
else if ( provider == "gdal" )
else
{
QStringList theURIParts = mDataSource.split( ":" );
if ( theURIParts[0] == "NETCDF" )
{
QString src = theURIParts[1];
src.replace( "\"", "" );
src = QgsProject::instance()->readPath( src );
theURIParts[1] = "\"" + src + "\"";
}
else if ( theURIParts[0] == "HDF4_SDS" )
{
theURIParts[2] = QgsProject::instance()->readPath( theURIParts[2] );
}
else if ( theURIParts[0] == "HDF5" )
{
theURIParts[1] = QgsProject::instance()->readPath( theURIParts[1] );
}
else if ( theURIParts[0] == "NITF_IM" )
{
theURIParts[2] = QgsProject::instance()->readPath( theURIParts[2] );
}
else if ( theURIParts[0] == "RADARSAT_2_CALIB" )
bool handled = false;

if ( provider == "gdal" )
{
theURIParts[2] = QgsProject::instance()->readPath( theURIParts[2] );
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;
}
}
}
mDataSource = theURIParts.join( ":" );
}
else
{
mDataSource = QgsProject::instance()->readPath( mDataSource );

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

// Set the CRS from project file, asking the user if necessary.
Expand Down Expand Up @@ -517,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" )
{
theURIParts[1] = QgsProject::instance()->writePath( theURIParts[1], relativeBasePath );
}
else if ( theURIParts[0] == "NITF_IM" )
QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( this );
// Update path for subdataset
if ( rlayer && rlayer->providerType() == "gdal" )
{
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 );
Expand Down

0 comments on commit 11ebf21

Please sign in to comment.