Skip to content

Commit

Permalink
Read/write relative paths to gpkg rasters in project (when enabled)
Browse files Browse the repository at this point in the history
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.

(cherry picked from commit 89a80c1)
  • Loading branch information
wonder-sk authored and nyalldawson committed Nov 6, 2018
1 parent 9f5664a commit d737e56
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/core/raster/qgsrasterlayer.cpp
Expand Up @@ -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 );
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d737e56

Please sign in to comment.