Skip to content
Permalink
Browse files

WMS server: avoid reading layer symbology if the request does not com…

…e from SLD
  • Loading branch information
marco
marco committed Oct 15, 2011
1 parent 54754f1 commit b3bebf3a9f6bd92b64220fcfefcaaedee90e30d3
@@ -42,6 +42,25 @@ QgsConfigParser::~QgsConfigParser()
{
delete it.value();
}

//remove the temporary files
for ( QList<QTemporaryFile*>::const_iterator it = mFilesToRemove.constBegin(); it != mFilesToRemove.constEnd(); ++it )
{
delete *it;
}

//and also those the temporary file paths
for ( QList<QString>::const_iterator it = mFilePathsToRemove.constBegin(); it != mFilePathsToRemove.constEnd(); ++it )
{
QFile::remove( *it );
}

//delete the layers in the list
QList<QgsMapLayer*>::iterator layer_it = mLayersToRemove.begin();
for ( ; layer_it != mLayersToRemove.end(); ++layer_it )
{
delete *layer_it;
}
}

void QgsConfigParser::setDefaultLegendSettings()
@@ -23,6 +23,7 @@
#include <QFont>
#include <QList>
#include <QSet>
#include <QTemporaryFile>

class QgsComposition;
class QgsComposerLabel;
@@ -41,8 +42,9 @@ class QgsConfigParser
virtual void layersAndStylesCapabilities( QDomElement& parentElement, QDomDocument& doc ) const = 0;

/**Returns one or possibly several maplayers for a given layer name and style. If there are several layers, the layers should be drawn in inverse list order.
If no layers/style are found, an empty list is returned*/
virtual QList<QgsMapLayer*> mapLayerFromStyle( const QString& layerName, const QString& styleName, bool allowCaching = true ) const = 0;
If no layers/style are found, an empty list is returned
@param allowCache true if layer can be read from / written to cache*/
virtual QList<QgsMapLayer*> mapLayerFromStyle( const QString& layerName, const QString& styleName, bool useCache = true ) const = 0;

/**Returns number of layers in configuration*/
virtual int numberOfLayers() const = 0;
@@ -124,6 +126,17 @@ class QgsConfigParser
/**List of GML datasets passed outside SLD (e.g. in a SOAP request). Key of the map is the layer name*/
QMap<QString, QDomDocument*> mExternalGMLDatasets;

//todo: leave this to the layer cash?
/**Stores pointers to layers that have to be removed in the destructor of QgsSLDParser*/
mutable QList<QgsMapLayer*> mLayersToRemove;

/**Stores the temporary file objects. The class takes ownership of the objects and deletes them in the destructor*/
mutable QList<QTemporaryFile*> mFilesToRemove;

/**Stores paths of files that need to be removed after each request (necessary because of contours shapefiles that \
cannot be handles with QTemporaryFile*/
mutable QList<QString> mFilePathsToRemove;

/**Layer font for GetLegendGraphics*/
QFont mLegendLayerFont;
/**Item font for GetLegendGraphics*/
@@ -299,37 +299,37 @@ void QgsProjectParser::combineExtentAndCrsOfGroupChildren( QDomElement& groupEle
appendLayerBoundingBoxes( groupElem, doc, combinedBBox, groupCRS );
}

QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle( const QString& lName, const QString& styleName, bool allowCaching ) const
QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle( const QString& lName, const QString& styleName, bool useCache ) const
{
Q_UNUSED( styleName );
Q_UNUSED( allowCaching );
Q_UNUSED( useCache );
QList<QgsMapLayer*> layerList;

//first assume lName refers to a leaf layer
QMap< QString, QDomElement > layerElemMap = projectLayerElementsByName();
QMap< QString, QDomElement >::const_iterator layerElemIt = layerElemMap.find( lName );
if ( layerElemIt != layerElemMap.constEnd() )
{
QgsMapLayer* layer = createLayerFromElement( layerElemIt.value() );
if ( layer )
{
layerList.push_back( layer );
return layerList;
}
}

//Check if layer name refers to the top level group for the project.
if ( lName == projectTitle() )
{
QList<QDomElement> layerElemList = projectLayerElements();
QList<QDomElement>::const_iterator layerElemIt = layerElemList.constBegin();
for ( ; layerElemIt != layerElemList.constEnd(); ++layerElemIt )
{
layerList.push_back( createLayerFromElement( *layerElemIt ) );
layerList.push_back( createLayerFromElement( *layerElemIt, useCache ) );
}
return layerList;
}

//does lName refer to a leaf layer
QMap< QString, QDomElement > layerElemMap = projectLayerElementsByName();
QMap< QString, QDomElement >::const_iterator layerElemIt = layerElemMap.find( lName );
if ( layerElemIt != layerElemMap.constEnd() )
{
QgsMapLayer* layer = createLayerFromElement( layerElemIt.value(), useCache );
if ( layer )
{
layerList.push_back( layer );
return layerList;
}
}

//maybe the layer is a goup. Check if lName is contained in the group list
QMap< QString, QDomElement > idLayerMap = projectLayerElementsById();

@@ -367,7 +367,7 @@ QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle( const QString& lName, c
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] );
QgsMapLayer* pLayer = p->createLayerFromElement( pLayerElems[pLayerId], useCache );
if ( pLayer )
{
layerList.push_back( pLayer );
@@ -384,7 +384,7 @@ QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle( const QString& lName, c
QMap< QString, QDomElement >::const_iterator layerEntry = idLayerMap.find( layerFileList.at( i ).toElement().attribute( "layerid" ) );
if ( layerEntry != idLayerMap.constEnd() )
{
layerList.push_back( createLayerFromElement( layerEntry.value() ) );
layerList.push_back( createLayerFromElement( layerEntry.value(), useCache ) );
}
}
}
@@ -413,7 +413,7 @@ QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle( const QString& lName, c
{
if ( otherLayerIt.value().firstChildElement( "layername" ).text() == lName )
{
layerList.push_back( otherParser->createLayerFromElement( otherLayerIt.value() ) );
layerList.push_back( otherParser->createLayerFromElement( otherLayerIt.value(), useCache ) );
return layerList;
}
}
@@ -832,7 +832,7 @@ QMap< QString, QDomElement > QgsProjectParser::projectLayerElementsByName() cons
return layerMap;
}

QgsMapLayer* QgsProjectParser::createLayerFromElement( const QDomElement& elem ) const
QgsMapLayer* QgsProjectParser::createLayerFromElement( const QDomElement& elem, bool useCache ) const
{
if ( elem.isNull() || !mXMLDoc )
{
@@ -855,12 +855,14 @@ QgsMapLayer* QgsProjectParser::createLayerFromElement( const QDomElement& elem )
}

QString id = layerId( elem );
QgsMapLayer* layer = QgsMSLayerCache::instance()->searchLayer( absoluteUri, id );
QgsMapLayer* layer = 0;
if ( useCache )
{
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
QString error;
layer->readSymbology( elem, error );
return layer;
}

@@ -896,7 +898,14 @@ 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( absoluteUri, id, layer );
if ( useCache )
{
QgsMSLayerCache::instance()->insertLayer( absoluteUri, id, layer );
}
else
{
mLayersToRemove.push_back( layer );
}
}
return layer;
}
@@ -41,7 +41,7 @@ class QgsProjectParser: public QgsConfigParser
int numberOfLayers() const;

/**Returns one or possibly several maplayers for a given layer name and style. If no layers/style are found, an empty list is returned*/
virtual QList<QgsMapLayer*> mapLayerFromStyle( const QString& lName, const QString& styleName, bool allowCaching = true ) const;
virtual QList<QgsMapLayer*> mapLayerFromStyle( const QString& lName, const QString& styleName, bool useCache = true ) const;

/**Fills a layer and a style list. The two list have the same number of entries and the style and the layer at a position belong together (similar to the HTTP parameters 'Layers' and 'Styles'. Returns 0 in case of success*/
virtual int layersAndStyles( QStringList& layers, QStringList& styles ) const;
@@ -118,7 +118,7 @@ class QgsProjectParser: public QgsConfigParser
QMap< QString, QDomElement > projectLayerElementsByName() const;
/**Creates a maplayer object from <maplayer> element. The layer cash owns the maplayer, so don't delete it
@return the maplayer or 0 in case of error*/
QgsMapLayer* createLayerFromElement( const QDomElement& elem ) const;
QgsMapLayer* createLayerFromElement( const QDomElement& elem, bool useCache = true ) const;
/**Returns the text of the <id> element for a layer element
@return id or a null string in case of error*/
QString layerId( const QDomElement& layerElem ) const;
@@ -121,25 +121,6 @@ QgsSLDParser::QgsSLDParser(): mXMLDoc( 0 )

