Skip to content

Commit

Permalink
WMS server: avoid reading layer symbology if the request does not com…
Browse files Browse the repository at this point in the history
…e from SLD
  • Loading branch information
marco committed Oct 15, 2011
1 parent 54754f1 commit b3bebf3
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 67 deletions.
19 changes: 19 additions & 0 deletions src/mapserver/qgsconfigparser.cpp
Expand Up @@ -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()
Expand Down
17 changes: 15 additions & 2 deletions src/mapserver/qgsconfigparser.h
Expand Up @@ -23,6 +23,7 @@
#include <QFont>
#include <QList>
#include <QSet>
#include <QTemporaryFile>

class QgsComposition;
class QgsComposerLabel;
Expand All @@ -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;
Expand Down Expand Up @@ -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*/
Expand Down
59 changes: 34 additions & 25 deletions src/mapserver/qgsprojectparser.cpp
Expand Up @@ -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();

Expand Down Expand Up @@ -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 );
Expand All @@ -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 ) );
}
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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 )
{
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/mapserver/qgsprojectparser.h
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
27 changes: 4 additions & 23 deletions src/mapserver/qgssldparser.cpp
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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 ) );
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down
16 changes: 1 addition & 15 deletions src/mapserver/qgssldparser.h
Expand Up @@ -34,7 +34,6 @@ class QgsRenderer;
#include <map>
#include <QList>
#include <QString>
#include <QTemporaryFile>

#ifdef DIAGRAMSERVER
#include "qgsdiagramcategory.h"
Expand All @@ -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;
Expand Down Expand Up @@ -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;
};

Expand Down

0 comments on commit b3bebf3

Please sign in to comment.