Skip to content

Commit

Permalink
optionally use layer ids for wms
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Aug 13, 2014
1 parent a2a66ea commit bacd2b6
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 48 deletions.
4 changes: 4 additions & 0 deletions src/app/qgsprojectproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
bool addWktGeometry = QgsProject::instance()->readBoolEntry( "WMSAddWktGeometry", "/" );
mAddWktGeometryCheckBox->setChecked( addWktGeometry );

bool useLayerIDs = QgsProject::instance()->readBoolEntry( "WMSUseLayerIDs", "/" );
mWmsUseLayerIDs->setChecked( useLayerIDs );

//WMS maxWidth / maxHeight
mMaxWidthLineEdit->setValidator( new QIntValidator( mMaxWidthLineEdit ) );
int maxWidth = QgsProject::instance()->readNumEntry( "WMSMaxWidth", "/", -1 );
Expand Down Expand Up @@ -794,6 +797,7 @@ void QgsProjectProperties::apply()
}

QgsProject::instance()->writeEntry( "WMSAddWktGeometry", "/", mAddWktGeometryCheckBox->isChecked() );
QgsProject::instance()->writeEntry( "WMSUseLayerIDs", "/", mWmsUseLayerIDs->isChecked() );

QString maxWidthText = mMaxWidthLineEdit->text();
if ( maxWidthText.isEmpty() )
Expand Down
4 changes: 2 additions & 2 deletions src/mapserver/qgsowsserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
class QgsOWSServer
{
public:
QgsOWSServer( const QString& configFilePath, const QMap<QString, QString>& parameters, QgsRequestHandler* rh ):
mParameters( parameters ), mRequestHandler( rh ), mConfigFilePath( configFilePath ) {}
QgsOWSServer( const QString& configFilePath, const QMap<QString, QString>& parameters, QgsRequestHandler* rh )
: mParameters( parameters ), mRequestHandler( rh ), mConfigFilePath( configFilePath ) {}
virtual ~QgsOWSServer() { delete mRequestHandler; }

virtual void executeRequest() = 0;
Expand Down
35 changes: 28 additions & 7 deletions src/mapserver/qgsserverprojectparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
#include <QStringList>
#include <QUrl>

QgsServerProjectParser::QgsServerProjectParser( QDomDocument* xmlDoc, const QString& filePath ):
mXMLDoc( xmlDoc ), mProjectPath( filePath )
QgsServerProjectParser::QgsServerProjectParser( QDomDocument* xmlDoc, const QString& filePath )
: mXMLDoc( xmlDoc )
, mProjectPath( filePath )
{
//accelerate the search for layers, groups and the creation of annotation items
if ( mXMLDoc )
Expand All @@ -58,12 +59,14 @@ QgsServerProjectParser::QgsServerProjectParser( QDomDocument* xmlDoc, const QStr
}

mRestrictedLayers = findRestrictedLayers();
mUseLayerIDs = findUseLayerIDs();
}
}

QgsServerProjectParser::QgsServerProjectParser(): mXMLDoc( 0 )
QgsServerProjectParser::QgsServerProjectParser()
: mXMLDoc( 0 )
, mUseLayerIDs( false )
{

}

QgsServerProjectParser::~QgsServerProjectParser()
Expand Down Expand Up @@ -1026,6 +1029,22 @@ QSet<QString> QgsServerProjectParser::findRestrictedLayers() const
return restrictedLayerSet;
}

bool QgsServerProjectParser::findUseLayerIDs() const
{
if ( !mXMLDoc )
return false;

QDomElement propertiesElem = mXMLDoc->documentElement().firstChildElement( "properties" );
if ( propertiesElem.isNull() )
return false;

QDomElement wktElem = propertiesElem.firstChildElement( "WMSUseLayerIDs" );
if ( wktElem.isNull() )
return false;

return wktElem.text().compare( "true", Qt::CaseInsensitive ) == 0;
}

