Skip to content

Commit e88c990

Browse files
author
mhugent
committedJan 26, 2011
Support relative project pathes in wms server
git-svn-id: http://svn.osgeo.org/qgis/trunk@15086 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 60c5104 commit e88c990

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed
 

‎src/mapserver/qgsconfigcache.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ QgsConfigParser* QgsConfigCache::insertConfiguration( const QString& filePath )
8686
}
8787
else if ( documentElem.tagName() == "qgis" )
8888
{
89-
configParser = new QgsProjectParser( configDoc );
89+
configParser = new QgsProjectParser( configDoc, filePath );
9090
}
9191
else
9292
{

‎src/mapserver/qgsprojectparser.cpp

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include "qgscomposershape.h"
3535

3636

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

674674
QgsMapLayer* QgsProjectParser::createLayerFromElement( const QDomElement& elem ) const
675675
{
676-
if ( elem.isNull() )
676+
if ( elem.isNull() || !mXMLDoc )
677677
{
678678
return 0;
679679
}
@@ -684,14 +684,22 @@ QgsMapLayer* QgsProjectParser::createLayerFromElement( const QDomElement& elem )
684684
return 0;
685685
}
686686

687+
//convert relative pathes to absolute ones if necessary
687688
QString uri = dataSourceElem.text();
689+
QString absoluteUri = convertToAbsolutePath( uri );
690+
if ( uri != absoluteUri )
691+
{
692+
QDomText absoluteTextNode = mXMLDoc->createTextNode( absoluteUri );
693+
dataSourceElem.replaceChild( absoluteTextNode, dataSourceElem.firstChild() );
694+
}
695+
688696
QString id = layerId( elem );
689697
if ( id.isNull() )
690698
{
691699
return 0;
692700
}
693701

694-
QgsMapLayer* layer = QgsMSLayerCache::instance()->searchLayer( uri, id );
702+
QgsMapLayer* layer = QgsMSLayerCache::instance()->searchLayer( absoluteUri, id );
695703
if ( layer )
696704
{
697705
//reading symbology every time is necessary because it could have been changed by a user SLD based request
@@ -714,7 +722,7 @@ QgsMapLayer* QgsProjectParser::createLayerFromElement( const QDomElement& elem )
714722
{
715723
layer->readXML( const_cast<QDomElement&>( elem ) ); //should be changed to const in QgsMapLayer
716724
layer->setLayerName( layerName( elem ) );
717-
QgsMSLayerCache::instance()->insertLayer( uri, id, layer );
725+
QgsMSLayerCache::instance()->insertLayer( absoluteUri, id, layer );
718726
}
719727
return layer;
720728
}
@@ -930,6 +938,8 @@ QgsComposition* QgsProjectParser::initComposition( const QString& composerTempla
930938
{
931939
QgsComposerPicture* picture = new QgsComposerPicture( composition );
932940
picture->readXML( currentElem, *mXMLDoc );
941+
//qgis mapserver needs an absolute file path
942+
picture->setPictureFile( convertToAbsolutePath( picture->pictureFile() ) );
933943
composition->addItem( picture );
934944
}
935945
else if ( elemName == "ComposerScaleBar" )
@@ -1138,3 +1148,55 @@ void QgsProjectParser::serviceCapabilities( QDomElement& parentElement, QDomDocu
11381148
parentElement.appendChild( contactInfoElem );
11391149
}
11401150

1151+
QString QgsProjectParser::convertToAbsolutePath( const QString& file ) const
1152+
{
1153+
if ( !file.startsWith( "./" ) && !file.startsWith( "../" ) )
1154+
{
1155+
return file;
1156+
}
1157+
1158+
QString srcPath = file;
1159+
QString projPath = mProjectPath;
1160+
1161+
#if defined(Q_OS_WIN)
1162+
srcPath.replace( "\\", "/" );
1163+
projPath.replace( "\\", "/" );
1164+
1165+
bool uncPath = projPath.startsWith( "//" );
1166+
#endif
1167+
1168+
QStringList srcElems = file.split( "/", QString::SkipEmptyParts );
1169+
QStringList projElems = mProjectPath.split( "/", QString::SkipEmptyParts );
1170+
1171+
#if defined(Q_OS_WIN)
1172+
if ( uncPath )
1173+
{
1174+
projElems.insert( 0, "" );
1175+
projElems.insert( 0, "" );
1176+
}
1177+
#endif
1178+
1179+
// remove project file element
1180+
projElems.removeLast();
1181+
1182+
// append source path elements
1183+
projElems << srcElems;
1184+
projElems.removeAll( "." );
1185+
1186+
// resolve ..
1187+
int pos;
1188+
while (( pos = projElems.indexOf( ".." ) ) > 0 )
1189+
{
1190+
// remove preceding element and ..
1191+
projElems.removeAt( pos - 1 );
1192+
projElems.removeAt( pos - 1 );
1193+
}
1194+
1195+
#if !defined(Q_OS_WIN)
1196+
// make path absolute
1197+
projElems.prepend( "" );
1198+
#endif
1199+
1200+
return projElems.join( "/" );
1201+
}
1202+

‎src/mapserver/qgsprojectparser.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class QgsProjectParser: public QgsConfigParser
3232
{
3333
public:
3434
/**Constructor. Takes ownership of xml document*/
35-
QgsProjectParser( QDomDocument* xmlDoc );
35+
QgsProjectParser( QDomDocument* xmlDoc, const QString& filePath );
3636
virtual ~QgsProjectParser();
3737

3838
/**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.*/
@@ -100,6 +100,9 @@ class QgsProjectParser: public QgsConfigParser
100100
/**Content of project file*/
101101
QDomDocument* mXMLDoc;
102102

103+
/**Absolute project file path (including file name)*/
104+
QString mProjectPath;
105+
103106
/**Get all layers of the project (ordered same as in the project file)*/
104107
QList<QDomElement> projectLayerElements() const;
105108
/**Returns all legend group elements*/
@@ -134,6 +137,9 @@ class QgsProjectParser: public QgsConfigParser
134137

135138
/**Returns dom element of composer (identified by composer title) or a null element in case of error*/
136139
QDomElement composerByName( const QString& composerName ) const;
140+
141+
/**Converts a (possibly relative) path to absolute*/
142+
QString convertToAbsolutePath( const QString& file ) const;
137143
};
138144

139145
#endif // QGSPROJECTPARSER_H

0 commit comments

Comments
 (0)
Please sign in to comment.