[QGIS Commit] r15086 - trunk/qgis/src/mapserver

svn_qgis at osgeo.org svn_qgis at osgeo.org
Wed Jan 26 08:49:53 EST 2011


Author: mhugent
Date: 2011-01-26 05:49:53 -0800 (Wed, 26 Jan 2011)
New Revision: 15086

Modified:
   trunk/qgis/src/mapserver/qgsconfigcache.cpp
   trunk/qgis/src/mapserver/qgsprojectparser.cpp
   trunk/qgis/src/mapserver/qgsprojectparser.h
Log:
Support relative project pathes in wms server

Modified: trunk/qgis/src/mapserver/qgsconfigcache.cpp
===================================================================
--- trunk/qgis/src/mapserver/qgsconfigcache.cpp	2011-01-26 13:22:04 UTC (rev 15085)
+++ trunk/qgis/src/mapserver/qgsconfigcache.cpp	2011-01-26 13:49:53 UTC (rev 15086)
@@ -86,7 +86,7 @@
   }
   else if ( documentElem.tagName() == "qgis" )
   {
-    configParser = new QgsProjectParser( configDoc );
+    configParser = new QgsProjectParser( configDoc, filePath );
   }
   else
   {

Modified: trunk/qgis/src/mapserver/qgsprojectparser.cpp
===================================================================
--- trunk/qgis/src/mapserver/qgsprojectparser.cpp	2011-01-26 13:22:04 UTC (rev 15085)
+++ trunk/qgis/src/mapserver/qgsprojectparser.cpp	2011-01-26 13:49:53 UTC (rev 15086)
@@ -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();
@@ -673,7 +673,7 @@
 
 QgsMapLayer* QgsProjectParser::createLayerFromElement( const QDomElement& elem ) const
 {
-  if ( elem.isNull() )
+  if ( elem.isNull() || !mXMLDoc )
   {
     return 0;
   }
@@ -684,14 +684,22 @@
     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
@@ -714,7 +722,7 @@
   {
     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;
 }
@@ -930,6 +938,8 @@
     {
       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" )
@@ -1138,3 +1148,55 @@
   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( "/" );
+}
+

Modified: trunk/qgis/src/mapserver/qgsprojectparser.h
===================================================================
--- trunk/qgis/src/mapserver/qgsprojectparser.h	2011-01-26 13:22:04 UTC (rev 15085)
+++ trunk/qgis/src/mapserver/qgsprojectparser.h	2011-01-26 13:49:53 UTC (rev 15086)
@@ -32,7 +32,7 @@
 {
   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.*/
@@ -100,6 +100,9 @@
     /**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*/
@@ -134,6 +137,9 @@
 
     /**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



More information about the QGIS-commit mailing list