Skip to content

Commit 1fe34b3

Browse files
committedSep 19, 2015
fix support for embedded ogr, gpx and delimited text layers
1 parent 25437c1 commit 1fe34b3

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed
 

‎src/core/qgsmaplayer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ bool QgsMapLayer::readLayerXML( const QDomElement& layerElement )
183183
mDataSource = mne.text();
184184

185185
// TODO: this should go to providers
186+
// see also QgsProject::createEmbeddedLayer
186187
if ( provider == "spatialite" )
187188
{
188189
QgsDataSourceURI uri( mDataSource );

‎src/core/qgsproject.cpp

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <QObject>
4545
#include <QTextStream>
4646
#include <QDir>
47+
#include <QUrl>
4748

4849
// canonical project instance
4950
QgsProject *QgsProject::theProject_ = 0;
@@ -1665,34 +1666,72 @@ bool QgsProject::createEmbeddedLayer( const QString &layerId, const QString &pro
16651666
mEmbeddedLayers.insert( layerId, qMakePair( projectFilePath, saveFlag ) );
16661667

16671668
// change datasource path from relative to absolute if necessary
1669+
// see also QgsMapLayer::readLayerXML
16681670
if ( !useAbsolutePathes )
16691671
{
1670-
QDomElement provider = mapLayerElem.firstChildElement( "provider" );
1671-
if ( provider.text() == "spatialite" )
1672+
QString provider( mapLayerElem.firstChildElement( "provider" ).text() );
1673+
QDomElement dsElem( mapLayerElem.firstChildElement( "datasource" ) );
1674+
QString datasource( dsElem.text() );
1675+
if ( provider == "spatialite" )
16721676
{
1673-
QDomElement dsElem = mapLayerElem.firstChildElement( "datasource" );
1674-
1675-
QgsDataSourceURI uri( dsElem.text() );
1676-
1677+
QgsDataSourceURI uri( datasource );
16771678
QFileInfo absoluteDs( QFileInfo( projectFilePath ).absolutePath() + "/" + uri.database() );
16781679
if ( absoluteDs.exists() )
16791680
{
16801681
uri.setDatabase( absoluteDs.absoluteFilePath() );
1681-
dsElem.removeChild( dsElem.childNodes().at( 0 ) );
1682-
dsElem.appendChild( projectDocument.createTextNode( uri.uri() ) );
1682+
datasource = uri.uri();
1683+
}
1684+
}
1685+
else if ( provider == "ogr" )
1686+
{
1687+
QStringList theURIParts( datasource.split( "|" ) );
1688+
QFileInfo absoluteDs( QFileInfo( projectFilePath ).absolutePath() + "/" + theURIParts[0] );
1689+
if ( absoluteDs.exists() )
1690+
{
1691+
theURIParts[0] = absoluteDs.absoluteFilePath();
1692+
datasource = theURIParts.join( "|" );
1693+
}
1694+
}
1695+
else if ( provider == "gpx" )
1696+
{
1697+
QStringList theURIParts( datasource.split( "?" ) );
1698+
QFileInfo absoluteDs( QFileInfo( projectFilePath ).absolutePath() + "/" + theURIParts[0] );
1699+
if ( absoluteDs.exists() )
1700+
{
1701+
theURIParts[0] = absoluteDs.absoluteFilePath();
1702+
datasource = theURIParts.join( "?" );
1703+
}
1704+
}
1705+
else if ( provider == "delimitedtext" )
1706+
{
1707+
QUrl urlSource( QUrl::fromEncoded( datasource.toAscii() ) );
1708+
1709+
if ( !datasource.startsWith( "file:" ) )
1710+
{
1711+
QUrl file( QUrl::fromLocalFile( datasource.left( datasource.indexOf( "?" ) ) ) );
1712+
urlSource.setScheme( "file" );
1713+
urlSource.setPath( file.path() );
1714+
}
1715+
1716+
QFileInfo absoluteDs( QFileInfo( projectFilePath ).absolutePath() + "/" + urlSource.toLocalFile() );
1717+
if ( absoluteDs.exists() )
1718+
{
1719+
QUrl urlDest = QUrl::fromLocalFile( absoluteDs.absoluteFilePath() );
1720+
urlDest.setQueryItems( urlSource.queryItems() );
1721+
datasource = QString::fromAscii( urlDest.toEncoded() );
16831722
}
16841723
}
16851724
else
16861725
{
1687-
QDomElement dsElem = mapLayerElem.firstChildElement( "datasource" );
1688-
QString debug( QFileInfo( projectFilePath ).absolutePath() + "/" + dsElem.text() );
1689-
QFileInfo absoluteDs( QFileInfo( projectFilePath ).absolutePath() + "/" + dsElem.text() );
1726+
QFileInfo absoluteDs( QFileInfo( projectFilePath ).absolutePath() + "/" + datasource );
16901727
if ( absoluteDs.exists() )
16911728
{
1692-
dsElem.removeChild( dsElem.childNodes().at( 0 ) );
1693-
dsElem.appendChild( projectDocument.createTextNode( absoluteDs.absoluteFilePath() ) );
1729+
datasource = absoluteDs.absoluteFilePath();
16941730
}
16951731
}
1732+
1733+
dsElem.removeChild( dsElem.childNodes().at( 0 ) );
1734+
dsElem.appendChild( projectDocument.createTextNode( datasource ) );
16961735
}
16971736

16981737
if ( addLayer( mapLayerElem, brokenNodes, vectorLayerList ) )

0 commit comments

Comments
 (0)
Please sign in to comment.