QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
qgsabstractmetadatabase.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  QgsAbstractMetadataBase.cpp
3  --------------------
4  begin : March 2018
5  copyright : (C) 2018 by Nyall Dawson
6  email : nyall dot dawson at gmail 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 
22 {
23  return mIdentifier;
24 }
25 
26 void QgsAbstractMetadataBase::setIdentifier( const QString &identifier )
27 {
29 }
30 
32 {
33  return mParentIdentifier;
34 }
35 
36 void QgsAbstractMetadataBase::setParentIdentifier( const QString &parentIdentifier )
37 {
39 }
40 
42 {
43  return mType;
44 }
45 
46 void QgsAbstractMetadataBase::setType( const QString &type )
47 {
48  mType = type;
49 }
50 
52 {
53  return mTitle;
54 }
55 
56 void QgsAbstractMetadataBase::setTitle( const QString &title )
57 {
58  mTitle = title;
59 }
60 
62 {
63  return mAbstract;
64 }
65 
66 void QgsAbstractMetadataBase::setAbstract( const QString &abstract )
67 {
68  mAbstract = abstract;
69 }
70 
72 {
73  return mHistory;
74 }
75 
76 void QgsAbstractMetadataBase::setHistory( const QStringList &history )
77 {
78  mHistory = history;
79 }
80 
81 void QgsAbstractMetadataBase::addHistoryItem( const QString &text )
82 {
83  mHistory << text;
84 }
85 
86 QMap<QString, QStringList> QgsAbstractMetadataBase::keywords() const
87 {
88  return mKeywords;
89 }
90 
91 void QgsAbstractMetadataBase::setKeywords( const QMap<QString, QStringList> &keywords )
92 {
94 }
95 
96 void QgsAbstractMetadataBase::addKeywords( const QString &vocabulary, const QStringList &keywords )
97 {
98  mKeywords.insert( vocabulary, keywords );
99 }
100 
101 bool QgsAbstractMetadataBase::removeKeywords( const QString &vocabulary )
102 {
103  return mKeywords.remove( vocabulary );
104 }
105 
107 {
108  return mKeywords.keys();
109 }
110 
111 QStringList QgsAbstractMetadataBase::keywords( const QString &vocabulary ) const
112 {
113  return mKeywords.value( vocabulary );
114 }
115 
117 {
118  if ( mKeywords.contains( QStringLiteral( "gmd:topicCategory" ) ) )
119  {
120  return mKeywords.value( QStringLiteral( "gmd:topicCategory" ) );
121  }
122  else
123  {
124  return QStringList();
125  }
126 }
127 
128 void QgsAbstractMetadataBase::setCategories( const QStringList &category )
129 {
130  mKeywords.insert( QStringLiteral( "gmd:topicCategory" ), category );
131 }
132 
133 QList<QgsAbstractMetadataBase::Contact> QgsAbstractMetadataBase::contacts() const
134 {
135  return mContacts;
136 }
137 
138 void QgsAbstractMetadataBase::setContacts( const QList<QgsAbstractMetadataBase::Contact> &contacts )
139 {
141 }
142 
144 {
145  mContacts << contact;
146 }
147 
148 QList<QgsAbstractMetadataBase::Link> QgsAbstractMetadataBase::links() const
149 {
150  return mLinks;
151 }
152 
153 void QgsAbstractMetadataBase::setLinks( const QList<QgsAbstractMetadataBase::Link> &links )
154 {
155  mLinks = links;
156 }
157 
159 {
160  mLinks << link;
161 }
162 
164 {
165  return mLanguage;
166 }
167 
168 void QgsAbstractMetadataBase::setLanguage( const QString &language )
169 {
171 }
172 
173 bool QgsAbstractMetadataBase::readMetadataXml( const QDomElement &metadataElement )
174 {
175  QDomNode mnl;
176  QDomElement mne;
177 
178  // set identifier
179  mnl = metadataElement.namedItem( QStringLiteral( "identifier" ) );
180  mIdentifier = mnl.toElement().text();
181 
182  // set parent identifier
183  mnl = metadataElement.namedItem( QStringLiteral( "parentidentifier" ) );
184  mParentIdentifier = mnl.toElement().text();
185 
186  // set language
187  mnl = metadataElement.namedItem( QStringLiteral( "language" ) );
188  mLanguage = mnl.toElement().text();
189 
190  // set type
191  mnl = metadataElement.namedItem( QStringLiteral( "type" ) );
192  mType = mnl.toElement().text();
193 
194  // set title
195  mnl = metadataElement.namedItem( QStringLiteral( "title" ) );
196  mTitle = mnl.toElement().text();
197 
198  // set abstract
199  mnl = metadataElement.namedItem( QStringLiteral( "abstract" ) );
200  mAbstract = mnl.toElement().text();
201 
202  // set keywords
203  const QDomNodeList keywords = metadataElement.elementsByTagName( QStringLiteral( "keywords" ) );
204  mKeywords.clear();
205  for ( int i = 0; i < keywords.size(); i++ )
206  {
207  QStringList keywordsList;
208  mnl = keywords.at( i );
209  mne = mnl.toElement();
210 
211  const QDomNodeList el = mne.elementsByTagName( QStringLiteral( "keyword" ) );
212  for ( int j = 0; j < el.size(); j++ )
213  {
214  keywordsList.append( el.at( j ).toElement().text() );
215  }
216  addKeywords( mne.attribute( QStringLiteral( "vocabulary" ) ), keywordsList );
217  }
218 
219  // contact
220  const QDomNodeList contactsList = metadataElement.elementsByTagName( QStringLiteral( "contact" ) );
221  mContacts.clear();
222  for ( int i = 0; i < contactsList.size(); i++ )
223  {
224  mnl = contactsList.at( i );
225  mne = mnl.toElement();
226 
228  oneContact.name = mne.namedItem( QStringLiteral( "name" ) ).toElement().text();
229  oneContact.organization = mne.namedItem( QStringLiteral( "organization" ) ).toElement().text();
230  oneContact.position = mne.namedItem( QStringLiteral( "position" ) ).toElement().text();
231  oneContact.voice = mne.namedItem( QStringLiteral( "voice" ) ).toElement().text();
232  oneContact.fax = mne.namedItem( QStringLiteral( "fax" ) ).toElement().text();
233  oneContact.email = mne.namedItem( QStringLiteral( "email" ) ).toElement().text();
234  oneContact.role = mne.namedItem( QStringLiteral( "role" ) ).toElement().text();
235 
236  QList< QgsAbstractMetadataBase::Address > addresses;
237  const QDomNodeList addressList = mne.elementsByTagName( QStringLiteral( "contactAddress" ) );
238  for ( int j = 0; j < addressList.size(); j++ )
239  {
240  const QDomElement addressElement = addressList.at( j ).toElement();
242  oneAddress.address = addressElement.namedItem( QStringLiteral( "address" ) ).toElement().text();
243  oneAddress.administrativeArea = addressElement.namedItem( QStringLiteral( "administrativearea" ) ).toElement().text();
244  oneAddress.city = addressElement.namedItem( QStringLiteral( "city" ) ).toElement().text();
245  oneAddress.country = addressElement.namedItem( QStringLiteral( "country" ) ).toElement().text();
246  oneAddress.postalCode = addressElement.namedItem( QStringLiteral( "postalcode" ) ).toElement().text();
247  oneAddress.type = addressElement.namedItem( QStringLiteral( "type" ) ).toElement().text();
248  addresses << oneAddress;
249  }
250  oneContact.addresses = addresses;
251  addContact( oneContact );
252  }
253 
254  // links
255  mnl = metadataElement.namedItem( QStringLiteral( "links" ) );
256  mne = mnl.toElement();
257  mLinks.clear();
258  const QDomNodeList el = mne.elementsByTagName( QStringLiteral( "link" ) );
259  for ( int i = 0; i < el.size(); i++ )
260  {
261  mne = el.at( i ).toElement();
263  oneLink.name = mne.attribute( QStringLiteral( "name" ) );
264  oneLink.type = mne.attribute( QStringLiteral( "type" ) );
265  oneLink.url = mne.attribute( QStringLiteral( "url" ) );
266  oneLink.description = mne.attribute( QStringLiteral( "description" ) );
267  oneLink.format = mne.attribute( QStringLiteral( "format" ) );
268  oneLink.mimeType = mne.attribute( QStringLiteral( "mimeType" ) );
269  oneLink.size = mne.attribute( QStringLiteral( "size" ) );
270  addLink( oneLink );
271  }
272 
273  // history
274  const QDomNodeList historyNodeList = metadataElement.elementsByTagName( QStringLiteral( "history" ) );
275  QStringList historyList;
276  for ( int i = 0; i < historyNodeList.size(); i++ )
277  {
278  mnl = historyNodeList.at( i );
279  mne = mnl.toElement();
280  historyList.append( mne.text() );
281  }
282  setHistory( historyList );
283 
284  return true;
285 }
286 
287 bool QgsAbstractMetadataBase::writeMetadataXml( QDomElement &metadataElement, QDomDocument &document ) const
288 {
289  // identifier
290  QDomElement identifier = document.createElement( QStringLiteral( "identifier" ) );
291  const QDomText identifierText = document.createTextNode( mIdentifier );
292  identifier.appendChild( identifierText );
293  metadataElement.appendChild( identifier );
294 
295  // parent identifier
296  QDomElement parentIdentifier = document.createElement( QStringLiteral( "parentidentifier" ) );
297  const QDomText parentIdentifierText = document.createTextNode( mParentIdentifier );
298  parentIdentifier.appendChild( parentIdentifierText );
299  metadataElement.appendChild( parentIdentifier );
300 
301  // language
302  QDomElement language = document.createElement( QStringLiteral( "language" ) );
303  const QDomText languageText = document.createTextNode( mLanguage );
304  language.appendChild( languageText );
305  metadataElement.appendChild( language );
306 
307  // type
308  QDomElement type = document.createElement( QStringLiteral( "type" ) );
309  const QDomText typeText = document.createTextNode( mType );
310  type.appendChild( typeText );
311  metadataElement.appendChild( type );
312 
313  // title
314  QDomElement title = document.createElement( QStringLiteral( "title" ) );
315  const QDomText titleText = document.createTextNode( mTitle );
316  title.appendChild( titleText );
317  metadataElement.appendChild( title );
318 
319  // abstract
320  QDomElement abstract = document.createElement( QStringLiteral( "abstract" ) );
321  const QDomText abstractText = document.createTextNode( mAbstract );
322  abstract.appendChild( abstractText );
323  metadataElement.appendChild( abstract );
324 
325  // keywords
326  QMapIterator<QString, QStringList> i( mKeywords );
327  while ( i.hasNext() )
328  {
329  i.next();
330  QDomElement keywordsElement = document.createElement( QStringLiteral( "keywords" ) );
331  keywordsElement.setAttribute( QStringLiteral( "vocabulary" ), i.key() );
332  const QStringList values = i.value();
333  for ( const QString &kw : values )
334  {
335  QDomElement keyword = document.createElement( QStringLiteral( "keyword" ) );
336  const QDomText keywordText = document.createTextNode( kw );
337  keyword.appendChild( keywordText );
338  keywordsElement.appendChild( keyword );
339  }
340  metadataElement.appendChild( keywordsElement );
341  }
342 
343  // contact
344  for ( const QgsAbstractMetadataBase::Contact &contact : mContacts )
345  {
346  QDomElement contactElement = document.createElement( QStringLiteral( "contact" ) );
347  QDomElement nameElement = document.createElement( QStringLiteral( "name" ) );
348  QDomElement organizationElement = document.createElement( QStringLiteral( "organization" ) );
349  QDomElement positionElement = document.createElement( QStringLiteral( "position" ) );
350  QDomElement voiceElement = document.createElement( QStringLiteral( "voice" ) );
351  QDomElement faxElement = document.createElement( QStringLiteral( "fax" ) );
352  QDomElement emailElement = document.createElement( QStringLiteral( "email" ) );
353  QDomElement roleElement = document.createElement( QStringLiteral( "role" ) );
354 
355  const QDomText nameText = document.createTextNode( contact.name );
356  const QDomText orgaText = document.createTextNode( contact.organization );
357  const QDomText positionText = document.createTextNode( contact.position );
358  const QDomText voiceText = document.createTextNode( contact.voice );
359  const QDomText faxText = document.createTextNode( contact.fax );
360  const QDomText emailText = document.createTextNode( contact.email );
361  const QDomText roleText = document.createTextNode( contact.role );
362 
363  for ( const QgsAbstractMetadataBase::Address &oneAddress : contact.addresses )
364  {
365  QDomElement addressElement = document.createElement( QStringLiteral( "contactAddress" ) );
366  QDomElement typeElement = document.createElement( QStringLiteral( "type" ) );
367  QDomElement addressDetailedElement = document.createElement( QStringLiteral( "address" ) );
368  QDomElement cityElement = document.createElement( QStringLiteral( "city" ) );
369  QDomElement administrativeAreaElement = document.createElement( QStringLiteral( "administrativearea" ) );
370  QDomElement postalCodeElement = document.createElement( QStringLiteral( "postalcode" ) );
371  QDomElement countryElement = document.createElement( QStringLiteral( "country" ) );
372 
373  typeElement.appendChild( document.createTextNode( oneAddress.type ) );
374  addressDetailedElement.appendChild( document.createTextNode( oneAddress.address ) );
375  cityElement.appendChild( document.createTextNode( oneAddress.city ) );
376  administrativeAreaElement.appendChild( document.createTextNode( oneAddress.administrativeArea ) );
377  postalCodeElement.appendChild( document.createTextNode( oneAddress.postalCode ) );
378  countryElement.appendChild( document.createTextNode( oneAddress.country ) );
379 
380  addressElement.appendChild( typeElement );
381  addressElement.appendChild( addressDetailedElement );
382  addressElement.appendChild( cityElement );
383  addressElement.appendChild( administrativeAreaElement );
384  addressElement.appendChild( postalCodeElement );
385  addressElement.appendChild( countryElement );
386  contactElement.appendChild( addressElement );
387  }
388 
389  nameElement.appendChild( nameText );
390  organizationElement.appendChild( orgaText );
391  positionElement.appendChild( positionText );
392  voiceElement.appendChild( voiceText );
393  faxElement.appendChild( faxText );
394  emailElement.appendChild( emailText );
395  roleElement.appendChild( roleText );
396 
397  contactElement.appendChild( nameElement );
398  contactElement.appendChild( organizationElement );
399  contactElement.appendChild( positionElement );
400  contactElement.appendChild( voiceElement );
401  contactElement.appendChild( faxElement );
402  contactElement.appendChild( emailElement );
403  contactElement.appendChild( roleElement );
404  metadataElement.appendChild( contactElement );
405  }
406 
407  // links
408  QDomElement links = document.createElement( QStringLiteral( "links" ) );
409  for ( const QgsAbstractMetadataBase::Link &link : mLinks )
410  {
411  QDomElement linkElement = document.createElement( QStringLiteral( "link" ) );
412  linkElement.setAttribute( QStringLiteral( "name" ), link.name );
413  linkElement.setAttribute( QStringLiteral( "type" ), link.type );
414  linkElement.setAttribute( QStringLiteral( "url" ), link.url );
415  linkElement.setAttribute( QStringLiteral( "description" ), link.description );
416  linkElement.setAttribute( QStringLiteral( "format" ), link.format );
417  linkElement.setAttribute( QStringLiteral( "mimeType" ), link.mimeType );
418  linkElement.setAttribute( QStringLiteral( "size" ), link.size );
419  links.appendChild( linkElement );
420  }
421  metadataElement.appendChild( links );
422 
423  // history
424  for ( const QString &history : mHistory )
425  {
426  QDomElement historyElement = document.createElement( QStringLiteral( "history" ) );
427  const QDomText historyText = document.createTextNode( history );
428  historyElement.appendChild( historyText );
429  metadataElement.appendChild( historyElement );
430  }
431 
432  return true;
433 }
434 
436 {
437  if ( !other )
438  return;
439 
440  if ( !other->identifier().isEmpty() )
441  mIdentifier = other->identifier();
442 
443  if ( !other->parentIdentifier().isEmpty() )
445 
446  if ( !other->language().isEmpty() )
447  mLanguage = other->language();
448 
449  if ( !other->type().isEmpty() )
450  mType = other->type();
451 
452  if ( !other->title().isEmpty() )
453  mTitle = other->title();
454 
455  if ( !other->abstract().isEmpty() )
456  mAbstract = other->abstract();
457 
458  if ( !other->history().isEmpty() )
459  mHistory = other->history();
460 
461  if ( !other->keywords().isEmpty() )
462  mKeywords = other->keywords();
463 
464  if ( !other->contacts().isEmpty() )
465  mContacts = other->contacts();
466 
467  if ( !other->links().isEmpty() )
468  mLinks = other->links();
469 }
470 
472 {
473  return ( ( mIdentifier == metadataOther.mIdentifier ) &&
474  ( mParentIdentifier == metadataOther.mParentIdentifier ) &&
475  ( mLanguage == metadataOther.mLanguage ) &&
476  ( mType == metadataOther.mType ) &&
477  ( mTitle == metadataOther.mTitle ) &&
478  ( mAbstract == metadataOther.mAbstract ) &&
479  ( mHistory == metadataOther.mHistory ) &&
480  ( mKeywords == metadataOther.mKeywords ) &&
481  ( mContacts == metadataOther.mContacts ) &&
482  ( mLinks == metadataOther.mLinks ) );
483 }
484 
485 
487 {
488  return name == other.name &&
489  organization == other.organization &&
490  position == other.position &&
491  addresses == other.addresses &&
492  voice == other.voice &&
493  fax == other.fax &&
494  email == other.email &&
495  role == other.role;
496 }
497 
499 {
500  return name == other.name &&
501  type == other.type &&
502  description == other.description &&
503  url == other.url &&
504  format == other.format &&
505  mimeType == other.mimeType &&
506  size == other.size;
507 }
508 
510 {
511  return type == other.type &&
512  address == other.address &&
513  city == other.city &&
514  administrativeArea == other.administrativeArea &&
515  postalCode == other.postalCode &&
516  country == other.country;
517 }
An abstract base class for metadata stores.
QgsAbstractMetadataBase::ContactList mContacts
virtual bool writeMetadataXml(QDomElement &metadataElement, QDomDocument &document) const
Stores state in a DOM node.
void setAbstract(const QString &abstract)
Sets a free-form abstract (description) of the resource.
void setType(const QString &type)
Sets the type (nature) of the resource.
void setParentIdentifier(const QString &parentIdentifier)
Sets a reference, URI, URL or some other mechanism to identify the parent resource that this resource...
void addContact(const QgsAbstractMetadataBase::Contact &contact)
Adds an individual contact to the existing contacts.
void setTitle(const QString &title)
Sets the human readable title (name) of the resource, typically displayed in search results.
void setHistory(const QStringList &history)
Sets the freeform description of the history or lineage of the resource.
QgsAbstractMetadataBase::ContactList contacts() const
Returns a list of contact persons or entities associated with the resource.
virtual void combine(const QgsAbstractMetadataBase *other)
Combines the metadata from this object with the metadata from an other object.
void setLinks(const QgsAbstractMetadataBase::LinkList &links)
Sets the list of online resources associated with the resource.
void setIdentifier(const QString &identifier)
Sets the reference, URI, URL or some other mechanism to identify the resource.
QStringList categories() const
Returns categories of the resource.
QString abstract() const
Returns a free-form description of the resource.
QStringList keywordVocabularies() const
Returns a list of keyword vocabularies contained in the metadata.
QString title() const
Returns the human readable name of the resource, typically displayed in search results.
void setContacts(const QgsAbstractMetadataBase::ContactList &contacts)
Sets the list of contacts or entities associated with the resource.
void setKeywords(const QgsAbstractMetadataBase::KeywordMap &keywords)
Sets the keywords map, which is a set of descriptive keywords associated with the resource.
QStringList history() const
Returns a freeform description of the history or lineage of the resource.
QgsAbstractMetadataBase::KeywordMap mKeywords
Keywords map.
QgsAbstractMetadataBase::KeywordMap keywords() const
Returns the keywords map, which is a set of descriptive keywords associated with the resource.
bool equals(const QgsAbstractMetadataBase &other) const
Tests whether the common metadata fields in this object are equal to other.
QgsAbstractMetadataBase::LinkList links() const
Returns a list of online resources associated with the resource.
virtual bool readMetadataXml(const QDomElement &metadataElement)
Sets state from DOM document.
void setLanguage(const QString &language)
Sets the human language associated with the resource.
QString parentIdentifier() const
A reference, URI, URL or some other mechanism to identify the parent resource that this resource is a...
QString language() const
Returns the human language associated with the resource.
void addKeywords(const QString &vocabulary, const QStringList &keywords)
Adds a list of descriptive keywords for a specified vocabulary.
void addLink(const QgsAbstractMetadataBase::Link &link)
Adds an individual link to the existing links.
bool removeKeywords(const QString &vocabulary)
Remove a vocabulary from the list.
QgsAbstractMetadataBase::LinkList mLinks
QString type() const
Returns the nature of the resource.
QString identifier() const
A reference, URI, URL or some other mechanism to identify the resource.
void setCategories(const QStringList &categories)
Sets categories of the resource.
void addHistoryItem(const QString &text)
Adds a single history text to the end of the existing history list.
bool operator==(const QgsAbstractMetadataBase::Address &other) const
QString administrativeArea
Administrative area (state, province/territory, etc.).
QString address
Free-form physical address component, e.g.
QString city
City or locality name.
QString type
Type of address, e.g.
QString country
Free-form country string.
QString postalCode
Postal (or ZIP) code.
QString position
Position/title of contact.
QList< QgsAbstractMetadataBase::Address > addresses
List of addresses associated with this contact.
QString email
Electronic mail address.
QString organization
Organization contact belongs to/represents.
bool operator==(const QgsAbstractMetadataBase::Contact &other) const