Skip to content

Commit

Permalink
Support relative project pathes in wms server
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15086 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Jan 26, 2011
1 parent 738de66 commit b0f064c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/mapserver/qgsconfigcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ QgsConfigParser* QgsConfigCache::insertConfiguration( const QString& filePath )
}
else if ( documentElem.tagName() == "qgis" )
{
configParser = new QgsProjectParser( configDoc );
configParser = new QgsProjectParser( configDoc, filePath );
}
else
{
Expand Down
70 changes: 66 additions & 4 deletions src/mapserver/qgsprojectparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "qgscomposershape.h"


QgsProjectParser::QgsProjectParser( QDomDocument* xmlDoc ): QgsConfigParser(), mXMLDoc( xmlDoc )
QgsProjectParser::QgsProjectParser( QDomDocument* xmlDoc, const QString& filePath ): QgsConfigParser(), mXMLDoc( xmlDoc ), mProjectPath( filePath )
{
mOutputUnits = QgsMapRenderer::Millimeters;
setLegendParametersFromProject();
Expand Down Expand Up @@ -673,7 +673,7 @@ QMap< QString, QDomElement > QgsProjectParser::projectLayerElementsByName() cons

QgsMapLayer* QgsProjectParser::createLayerFromElement( const QDomElement& elem ) const
{
if ( elem.isNull() )
if ( elem.isNull() || !mXMLDoc )
{
return 0;
}
Expand All @@ -684,14 +684,22 @@ QgsMapLayer* QgsProjectParser::createLayerFromElement( const QDomElement& elem )
return 0;
}

//convert relative pathes to absolute ones if necessary
QString uri = dataSourceElem.text();
QString absoluteUri = convertToAbsolutePath( uri );
if ( uri != absoluteUri )
{
QDomText absoluteTextNode = mXMLDoc->createTextNode( absoluteUri );
dataSourceElem.replaceChild( absoluteTextNode, dataSourceElem.firstChild() );
}

QString id = layerId( elem );
if ( id.isNull() )
{
return 0;
}

QgsMapLayer* layer = QgsMSLayerCache::instance()->searchLayer( uri, id );
QgsMapLayer* layer = QgsMSLayerCache::instance()->searchLayer( absoluteUri, id );
if ( layer )
{
//reading symbology every time is necessary because it could have been changed by a user SLD based request
Expand All @@ -714,7 +722,7 @@ QgsMapLayer* QgsProjectParser::createLayerFromElement( const QDomElement& elem )
{
layer->readXML( const_cast<QDomElement&>( elem ) ); //should be changed to const in QgsMapLayer
layer->setLayerName( layerName( elem ) );
QgsMSLayerCache::instance()->insertLayer( uri, id, layer );
QgsMSLayerCache::instance()->insertLayer( absoluteUri, id, layer );
}
return layer;
}
Expand Down Expand Up @@ -930,6 +938,8 @@ QgsComposition* QgsProjectParser::initComposition( const QString& composerTempla
{
QgsComposerPicture* picture = new QgsComposerPicture( composition );
picture->readXML( currentElem, *mXMLDoc );
//qgis mapserver needs an absolute file path
picture->setPictureFile( convertToAbsolutePath( picture->pictureFile() ) );
composition->addItem( picture );
}
else if ( elemName == "ComposerScaleBar" )
Expand Down Expand Up @@ -1138,3 +1148,55 @@ void QgsProjectParser::serviceCapabilities( QDomElement& parentElement, QDomDocu
parentElement.appendChild( contactInfoElem );
}

QString QgsProjectParser::convertToAbsolutePath( const QString& file ) const
{
if ( !file.startsWith( "./" ) && !file.startsWith( "../" ) )
{
return file;
}

QString srcPath = file;
QString projPath = mProjectPath;

#if defined(Q_OS_WIN)
srcPath.replace( "\\", "/" );
projPath.replace( "\\", "/" );

bool uncPath = projPath.startsWith( "//" );
#endif

QStringList srcElems = file.split( "/", QString::SkipEmptyParts );
QStringList projElems = mProjectPath.split( "/", QString::SkipEmptyParts );

#if defined(Q_OS_WIN)
if ( uncPath )
{
projElems.insert( 0, "" );
projElems.insert( 0, "" );
}
#endif

// remove project file element
projElems.removeLast();

// append source path elements
projElems << srcElems;
projElems.removeAll( "." );

// resolve ..
int pos;
while (( pos = projElems.indexOf( ".." ) ) > 0 )
{
// remove preceding element and ..
projElems.removeAt( pos - 1 );
projElems.removeAt( pos - 1 );
}

#if !defined(Q_OS_WIN)
// make path absolute
projElems.prepend( "" );
#endif

return projElems.join( "/" );
}

8 changes: 7 additions & 1 deletion src/mapserver/qgsprojectparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class QgsProjectParser: public QgsConfigParser
{
public:
/**Constructor. Takes ownership of xml document*/
QgsProjectParser( QDomDocument* xmlDoc );
QgsProjectParser( QDomDocument* xmlDoc, const QString& filePath );
virtual ~QgsProjectParser();

/**Adds layer and style specific capabilities elements to the parent node. This includes the individual layers and styles, their description, native CRS, bounding boxes, etc.*/
Expand Down Expand Up @@ -100,6 +100,9 @@ class QgsProjectParser: public QgsConfigParser
/**Content of project file*/
QDomDocument* mXMLDoc;

/**Absolute project file path (including file name)*/
QString mProjectPath;

/**Get all layers of the project (ordered same as in the project file)*/
QList<QDomElement> projectLayerElements() const;
/**Returns all legend group elements*/
Expand Down Expand Up @@ -134,6 +137,9 @@ class QgsProjectParser: public QgsConfigParser

/**Returns dom element of composer (identified by composer title) or a null element in case of error*/
QDomElement composerByName( const QString& composerName ) const;

/**Converts a (possibly relative) path to absolute*/
QString convertToAbsolutePath( const QString& file ) const;
};

#endif // QGSPROJECTPARSER_H

0 comments on commit b0f064c

Please sign in to comment.