Skip to content

Commit 19b0dbc

Browse files
author
Marco Hugentobler
committed
Implement attribute restrictions in wms/wfs servers
1 parent a02225c commit 19b0dbc

7 files changed

+77
-59
lines changed

src/mapserver/qgsconfigparser.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,11 @@ class QgsConfigParser
109109
Default implementation returns an empty map*/
110110
virtual QMap< QString, QMap< int, QString > > layerAliasInfo() const { return QMap< QString, QMap<int, QString> > (); }
111111

112-
/**Returns information about vector attributes with hidden edit type. Key is layer id, value is a set containing the names of the hidden attributes*/
113-
virtual QMap< QString, QSet<QString> > hiddenAttributes() const { return QMap< QString, QSet<QString> >(); }
112+
/**Returns attributes excluded from WMS publication. Key is layer id, value is a set containing the names of the hidden attributes*/
113+
virtual QMap< QString, QSet<QString> > wmsExcludedAttributes() const { return QMap< QString, QSet<QString> >(); }
114+
115+
/**Returns attributes excluded from WFS publication. Key is layer id, value is a set containing the names of the hidden attributes*/
116+
virtual QMap< QString, QSet<QString> > wfsExcludedAttributes() const { return QMap< QString, QSet<QString> >(); }
114117

115118
/**Creates a print composition, usually for a GetPrint request. Replaces map and label parameters*/
116119
QgsComposition* createPrintComposition( const QString& composerTemplate, QgsMapRenderer* mapRenderer, const QMap< QString, QString >& parameterMap ) const;

src/mapserver/qgsprojectparser.cpp

