Skip to content

Commit 350bbb7

Browse files
committed
[BUGFIX] Relative path for GDAL subdataset
For some raster layer, the datasource is not stored in the project as relative. It's the case for NETCDF, HDF4, HDF5, NITF and RADARSAT2 raster format. This patch keeps the datasource structure but updates the path part. It will be interesting to backport it to 2.8 branch.
1 parent 24417fd commit 350bbb7

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/core/qgsmaplayer.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "qgsrectangle.h"
4747
#include "qgsvectorlayer.h"
4848

49+
4950
QgsMapLayer::QgsMapLayer( QgsMapLayer::LayerType type,
5051
QString lyrname,
5152
QString source ) :
@@ -291,6 +292,34 @@ bool QgsMapLayer::readLayerXML( const QDomElement& layerElement )
291292
}
292293
// <<< BACKWARD COMPATIBILITY < 1.9
293294
}
295+
else if ( provider == "gdal" )
296+
{
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" )
318+
{
319+
theURIParts[2] = QgsProject::instance()->readPath( theURIParts[2] );
320+
}
321+
mDataSource = theURIParts.join( ":" );
322+
}
294323
else
295324
{
296325
mDataSource = QgsProject::instance()->readPath( mDataSource );
@@ -488,6 +517,39 @@ bool QgsMapLayer::writeLayerXML( QDomElement& layerElement, QDomDocument& docume
488517
urlDest.setQueryItems( urlSource.queryItems() );
489518
src = QString::fromAscii( urlDest.toEncoded() );
490519
}
520+
else if ( !vlayer )
521+
{
522+
QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( this );
523+
// Update path for subdataset
524+
if ( rlayer && rlayer->providerType() == "gdal" )
525+
{
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" )
543+
{
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 );
549+
}
550+
src = theURIParts.join( ":" );
551+
}
552+
}
491553
else
492554
{
493555
src = QgsProject::instance()->writePath( src, relativeBasePath );

0 commit comments

Comments
 (0)