QgsSLDParser::~QgsSLDParser()
{
//remove the temporary files
for ( QList<QTemporaryFile*>::const_iterator it = mFilesToRemove.constBegin(); it != mFilesToRemove.constEnd(); ++it )
{
delete *it;
}

//and also those the temporary file paths
for ( QList<QString>::const_iterator it = mFilePathsToRemove.constBegin(); it != mFilePathsToRemove.constEnd(); ++it )
{
QFile::remove( *it );
}

//delete the layers in the list
QList<QgsMapLayer*>::iterator layer_it = mLayersToRemove.begin();
for ( ; layer_it != mLayersToRemove.end(); ++layer_it )
{
delete *layer_it;
}

delete mXMLDoc;
}

@@ -275,7 +256,7 @@ void QgsSLDParser::layersAndStylesCapabilities( QDomElement& parentElement, QDom
}
}

QList<QgsMapLayer*> QgsSLDParser::mapLayerFromStyle( const QString& layerName, const QString& styleName, bool allowCaching ) const
QList<QgsMapLayer*> QgsSLDParser::mapLayerFromStyle( const QString& layerName, const QString& styleName, bool useCache ) const
{
QList<QgsMapLayer*> fallbackLayerList;
QList<QgsMapLayer*> resultList;
@@ -287,7 +268,7 @@ QList<QgsMapLayer*> QgsSLDParser::mapLayerFromStyle( const QString& layerName, c
QDomElement userStyleElement = findUserStyleElement( namedLayerElemList[i], styleName );
if ( !userStyleElement.isNull() )
{
fallbackLayerList = mFallbackParser->mapLayerFromStyle( layerName, "", allowCaching );
fallbackLayerList = mFallbackParser->mapLayerFromStyle( layerName, "", false );
if ( fallbackLayerList.size() > 0 )
{
QgsVectorLayer* v = dynamic_cast<QgsVectorLayer*>( fallbackLayerList.at( 0 ) );
@@ -335,7 +316,7 @@ QList<QgsMapLayer*> QgsSLDParser::mapLayerFromStyle( const QString& layerName, c
//maybe named layer and named style is defined in the fallback SLD?
if ( mFallbackParser )
{
resultList = mFallbackParser->mapLayerFromStyle( layerName, styleName, allowCaching );
resultList = mFallbackParser->mapLayerFromStyle( layerName, styleName, useCache );
}

QList<QgsMapLayer*>::iterator it = resultList.begin();
@@ -349,7 +330,7 @@ QList<QgsMapLayer*> QgsSLDParser::mapLayerFromStyle( const QString& layerName, c

QDomElement userStyleElement = findUserStyleElement( userLayerElement, styleName );

QgsMapLayer* theMapLayer = mapLayerFromUserLayer( userLayerElement, layerName, allowCaching );
QgsMapLayer* theMapLayer = mapLayerFromUserLayer( userLayerElement, layerName, useCache );
if ( !theMapLayer )
{
return resultList;
@@ -34,7 +34,6 @@ class QgsRenderer;
#include <map>
#include <QList>
#include <QString>
#include <QTemporaryFile>

#ifdef DIAGRAMSERVER
#include "qgsdiagramcategory.h"
@@ -61,7 +60,7 @@ class QgsSLDParser: public QgsConfigParser
int numberOfLayers() const;

/**Returns one or possibly several maplayers for a given layer name and style. If no layers/style are found, an empty list is returned*/
QList<QgsMapLayer*> mapLayerFromStyle( const QString& layerName, const QString& styleName, bool allowCaching = true ) const;
QList<QgsMapLayer*> mapLayerFromStyle( const QString& layerName, const QString& styleName, bool useCache = true ) const;

/**Fills a layer and a style list. The two list have the same number of entries and the style and the layer at a position belong together (similar to the HTTP parameters 'Layers' and 'Styles'. Returns 0 in case of success*/
int layersAndStyles( QStringList& layers, QStringList& styles ) const;
@@ -144,25 +143,12 @@ class QgsSLDParser: public QgsConfigParser
double scaleFactorFromScaleTag( const QDomElement& scaleElem ) const;
#endif //DIAGRAMSERVER



/**SLD as dom document*/
QDomDocument* mXMLDoc;

/**Map containing the WMS parameters of the request*/
std::map<QString, QString> mParameterMap;

//todo: leave this to the layer cash?
/**Stores pointers to layers that have to be removed in the destructor of QgsSLDParser*/
mutable QList<QgsMapLayer*> mLayersToRemove;


/**Stores the temporary file objects. The class takes ownership of the objects and deletes them in the destructor*/
mutable QList<QTemporaryFile*> mFilesToRemove;
/**Stores paths of files that need to be removed after each request (necessary because of contours shapefiles that \
cannot be handles with QTemporaryFile*/
mutable QList<QString> mFilePathsToRemove;

QString mSLDNamespace;
};

0 comments on commit b3bebf3

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