+33-21
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ void QgsProjectParser::describeFeatureType( const QString& aTypeName, QDomElemen
210210

211211
QStringList wfsLayersId = wfsLayers();
212212
QMap< QString, QMap< int, QString > > aliasInfo = layerAliasInfo();
213-
QMap< QString, QSet<QString> > hiddenAttrs = hiddenAttributes();
213+
QMap< QString, QSet<QString> > excludedAttrs = wfsExcludedAttributes();
214214

215215
foreach ( const QDomElement &elem, mProjectLayerElements )
216216
{
@@ -237,11 +237,11 @@ void QgsProjectParser::describeFeatureType( const QString& aTypeName, QDomElemen
237237
}
238238

239239
//hidden attributes for this layer
240-
QSet<QString> layerHiddenAttributes;
241-
QMap< QString, QSet<QString> >::const_iterator hiddenIt = hiddenAttrs.find( mLayer->id() );
242-
if ( hiddenIt != hiddenAttrs.constEnd() )
240+
QSet<QString> layerExcludedAttributes;
241+
QMap< QString, QSet<QString> >::const_iterator exclIt = excludedAttrs.find( mLayer->id() );
242+
if ( exclIt != excludedAttrs.constEnd() )
243243
{
244-
layerHiddenAttributes = hiddenIt.value();
244+
layerExcludedAttributes = exclIt.value();
245245
}
246246

247247
QString typeName = layer->name();
@@ -316,7 +316,7 @@ void QgsProjectParser::describeFeatureType( const QString& aTypeName, QDomElemen
316316

317317
QString attributeName = it.value().name();
318318
//skip attribute if it has edit type 'hidden'
319-
if ( layerHiddenAttributes.contains( attributeName ) )
319+
if ( layerExcludedAttributes.contains( attributeName ) )
320320
{
321321
continue;
322322
}
@@ -1053,34 +1053,46 @@ QMap< QString, QMap< int, QString > > QgsProjectParser::layerAliasInfo() const
10531053
return resultMap;
10541054
}
10551055

1056-
QMap< QString, QSet<QString> > QgsProjectParser::hiddenAttributes() const
1056+
QMap< QString, QSet<QString> > QgsProjectParser::wmsExcludedAttributes() const
10571057
{
10581058
QMap< QString, QSet<QString> > resultMap;
10591059
QList<QDomElement>::const_iterator layerIt = mProjectLayerElements.constBegin();
10601060
for ( ; layerIt != mProjectLayerElements.constEnd(); ++layerIt )
10611061
{
1062-
QDomNodeList editTypesList = layerIt->elementsByTagName( "edittypes" );
1063-
if ( editTypesList.size() > 0 )
1062+
QDomElement excludeWMSElem = layerIt->firstChildElement( "excludeAttributesWMS" );
1063+
QDomNodeList attributeNodeList = excludeWMSElem.elementsByTagName( "attribute" );
1064+
if ( attributeNodeList.size() > 0 )
10641065
{
1065-
QSet< QString > hiddenAttributes;
1066-
QDomElement editTypesElem = editTypesList.at( 0 ).toElement();
1067-
QDomNodeList editTypeList = editTypesElem.elementsByTagName( "edittype" );
1068-
for ( int i = 0; i < editTypeList.size(); ++i )
1066+
QSet<QString> layerExcludedAttributes;
1067+
for ( int i = 0; i < attributeNodeList.size(); ++i )
10691068
{
1070-
QDomElement editTypeElem = editTypeList.at( i ).toElement();
1071-
if ( editTypeElem.attribute( "type" ).toInt() == QgsVectorLayer::Hidden )
1072-
{
1073-
hiddenAttributes.insert( editTypeElem.attribute( "name" ) );
1074-
}
1069+
layerExcludedAttributes.insert( attributeNodeList.at( i ).toElement().text() );
10751070
}
1071+
resultMap.insert( layerId( *layerIt ), layerExcludedAttributes );
1072+
}
1073+
}
1074+
return resultMap;
1075+
}
10761076

1077-
if ( hiddenAttributes.size() > 0 )
1077+
/**Returns attributes excluded from WFS publication. Key is layer id, value is a set containing the names of the hidden attributes*/
1078+
QMap< QString, QSet<QString> > QgsProjectParser::wfsExcludedAttributes() const
1079+
{
1080+
QMap< QString, QSet<QString> > resultMap;
1081+
QList<QDomElement>::const_iterator layerIt = mProjectLayerElements.constBegin();
1082+
for ( ; layerIt != mProjectLayerElements.constEnd(); ++layerIt )
1083+
{
1084+
QDomElement excludeWMSElem = layerIt->firstChildElement( "excludeAttributesWFS" );
1085+
QDomNodeList attributeNodeList = excludeWMSElem.elementsByTagName( "attribute" );
1086+
if ( attributeNodeList.size() > 0 )
1087+
{
1088+
QSet<QString> layerExcludedAttributes;
1089+
for ( int i = 0; i < attributeNodeList.size(); ++i )
10781090
{
1079-
resultMap.insert( layerId( *layerIt ), hiddenAttributes );
1091+
layerExcludedAttributes.insert( attributeNodeList.at( i ).toElement().text() );
10801092
}
1093+
resultMap.insert( layerId( *layerIt ), layerExcludedAttributes );
10811094
}
10821095
}
1083-
10841096
return resultMap;
10851097
}
10861098

src/mapserver/qgsprojectparser.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,11 @@ class QgsProjectParser: public QgsConfigParser
8787
Default implementation returns an empty map*/
8888
virtual QMap< QString, QMap< int, QString > > layerAliasInfo() const;
8989

90-
/**Returns a stringlist containing the names of the attributes with hidden edit types*/
91-
virtual QMap< QString, QSet<QString> > hiddenAttributes() const;
90+
/**Returns attributes excluded from WMS publication. Key is layer id, value is a set containing the names of the hidden attributes*/
91+
virtual QMap< QString, QSet<QString> > wmsExcludedAttributes() const;
92+
93+
/**Returns attributes excluded from WFS publication. Key is layer id, value is a set containing the names of the hidden attributes*/
94+
virtual QMap< QString, QSet<QString> > wfsExcludedAttributes() const;
9295

9396
/**Returns map rectangle for the project file*/
9497
QgsRectangle mapRectangle() const;

src/mapserver/qgswfsserver.cpp

+20-20
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format
287287

288288
QStringList wfsLayersId = mConfigParser->wfsLayers();
289289
QMap< QString, QMap< int, QString > > aliasInfo = mConfigParser->layerAliasInfo();
290-
QMap< QString, QSet<QString> > hiddenAttributes = mConfigParser->hiddenAttributes();
290+
QMap< QString, QSet<QString> > excludedAttributes = mConfigParser->wfsExcludedAttributes();
291291

292292
QList<QgsMapLayer*> layerList;
293293
QgsMapLayer* currentLayer = 0;
@@ -306,12 +306,12 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format
306306
layerAliasInfo = aliasIt.value();
307307
}
308308

309-
//hidden attributes for this layer
310-
QSet<QString> layerHiddenAttributes;
311-
QMap< QString, QSet<QString> >::const_iterator hiddenIt = hiddenAttributes.find( currentLayer->id() );
312-
if ( hiddenIt != hiddenAttributes.constEnd() )
309+
//excluded attributes for this layer
310+
QSet<QString> layerExcludedAttributes;
311+
QMap< QString, QSet<QString> >::const_iterator exclIt = excludedAttributes.find( currentLayer->id() );
312+
if ( exclIt != excludedAttributes.constEnd() )
313313
{
314-
layerHiddenAttributes = hiddenIt.value();
314+
layerExcludedAttributes = exclIt.value();
315315
}
316316

317317
//do a select with searchRect and go through all the features
@@ -455,7 +455,7 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format
455455
if ( fidOk )
456456
{
457457
provider->featureAtId( fid.toInt(), feature, mWithGeom, attrIndexes );
458-
sendGetFeature( request, format, &feature, 0, layerCrs, fields, layerHiddenAttributes );
458+
sendGetFeature( request, format, &feature, 0, layerCrs, fields, layerExcludedAttributes );
459459
}
460460
else if ( filterOk )
461461
{
@@ -469,13 +469,13 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format
469469
{
470470
if ( mFilter->evaluate( feature ) )
471471
{
472-
sendGetFeature( request, format, &feature, featureCounter, layerCrs, fields, layerHiddenAttributes );
472+
sendGetFeature( request, format, &feature, featureCounter, layerCrs, fields, layerExcludedAttributes );
473473
++featureCounter;
474474
}
475475
}
476476
else
477477
{
478-
sendGetFeature( request, format, &feature, featureCounter, layerCrs, fields, layerHiddenAttributes );
478+
sendGetFeature( request, format, &feature, featureCounter, layerCrs, fields, layerExcludedAttributes );
479479
++featureCounter;
480480
}
481481
}
@@ -487,7 +487,7 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format
487487

488488
while ( provider->nextFeature( feature ) && featureCounter < maxFeat )
489489
{
490-
sendGetFeature( request, format, &feature, featureCounter, layerCrs, fields, layerHiddenAttributes );
490+
sendGetFeature( request, format, &feature, featureCounter, layerCrs, fields, layerExcludedAttributes );
491491
++featureCounter;
492492
}
493493
}
@@ -497,7 +497,7 @@ int QgsWFSServer::getFeature( QgsRequestHandler& request, const QString& format
497497
provider->select( attrIndexes, searchRect, mWithGeom, true );
498498
while ( provider->nextFeature( feature ) && featureCounter < maxFeat )
499499
{
500-
sendGetFeature( request, format, &feature, featureCounter, layerCrs, fields, layerHiddenAttributes );
500+
sendGetFeature( request, format, &feature, featureCounter, layerCrs, fields, layerExcludedAttributes );
501501
++featureCounter;
502502
}
503503
}
@@ -633,7 +633,7 @@ void QgsWFSServer::startGetFeature( QgsRequestHandler& request, const QString& f
633633
fcString = "";
634634
}
635635

