QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgsvectorlayerserverproperties.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorlayerserverproperties.cpp
3  ------------------
4  begin : August 23, 2019
5  copyright : (C) 2019 by René-Luc D'Hont
6  email : rldhont at 3liz dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 #include "qgsvectorlayer.h"
20 
21 #include <QDomNode>
22 
24  : mLayer( layer )
25 {
26 }
27 
29 {
30  QMap<int, QString> labels;
31  labels[QgsVectorLayerServerProperties::WmsDimensionInfo::AllValues] = QObject::tr( "All values" );
32  labels[QgsVectorLayerServerProperties::WmsDimensionInfo::MinValue] = QObject::tr( "Min value" );
33  labels[QgsVectorLayerServerProperties::WmsDimensionInfo::MaxValue] = QObject::tr( "Max value" );
34  labels[QgsVectorLayerServerProperties::WmsDimensionInfo::ReferenceValue] = QObject::tr( "Reference value" );
35  return labels;
36 }
37 
39 {
40  for ( const QgsVectorLayerServerProperties::WmsDimensionInfo &dim : mWmsDimensions )
41  {
42  if ( dim.name == wmsDimInfo.name )
43  {
44  return false;
45  }
46  }
47  mWmsDimensions.append( wmsDimInfo );
48  return true;
49 }
50 
51 bool QgsVectorLayerServerProperties::removeWmsDimension( const QString &wmsDimName )
52 {
53  for ( int i = 0; i < mWmsDimensions.size(); ++i )
54  {
55  if ( mWmsDimensions[ i ].name == wmsDimName )
56  {
57  mWmsDimensions.removeAt( i );
58  return true;
59  }
60  }
61  return false;
62 }
63 
64 const QList< QgsVectorLayerServerProperties::WmsDimensionInfo > QgsVectorLayerServerProperties::wmsDimensions() const
65 {
66  return mWmsDimensions;
67 }
68 
69 void QgsVectorLayerServerProperties::readXml( const QDomNode &layer_node )
70 {
71  const QgsFields fields = mLayer->fields();
72  // QGIS Server WMS Dimensions
73  const QDomNode wmsDimsNode = layer_node.namedItem( QStringLiteral( "wmsDimensions" ) );
74  if ( !wmsDimsNode.isNull() )
75  {
76  const QDomElement wmsDimsElem = wmsDimsNode.toElement();
77  QDomNodeList wmsDimsList = wmsDimsElem.elementsByTagName( QStringLiteral( "dimension" ) );
78  for ( int i = 0; i < wmsDimsList.size(); ++i )
79  {
80  QDomElement dimElem = wmsDimsList.at( i ).toElement();
81  QString dimName = dimElem.attribute( QStringLiteral( "name" ) );
82  QString dimFieldName = dimElem.attribute( QStringLiteral( "fieldName" ) );
83  // check field name
84  int dimFieldNameIndex = fields.indexOf( dimFieldName );
85  if ( dimFieldNameIndex == -1 )
86  {
87  continue;
88  }
89  QVariant dimRefValue;
90  int dimDefaultDisplayType = dimElem.attribute( QStringLiteral( "defaultDisplayType" ) ).toInt();
92  {
93  QString dimRefValueStr = dimElem.attribute( QStringLiteral( "referenceValue" ) );
94  if ( !dimRefValueStr.isEmpty() )
95  {
96  QgsField dimField = fields.at( dimFieldNameIndex );
97  dimRefValue = QVariant( dimRefValueStr );
98  if ( !dimField.convertCompatible( dimRefValue ) )
99  {
100  continue;
101  }
102  }
103  }
104  QgsVectorLayerServerProperties::WmsDimensionInfo dim( dimName, dimFieldName,
105  dimElem.attribute( QStringLiteral( "endFieldName" ) ),
106  dimElem.attribute( QStringLiteral( "units" ) ),
107  dimElem.attribute( QStringLiteral( "unitSymbol" ) ),
108  dimDefaultDisplayType, dimRefValue );
109  addWmsDimension( dim );
110  }
111  }
112 }
113 
114 void QgsVectorLayerServerProperties::writeXml( QDomNode &layer_node, QDomDocument &document ) const
115 {
116  // save QGIS Server WMS Dimension definitions
117  if ( mWmsDimensions.size() > 0 )
118  {
119  QDomElement wmsDimsElem = document.createElement( QStringLiteral( "wmsDimensions" ) );
120  for ( const QgsVectorLayerServerProperties::WmsDimensionInfo &dim : mWmsDimensions )
121  {
122  QDomElement dimElem = document.createElement( QStringLiteral( "dimension" ) );
123  dimElem.setAttribute( QStringLiteral( "name" ), dim.name );
124  dimElem.setAttribute( QStringLiteral( "fieldName" ), dim.fieldName );
125  dimElem.setAttribute( QStringLiteral( "endFieldName" ), dim.endFieldName );
126  dimElem.setAttribute( QStringLiteral( "units" ), dim.units );
127  dimElem.setAttribute( QStringLiteral( "unitSymbol" ), dim.unitSymbol );
128  dimElem.setAttribute( QStringLiteral( "defaultDisplayType" ), dim.defaultDisplayType );
129  dimElem.setAttribute( QStringLiteral( "referenceValue" ), dim.referenceValue.toString() );
130  wmsDimsElem.appendChild( dimElem );
131  }
132  layer_node.appendChild( wmsDimsElem );
133  }
134 }
QgsVectorLayerServerProperties(QgsVectorLayer *layer=nullptr)
Constructor - Creates a Vector Layer QGIS Server Properties.
Setting to define QGIS Server WMS Dimension.
Modify current selection to include only select features which match.
const QList< QgsVectorLayerServerProperties::WmsDimensionInfo > wmsDimensions() const
Returns the QGIS Server WMS Dimension list.
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format.
Definition: qgsfield.cpp:281
Container of fields for a vector layer.
Definition: qgsfields.h:42
void writeXml(QDomNode &layer_node, QDomDocument &document) const
Saves server properties to xml under the layer node.
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:163
static QMap< int, QString > wmsDimensionDefaultDisplayLabels()
Returns WMS Dimension default display labels.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
bool removeWmsDimension(const QString &wmsDimName)
Removes a QGIS Server WMS Dimension.
void readXml(const QDomNode &layer_node)
Reads server properties from project file.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
int indexOf(const QString &fieldName) const
Gets the field index from the field name.
Definition: qgsfields.cpp:207
bool addWmsDimension(const QgsVectorLayerServerProperties::WmsDimensionInfo &wmsDimInfo)
Adds a QGIS Server WMS Dimension.
Represents a vector layer which manages a vector based data sets.