void QgsServerProjectParser::layerFromLegendLayer( const QDomElement& legendLayerElem, QMap< int, QgsMapLayer*>& layers, bool useCache ) const
{
QString id = legendLayerElem.firstChild().firstChild().toElement().attribute( "layerid" );
Expand Down Expand Up @@ -1107,7 +1126,7 @@ QStringList QgsServerProjectParser::wfsLayerNames() const
currentLayer = layerMapIt.value();
if ( currentLayer )
{
layerNameList.append( currentLayer->name() );
layerNameList.append( mUseLayerIDs ? currentLayer->id() : currentLayer->name() );
}
}
}
Expand Down Expand Up @@ -1284,9 +1303,11 @@ void QgsServerProjectParser::addValueRelationLayersForElement( const QDomElement
if ( type == QgsVectorLayer::ValueRelation )
{
QString layerId = editTypeElem.attribute( "layer" );
/*QString keyAttribute = editTypeEleml.attribute( "id" ); //relation attribute in other layer
#if 0
QString keyAttribute = editTypeEleml.attribute( "id" ); //relation attribute in other layer
QString valueAttribute = editTypeElem.attribute( "value" ); //value attribute in other layer
QString relationAttribute = editTypeElem.attribute( "name" );*/
QString relationAttribute = editTypeElem.attribute( "name" );
#endif

QgsMapLayer* layer = mapLayerFromLayerId( layerId, useCache );
if ( layer )
Expand Down
6 changes: 6 additions & 0 deletions src/mapserver/qgsserverprojectparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ class QgsServerProjectParser
QDomElement propertiesElem() const;

const QSet<QString>& restrictedLayers() const { return mRestrictedLayers; }
bool useLayerIDs() const { return mUseLayerIDs; }

const QHash< QString, QDomElement >& projectLayerElementsByName() const { return mProjectLayerElementsByName; }
const QHash< QString, QDomElement >& projectLayerElementsById() const { return mProjectLayerElementsById; }

void layerFromLegendLayer( const QDomElement& legendLayerElem, QMap< int, QgsMapLayer*>& layers, bool useCache = true ) const;

Expand Down Expand Up @@ -143,11 +145,15 @@ class QgsServerProjectParser
/**Names of layers and groups which should not be published*/
QSet<QString> mRestrictedLayers;

bool mUseLayerIDs;

QgsServerProjectParser(); //forbidden

/**Returns a complete string set with all the restricted layer names (layers/groups that are not to be published)*/
QSet<QString> findRestrictedLayers() const;

bool findUseLayerIDs() const;

/**Adds sublayers of an embedded group to layer set*/
static void sublayersOfEmbeddedGroup( const QString& projectFilePath, const QString& groupName, QSet<QString>& layerSet );
};
Expand Down
4 changes: 3 additions & 1 deletion src/mapserver/qgssldconfigparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class QgsVectorLayer;
class QgsRasterLayer;
class QTemporaryFile;

class QgsSLDConfigParser: public QgsWMSConfigParser
class QgsSLDConfigParser : public QgsWMSConfigParser
{
public:
/**Constructor takes a dom document as argument. The class takes ownership of the document and deletes it in the destructor
Expand Down Expand Up @@ -184,6 +184,8 @@ class QgsSLDConfigParser: public QgsWMSConfigParser

/**Reads attributes "epsg" or "proj" from layer element and sets specified CRS if present*/
void setCrsForLayer( const QDomElement& layerElem, QgsMapLayer* ml ) const;

bool useLayerIDs() const { return false; }
};

#endif // QGSSLDCONFIGPARSER_H
2 changes: 2 additions & 0 deletions src/mapserver/qgswmsconfigparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ class QgsWMSConfigParser

virtual void serviceCapabilities( QDomElement& parentElement, QDomDocument& doc ) const = 0;

virtual bool useLayerIDs() const = 0;

#if 0
/**List of GML datasets passed outside SLD (e.g. in a SOAP request). Key of the map is the layer name*/
QMap<QString, QDomDocument*> mExternalGMLDatasets;
Expand Down
36 changes: 20 additions & 16 deletions src/mapserver/qgswmsprojectparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
#include <QFileInfo>
#include <QTextDocument>

QgsWMSProjectParser::QgsWMSProjectParser( QDomDocument* xmlDoc, const QString& filePath ): QgsWMSConfigParser(),
mProjectParser( xmlDoc, filePath )
QgsWMSProjectParser::QgsWMSProjectParser( QDomDocument* xmlDoc, const QString& filePath )
: QgsWMSConfigParser()
, mProjectParser( xmlDoc, filePath )
{
mLegendLayerFont.fromString( mProjectParser.firstComposerLegendElement().attribute( "layerFont" ) );
mLegendItemFont.fromString( mProjectParser.firstComposerLegendElement().attribute( "itemFont" ) );
Expand Down Expand Up @@ -104,11 +105,11 @@ QList<QgsMapLayer*> QgsWMSProjectParser::mapLayerFromStyle( const QString& lName
}

//does lName refer to a leaf layer
const QHash< QString, QDomElement >& projectLayerElementsByName = mProjectParser.projectLayerElementsByName();
QHash< QString, QDomElement >::const_iterator layerElemIt = projectLayerElementsByName.find( lName );
if ( layerElemIt != projectLayerElementsByName.constEnd() )
const QHash< QString, QDomElement > &projectLayerElements = mProjectParser.useLayerIDs() ? mProjectParser.projectLayerElementsById() : mProjectParser.projectLayerElementsByName();
QHash< QString, QDomElement >::const_iterator layerElemIt = projectLayerElements.find( lName );
if ( layerElemIt != projectLayerElements.constEnd() )
{
return ( QList<QgsMapLayer*>() << mProjectParser.createLayerFromElement( layerElemIt.value(), useCache ) );
return QList<QgsMapLayer*>() << mProjectParser.createLayerFromElement( layerElemIt.value(), useCache );
}

//group or project name
Expand Down Expand Up @@ -685,7 +686,7 @@ void QgsWMSProjectParser::addDrawingOrder( QDomElement& parentElem, QDomDocument
return;
}

bool useDrawingOrder = ( legendElement.attribute( "updateDrawingOrder" ) == "false" );
bool useDrawingOrder = legendElement.attribute( "updateDrawingOrder" ) == "false";
QMap<int, QString> orderedLayerNames;

QDomNodeList legendChildren = legendElement.childNodes();
Expand Down Expand Up @@ -767,7 +768,7 @@ void QgsWMSProjectParser::addDrawingOrderEmbeddedGroup( QDomElement groupElem, b
for ( int i = 0; i < layerNodeList.size(); ++i )
{
layerElem = layerNodeList.at( i ).toElement();
layerName = layerElem.attribute( "name" );
layerName = mProjectParser.useLayerIDs() ? layerElem.attribute( "id" ) : layerElem.attribute( "name" );

int layerDrawingOrder = updateDrawingOrder ? -1 : layerElem.attribute( "drawingOrder", "-1" ).toInt();
if ( layerDrawingOrder == -1 )
Expand Down Expand Up @@ -824,7 +825,10 @@ void QgsWMSProjectParser::addDrawingOrder( QDomElement elem, bool useDrawingOrde
}
else if ( elem.tagName() == "legendlayer" )
{
QString layerName = elem.attribute( "name" );
QString layerName = mProjectParser.useLayerIDs()
? mProjectParser.layerIdFromLegendLayer( elem )
: elem.attribute( "name" );

if ( useDrawingOrder )
{
int drawingOrder = elem.attribute( "drawingOrder", "-1" ).toInt();
Expand Down Expand Up @@ -860,10 +864,11 @@ void QgsWMSProjectParser::addLayers( QDomDocument &doc,
{
layerElem.setAttribute( "queryable", "1" );
QString name = currentChildElem.attribute( "name" );
if ( mRestrictedLayers.contains( name ) ) //unpublished group
if ( mProjectParser.restrictedLayers().contains( name ) ) //unpublished group
{
continue;
}

QDomElement nameElem = doc.createElement( "Name" );
QDomText nameText = doc.createTextNode( name );
nameElem.appendChild( nameText );
Expand Down Expand Up @@ -932,7 +937,7 @@ void QgsWMSProjectParser::addLayers( QDomDocument &doc,
continue;
}

if ( mRestrictedLayers.contains( currentLayer->name() ) ) //unpublished layer
if ( mProjectParser.restrictedLayers().contains( mProjectParser.useLayerIDs() ? currentLayer->id() : currentLayer->name() ) ) //unpublished layer
{
continue;
}
Expand All @@ -949,7 +954,7 @@ void QgsWMSProjectParser::addLayers( QDomDocument &doc,
QDomElement nameElem = doc.createElement( "Name" );
//We use the layer name even though it might not be unique.
//Because the id sometimes contains user/pw information and the name is more descriptive
QDomText nameText = doc.createTextNode( currentLayer->name() );
QDomText nameText = doc.createTextNode( mProjectParser.useLayerIDs() ? currentLayer->id() : currentLayer->name() );
nameElem.appendChild( nameText );
layerElem.appendChild( nameElem );

Expand Down Expand Up @@ -1074,7 +1079,7 @@ void QgsWMSProjectParser::addLayers( QDomDocument &doc,
mapUrl.addQueryItem( "SERVICE", "WMS" );
mapUrl.addQueryItem( "VERSION", version );
mapUrl.addQueryItem( "REQUEST", "GetLegendGraphic" );
mapUrl.addQueryItem( "LAYER", currentLayer->name() );
mapUrl.addQueryItem( "LAYER", mProjectParser.useLayerIDs() ? currentLayer->id() : currentLayer->name() );
mapUrl.addQueryItem( "FORMAT", "image/png" );
mapUrl.addQueryItem( "STYLE", styleNameText.data() );
if ( version == "1.3.0" )
Expand Down Expand Up @@ -1305,7 +1310,7 @@ void QgsWMSProjectParser::addOWSLayers( QDomDocument &doc,
continue;
}

if ( mRestrictedLayers.contains( currentLayer->name() ) ) //unpublished layer
if ( mProjectParser.restrictedLayers().contains( mProjectParser.useLayerIDs() ? currentLayer->id() : currentLayer->name() ) ) //unpublished layer
{
continue;
}
Expand Down Expand Up @@ -1336,7 +1341,7 @@ void QgsWMSProjectParser::addOWSLayers( QDomDocument &doc,
// OWSContext Layer opacity is set to 1
layerElem.setAttribute( "opacity", 1 );

QString lyrname = currentLayer->name();
QString lyrname = mProjectParser.useLayerIDs() ? currentLayer->id() : currentLayer->name();
layerElem.setAttribute( "name", lyrname );

// define an id based on layer name
Expand Down Expand Up @@ -1594,7 +1599,6 @@ bool QgsWMSProjectParser::featureInfoWithWktGeometry() const
return ( wktElem.text().compare( "true", Qt::CaseInsensitive ) == 0 );
}


QHash<QString, QString> QgsWMSProjectParser::featureInfoLayerAliasMap() const
{
QHash<QString, QString> aliasMap;
Expand Down
8 changes: 3 additions & 5 deletions src/mapserver/qgswmsprojectparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class QTextDocument;
class QSvgRenderer;

class QgsWMSProjectParser: public QgsWMSConfigParser
class QgsWMSProjectParser : public QgsWMSConfigParser
{
public:
QgsWMSProjectParser( QDomDocument* xmlDoc, const QString& filePath );
Expand Down Expand Up @@ -105,14 +105,12 @@ class QgsWMSProjectParser: public QgsWMSConfigParser

void serviceCapabilities( QDomElement& parentElement, QDomDocument& doc ) const;

bool useLayerIDs() const { return mProjectParser.useLayerIDs(); }

private:
QgsServerProjectParser mProjectParser;

/**Names of layers and groups which should not be published*/
QSet<QString> mRestrictedLayers;

mutable QFont mLegendLayerFont;

mutable QFont mLegendItemFont;

/**Watermark text items*/
Expand Down
21 changes: 11 additions & 10 deletions src/mapserver/qgswmsserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@
QgsWMSServer::QgsWMSServer( const QString& configFilePath, QMap<QString, QString> parameters, QgsWMSConfigParser* cp,
QgsRequestHandler* rh, QgsMapRenderer* renderer, QgsCapabilitiesCache* capCache )
: QgsOWSServer( configFilePath, parameters, rh )
, mMapRenderer( renderer ), mCapabilitiesCache( capCache ), mConfigParser( cp ), mOwnsConfigParser( false )
, mMapRenderer( renderer )
, mCapabilitiesCache( capCache )
, mConfigParser( cp )
, mOwnsConfigParser( false )
{
}

Expand Down Expand Up @@ -1034,7 +1037,7 @@ int QgsWMSServer::getFeatureInfo( QDomDocument& result, QString version )

for ( QMap<QString, QString>::iterator it = mParameters.begin(); it != mParameters.end(); ++it )
{
QgsDebugMsg( QString( "%1 // %2" ).arg( it.key() ).arg( it.value() ) );
QgsDebugMsg( QString( "%1 // %2" ).arg( it.key() ).arg( it.value() ) );
}

if ( readLayersAndStyles( layersList, stylesList ) != 0 )
Expand Down Expand Up @@ -1225,7 +1228,7 @@ int QgsWMSServer::getFeatureInfo( QDomDocument& result, QString version )
else
{
layerElement = result.createElement( "Layer" );
QString layerName = currentLayer->name();
QString layerName = mConfigParser->useLayerIDs() ? currentLayer->id() : currentLayer->name();

//check if the layer is given a different name for GetFeatureInfo output
QHash<QString, QString>::const_iterator layerAliasIt = layerAliasMap.find( layerName );
Expand Down Expand Up @@ -1731,7 +1734,6 @@ int QgsWMSServer::featureInfoFromVectorLayer( QgsVectorLayer* layer,
break;
}


QgsFeatureRendererV2* r2 = layer->rendererV2();
if ( !r2 )
{
Expand Down Expand Up @@ -1774,7 +1776,7 @@ int QgsWMSServer::featureInfoFromVectorLayer( QgsVectorLayer* layer,
{
bool withGeom = layer->wkbType() != QGis::WKBNoGeometry && addWktGeometry;
int version = infoFormat.startsWith( "application/vnd.ogc.gml/3" ) ? 3 : 2;
QDomElement elem = createFeatureGML( &feature, layer, infoDocument, outputCrs, layer->name(), withGeom, version );
QDomElement elem = createFeatureGML( &feature, layer, infoDocument, outputCrs, mConfigParser->useLayerIDs() ? layer->id() : layer->name(), withGeom, version );
QDomElement featureMemberElem = infoDocument.createElement( "gml:featureMember"/*wfs:FeatureMember*/ );
featureMemberElem.appendChild( elem );
layerElement.appendChild( featureMemberElem );
Expand Down Expand Up @@ -1897,7 +1899,7 @@ int QgsWMSServer::featureInfoFromRasterLayer( QgsRasterLayer* layer,

QgsCoordinateReferenceSystem layerCrs = layer->crs();
int version = infoFormat.startsWith( "application/vnd.ogc.gml/3" ) ? 3 : 2;
QDomElement elem = createFeatureGML( &feature, 0, infoDocument, layerCrs, layer->name(), false, version );
QDomElement elem = createFeatureGML( &feature, 0, infoDocument, layerCrs, mConfigParser->useLayerIDs() ? layer->id() : layer->name(), false, version );
layerElement.appendChild( elem );
}
else
Expand Down Expand Up @@ -1925,7 +1927,6 @@ QStringList QgsWMSServer::layerSet( const QStringList &layersList,
QgsDebugMsg( QString( "Calculating layerset using %1 layers, %2 styles and CRS %3" ).arg( layersList.count() ).arg( stylesList.count() ).arg( destCRS.description() ) );
for ( llstIt = layersList.begin(), slstIt = stylesList.begin(); llstIt != layersList.end(); ++llstIt )
{

QString styleName;
if ( slstIt != stylesList.end() )
{
Expand All @@ -1950,7 +1951,7 @@ QStringList QgsWMSServer::layerSet( const QStringList &layersList,
theMapLayer = layerList.at( listIndex );
if ( theMapLayer )
{
QgsDebugMsg( QString( "Checking layer: %1" ).arg( theMapLayer->name() ) );
QgsDebugMsg( QString( "Checking layer: %1" ).arg( mConfigParser->useLayerIDs() ? theMapLayer->id() : theMapLayer->name() ) );
//test if layer is visible in requested scale
bool useScaleConstraint = ( scaleDenominator > 0 && theMapLayer->hasScaleBasedVisibility() );
if ( !useScaleConstraint ||
Expand Down Expand Up @@ -2199,7 +2200,7 @@ QMap<QString, QString> QgsWMSServer::applyRequestedLayerFilters( const QStringLi

foreach ( QgsMapLayer *layer, QgsMapLayerRegistry::instance()->mapLayers() )
{
if ( layer && layer->name() == eqSplit.at( 0 ) )
if ( layer && ( mConfigParser->useLayerIDs() ? layer->id() : layer->name() ) == eqSplit.at( 0 ) )
{
layersToFilter.push_back( layer );
}
Expand Down Expand Up @@ -2426,7 +2427,7 @@ QStringList QgsWMSServer::applyFeatureSelections( const QStringList& layerList )

foreach ( QgsMapLayer *layer, QgsMapLayerRegistry::instance()->mapLayers() )
{
if ( layer && layer->name() == layerName )
if ( layer && ( mConfigParser->useLayerIDs() ? layer->id() : layer->name() ) == layerName )
{
vLayer = qobject_cast<QgsVectorLayer*>( layer );
layersWithSelections.push_back( vLayer->id() );
Expand Down
Loading

0 comments on commit bacd2b6

Please sign in to comment.