636-
void QgsWFSServer::sendGetFeature( QgsRequestHandler& request, const QString& format, QgsFeature* feat, int featIdx, QgsCoordinateReferenceSystem& crs, QMap< int, QgsField > fields, QSet<QString> hiddenAttributes ) /*const*/
636+
void QgsWFSServer::sendGetFeature( QgsRequestHandler& request, const QString& format, QgsFeature* feat, int featIdx, QgsCoordinateReferenceSystem& crs, QMap< int, QgsField > fields, QSet<QString> excludedAttributes ) /*const*/
637637
{
638638
QByteArray result;
639639
if ( format == "GeoJSON" )
@@ -643,7 +643,7 @@ void QgsWFSServer::sendGetFeature( QgsRequestHandler& request, const QString& fo
643643
fcString += " ";
644644
else
645645
fcString += " ,";
646-
fcString += createFeatureGeoJSON( feat, crs, fields, hiddenAttributes );
646+
fcString += createFeatureGeoJSON( feat, crs, fields, excludedAttributes );
647647
fcString += "\n";
648648

649649
result = fcString.toUtf8();
@@ -653,7 +653,7 @@ void QgsWFSServer::sendGetFeature( QgsRequestHandler& request, const QString& fo
653653
else
654654
{
655655
QDomDocument gmlDoc;
656-
QDomElement featureElement = createFeatureElem( feat, gmlDoc, crs, fields, hiddenAttributes );
656+
QDomElement featureElement = createFeatureElem( feat, gmlDoc, crs, fields, excludedAttributes );
657657
gmlDoc.appendChild( featureElement );
658658

659659
result = gmlDoc.toByteArray();
@@ -684,7 +684,7 @@ void QgsWFSServer::endGetFeature( QgsRequestHandler& request, const QString& for
684684
}
685685
}
686686

687-
QString QgsWFSServer::createFeatureGeoJSON( QgsFeature* feat, QgsCoordinateReferenceSystem &, QMap< int, QgsField > fields, QSet<QString> hiddenAttributes ) /*const*/
687+
QString QgsWFSServer::createFeatureGeoJSON( QgsFeature* feat, QgsCoordinateReferenceSystem &, QMap< int, QgsField > fields, QSet<QString> excludedAttributes ) /*const*/
688688
{
689689
QString fStr = "{\"type\": \"Feature\",\n";
690690

@@ -711,8 +711,8 @@ QString QgsWFSServer::createFeatureGeoJSON( QgsFeature* feat, QgsCoordinateRefer
711711
for ( QgsAttributeMap::const_iterator it = featureAttributes.begin(); it != featureAttributes.end(); ++it )
712712
{
713713
QString attributeName = fields[it.key()].name();
714-
//skip attribute if it has edit type 'hidden'
715-
if ( hiddenAttributes.contains( attributeName ) )
714+
//skip attribute if it is excluded from WFS publication
715+
if ( excludedAttributes.contains( attributeName ) )
716716
{
717717
continue;
718718
}
@@ -744,7 +744,7 @@ QString QgsWFSServer::createFeatureGeoJSON( QgsFeature* feat, QgsCoordinateRefer
744744
return fStr;
745745
}
746746

747-
QDomElement QgsWFSServer::createFeatureElem( QgsFeature* feat, QDomDocument& doc, QgsCoordinateReferenceSystem& crs, QMap< int, QgsField > fields, QSet<QString> hiddenAttributes ) /*const*/
747+
QDomElement QgsWFSServer::createFeatureElem( QgsFeature* feat, QDomDocument& doc, QgsCoordinateReferenceSystem& crs, QMap< int, QgsField > fields, QSet<QString> excludedAttributes ) /*const*/
748748
{
749749
//gml:FeatureMember
750750
QDomElement featureElement = doc.createElement( "gml:featureMember"/*wfs:FeatureMember*/ );
@@ -787,8 +787,8 @@ QDomElement QgsWFSServer::createFeatureElem( QgsFeature* feat, QDomDocument& doc
787787
{
788788

789789
QString attributeName = fields[it.key()].name();
790-
//skip attribute if it has edit type 'hidden'
791-
if ( hiddenAttributes.contains( attributeName ) )
790+
//skip attribute if is explicitely excluded from WFS publication
791+
if ( excludedAttributes.contains( attributeName ) )
792792
{
793793
continue;
794794
}

src/mapserver/qgswfsserver.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ class QgsWFSServer
8282
protected:
8383

8484
void startGetFeature( QgsRequestHandler& request, const QString& format, QgsCoordinateReferenceSystem& crs, QgsRectangle* rect );
85-
void sendGetFeature( QgsRequestHandler& request, const QString& format, QgsFeature* feat, int featIdx, QgsCoordinateReferenceSystem& crs, QMap< int, QgsField > fields, QSet<QString> hiddenAttributes );
85+
void sendGetFeature( QgsRequestHandler& request, const QString& format, QgsFeature* feat, int featIdx, QgsCoordinateReferenceSystem& crs, QMap< int, QgsField > fields, QSet<QString> excludedAttributes );
8686
void endGetFeature( QgsRequestHandler& request, const QString& format );
8787

8888
//methods to write GeoJSON
89-
QString createFeatureGeoJSON( QgsFeature* feat, QgsCoordinateReferenceSystem& crs, QMap< int, QgsField > fields, QSet<QString> hiddenAttributes ) /*const*/;
89+
QString createFeatureGeoJSON( QgsFeature* feat, QgsCoordinateReferenceSystem& crs, QMap< int, QgsField > fields, QSet<QString> excludedAttributes ) /*const*/;
9090

9191
//methods to write GML2
92-
QDomElement createFeatureElem( QgsFeature* feat, QDomDocument& doc, QgsCoordinateReferenceSystem& crs, QMap< int, QgsField > fields, QSet<QString> hiddenAttributes ) /*const*/;
92+
QDomElement createFeatureElem( QgsFeature* feat, QDomDocument& doc, QgsCoordinateReferenceSystem& crs, QMap< int, QgsField > fields, QSet<QString> excludedAttributes ) /*const*/;
9393

9494
QDomElement createBoxElem( QgsRectangle* box, QDomDocument& doc ) /* const */;
9595
QDomElement createGeometryElem( QgsGeometry* g, QDomDocument& doc ) /*const*/;

src/mapserver/qgswmsserver.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ int QgsWMSServer::getFeatureInfo( QDomDocument& result, QString version )
727727

728728
QStringList nonIdentifiableLayers = mConfigParser->identifyDisabledLayers();
729729
QMap< QString, QMap< int, QString > > aliasInfo = mConfigParser->layerAliasInfo();
730-
QMap< QString, QSet<QString> > hiddenAttributes = mConfigParser->hiddenAttributes();
730+
QMap< QString, QSet<QString> > excludedAttributes = mConfigParser->wmsExcludedAttributes();
731731

732732
//Render context is needed to determine feature visibility for vector layers
733733
QgsRenderContext renderContext;
@@ -784,16 +784,16 @@ int QgsWMSServer::getFeatureInfo( QDomDocument& result, QString version )
784784
layerAliasInfo = aliasIt.value();
785785
}
786786

787-
//hidden attributes for this layer
788-
QSet<QString> layerHiddenAttributes;
789-
QMap< QString, QSet<QString> >::const_iterator hiddenIt = hiddenAttributes.find( currentLayer->id() );
790-
if ( hiddenIt != hiddenAttributes.constEnd() )
787+
//excluded attributes for this layer
788+
QSet<QString> layerExcludedAttributes;
789+
QMap< QString, QSet<QString> >::const_iterator excludedIt = excludedAttributes.find( currentLayer->id() );
790+
if ( excludedIt != excludedAttributes.constEnd() )
791791
{
792-
layerHiddenAttributes = hiddenIt.value();
792+
layerExcludedAttributes = excludedIt.value();
793793
}
794794

795795
if ( featureInfoFromVectorLayer( vectorLayer, infoPoint, featureCount, result, layerElement, mMapRenderer, renderContext,
796-
layerAliasInfo, layerHiddenAttributes, version, featuresRect ) != 0 )
796+
layerAliasInfo, layerExcludedAttributes, version, featuresRect ) != 0 )
797797
{
798798
continue;
799799
}
@@ -1141,7 +1141,7 @@ int QgsWMSServer::featureInfoFromVectorLayer( QgsVectorLayer* layer,
11411141
QgsMapRenderer* mapRender,
11421142
QgsRenderContext& renderContext,
11431143
QMap<int, QString>& aliasMap,
1144-
QSet<QString>& hiddenAttributes,
1144+
QSet<QString>& excludedAttributes,
11451145
QString version,
11461146
QgsRectangle* featureBBox ) const
11471147
{
@@ -1231,8 +1231,8 @@ int QgsWMSServer::featureInfoFromVectorLayer( QgsVectorLayer* layer,
12311231
{
12321232

12331233
QString attributeName = fields[it.key()].name();
1234-
//skip attribute if it has edit type 'hidden'
1235-
if ( hiddenAttributes.contains( attributeName ) )
1234+
//skip attribute if it is explicitely excluded from WMS publication
1235+
if ( excludedAttributes.contains( attributeName ) )
12361236
{
12371237
continue;
12381238
}

src/mapserver/qgswmsserver.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class QgsWMSServer
117117
@param featureBBox the bounding box of the selected features in output CRS
118118
@return 0 in case of success*/
119119
int featureInfoFromVectorLayer( QgsVectorLayer* layer, const QgsPoint* infoPoint, int nFeatures, QDomDocument& infoDocument, QDomElement& layerElement, QgsMapRenderer* mapRender,
120-
QgsRenderContext& renderContext, QMap<int, QString>& aliasMap, QSet<QString>& hiddenAttributes, QString version, QgsRectangle* featureBBox = 0 ) const;
120+
QgsRenderContext& renderContext, QMap<int, QString>& aliasMap, QSet<QString>& excludedAttributes, QString version, QgsRectangle* featureBBox = 0 ) const;
121121
/**Appends feature info xml for the layer to the layer element of the dom document*/
122122
int featureInfoFromRasterLayer( QgsRasterLayer* layer, const QgsPoint* infoPoint, QDomDocument& infoDocument, QDomElement& layerElement, QString version ) const;
123123

0 commit comments

Comments
 (0)