Skip to content

Commit

Permalink
fix support for embedded ogr, gpx and delimited text layers
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Sep 19, 2015
1 parent 25437c1 commit 1fe34b3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/core/qgsmaplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ bool QgsMapLayer::readLayerXML( const QDomElement& layerElement )
mDataSource = mne.text();

// TODO: this should go to providers
// see also QgsProject::createEmbeddedLayer
if ( provider == "spatialite" )
{
QgsDataSourceURI uri( mDataSource );
Expand Down
65 changes: 52 additions & 13 deletions src/core/qgsproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <QObject>
#include <QTextStream>
#include <QDir>
#include <QUrl>

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

// change datasource path from relative to absolute if necessary
// see also QgsMapLayer::readLayerXML
if ( !useAbsolutePathes )
{
QDomElement provider = mapLayerElem.firstChildElement( "provider" );
if ( provider.text() == "spatialite" )
QString provider( mapLayerElem.firstChildElement( "provider" ).text() );
QDomElement dsElem( mapLayerElem.firstChildElement( "datasource" ) );
QString datasource( dsElem.text() );
if ( provider == "spatialite" )
{
QDomElement dsElem = mapLayerElem.firstChildElement( "datasource" );

QgsDataSourceURI uri( dsElem.text() );

QgsDataSourceURI uri( datasource );
QFileInfo absoluteDs( QFileInfo( projectFilePath ).absolutePath() + "/" + uri.database() );
if ( absoluteDs.exists() )
{
uri.setDatabase( absoluteDs.absoluteFilePath() );
dsElem.removeChild( dsElem.childNodes().at( 0 ) );
dsElem.appendChild( projectDocument.createTextNode( uri.uri() ) );
datasource = uri.uri();
}
}
else if ( provider == "ogr" )
{
QStringList theURIParts( datasource.split( "|" ) );
QFileInfo absoluteDs( QFileInfo( projectFilePath ).absolutePath() + "/" + theURIParts[0] );
if ( absoluteDs.exists() )
{
theURIParts[0] = absoluteDs.absoluteFilePath();
datasource = theURIParts.join( "|" );
}
}
else if ( provider == "gpx" )
{
QStringList theURIParts( datasource.split( "?" ) );
QFileInfo absoluteDs( QFileInfo( projectFilePath ).absolutePath() + "/" + theURIParts[0] );
if ( absoluteDs.exists() )
{
theURIParts[0] = absoluteDs.absoluteFilePath();
datasource = theURIParts.join( "?" );
}
}
else if ( provider == "delimitedtext" )
{
QUrl urlSource( QUrl::fromEncoded( datasource.toAscii() ) );

if ( !datasource.startsWith( "file:" ) )
{
QUrl file( QUrl::fromLocalFile( datasource.left( datasource.indexOf( "?" ) ) ) );
urlSource.setScheme( "file" );
urlSource.setPath( file.path() );
}

QFileInfo absoluteDs( QFileInfo( projectFilePath ).absolutePath() + "/" + urlSource.toLocalFile() );
if ( absoluteDs.exists() )
{
QUrl urlDest = QUrl::fromLocalFile( absoluteDs.absoluteFilePath() );
urlDest.setQueryItems( urlSource.queryItems() );
datasource = QString::fromAscii( urlDest.toEncoded() );
}
}
else
{
QDomElement dsElem = mapLayerElem.firstChildElement( "datasource" );
QString debug( QFileInfo( projectFilePath ).absolutePath() + "/" + dsElem.text() );
QFileInfo absoluteDs( QFileInfo( projectFilePath ).absolutePath() + "/" + dsElem.text() );
QFileInfo absoluteDs( QFileInfo( projectFilePath ).absolutePath() + "/" + datasource );
if ( absoluteDs.exists() )
{
dsElem.removeChild( dsElem.childNodes().at( 0 ) );
dsElem.appendChild( projectDocument.createTextNode( absoluteDs.absoluteFilePath() ) );
datasource = absoluteDs.absoluteFilePath();
}
}

dsElem.removeChild( dsElem.childNodes().at( 0 ) );
dsElem.appendChild( projectDocument.createTextNode( datasource ) );
}

if ( addLayer( mapLayerElem, brokenNodes, vectorLayerList ) )
Expand Down

0 comments on commit 1fe34b3

Please sign in to comment.