Skip to content

Commit b3bebf3

Browse files
author
marco
committedOct 15, 2011
WMS server: avoid reading layer symbology if the request does not come from SLD
1 parent 54754f1 commit b3bebf3

File tree

6 files changed

+75
-67
lines changed

6 files changed

+75
-67
lines changed
 

‎src/mapserver/qgsconfigparser.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ QgsConfigParser::~QgsConfigParser()
4242
{
4343
delete it.value();
4444
}
45+
46+
//remove the temporary files
47+
for ( QList<QTemporaryFile*>::const_iterator it = mFilesToRemove.constBegin(); it != mFilesToRemove.constEnd(); ++it )
48+
{
49+
delete *it;
50+
}
51+
52+
//and also those the temporary file paths
53+
for ( QList<QString>::const_iterator it = mFilePathsToRemove.constBegin(); it != mFilePathsToRemove.constEnd(); ++it )
54+
{
55+
QFile::remove( *it );
56+
}
57+
58+
//delete the layers in the list
59+
QList<QgsMapLayer*>::iterator layer_it = mLayersToRemove.begin();
60+
for ( ; layer_it != mLayersToRemove.end(); ++layer_it )
61+
{
62+
delete *layer_it;
63+
}
4564
}
4665

4766
void QgsConfigParser::setDefaultLegendSettings()

‎src/mapserver/qgsconfigparser.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <QFont>
2424
#include <QList>
2525
#include <QSet>
26+
#include <QTemporaryFile>
2627

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

4344
/**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.
44-
If no layers/style are found, an empty list is returned*/
45-
virtual QList<QgsMapLayer*> mapLayerFromStyle( const QString& layerName, const QString& styleName, bool allowCaching = true ) const = 0;
45+
If no layers/style are found, an empty list is returned
46+
@param allowCache true if layer can be read from / written to cache*/
47+
virtual QList<QgsMapLayer*> mapLayerFromStyle( const QString& layerName, const QString& styleName, bool useCache = true ) const = 0;
4648

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

129+
//todo: leave this to the layer cash?
130+
/**Stores pointers to layers that have to be removed in the destructor of QgsSLDParser*/
131+
mutable QList<QgsMapLayer*> mLayersToRemove;
132+
133+
/**Stores the temporary file objects. The class takes ownership of the objects and deletes them in the destructor*/
134+
mutable QList<QTemporaryFile*> mFilesToRemove;
135+
136+
/**Stores paths of files that need to be removed after each request (necessary because of contours shapefiles that \
137+
cannot be handles with QTemporaryFile*/
138+
mutable QList<QString> mFilePathsToRemove;
139+
127140
/**Layer font for GetLegendGraphics*/
128141
QFont mLegendLayerFont;
129142
/**Item font for GetLegendGraphics*/

‎src/mapserver/qgsprojectparser.cpp

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -299,37 +299,37 @@ void QgsProjectParser::combineExtentAndCrsOfGroupChildren( QDomElement& groupEle
299299
appendLayerBoundingBoxes( groupElem, doc, combinedBBox, groupCRS );
300300
}
301301

