Skip to content
Permalink
Browse files
More flexible limit of layer number in qgis server depending on numbe…
…r of layers in project. Fixes bug #3502

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15707 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Apr 15, 2011
1 parent 9fec25b commit 6129c4ea38224c097e15a65d2a3b837a5da32859
@@ -17,6 +17,7 @@

#include "qgsconfigcache.h"
#include "qgsmapserverlogger.h"
#include "qgsmslayercache.h"
#include "qgsprojectparser.h"
#include "qgssldparser.h"

@@ -35,17 +36,25 @@ QgsConfigCache::~QgsConfigCache()

QgsConfigParser* QgsConfigCache::searchConfiguration( const QString& filePath )
{
QgsConfigParser* p = 0;
QMap<QString, QgsConfigParser*>::const_iterator configIt = mCachedConfigurations.find( filePath );
if ( configIt == mCachedConfigurations.constEnd() )
{
QgsMSDebugMsg( "Create new configuration" );
return insertConfiguration( filePath );
p = insertConfiguration( filePath );
}
else
{
QgsMSDebugMsg( "Return configuration from cache" );
return configIt.value();
p = configIt.value();
}

if ( p )
{
//there could be more layers in a project than allowed by the cache per default
QgsMSLayerCache::instance()->setProjectMaxLayers( p->numberOfLayers() );
}
return p;
}

QgsConfigParser* QgsConfigCache::insertConfiguration( const QString& filePath )
@@ -44,6 +44,9 @@ class QgsConfigParser
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;

/**Returns number of layers in configuration*/
virtual int numberOfLayers() const = 0;

/**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 = 0;

@@ -20,7 +20,7 @@
#include "qgsmapserverlogger.h"

//maximum number of layers in the cache (and upper limit for layers in one published project)
#define MAX_N_LAYERS 100
#define DEFAULT_MAX_N_LAYERS 50

QgsMSLayerCache* QgsMSLayerCache::mInstance = 0;

@@ -52,7 +52,7 @@ QgsMSLayerCache::~QgsMSLayerCache()
void QgsMSLayerCache::insertLayer( const QString& url, const QString& layerName, QgsMapLayer* layer, const QList<QString>& tempFiles )
{
QgsMSDebugMsg( "inserting layer" );
if ( mEntries.size() > MAX_N_LAYERS ) //force cache layer examination after 10 inserted layers
if ( mEntries.size() > std::max( DEFAULT_MAX_N_LAYERS, mProjectMaxLayers ) ) //force cache layer examination after 10 inserted layers
{
updateEntries();
}
@@ -102,7 +102,7 @@ QgsMapLayer* QgsMSLayerCache::searchLayer( const QString& url, const QString& la
void QgsMSLayerCache::updateEntries()
{
QgsMSDebugMsg( "updateEntries" );
int entriesToDelete = mEntries.size() - MAX_N_LAYERS;
int entriesToDelete = mEntries.size() - std::max( DEFAULT_MAX_N_LAYERS, mProjectMaxLayers );
if ( entriesToDelete < 1 )
{
return;
@@ -51,6 +51,10 @@ class QgsMSLayerCache
@return a pointer to the layer or 0 if no such layer*/
QgsMapLayer* searchLayer( const QString& url, const QString& layerName );

int projectsMaxLayers() const { return mProjectMaxLayers; }

void setProjectMaxLayers( int n ) { mProjectMaxLayers = n; }

protected:
/**Protected singleton constructor*/
QgsMSLayerCache();
@@ -70,6 +74,9 @@ class QgsMSLayerCache
url is used several time in a request. It ensures that different layer instances are created for different
layer names*/
QHash<QPair<QString, QString>, QgsMSLayerCacheEntry> mEntries;

/**Maximum number of layers in the cache, overrides DEFAULT_MAX_N_LAYERS if larger*/
int mProjectMaxLayers;
};

#endif
@@ -47,6 +47,12 @@ QgsProjectParser::~QgsProjectParser()
delete mXMLDoc;
}

int QgsProjectParser::numberOfLayers() const
{
QList<QDomElement> layerElems = projectLayerElements();
return layerElems.size();
}

void QgsProjectParser::layersAndStylesCapabilities( QDomElement& parentElement, QDomDocument& doc ) const
{
QList<QDomElement> layerElems = projectLayerElements();
@@ -38,6 +38,8 @@ class QgsProjectParser: public QgsConfigParser
/**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.*/
virtual void layersAndStylesCapabilities( QDomElement& parentElement, QDomDocument& doc ) const;

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;

@@ -142,6 +142,23 @@ QgsSLDParser::~QgsSLDParser()
delete mXMLDoc;
}

int QgsSLDParser::numberOfLayers() const
{
if ( !mXMLDoc )
{
return 0;
}

QDomElement sldElem = mXMLDoc->documentElement().toElement();
if ( sldElem.isNull() )
{
return 0;
}
QDomNodeList userLayerList = sldElem.elementsByTagName( "UserLayer" );
QDomNodeList namedLayerList = sldElem.elementsByTagName( "NamedLayer" );
return ( userLayerList.size() + namedLayerList.size() );
}

void QgsSLDParser::layersAndStylesCapabilities( QDomElement& parentElement, QDomDocument& doc ) const
{
//iterate over all <UserLayer> nodes
@@ -57,6 +57,9 @@ class QgsSLDParser: public QgsConfigParser
/**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.*/
void layersAndStylesCapabilities( QDomElement& parentElement, QDomDocument& doc ) const;

/**Returns number of layers in configuration*/
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;

0 comments on commit 6129c4e

Please sign in to comment.