Skip to content
Permalink
Browse files

Port server to expression contexts

  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent a1e21f3 commit 786de4b8206ec6bd3b234db37570dc6006d6b517
Showing with 30 additions and 8 deletions.
  1. +19 −4 src/server/qgswfsserver.cpp
  2. +11 −4 src/server/qgswmsserver.cpp
@@ -393,6 +393,10 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format
long featureCounter = 0;
int layerPrec = 8;

QgsExpressionContext expressionContext;
expressionContext << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope();

QDomDocument doc;
QString errorMsg;
if ( doc.setContent( mParameters.value( "REQUEST_BODY" ), true, &errorMsg ) )
@@ -434,6 +438,8 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format
QgsVectorLayer* layer = dynamic_cast<QgsVectorLayer*>( currentLayer );
if ( layer && wfsLayersId.contains( layer->id() ) )
{
expressionContext << QgsExpressionContextUtils::layerScope( layer );

//is there alias info for this vector layer?
QMap< int, QString > layerAliasInfo;
const QMap< QString, QString >& aliasMap = layer->attributeAliases();
@@ -599,7 +605,9 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format
}
while ( fit.nextFeature( feature ) && ( maxFeatures == -1 || featureCounter < maxFeat ) )
{
QVariant res = filter->evaluate( &feature, fields );
expressionContext.setFeature( feature );

QVariant res = filter->evaluate( &expressionContext );
if ( filter->hasEvalError() )
{
throw QgsMapServiceException( "RequestNotWellFormed", filter->evalErrorString() );
@@ -783,6 +791,8 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format
QgsVectorLayer* layer = dynamic_cast<QgsVectorLayer*>( currentLayer );
if ( layer && wfsLayersId.contains( layer->id() ) )
{
expressionContext << QgsExpressionContextUtils::layerScope( layer );

//is there alias info for this vector layer?
QMap< int, QString > layerAliasInfo;
const QMap< QString, QString >& aliasMap = layer->attributeAliases();
@@ -908,7 +918,8 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format
}
while ( fit.nextFeature( feature ) && ( maxFeatures == -1 || featureCounter < maxFeat ) )
{
QVariant res = filter->evaluate( &feature, fields );
expressionContext.setFeature( feature );
QVariant res = filter->evaluate( &expressionContext );
if ( filter->hasEvalError() )
{
throw QgsMapServiceException( "RequestNotWellFormed", QString( "Expression filter eval error message: %1." ).arg( filter->evalErrorString() ) );
@@ -1026,7 +1037,8 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format
QgsFeatureIterator fit = layer->getFeatures( req );
while ( fit.nextFeature( feature ) && ( maxFeatures == -1 || featureCounter < maxFeat ) )
{
QVariant res = filter->evaluate( &feature, fields );
expressionContext.setFeature( feature );
QVariant res = filter->evaluate( &expressionContext );
if ( filter->hasEvalError() )
{
throw QgsMapServiceException( "RequestNotWellFormed", QString( "OGC expression filter eval error message: %1." ).arg( filter->evalErrorString() ) );
@@ -1662,9 +1674,12 @@ QgsFeatureIds QgsWFSServer::getFeatureIdsFromFilter( QDomElement filterElem, Qgs
QgsFeature feature;
const QgsFields& fields = provider->fields();
QgsFeatureIterator fit = layer->getFeatures();
QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( feature, fields );

while ( fit.nextFeature( feature ) )
{
QVariant res = filter->evaluate( &feature, fields );
context.setFeature( feature );
QVariant res = filter->evaluate( &context );
if ( filter->hasEvalError() )
{
throw QgsMapServiceException( "RequestNotWellFormed", filter->evalErrorString() );
@@ -2143,7 +2143,7 @@ int QgsWMSServer::featureInfoFromVectorLayer( QgsVectorLayer* layer,
attributeElement.setAttribute( "value",
replaceValueMapAndRelation(
layer, i,
featureAttributes[i].isNull() ? QString::null : QgsExpression::replaceExpressionText( featureAttributes[i].toString(), &feature, layer )
featureAttributes[i].isNull() ? QString::null : QgsExpression::replaceExpressionText( featureAttributes[i].toString(), &renderContext.expressionContext() )
)
);
featureElement.appendChild( attributeElement );
@@ -2157,7 +2157,7 @@ int QgsWMSServer::featureInfoFromVectorLayer( QgsVectorLayer* layer,
{
QDomElement maptipElem = infoDocument.createElement( "Attribute" );
maptipElem.setAttribute( "name", "maptip" );
maptipElem.setAttribute( "value", QgsExpression::replaceExpressionText( displayField, &feature, layer ) );
maptipElem.setAttribute( "value", QgsExpression::replaceExpressionText( displayField, &renderContext.expressionContext() ) );
featureElement.appendChild( maptipElem );
}
}
@@ -3001,6 +3001,13 @@ QDomElement QgsWMSServer::createFeatureGML(

QgsGeometry* geom = feat->geometry();

QgsExpressionContext expressionContext;
expressionContext << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope();
if ( layer )
expressionContext << QgsExpressionContextUtils::layerScope( layer );
expressionContext.setFeature( *feat );

// always add bounding box info if feature contains geometry
if ( geom && geom->type() != QGis::UnknownGeometry && geom->type() != QGis::NoGeometry )
{
@@ -3083,7 +3090,7 @@ QDomElement QgsWMSServer::createFeatureGML(
QString fieldTextString = featureAttributes[i].toString();
if ( layer )
{
fieldTextString = replaceValueMapAndRelation( layer, i, QgsExpression::replaceExpressionText( fieldTextString, feat, layer ) );
fieldTextString = replaceValueMapAndRelation( layer, i, QgsExpression::replaceExpressionText( fieldTextString, &expressionContext ) );
}
QDomText fieldText = doc.createTextNode( fieldTextString );
fieldElem.appendChild( fieldText );
@@ -3096,7 +3103,7 @@ QDomElement QgsWMSServer::createFeatureGML(
QString displayField = layer->displayField();
if ( !displayField.isEmpty() )
{
QString fieldTextString = QgsExpression::replaceExpressionText( displayField, feat, layer );
QString fieldTextString = QgsExpression::replaceExpressionText( displayField, &expressionContext );
QDomElement fieldElem = doc.createElement( "qgs:maptip" );
QDomText maptipText = doc.createTextNode( fieldTextString );
fieldElem.appendChild( maptipText );

0 comments on commit 786de4b

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