302-
QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle( const QString& lName, const QString& styleName, bool allowCaching ) const
302+
QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle( const QString& lName, const QString& styleName, bool useCache ) const
303303
{
304304
Q_UNUSED( styleName );
305-
Q_UNUSED( allowCaching );
305+
Q_UNUSED( useCache );
306306
QList<QgsMapLayer*> layerList;
307307

308-
//first assume lName refers to a leaf layer
309-
QMap< QString, QDomElement > layerElemMap = projectLayerElementsByName();
310-
QMap< QString, QDomElement >::const_iterator layerElemIt = layerElemMap.find( lName );
311-
if ( layerElemIt != layerElemMap.constEnd() )
312-
{
313-
QgsMapLayer* layer = createLayerFromElement( layerElemIt.value() );
314-
if ( layer )
315-
{
316-
layerList.push_back( layer );
317-
return layerList;
318-
}
319-
}
320-
321308
//Check if layer name refers to the top level group for the project.
322309
if ( lName == projectTitle() )
323310
{
324311
QList<QDomElement> layerElemList = projectLayerElements();
325312
QList<QDomElement>::const_iterator layerElemIt = layerElemList.constBegin();
326313
for ( ; layerElemIt != layerElemList.constEnd(); ++layerElemIt )
327314
{
328-
layerList.push_back( createLayerFromElement( *layerElemIt ) );
315+
layerList.push_back( createLayerFromElement( *layerElemIt, useCache ) );
329316
}
330317
return layerList;
331318
}
332319

320+
//does lName refer to a leaf layer
321+
QMap< QString, QDomElement > layerElemMap = projectLayerElementsByName();
322+
QMap< QString, QDomElement >::const_iterator layerElemIt = layerElemMap.find( lName );
323+
if ( layerElemIt != layerElemMap.constEnd() )
324+
{
325+
QgsMapLayer* layer = createLayerFromElement( layerElemIt.value(), useCache );
326+
if ( layer )
327+
{
328+
layerList.push_back( layer );
329+
return layerList;
330+
}
331+
}
332+
333333
//maybe the layer is a goup. Check if lName is contained in the group list
334334
QMap< QString, QDomElement > idLayerMap = projectLayerElementsById();
335335

@@ -367,7 +367,7 @@ QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle( const QString& lName, c
367367
for ( int i = 0; i < pLayerNodes.size(); ++i )
368368
{
369369
QString pLayerId = pLayerNodes.at( i ).toElement().firstChildElement( "filegroup" ).firstChildElement( "legendlayerfile" ).attribute( "layerid" );
370-
QgsMapLayer* pLayer = p->createLayerFromElement( pLayerElems[pLayerId] );
370+
QgsMapLayer* pLayer = p->createLayerFromElement( pLayerElems[pLayerId], useCache );
371371
if ( pLayer )
372372
{
373373
layerList.push_back( pLayer );
@@ -384,7 +384,7 @@ QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle( const QString& lName, c
384384
QMap< QString, QDomElement >::const_iterator layerEntry = idLayerMap.find( layerFileList.at( i ).toElement().attribute( "layerid" ) );
385385
if ( layerEntry != idLayerMap.constEnd() )
386386
{
387-
layerList.push_back( createLayerFromElement( layerEntry.value() ) );
387+
layerList.push_back( createLayerFromElement( layerEntry.value(), useCache ) );
388388
}
389389
}
390390
}
@@ -413,7 +413,7 @@ QList<QgsMapLayer*> QgsProjectParser::mapLayerFromStyle( const QString& lName, c
413413
{
414414
if ( otherLayerIt.value().firstChildElement( "layername" ).text() == lName )
415415
{
416-
layerList.push_back( otherParser->createLayerFromElement( otherLayerIt.value() ) );
416+
layerList.push_back( otherParser->createLayerFromElement( otherLayerIt.value(), useCache ) );
417417
return layerList;
418418
}
419419
}
@@ -832,7 +832,7 @@ QMap< QString, QDomElement > QgsProjectParser::projectLayerElementsByName() cons
832832
return layerMap;
833833
}
834834

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

857857
QString id = layerId( elem );
858-
QgsMapLayer* layer = QgsMSLayerCache::instance()->searchLayer( absoluteUri, id );
858+
QgsMapLayer* layer = 0;
859+
if ( useCache )
860+
{
861+
layer = QgsMSLayerCache::instance()->searchLayer( absoluteUri, id );
862+
}
863+
859864
if ( layer )
860865
{
861-
//reading symbology every time is necessary because it could have been changed by a user SLD based request
862-
QString error;
863-
layer->readSymbology( elem, error );
864866
return layer;
865867
}
866868

@@ -896,7 +898,14 @@ QgsMapLayer* QgsProjectParser::createLayerFromElement( const QDomElement& elem )
896898
{
897899
layer->readXML( const_cast<QDomElement&>( elem ) ); //should be changed to const in QgsMapLayer
898900
layer->setLayerName( layerName( elem ) );
899-
QgsMSLayerCache::instance()->insertLayer( absoluteUri, id, layer );
901+
if ( useCache )
902+
{
903+
QgsMSLayerCache::instance()->insertLayer( absoluteUri, id, layer );
904+
}
905+
else
906+
{
907+
mLayersToRemove.push_back( layer );
908+
}
900909
}
901910
return layer;
902911
}

‎src/mapserver/qgsprojectparser.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class QgsProjectParser: public QgsConfigParser
4141
int numberOfLayers() const;
4242

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

4646
/**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*/
4747
virtual int layersAndStyles( QStringList& layers, QStringList& styles ) const;
@@ -118,7 +118,7 @@ class QgsProjectParser: public QgsConfigParser
118118
QMap< QString, QDomElement > projectLayerElementsByName() const;
119119
/**Creates a maplayer object from <maplayer> element. The layer cash owns the maplayer, so don't delete it
120120
@return the maplayer or 0 in case of error*/
121-
QgsMapLayer* createLayerFromElement( const QDomElement& elem ) const;
121+
QgsMapLayer* createLayerFromElement( const QDomElement& elem, bool useCache = true ) const;
122122
/**Returns the text of the <id> element for a layer element
123123
@return id or a null string in case of error*/
124124
QString layerId( const QDomElement& layerElem ) const;

‎src/mapserver/qgssldparser.cpp

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -121,25 +121,6 @@ QgsSLDParser::QgsSLDParser(): mXMLDoc( 0 )
121121

122122
QgsSLDParser::~QgsSLDParser()
123123
{
124-
//remove the temporary files
125-
for ( QList<QTemporaryFile*>::const_iterator it = mFilesToRemove.constBegin(); it != mFilesToRemove.constEnd(); ++it )
126-
{
127-
delete *it;
128-
}
129-
130-
//and also those the temporary file paths
131-
for ( QList<QString>::const_iterator it = mFilePathsToRemove.constBegin(); it != mFilePathsToRemove.constEnd(); ++it )
132-
{
133-
QFile::remove( *it );
134-
}
135-
136-
//delete the layers in the list
137-
QList<QgsMapLayer*>::iterator layer_it = mLayersToRemove.begin();
138-
for ( ; layer_it != mLayersToRemove.end(); ++layer_it )
139-
{
140-
delete *layer_it;
141-
}
142-
143124
delete mXMLDoc;
144125
}
145126

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

278-
QList<QgsMapLayer*> QgsSLDParser::mapLayerFromStyle( const QString& layerName, const QString& styleName, bool allowCaching ) const
259+
QList<QgsMapLayer*> QgsSLDParser::mapLayerFromStyle( const QString& layerName, const QString& styleName, bool useCache ) const
279260
{
280261
QList<QgsMapLayer*> fallbackLayerList;
281262
QList<QgsMapLayer*> resultList;
@@ -287,7 +268,7 @@ QList<QgsMapLayer*> QgsSLDParser::mapLayerFromStyle( const QString& layerName, c
287268
QDomElement userStyleElement = findUserStyleElement( namedLayerElemList[i], styleName );
288269
if ( !userStyleElement.isNull() )
289270
{
290-
fallbackLayerList = mFallbackParser->mapLayerFromStyle( layerName, "", allowCaching );
271+
fallbackLayerList = mFallbackParser->mapLayerFromStyle( layerName, "", false );
291272
if ( fallbackLayerList.size() > 0 )
292273
{
293274
QgsVectorLayer* v = dynamic_cast<QgsVectorLayer*>( fallbackLayerList.at( 0 ) );
@@ -335,7 +316,7 @@ QList<QgsMapLayer*> QgsSLDParser::mapLayerFromStyle( const QString& layerName, c
335316
//maybe named layer and named style is defined in the fallback SLD?
336317
if ( mFallbackParser )
337318
{
338-
resultList = mFallbackParser->mapLayerFromStyle( layerName, styleName, allowCaching );
319+
resultList = mFallbackParser->mapLayerFromStyle( layerName, styleName, useCache );
339320
}
340321

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

350331
QDomElement userStyleElement = findUserStyleElement( userLayerElement, styleName );
351332

352-
QgsMapLayer* theMapLayer = mapLayerFromUserLayer( userLayerElement, layerName, allowCaching );
333+
QgsMapLayer* theMapLayer = mapLayerFromUserLayer( userLayerElement, layerName, useCache );
353334
if ( !theMapLayer )
354335
{
355336
return resultList;

‎src/mapserver/qgssldparser.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class QgsRenderer;
3434
#include <map>
3535
#include <QList>
3636
#include <QString>
37-
#include <QTemporaryFile>
3837

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

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

6665
/**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*/
6766
int layersAndStyles( QStringList& layers, QStringList& styles ) const;
@@ -144,25 +143,12 @@ class QgsSLDParser: public QgsConfigParser
144143
double scaleFactorFromScaleTag( const QDomElement& scaleElem ) const;
145144
#endif //DIAGRAMSERVER
146145

147-
148-
149146
/**SLD as dom document*/
150147
QDomDocument* mXMLDoc;
151148

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

155-
//todo: leave this to the layer cash?
156-
/**Stores pointers to layers that have to be removed in the destructor of QgsSLDParser*/
157-
mutable QList<QgsMapLayer*> mLayersToRemove;
158-
159-
160-
/**Stores the temporary file objects. The class takes ownership of the objects and deletes them in the destructor*/
161-
mutable QList<QTemporaryFile*> mFilesToRemove;
162-
/**Stores paths of files that need to be removed after each request (necessary because of contours shapefiles that \
163-
cannot be handles with QTemporaryFile*/
164-
mutable QList<QString> mFilePathsToRemove;
165-
166152
QString mSLDNamespace;
167153
};
168154

0 commit comments

Comments
 (0)
Please sign in to comment.