QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
20#include "qgsmaplayer.h"
22
23#include <QDomNode>
24
25#include "moc_qgsmaplayerserverproperties.cpp"
26
27// QgsServerMetadataUrlProperties
28
30{
31 return url == other.url &&
32 type == other.type &&
33 format == other.format;
34}
35
37{
38 return mMetadataUrls == other.mMetadataUrls;
39}
40
42{
43 return !( *this == other );
44}
45
47{
48 return name == other.name
49 && fieldName == other.fieldName
50 && endFieldName == other.endFieldName
51 && units == other.units
52 && unitSymbol == other.unitSymbol
55}
56
58{
59 return !( *this == other );
60}
61
66
68{
69 mMetadataUrls.clear();
70}
71
72void QgsServerMetadataUrlProperties::readXml( const QDomNode &layer_node )
73{
74 QDomElement element = layer_node.namedItem( QStringLiteral( "metadataUrls" ) ).toElement();
75 mMetadataUrls.clear();
76 const QDomNodeList el = element.elementsByTagName( QStringLiteral( "metadataUrl" ) );
77 for ( int i = 0; i < el.size(); i++ )
78 {
79 element = el.at( i ).toElement();
81 oneUrl.type = element.attribute( QStringLiteral( "type" ) );
82 oneUrl.format = element.attribute( QStringLiteral( "format" ) );
83 oneUrl.url = element.text();
84 addMetadataUrl( oneUrl );
85 }
86}
87
88void QgsServerMetadataUrlProperties::writeXml( QDomNode &layer_node, QDomDocument &document ) const
89{
90 if ( !mMetadataUrls.empty() )
91 {
92 QDomElement urls = document.createElement( QStringLiteral( "metadataUrls" ) );
93 for ( const QgsMapLayerServerProperties::MetadataUrl &url : mMetadataUrls )
94 {
95 QDomElement urlElement = document.createElement( QStringLiteral( "metadataUrl" ) );
96 urlElement.setAttribute( QStringLiteral( "type" ), url.type );
97 urlElement.setAttribute( QStringLiteral( "format" ), url.format );
98 urlElement.appendChild( document.createTextNode( url.url ) );
99 urls.appendChild( urlElement );
100 }
101 layer_node.appendChild( urls );
102 }
103}
104
105// QgsServerWmsDimensionProperties
106
108{
109 return mWmsDimensions == other.mWmsDimensions;
110}
111
113{
114 return !( *this == other );
115}
116
121
123{
124 mWmsDimensions.clear();
125}
126
127void QgsServerWmsDimensionProperties::setWmsDimensions( const QList<QgsServerWmsDimensionProperties::WmsDimensionInfo> &dimensions )
128{
129 mWmsDimensions = dimensions;
130}
131
133{
134 QMap<int, QString> labels;
135 labels[QgsServerWmsDimensionProperties::WmsDimensionInfo::AllValues] = QObject::tr( "All values" );
136 labels[QgsServerWmsDimensionProperties::WmsDimensionInfo::MinValue] = QObject::tr( "Min value" );
137 labels[QgsServerWmsDimensionProperties::WmsDimensionInfo::MaxValue] = QObject::tr( "Max value" );
138 labels[QgsServerWmsDimensionProperties::WmsDimensionInfo::ReferenceValue] = QObject::tr( "Reference value" );
139 return labels;
140}
141
143{
144 for ( const QgsServerWmsDimensionProperties::WmsDimensionInfo &dim : std::as_const( mWmsDimensions ) )
145 {
146 if ( dim.name == wmsDimInfo.name )
147 {
148 return false;
149 }
150 }
151 mWmsDimensions.append( wmsDimInfo );
152 return true;
153}
154
156{
157 for ( int i = 0; i < mWmsDimensions.size(); ++i )
158 {
159 if ( mWmsDimensions[ i ].name == wmsDimName )
160 {
161 mWmsDimensions.removeAt( i );
162 return true;
163 }
164 }
165 return false;
166}
167
168const QList< QgsServerWmsDimensionProperties::WmsDimensionInfo > QgsServerWmsDimensionProperties::wmsDimensions() const
169{
170 return mWmsDimensions;
171}
172
173void QgsServerWmsDimensionProperties::readXml( const QDomNode &layer_node )
174{
175 reset();
176
177 // Apply only for vector layers
178 if ( !layer() || layer()->type() != Qgis::LayerType::Vector )
179 return;
180
181 const QgsFields fields = static_cast<const QgsVectorLayer *>( layer() )->fields();
182 // QGIS Server WMS Dimensions
183 const QDomNode wmsDimsNode = layer_node.namedItem( QStringLiteral( "wmsDimensions" ) );
184 if ( wmsDimsNode.isNull() )
185 {
186 return;
187 }
188 const QDomElement wmsDimsElem = wmsDimsNode.toElement();
189 const QDomNodeList wmsDimsList = wmsDimsElem.elementsByTagName( QStringLiteral( "dimension" ) );
190 for ( int i = 0; i < wmsDimsList.size(); ++i )
191 {
192 const QDomElement dimElem = wmsDimsList.at( i ).toElement();
193 const QString dimName = dimElem.attribute( QStringLiteral( "name" ) );
194 const QString dimFieldName = dimElem.attribute( QStringLiteral( "fieldName" ) );
195 // check field name
196 const int dimFieldNameIndex = fields.indexOf( dimFieldName );
197 if ( dimFieldNameIndex == -1 )
198 {
199 continue;
200 }
201 QVariant dimRefValue;
202 const int dimDefaultDisplayType = dimElem.attribute( QStringLiteral( "defaultDisplayType" ) ).toInt();
204 {
205 const QString dimRefValueStr = dimElem.attribute( QStringLiteral( "referenceValue" ) );
206 if ( !dimRefValueStr.isEmpty() )
207 {
208 const QgsField dimField = fields.at( dimFieldNameIndex );
209 dimRefValue = QVariant( dimRefValueStr );
210 if ( !dimField.convertCompatible( dimRefValue ) )
211 {
212 continue;
213 }
214 }
215 }
216 QgsServerWmsDimensionProperties::WmsDimensionInfo dim( dimName, dimFieldName,
217 dimElem.attribute( QStringLiteral( "endFieldName" ) ),
218 dimElem.attribute( QStringLiteral( "units" ) ),
219 dimElem.attribute( QStringLiteral( "unitSymbol" ) ),
220 dimDefaultDisplayType, dimRefValue );
221 //XXX This add O(n^2) complexity !!!!
222 // addWmsDimension( dim );
223 // Better to trust the XML:
224 mWmsDimensions.append( dim );
225 }
226}
227
228void QgsServerWmsDimensionProperties::writeXml( QDomNode &layer_node, QDomDocument &document ) const
229{
230 // save QGIS Server WMS Dimension definitions
231 if ( ! mWmsDimensions.isEmpty() )
232 {
233 QDomElement wmsDimsElem = document.createElement( QStringLiteral( "wmsDimensions" ) );
234 for ( const QgsServerWmsDimensionProperties::WmsDimensionInfo &dim : mWmsDimensions )
235 {
236 QDomElement dimElem = document.createElement( QStringLiteral( "dimension" ) );
237 dimElem.setAttribute( QStringLiteral( "name" ), dim.name );
238 dimElem.setAttribute( QStringLiteral( "fieldName" ), dim.fieldName );
239 dimElem.setAttribute( QStringLiteral( "endFieldName" ), dim.endFieldName );
240 dimElem.setAttribute( QStringLiteral( "units" ), dim.units );
241 dimElem.setAttribute( QStringLiteral( "unitSymbol" ), dim.unitSymbol );
242 dimElem.setAttribute( QStringLiteral( "defaultDisplayType" ), dim.defaultDisplayType );
243 dimElem.setAttribute( QStringLiteral( "referenceValue" ), dim.referenceValue.toString() );
244 wmsDimsElem.appendChild( dimElem );
245 }
246 layer_node.appendChild( wmsDimsElem );
247 }
248}
249
250
251// QgsMapLayerServerProperties
252
257
259{
262
263 properties->setShortName( mShortName );
264 properties->setTitle( mTitle );
265 properties->setAbstract( mAbstract );
266 properties->setKeywordList( mKeywordList );
267 properties->setDataUrl( mDataUrl );
268 properties->setDataUrlFormat( mDataUrlFormat );
269 properties->setAttribution( mAttribution );
270 properties->setAttributionUrl( mAttributionUrl );
271 properties->setLegendUrl( mLegendUrl );
272 properties->setLegendUrlFormat( mLegendUrlFormat );
273}
274
276{
279 && mShortName == other.mShortName
280 && mTitle == other.mTitle
281 && mAbstract == other.mAbstract
282 && mKeywordList == other.mKeywordList
283 && mDataUrl == other.mDataUrl
284 && mDataUrlFormat == other.mDataUrlFormat
285 && mAttribution == other.mAttribution
286 && mAttributionUrl == other.mAttributionUrl
287 && mLegendUrl == other.mLegendUrl
288 && mLegendUrlFormat == other.mLegendUrlFormat;
289}
290
292{
293 return !( *this == other );
294}
295
301
303{
304 QString name = mLayer ? mLayer->name() : QString();
305 if ( !mShortName.isEmpty() )
306 name = mShortName;
307
308 name.replace( ' ', '_' ).replace( ':', '-' ).replace( QChar( 0x2014 ) /* em-dash */, '-' );
309
310 return name.toLocal8Bit();
311}
312
313void QgsMapLayerServerProperties::readXml( const QDomNode &layerNode ) // cppcheck-suppress duplInheritedMember
314{
316 if ( metadataUrls().isEmpty() )
317 {
318 // metadataUrl is still empty, maybe it's a QGIS Project < 3.22
319 // keep for legacy
320 const QDomElement metaUrlElem = layerNode.firstChildElement( QStringLiteral( "metadataUrl" ) );
321 if ( !metaUrlElem.isNull() )
322 {
323 const QString url = metaUrlElem.text();
324 const QString type = metaUrlElem.attribute( QStringLiteral( "type" ), QString() );
325 const QString format = metaUrlElem.attribute( QStringLiteral( "format" ), QString() );
326 const QgsMapLayerServerProperties::MetadataUrl newItem( url, type, format );
327 setMetadataUrls( QList<QgsMapLayerServerProperties::MetadataUrl>() << newItem );
328 }
329 }
330
332
333 //short name
334 const QDomElement shortNameElem = layerNode.firstChildElement( QStringLiteral( "shortname" ) );
335 if ( !shortNameElem.isNull() )
336 {
337 mShortName = shortNameElem.text();
338 }
339
340 //title
341 const QDomElement titleElem = layerNode.firstChildElement( QStringLiteral( "title" ) );
342 if ( !titleElem.isNull() )
343 {
344 mTitle = titleElem.text();
345 mWfsTitle = titleElem.attribute( QStringLiteral( "wfs" ) );
346 }
347
348 //abstract
349 const QDomElement abstractElem = layerNode.firstChildElement( QStringLiteral( "abstract" ) );
350 if ( !abstractElem.isNull() )
351 {
352 mAbstract = abstractElem.text();
353 }
354
355 //keywordList
356 const QDomElement keywordListElem = layerNode.firstChildElement( QStringLiteral( "keywordList" ) );
357 if ( !keywordListElem.isNull() )
358 {
359 QStringList kwdList;
360 for ( QDomNode n = keywordListElem.firstChild(); !n.isNull(); n = n.nextSibling() )
361 {
362 const QString keyword = n.toElement().text();
363 if ( !keyword.isEmpty() )
364 kwdList << keyword;
365 }
366 mKeywordList = kwdList.join( QLatin1String( ", " ) );
367 }
368
369 //dataUrl
370 const QDomElement dataUrlElem = layerNode.firstChildElement( QStringLiteral( "dataUrl" ) );
371 if ( !dataUrlElem.isNull() )
372 {
373 mDataUrl = dataUrlElem.text();
374 mDataUrlFormat = dataUrlElem.attribute( QStringLiteral( "format" ), QString() );
375 }
376
377 //attribution
378 const QDomElement attribElem = layerNode.firstChildElement( QStringLiteral( "attribution" ) );
379 if ( !attribElem.isNull() )
380 {
381 mAttribution = attribElem.text();
382 mAttributionUrl = attribElem.attribute( QStringLiteral( "href" ), QString() );
383 }
384
385 //legendUrl
386 const QDomElement legendUrlElem = layerNode.firstChildElement( QStringLiteral( "legendUrl" ) );
387 if ( !legendUrlElem.isNull() )
388 {
389 mLegendUrl = legendUrlElem.text();
390 mLegendUrlFormat = legendUrlElem.attribute( QStringLiteral( "format" ), QString() );
391 }
392}
393
394void QgsMapLayerServerProperties::writeXml( QDomNode &layerNode, QDomDocument &document ) const // cppcheck-suppress duplInheritedMember
395{
396 QgsServerMetadataUrlProperties::writeXml( layerNode, document );
397 QgsServerWmsDimensionProperties::writeXml( layerNode, document );
398
399 // layer short name
400 if ( !mShortName.isEmpty() )
401 {
402 QDomElement layerShortName = document.createElement( QStringLiteral( "shortname" ) );
403 const QDomText layerShortNameText = document.createTextNode( mShortName );
404 layerShortName.appendChild( layerShortNameText );
405 layerNode.appendChild( layerShortName );
406 }
407
408 // layer title
409 if ( !mTitle.isEmpty() )
410 {
411 QDomElement layerTitle = document.createElement( QStringLiteral( "title" ) );
412 const QDomText layerTitleText = document.createTextNode( mTitle );
413 layerTitle.appendChild( layerTitleText );
414
415 if ( mTitle != mWfsTitle )
416 {
417 layerTitle.setAttribute( "wfs", mWfsTitle );
418 }
419
420 layerNode.appendChild( layerTitle );
421 }
422
423 // layer abstract
424 if ( !mAbstract.isEmpty() )
425 {
426 QDomElement layerAbstract = document.createElement( QStringLiteral( "abstract" ) );
427 const QDomText layerAbstractText = document.createTextNode( mAbstract );
428 layerAbstract.appendChild( layerAbstractText );
429 layerNode.appendChild( layerAbstract );
430 }
431
432 // layer keyword list
433 const QStringList keywordStringList = mKeywordList.split( ',', Qt::SkipEmptyParts );
434 if ( !keywordStringList.isEmpty() )
435 {
436 QDomElement layerKeywordList = document.createElement( QStringLiteral( "keywordList" ) );
437 for ( int i = 0; i < keywordStringList.size(); ++i )
438 {
439 QDomElement layerKeywordValue = document.createElement( QStringLiteral( "value" ) );
440 const QDomText layerKeywordText = document.createTextNode( keywordStringList.at( i ).trimmed() );
441 layerKeywordValue.appendChild( layerKeywordText );
442 layerKeywordList.appendChild( layerKeywordValue );
443 }
444 layerNode.appendChild( layerKeywordList );
445 }
446
447 // layer dataUrl
448 if ( !mDataUrl.isEmpty() )
449 {
450 QDomElement layerDataUrl = document.createElement( QStringLiteral( "dataUrl" ) );
451 const QDomText layerDataUrlText = document.createTextNode( mDataUrl );
452 layerDataUrl.appendChild( layerDataUrlText );
453 layerDataUrl.setAttribute( QStringLiteral( "format" ), mDataUrlFormat );
454 layerNode.appendChild( layerDataUrl );
455 }
456
457 // layer legendUrl
458 if ( !mLegendUrl.isEmpty() )
459 {
460 QDomElement layerLegendUrl = document.createElement( QStringLiteral( "legendUrl" ) );
461 const QDomText layerLegendUrlText = document.createTextNode( mLegendUrl );
462 layerLegendUrl.appendChild( layerLegendUrlText );
463 layerLegendUrl.setAttribute( QStringLiteral( "format" ), mLegendUrlFormat );
464 layerNode.appendChild( layerLegendUrl );
465 }
466
467 // layer attribution
468 if ( !mAttribution.isEmpty() )
469 {
470 QDomElement layerAttribution = document.createElement( QStringLiteral( "attribution" ) );
471 const QDomText layerAttributionText = document.createTextNode( mAttribution );
472 layerAttribution.appendChild( layerAttributionText );
473 layerAttribution.setAttribute( QStringLiteral( "href" ), mAttributionUrl );
474 layerNode.appendChild( layerAttribution );
475 }
476}
@ Vector
Vector layer.
Definition qgis.h:191
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:54
bool convertCompatible(QVariant &v, QString *errorMessage=nullptr) const
Converts the provided variant to a compatible format.
Definition qgsfield.cpp:475
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).
void setLegendUrl(const QString &legendUrl)
Sets the URL for the layer's legend.
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 setLegendUrlFormat(const QString &legendUrlFormat)
Sets the format for a URL based layer legend.
void copyTo(QgsMapLayerServerProperties *properties) const
Copy properties to another instance.
bool operator==(const QgsMapLayerServerProperties &other) const
QgsMapLayerServerProperties(QgsMapLayer *layer=nullptr)
Constructor - Creates a Map Layer QGIS Server Properties.
QString wfsTypeName() const
Returns WFS typename for the layer.
const QgsMapLayer * layer() const override
Gets the parent layer.
bool operator!=(const QgsMapLayerServerProperties &other) const
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:80
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.
bool operator==(const QgsServerMetadataUrlProperties &other) const
void writeXml(QDomNode &layerNode, 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.
bool operator!=(const QgsServerMetadataUrlProperties &other) const
void copyTo(QgsServerMetadataUrlProperties *properties) const
Copy properties to another instance.
void readXml(const QDomNode &layerNode)
Reads server properties from project file.
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 operator==(const QgsServerWmsDimensionProperties &other) const
bool operator!=(const QgsServerWmsDimensionProperties &other) const
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 dataset.
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.
WmsDimensionInfo(const QString &dimName, const QString &dimFieldName, const QString &dimEndFieldName=QString(), const QString &dimUnits=QString(), const QString &dimUnitSymbol=QString(), const int &dimDefaultDisplayType=QgsServerWmsDimensionProperties::WmsDimensionInfo::AllValues, const QVariant &dimReferenceValue=QVariant())
Constructor for WmsDimensionInfo.
@ MaxValue
Modify current selection to include only select features which match.