Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added wms filter support
  • Loading branch information
mhugent committed May 10, 2011
1 parent 1455636 commit d05b140
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/mapserver/qgswmsserver.cpp
Expand Up @@ -350,6 +350,8 @@ QByteArray* QgsWMSServer::getPrint( const QString& formatString )
}
delete theImage;

QMap<QString, QString> originalLayerFilters = applyRequestedLayerFilters( layersList, layerIdList );

//GetPrint request needs a template parameter
std::map<QString, QString>::const_iterator templateIt = mParameterMap.find( "TEMPLATE" );
if ( templateIt == mParameterMap.end() )
Expand All @@ -360,6 +362,7 @@ QByteArray* QgsWMSServer::getPrint( const QString& formatString )
QgsComposition* c = mConfigParser->createPrintComposition( templateIt->second, mMapRenderer, QMap<QString, QString>( mParameterMap ) );
if ( !c )
{
restoreLayerFilters( originalLayerFilters );
return 0;
}

Expand Down Expand Up @@ -416,6 +419,7 @@ QByteArray* QgsWMSServer::getPrint( const QString& formatString )
if ( !tempFile.open() )
{
delete c;
restoreLayerFilters( originalLayerFilters );
return 0;
}

Expand Down Expand Up @@ -450,6 +454,7 @@ QByteArray* QgsWMSServer::getPrint( const QString& formatString )
{
throw QgsMapServiceException( "InvalidFormat", "Output format '" + formatString + "' is not supported in the GetPrint request" );
}
restoreLayerFilters( originalLayerFilters );

delete c;
return ba;
Expand Down Expand Up @@ -478,7 +483,10 @@ QImage* QgsWMSServer::getMap()

QPainter thePainter( theImage );
thePainter.setRenderHint( QPainter::Antialiasing ); //make it look nicer

QMap<QString, QString> originalLayerFilters = applyRequestedLayerFilters( layersList, layerIdList );
mMapRenderer->render( &thePainter );
restoreLayerFilters( originalLayerFilters );

QgsMapLayerRegistry::instance()->mapLayers().clear();
return theImage;
Expand Down Expand Up @@ -1500,3 +1508,76 @@ void QgsWMSServer::drawRasterSymbol( QgsComposerLegendItem* item, QPainter* p, d
}
p->setPen( savedPen );
}

QMap<QString, QString> QgsWMSServer::applyRequestedLayerFilters( const QStringList& layerList, const QStringList& layerIds ) const
{
QMap<QString, QString> filterMap;

if ( layerList.isEmpty() || layerIds.isEmpty() )
{
return filterMap;
}

std::map<QString, QString>::const_iterator filterIt = mParameterMap.find( "FILTER" );
if ( filterIt != mParameterMap.end() )
{
QString filterParameter = filterIt->second;
QStringList layerSplit = filterParameter.split( "," );
QStringList::const_iterator layerIt = layerSplit.constBegin();
for ( ; layerIt != layerSplit.constEnd(); ++layerIt )
{
QStringList eqSplit = layerIt->split( ":" );
if ( eqSplit.size() < 2 )
{
continue;
}

//we know the layer name, but need to go through the list because a layer could be there several times...
int listPos = 1;
QStringList::const_iterator layerIt = layerList.constBegin();
for ( ; layerIt != layerList.constEnd(); ++layerIt )
{
if ( *layerIt == eqSplit.at( 0 ) )
{
QString layerId = layerIds.at( layerIds.size() - listPos );
QgsVectorLayer* filteredLayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
if ( filteredLayer )
{
QgsVectorDataProvider* dp = filteredLayer->dataProvider();
if ( dp )
{
filterMap.insert( layerId, dp->subsetString() );
}

QString newSubsetString = eqSplit.at( 1 );
if ( !dp->subsetString().isEmpty() )
{
newSubsetString.prepend( " AND " );
newSubsetString.prepend( dp->subsetString() );
}
dp->setSubsetString( newSubsetString );
}
}
++listPos;
}
}
}
return filterMap;
}

void QgsWMSServer::restoreLayerFilters( const QMap < QString, QString >& filterMap ) const
{
QMap < QString, QString >::const_iterator filterIt = filterMap.constBegin();
for ( ; filterIt != filterMap.constEnd(); ++filterIt )
{
QgsVectorLayer* filteredLayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( filterIt.key() ) );
if ( filteredLayer )
{
QgsVectorDataProvider* dp = filteredLayer->dataProvider();
if ( dp )
{
dp->setSubsetString( filterIt.value() );
}
}
}
}
6 changes: 6 additions & 0 deletions src/mapserver/qgswmsserver.h
Expand Up @@ -138,6 +138,12 @@ class QgsWMSServer

QImage* printCompositionToImage( QgsComposition* c ) const;

/**Apply filter (subset) strings from the request to the layers. Example: '&FILTER=<layer1>:"AND property > 100",<layer2>:"AND bla = 'hallo!'" '
@return a map with the original filters ( layer id / filter string )*/
QMap<QString, QString> applyRequestedLayerFilters( const QStringList& layerList, const QStringList& layerIds ) const;
/**Restores the original layer filters*/
void restoreLayerFilters( const QMap < QString, QString >& filterMap ) const;

/**Map containing the WMS parameters*/
std::map<QString, QString> mParameterMap;
QgsConfigParser* mConfigParser;
Expand Down

0 comments on commit d05b140

Please sign in to comment.