QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
qgsmaplayerserverproperties.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaplayerserverproperties.cpp
3  ------------------
4  begin : June 21, 2021
5  copyright : (C) 2021 by Etienne Trimaille
6  email : etrimaille 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 "qgsmaplayer.h"
20 #include "vector/qgsvectorlayer.h"
21 
22 #include <QDomNode>
23 
24 // QgsServerMetadataUrlProperties
25 
27 {
28  return url == other.url &&
29  type == other.type &&
30  format == other.format;
31 }
32 
34 {
35  properties->setMetadataUrls( metadataUrls() );
36 }
37 
39 {
40  mMetadataUrls.clear();
41 }
42 
43 void QgsServerMetadataUrlProperties::readXml( const QDomNode &layer_node )
44 {
45  QDomElement element = layer_node.namedItem( QStringLiteral( "metadataUrls" ) ).toElement();
46  mMetadataUrls.clear();
47  const QDomNodeList el = element.elementsByTagName( QStringLiteral( "metadataUrl" ) );
48  for ( int i = 0; i < el.size(); i++ )
49  {
50  element = el.at( i ).toElement();
52  oneUrl.type = element.attribute( QStringLiteral( "type" ) );
53  oneUrl.format = element.attribute( QStringLiteral( "format" ) );
54  oneUrl.url = element.text();
55  addMetadataUrl( oneUrl );
56  }
57 }
58 
59 void QgsServerMetadataUrlProperties::writeXml( QDomNode &layer_node, QDomDocument &document ) const
60 {
61  QDomElement urls = document.createElement( QStringLiteral( "metadataUrls" ) );
62  for ( const QgsMapLayerServerProperties::MetadataUrl &url : mMetadataUrls )
63  {
64  QDomElement urlElement = document.createElement( QStringLiteral( "metadataUrl" ) );
65  urlElement.setAttribute( QStringLiteral( "type" ), url.type );
66  urlElement.setAttribute( QStringLiteral( "format" ), url.format );
67  urlElement.appendChild( document.createTextNode( url.url ) );
68  urls.appendChild( urlElement );
69  }
70  layer_node.appendChild( urls );
71 }
72 
73 // QgsServerWmsDimensionProperties
74 
76 {
77  properties->setWmsDimensions( wmsDimensions() );
78 }
79 
81 {
82  mWmsDimensions.clear();
83 }
84 
85 void QgsServerWmsDimensionProperties::setWmsDimensions( const QList<QgsServerWmsDimensionProperties::WmsDimensionInfo> &dimensions )
86 {
87  mWmsDimensions = dimensions;
88 }
89 
91 {
92  QMap<int, QString> labels;
93  labels[QgsServerWmsDimensionProperties::WmsDimensionInfo::AllValues] = QObject::tr( "All values" );
94  labels[QgsServerWmsDimensionProperties::WmsDimensionInfo::MinValue] = QObject::tr( "Min value" );
95  labels[QgsServerWmsDimensionProperties::WmsDimensionInfo::MaxValue] = QObject::tr( "Max value" );
96  labels[QgsServerWmsDimensionProperties::WmsDimensionInfo::ReferenceValue] = QObject::tr( "Reference value" );
97  return labels;
98 }
99 
101 {
102  for ( const QgsServerWmsDimensionProperties::WmsDimensionInfo &dim : mWmsDimensions )
103  {
104  if ( dim.name == wmsDimInfo.name )
105  {
106  return false;
107  }
108  }
109  mWmsDimensions.append( wmsDimInfo );
110  return true;
111 }
112 
114 {
115  for ( int i = 0; i < mWmsDimensions.size(); ++i )
116  {
117  if ( mWmsDimensions[ i ].name == wmsDimName )
118  {
119  mWmsDimensions.removeAt( i );
120  return true;
121  }
122  }
123  return false;
124 }
125 
126 const QList< QgsServerWmsDimensionProperties::WmsDimensionInfo > QgsServerWmsDimensionProperties::wmsDimensions() const
127 {
128  return mWmsDimensions;
129 }
130 
131 void QgsServerWmsDimensionProperties::readXml( const QDomNode &layer_node )
132 {
133  reset();
134 
135  // Apply only for vector layers
136  if ( layer()->type() != QgsMapLayerType::VectorLayer )
137  return;
138 
139  const QgsFields fields = static_cast<const QgsVectorLayer *>( layer() )->fields();
140  // QGIS Server WMS Dimensions
141  const QDomNode wmsDimsNode = layer_node.namedItem( QStringLiteral( "wmsDimensions" ) );
142  if ( wmsDimsNode.isNull() )
143  {
144  return;
145  }
146  const QDomElement wmsDimsElem = wmsDimsNode.toElement();
147  const QDomNodeList wmsDimsList = wmsDimsElem.elementsByTagName( QStringLiteral( "dimension" ) );
148  for ( int i = 0; i < wmsDimsList.size(); ++i )
149  {
150  const QDomElement dimElem = wmsDimsList.at( i ).toElement();
151  const QString dimName = dimElem.attribute( QStringLiteral( "name" ) );
152  const QString dimFieldName = dimElem.attribute( QStringLiteral( "fieldName" ) );
153  // check field name
154  const int dimFieldNameIndex = fields.indexOf( dimFieldName );
155  if ( dimFieldNameIndex == -1 )
156  {
157  continue;
158  }
159  QVariant dimRefValue;
160  const int dimDefaultDisplayType = dimElem.attribute( QStringLiteral( "defaultDisplayType" ) ).toInt();
162  {
163  const QString dimRefValueStr = dimElem.attribute( QStringLiteral( "referenceValue" ) );
164  if ( !dimRefValueStr.isEmpty() )
165  {
166  const QgsField dimField = fields.at( dimFieldNameIndex );
167  dimRefValue = QVariant( dimRefValueStr );
168  if ( !dimField.convertCompatible( dimRefValue ) )
169  {
170  continue;
171  }
172  }
173  }
174  QgsServerWmsDimensionProperties::WmsDimensionInfo dim( dimName, dimFieldName,
175  dimElem.attribute( QStringLiteral( "endFieldName" ) ),
176  dimElem.attribute( QStringLiteral( "units" ) ),
177  dimElem.attribute( QStringLiteral( "unitSymbol" ) ),
178  dimDefaultDisplayType, dimRefValue );
179  //XXX This add O(n^2) complexity !!!!
180  // addWmsDimension( dim );
181  // Better to trust the XML:
182  mWmsDimensions.append( dim );
183  }
184 }
185 
186 void QgsServerWmsDimensionProperties::writeXml( QDomNode &layer_node, QDomDocument &document ) const
187 {
188  // save QGIS Server WMS Dimension definitions
189  if ( ! mWmsDimensions.isEmpty() )
190  {
191  QDomElement wmsDimsElem = document.createElement( QStringLiteral( "wmsDimensions" ) );
192  for ( const QgsServerWmsDimensionProperties::WmsDimensionInfo &dim : mWmsDimensions )
193  {
194  QDomElement dimElem = document.createElement( QStringLiteral( "dimension" ) );
195  dimElem.setAttribute( QStringLiteral( "name" ), dim.name );
196  dimElem.setAttribute( QStringLiteral( "fieldName" ), dim.fieldName );
197  dimElem.setAttribute( QStringLiteral( "endFieldName" ), dim.endFieldName );
198  dimElem.setAttribute( QStringLiteral( "units" ), dim.units );
199  dimElem.setAttribute( QStringLiteral( "unitSymbol" ), dim.unitSymbol );
200  dimElem.setAttribute( QStringLiteral( "defaultDisplayType" ), dim.defaultDisplayType );
201  dimElem.setAttribute( QStringLiteral( "referenceValue" ), dim.referenceValue.toString() );
202  wmsDimsElem.appendChild( dimElem );
203  }
204  layer_node.appendChild( wmsDimsElem );
205  }
206 }
207 
208 
209 // QgsMapLayerServerProperties
210 
212  : mLayer( layer )
213 {
214 }
215 
217 {
220 }
221 
223 {
226 }
227 
228 void QgsMapLayerServerProperties::readXml( const QDomNode &layer_node )
229 {
232 }
233 
234 void QgsMapLayerServerProperties::writeXml( QDomNode &layer_node, QDomDocument &document ) const
235 {
236  QgsServerMetadataUrlProperties::writeXml( layer_node, document );
237  QgsServerWmsDimensionProperties::writeXml( layer_node, document );
238 }
239 
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:51
bool convertCompatible(QVariant &v, QString *errorMessage=nullptr) const
Converts the provided variant to a compatible format.
Definition: qgsfield.cpp:384
Container of fields for a vector layer.
Definition: qgsfields.h:45
int indexOf(const QString &fieldName) const
Gets the field index from the field name.
Definition: qgsfields.cpp:207
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Definition: qgsfields.cpp:163
Manages QGIS Server properties for a map layer.
void readXml(const QDomNode &layer_node)
Reads server properties from project file.
void copyTo(QgsMapLayerServerProperties *properties) const
Copy properties to another instance.
QgsMapLayerServerProperties(QgsMapLayer *layer=nullptr)
Constructor - Creates a Map Layer QGIS Server Properties.
void writeXml(QDomNode &layer_node, QDomDocument &document) const
Saves server properties to xml under the layer node.
void reset()
Reset properties to default.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
Manages QGIS Server properties for a map layer.
void addMetadataUrl(const QgsServerMetadataUrlProperties::MetadataUrl &metaUrl)
Add a metadataUrl for the layer.
void writeXml(QDomNode &layer_node, QDomDocument &document) const
Saves server properties to xml under the layer node.
void reset()
Reset properties to default.
void setMetadataUrls(const QList< QgsServerMetadataUrlProperties::MetadataUrl > &metaUrls)
Sets a the list of metadata URL for the layer.
void readXml(const QDomNode &layer_node)
Reads server properties from project file.
QList< QgsServerMetadataUrlProperties::MetadataUrl > metadataUrls() const
Returns a list of metadataUrl resources associated for the layer.
virtual const QgsMapLayer * layer() const =0
Gets the parent layer.
void copyTo(QgsServerMetadataUrlProperties *properties) const
Copy properties to another instance.
Manages QGIS Server properties for Wms dimensions.
void readXml(const QDomNode &layer_node)
Reads server properties from project file.
static QMap< int, QString > wmsDimensionDefaultDisplayLabels()
Returns WMS Dimension default display labels.
void writeXml(QDomNode &layer_node, QDomDocument &document) const
Saves server properties to xml under the layer node.
void reset()
Reset properties to default.
bool addWmsDimension(const QgsServerWmsDimensionProperties::WmsDimensionInfo &wmsDimInfo)
Adds a QGIS Server WMS Dimension.
void copyTo(QgsServerWmsDimensionProperties *properties) const
Copy properties to another instance.
bool removeWmsDimension(const QString &wmsDimName)
Removes a QGIS Server WMS Dimension.
const QList< QgsServerWmsDimensionProperties::WmsDimensionInfo > wmsDimensions() const
Returns the QGIS Server WMS Dimension list.
void setWmsDimensions(const QList< QgsServerWmsDimensionProperties::WmsDimensionInfo > &dimensions)
Set WMS Dimensions.
Represents a vector layer which manages a vector based data sets.
bool operator==(const QgsServerMetadataUrlProperties::MetadataUrl &other) const
Compare two MetadataUrl structure.
QString format
Format specification of online resource.
Setting to define QGIS Server WMS Dimension.
@ MaxValue
Modify current selection to include only select features which match.