Skip to content
Permalink
Browse files

Support embedded layers and groups in server

  • Loading branch information
Marco Hugentobler authored and mhugent committed Jun 8, 2011
1 parent c4cd43b commit b1c8d03fc7c5dd43c63855ef607b176a1630a0df
@@ -176,7 +176,6 @@ int main( int argc, char * argv[] )

//create config cache and search for config files in the current directory.
//These configurations are used if no mapfile parameter is present in the request
QgsConfigCache configCache;
QString defaultConfigFilePath;
QFileInfo projectFileInfo = defaultProjectFile(); //try to find a .qgs file in the server directory
if ( projectFileInfo.exists() )
@@ -245,7 +244,7 @@ int main( int argc, char * argv[] )
configFilePath = mapFileIt->second;
}

QgsConfigParser* adminConfigParser = configCache.searchConfiguration( configFilePath );
QgsConfigParser* adminConfigParser = QgsConfigCache::instance()->searchConfiguration( configFilePath );
if ( !adminConfigParser )
{
QgsMSDebugMsg( "parse error on config file " + configFilePath );
@@ -22,6 +22,17 @@
#include "qgssldparser.h"
#include <QCoreApplication>

QgsConfigCache* QgsConfigCache::mInstance = 0;

QgsConfigCache* QgsConfigCache::instance()
{
if ( !mInstance )
{
mInstance = new QgsConfigCache();
}
return mInstance;
}

QgsConfigCache::QgsConfigCache()
{
QObject::connect( &mFileSystemWatcher, SIGNAL( fileChanged( const QString& ) ), this, SLOT( removeChangedEntry( const QString& ) ) );
@@ -30,13 +30,18 @@ class QgsConfigCache: public QObject
{
Q_OBJECT
public:
QgsConfigCache();
static QgsConfigCache* instance();
~QgsConfigCache();

/**Returns configuration for given config file path. The calling function does _not_ take ownership*/
QgsConfigParser* searchConfiguration( const QString& filePath );

protected:
QgsConfigCache();

private:
static QgsConfigCache* mInstance;

/**Creates configuration parser depending on the file type and, if successfull, inserts it to the cached configuration map
@param filePath path of the configuration file
@return the inserted config parser or 0 in case of error*/
@@ -16,6 +16,7 @@
***************************************************************************/

#include "qgsprojectparser.h"
#include "qgsconfigcache.h"
#include "qgsepsgcache.h"
#include "qgsmslayercache.h"
#include "qgsmapserverlogger.h"
@@ -143,7 +144,43 @@ void QgsProjectParser::addLayers( QDomDocument &doc,
titleElem.appendChild( titleText );
layerElem.appendChild( titleElem );

addLayers( doc, layerElem, currentChildElem, layerMap, nonIdentifiableLayers, mapExtent, mapCRS );
if( currentChildElem.attribute("embedded") == "1" )
{
//add layers from other project files and embed into this group
QString project = convertToAbsolutePath( currentChildElem.attribute("project") );
QString embeddedGroupName = currentChildElem.attribute("name");
QgsProjectParser* p = dynamic_cast<QgsProjectParser*>( QgsConfigCache::instance()->searchConfiguration( project ) );
if( p )
{
QStringList pIdDisabled = p->identifyDisabledLayers();

QDomElement embeddedGroupElem;
QList<QDomElement> pLegendElems = p->legendGroupElements();
QList<QDomElement>::const_iterator pLegendIt = pLegendElems.constBegin();
for(; pLegendIt != pLegendElems.constEnd(); ++pLegendIt )
{
if( pLegendIt->attribute("name") == embeddedGroupName )
{
embeddedGroupElem = *pLegendIt;
break;
}
}

QList<QDomElement> pLayerElems = p->projectLayerElements();
QMap<QString, QgsMapLayer *> pLayerMap;
QList<QDomElement>::const_iterator pLayerIt = pLayerElems.constBegin();
for(; pLayerIt != pLayerElems.constEnd(); ++pLayerIt )
{
pLayerMap.insert( layerId( *pLayerIt ), p->createLayerFromElement( *pLayerIt ) );
}

p->addLayers( doc, layerElem, embeddedGroupElem, pLayerMap, pIdDisabled, mapExtent, mapCRS);
}
}
else //normal (not embedded) legend group
{
addLayers( doc, layerElem, currentChildElem, layerMap, nonIdentifiableLayers, mapExtent, mapCRS );
}

// combine bounding boxes of childs (groups/layers)

@@ -283,7 +320,6 @@ void QgsProjectParser::addLayers( QDomDocument &doc,
QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle( const QString& lName, const QString& styleName, bool allowCaching ) const
{
QList<QgsMapLayer*> layerList;
bool layerFound = false;

//first assume lName refers to a leaf layer
QMap< QString, QDomElement > layerElemMap = projectLayerElementsByName();
@@ -294,18 +330,11 @@ QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle( const QString& lName, c
if ( layer )
{
layerList.push_back( layer );
return layerList;
}
layerFound = true;
}

if ( layerFound )
{
return layerList;
}

//Check if layer name refers to the top level group for the project.
//The project group is not contained in the groupLayerRelationship list
//because the list (and the qgis legend) does not support nested groups
if ( lName == projectTitle() )
{
QList<QDomElement> layerElemList = projectLayerElements();
@@ -326,18 +355,110 @@ QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle( const QString& lName, c
{
if ( groupIt->attribute( "name" ) == lName )
{
QDomNodeList layerFileList = groupIt->elementsByTagName( "legendlayerfile" );
for ( int i = 0; i < layerFileList.size(); ++i )
if( groupIt->attribute("embedded") == "1") //requested group is embedded from another project
{
QMap< QString, QDomElement >::const_iterator layerEntry = idLayerMap.find( layerFileList.at( i ).toElement().attribute( "layerid" ) );
if ( layerEntry != idLayerMap.constEnd() )
QgsProjectParser* p = dynamic_cast<QgsProjectParser*>(QgsConfigCache::instance()->searchConfiguration( convertToAbsolutePath(groupIt->attribute("project") ) ) );
if( p )
{
layerList.push_back( createLayerFromElement( layerEntry.value() ) );
QList<QDomElement> pGroupElems = p->legendGroupElements();
QList<QDomElement>::const_iterator pGroupIt = pGroupElems.constBegin();
QDomElement embeddedGroupElem;

for(; pGroupIt != pGroupElems.constEnd(); ++pGroupIt )
{
if( pGroupIt->attribute("name") == lName )
{
embeddedGroupElem = *pGroupIt;
break;
}
}

if( !embeddedGroupElem.isNull() )
{
//add all the layers under the group
QMap< QString, QDomElement > pLayerElems = p->projectLayerElementsById();
QDomNodeList pLayerNodes = embeddedGroupElem.elementsByTagName("legendlayer");
for( int i = 0; i < pLayerNodes.size(); ++i )
{
QString pLayerId = pLayerNodes.at(i).toElement().firstChildElement("filegroup").firstChildElement("legendlayerfile").attribute("layerid");
QgsMapLayer* pLayer = p->createLayerFromElement( pLayerElems[pLayerId] );
if( pLayer )
{
layerList.push_back( pLayer );
}
}
}
}
}
else //normal (not embedded) group
{
QDomNodeList layerFileList = groupIt->elementsByTagName( "legendlayerfile" );
for ( int i = 0; i < layerFileList.size(); ++i )
{
QMap< QString, QDomElement >::const_iterator layerEntry = idLayerMap.find( layerFileList.at( i ).toElement().attribute( "layerid" ) );
if ( layerEntry != idLayerMap.constEnd() )
{
layerList.push_back( createLayerFromElement( layerEntry.value() ) );
}
}
}
return layerList;
}
}

//maybe the layer is embedded from another project
QMap< QString, QDomElement >::const_iterator layerIt = idLayerMap.constBegin();
for(; layerIt != idLayerMap.constEnd(); ++layerIt )
{
if( layerIt.value().attribute("embedded") == "1" )
{
QString id = layerIt.value().attribute("id");
QString project = layerIt.value().attribute("project");

//get config parser from cache
QgsProjectParser* otherParser = dynamic_cast<QgsProjectParser*>(QgsConfigCache::instance()->searchConfiguration( project ) );
if( otherParser )
{
//get element by id
QMap< QString, QDomElement > otherLayerElems = otherParser->projectLayerElementsById();
QMap< QString, QDomElement >::const_iterator otherLayerIt = otherLayerElems.find( id );
if( otherLayerIt != otherLayerElems.constEnd() )
{
if( otherLayerIt.value().firstChildElement("layername").text() == lName )
{
layerList.push_back( otherParser->createLayerFromElement( otherLayerIt.value() ) );
return layerList;
}
}
}
}
}

//layer still not found. Check if it is a single layer contained in a embedded layer group
groupIt = legendGroups.constBegin();
for(; groupIt != legendGroups.constEnd(); ++groupIt )
{
if( groupIt->attribute("embedded") == "1")
{
QgsProjectParser* p = dynamic_cast<QgsProjectParser*>(QgsConfigCache::instance()->searchConfiguration( convertToAbsolutePath(groupIt->attribute("project") ) ) );
if( p )
{
QMap< QString, QDomElement > pLayers = p->projectLayerElementsByName();
QMap< QString, QDomElement >::const_iterator pLayerIt = pLayers.find( lName );
if( pLayerIt != pLayers.constEnd() )
{
QgsMapLayer* layer = p->createLayerFromElement( pLayerIt.value() );
if( layer )
{
layerList.push_back( layer );
return layerList;
}
}
}
}
}

//layer not found, return empty list
return layerList;
}

@@ -728,27 +849,22 @@ QgsMapLayer* QgsProjectParser::createLayerFromElement( const QDomElement& elem )
return 0;
}

QString uri;
QString absoluteUri;
QDomElement dataSourceElem = elem.firstChildElement( "datasource" );
if ( dataSourceElem.isNull() )
if ( !dataSourceElem.isNull() )
{
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() );
//convert relative pathes to absolute ones if necessary
uri = dataSourceElem.text();
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( absoluteUri, id );
if ( layer )
{
@@ -767,6 +883,22 @@ QgsMapLayer* QgsProjectParser::createLayerFromElement( const QDomElement& elem )
{
layer = new QgsRasterLayer();
}
else if ( elem.attribute("embedded") == "1" ) //layer is embedded from another project file
{
QgsProjectParser* otherConfig = dynamic_cast<QgsProjectParser*>( QgsConfigCache::instance()->searchConfiguration( convertToAbsolutePath( elem.attribute( "project" ) ) ) );
if( !otherConfig )
{
return 0;
}

QMap< QString, QDomElement > layerMap = otherConfig->projectLayerElementsById();
QMap< QString, QDomElement >::const_iterator layerIt = layerMap.find( elem.attribute( "id" ) );
if( layerIt == layerMap.constEnd() )
{
return 0;
}
return otherConfig->createLayerFromElement( layerIt.value() );
}

if ( layer )
{

0 comments on commit b1c8d03

Please sign in to comment.
You can’t perform that action at this time.