Skip to content

Commit 89a80c1

Browse files
committedNov 5, 2018
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.
1 parent ceb33f0 commit 89a80c1

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
 

‎src/core/raster/qgsrasterlayer.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,6 +1646,32 @@ bool QgsRasterLayer::writeXml( QDomNode &layer_node,
16461646
return writeSymbology( layer_node, document, errorMsg, context );
16471647
}
16481648

1649+
// TODO: this should ideally go to gdal provider (together with most of encodedSource() + decodedSource())
1650+
static bool _parseGpkgColons( const QString &src, QString &filename, QString &tablename )
1651+
{
1652+
// GDAL accepts the following input format: GPKG:filename:table
1653+
// (GDAL won't accept quoted filename)
1654+
1655+
QStringList lst = src.split( ':' );
1656+
if ( lst.count() != 3 && lst.count() != 4 )
1657+
return false;
1658+
1659+
tablename = lst.last();
1660+
if ( lst.count() == 3 )
1661+
{
1662+
filename = lst[1];
1663+
return true;
1664+
}
1665+
else if ( lst.count() == 4 && lst[1].count() == 1 && ( lst[2][0] == '/' || lst[2][0] == '\\' ) )
1666+
{
1667+
// a bit of handling to make sure that filename C:\hello.gpkg is parsed correctly
1668+
filename = lst[1] + ":" + lst[2];
1669+
return true;
1670+
}
1671+
return false;
1672+
}
1673+
1674+
16491675
QString QgsRasterLayer::encodedSource( const QString &source, const QgsReadWriteContext &context ) const
16501676
{
16511677
QString src( source );
@@ -1668,6 +1694,17 @@ QString QgsRasterLayer::encodedSource( const QString &source, const QgsReadWrite
16681694
handled = true;
16691695
}
16701696
}
1697+
else if ( src.startsWith( QLatin1String( "GPKG:" ) ) )
1698+
{
1699+
// GPKG:filename:table
1700+
QString filename, tablename;
1701+
if ( _parseGpkgColons( src, filename, tablename ) )
1702+
{
1703+
filename = context.pathResolver().writePath( filename );
1704+
src = QStringLiteral( "GPKG:%1:%2" ).arg( filename, tablename );
1705+
handled = true;
1706+
}
1707+
}
16711708
else if ( src.startsWith( QLatin1String( "HDF4_SDS:" ) ) )
16721709
{
16731710
// HDF4_SDS:subdataset_type:file_name:subdataset_index
@@ -1817,6 +1854,17 @@ QString QgsRasterLayer::decodedSource( const QString &source, const QString &pro
18171854
handled = true;
18181855
}
18191856
}
1857+
else if ( src.startsWith( QLatin1String( "GPKG:" ) ) )
1858+
{
1859+
// GPKG:filename:table
1860+
QString filename, tablename;
1861+
if ( _parseGpkgColons( src, filename, tablename ) )
1862+
{
1863+
filename = context.pathResolver().readPath( filename );
1864+
src = QStringLiteral( "GPKG:%1:%2" ).arg( filename, tablename );
1865+
handled = true;
1866+
}
1867+
}
18201868
else if ( src.startsWith( QLatin1String( "HDF4_SDS:" ) ) )
18211869
{
18221870
// HDF4_SDS:subdataset_type:file_name:subdataset_index

0 commit comments

Comments
 (0)