QGIS API Documentation 3.37.0-Master (684a802617f)
Loading...
Searching...
No Matches
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"
21
22#include <QDomNode>
23
24// QgsServerMetadataUrlProperties
25
27{
28 return url == other.url &&
29 type == other.type &&
30 format == other.format;
31}
32
37
39{
40 mMetadataUrls.clear();
41}
42
43void 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
59void 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
85void 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 : std::as_const( 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
126const QList< QgsServerWmsDimensionProperties::WmsDimensionInfo > QgsServerWmsDimensionProperties::wmsDimensions() const
127{
128 return mWmsDimensions;
129}
130
131void QgsServerWmsDimensionProperties::readXml( const QDomNode &layer_node )
132{
133 reset();
134
135 // Apply only for vector layers
136 if ( layer()->type() != Qgis::LayerType::Vector )
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
186void 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
215
217{
220
221 properties->setShortName( mShortName );
222 properties->setTitle( mTitle );
223 properties->setAbstract( mAbstract );
224 properties->setKeywordList( mKeywordList );
225 properties->setDataUrl( mDataUrl );
226 properties->setDataUrlFormat( mDataUrlFormat );
227 properties->setAttribution( mAttribution );
228 properties->setAttributionUrl( mAttributionUrl );
229}
230
236
237void QgsMapLayerServerProperties::readXml( const QDomNode &layer_node )
238{
241
242 //short name
243 const QDomElement shortNameElem = layer_node.firstChildElement( QStringLiteral( "shortname" ) );
244 if ( !shortNameElem.isNull() )
245 {
246 mShortName = shortNameElem.text();
247 }
248
249 //title
250 const QDomElement titleElem = layer_node.firstChildElement( QStringLiteral( "title" ) );
251 if ( !titleElem.isNull() )
252 {
253 mTitle = titleElem.text();
254 }
255
256 //abstract
257 const QDomElement abstractElem = layer_node.firstChildElement( QStringLiteral( "abstract" ) );
258 if ( !abstractElem.isNull() )
259 {
260 mAbstract = abstractElem.text();
261 }
262
263 //keywordList
264 const QDomElement keywordListElem = layer_node.firstChildElement( QStringLiteral( "keywordList" ) );
265 if ( !keywordListElem.isNull() )
266 {
267 QStringList kwdList;
268 for ( QDomNode n = keywordListElem.firstChild(); !n.isNull(); n = n.nextSibling() )
269 {
270 kwdList << n.toElement().text();
271 }
272 mKeywordList = kwdList.join( QLatin1String( ", " ) );
273 }
274
275 //dataUrl
276 const QDomElement dataUrlElem = layer_node.firstChildElement( QStringLiteral( "dataUrl" ) );
277 if ( !dataUrlElem.isNull() )
278 {
279 mDataUrl = dataUrlElem.text();
280 mDataUrlFormat = dataUrlElem.attribute( QStringLiteral( "format" ), QString() );
281 }
282
283 //attribution
284 const QDomElement attribElem = layer_node.firstChildElement( QStringLiteral( "attribution" ) );
285 if ( !attribElem.isNull() )
286 {
287 mAttribution = attribElem.text();
288 mAttributionUrl = attribElem.attribute( QStringLiteral( "href" ), QString() );
289 }
290}
291
292void QgsMapLayerServerProperties::writeXml( QDomNode &layer_node, QDomDocument &document ) const
293{
294 QgsServerMetadataUrlProperties::writeXml( layer_node, document );
295 QgsServerWmsDimensionProperties::writeXml( layer_node, document );
296
297 // TODO -- ideally we should also be writing mTitle, mAbstract etc, but this method is currently
298 // only called for SOME map layer subclasses!
299 // Accordingly that logic is currently left in QgsMapLayer::writeLayerXml
300}
301
@ Vector
Vector layer.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
bool convertCompatible(QVariant &v, QString *errorMessage=nullptr) const
Converts the provided variant to a compatible format.
Definition qgsfield.cpp:472
Container of fields for a vector layer.
Definition qgsfields.h:46
Q_INVOKABLE int indexOf(const QString &fieldName) const
Gets the field index from the field name.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Manages QGIS Server properties for a map layer.
void readXml(const QDomNode &layer_node)
Reads server properties from project file.
void setDataUrl(const QString &dataUrl)
Sets the DataUrl of the layer used by QGIS Server in GetCapabilities request.
void setAbstract(const QString &abstract)
Sets the abstract of the layer used by QGIS Server in GetCapabilities request.
void setDataUrlFormat(const QString &dataUrlFormat)
Sets the DataUrl format of the layerused by QGIS Server in GetCapabilities request.
void setAttributionUrl(const QString &url)
Sets the attribution url of the layer used by QGIS Server in GetCapabilities request.
void setAttribution(const QString &attrib)
Sets the attribution of the layer used by QGIS Server in GetCapabilities request.
void setShortName(const QString &name)
Sets the short name of the layer used by QGIS Server to identify the layer.
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 setTitle(const QString &title)
Sets the title of the layer used by QGIS Server in GetCapabilities request.
void setKeywordList(const QString &keywords)
Sets the keywords list of the layerused by QGIS Server in GetCapabilities request.
void reset()
Reset properties to default.
Base class for all map layer types.
Definition qgsmaplayer.h:75
Manages QGIS Server properties for a map layer.
virtual const QgsMapLayer * layer() const =0
Gets the parent 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.
QList< QgsServerMetadataUrlProperties::MetadataUrl > metadataUrls() const
Returns a list of metadataUrl resources associated for the layer.
void readXml(const QDomNode &layer_node)
Reads server properties from project file.
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.