Skip to content
Permalink
Browse files

Read/write relative paths to gpkg rasters in project (when enabled)

Routines in raster layer were not aware of "GDAL:filename:table" syntax
used for raster layers from GeoPackages so project files were storing
absolute paths even when relative paths should have been used.
  • Loading branch information
wonder-sk committed Nov 4, 2018
1 parent ceb33f0 commit 89a80c1b550f66eaf47abb5e57e8cad440d423e4
Showing with 48 additions and 0 deletions.
  1. +48 −0 src/core/raster/qgsrasterlayer.cpp
@@ -1646,6 +1646,32 @@ bool QgsRasterLayer::writeXml( QDomNode &layer_node,
return writeSymbology( layer_node, document, errorMsg, context );
}

// TODO: this should ideally go to gdal provider (together with most of encodedSource() + decodedSource())
static bool _parseGpkgColons( const QString &src, QString &filename, QString &tablename )
{
// GDAL accepts the following input format: GPKG:filename:table
// (GDAL won't accept quoted filename)

QStringList lst = src.split( ':' );
if ( lst.count() != 3 && lst.count() != 4 )
return false;

tablename = lst.last();
if ( lst.count() == 3 )
{
filename = lst[1];
return true;
}
else if ( lst.count() == 4 && lst[1].count() == 1 && ( lst[2][0] == '/' || lst[2][0] == '\\' ) )
{
// a bit of handling to make sure that filename C:\hello.gpkg is parsed correctly
filename = lst[1] + ":" + lst[2];
return true;
}
return false;
}


QString QgsRasterLayer::encodedSource( const QString &source, const QgsReadWriteContext &context ) const
{
QString src( source );
@@ -1668,6 +1694,17 @@ QString QgsRasterLayer::encodedSource( const QString &source, const QgsReadWrite
handled = true;
}
}
else if ( src.startsWith( QLatin1String( "GPKG:" ) ) )
{
// GPKG:filename:table
QString filename, tablename;
if ( _parseGpkgColons( src, filename, tablename ) )
{
filename = context.pathResolver().writePath( filename );
src = QStringLiteral( "GPKG:%1:%2" ).arg( filename, tablename );
handled = true;
}
}
else if ( src.startsWith( QLatin1String( "HDF4_SDS:" ) ) )
{
// HDF4_SDS:subdataset_type:file_name:subdataset_index
@@ -1817,6 +1854,17 @@ QString QgsRasterLayer::decodedSource( const QString &source, const QString &pro
handled = true;
}
}
else if ( src.startsWith( QLatin1String( "GPKG:" ) ) )
{
// GPKG:filename:table
QString filename, tablename;
if ( _parseGpkgColons( src, filename, tablename ) )
{
filename = context.pathResolver().readPath( filename );
src = QStringLiteral( "GPKG:%1:%2" ).arg( filename, tablename );
handled = true;
}
}
else if ( src.startsWith( QLatin1String( "HDF4_SDS:" ) ) )
{
// HDF4_SDS:subdataset_type:file_name:subdataset_index

0 comments on commit 89a80c1

Please sign in to comment.
You can’t perform that action at this time.