QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
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
26void QgsAbstractMetadataBase::setIdentifier( const QString &identifier )
27{
29}
30
35
36void QgsAbstractMetadataBase::setParentIdentifier( const QString &parentIdentifier )
37{
39}
40
42{
43 return mType;
44}
45
46void QgsAbstractMetadataBase::setType( const QString &type )
47{
48 mType = type;
49}
50
52{
53 return mTitle;
54}
55
56void QgsAbstractMetadataBase::setTitle( const QString &title )
57{
58 mTitle = title;
59}
60
62{
63 return mAbstract;
64}
65
66void QgsAbstractMetadataBase::setAbstract( const QString &abstract )
67{
69}
70
72{
73 return mHistory;
74}
75
76void QgsAbstractMetadataBase::setHistory( const QStringList &history )
77{
79}
80
81void QgsAbstractMetadataBase::addHistoryItem( const QString &text )
82{
83 mHistory << text;
84}
85
86QMap<QString, QStringList> QgsAbstractMetadataBase::keywords() const
87{
88 return mKeywords;
89}
90
91void QgsAbstractMetadataBase::setKeywords( const QMap<QString, QStringList> &keywords )
92{
94}
95
96void QgsAbstractMetadataBase::addKeywords( const QString &vocabulary, const QStringList &keywords )
97{
98 mKeywords.insert( vocabulary, keywords );
99}
100
101bool QgsAbstractMetadataBase::removeKeywords( const QString &vocabulary )
102{
103 return mKeywords.remove( vocabulary );
104}
105
107{
108 return mKeywords.keys();
109}
110
111QStringList 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
128void QgsAbstractMetadataBase::setCategories( const QStringList &category )
129{
130 mKeywords.insert( QStringLiteral( "gmd:topicCategory" ), category );
131}
132
133QList<QgsAbstractMetadataBase::Contact> QgsAbstractMetadataBase::contacts() const
134{
135 return mContacts;
136}
137
138void QgsAbstractMetadataBase::setContacts( const QList<QgsAbstractMetadataBase::Contact> &contacts )
139{
141}
142
147
148QList<QgsAbstractMetadataBase::Link> QgsAbstractMetadataBase::links() const
149{
150 return mLinks;
151}
152
153void QgsAbstractMetadataBase::setLinks( const QList<QgsAbstractMetadataBase::Link> &links )
154{
155 mLinks = links;
156}
157
162
164{
165 return mDates.value( type );
166}
167
169{
170 if ( !date.isValid() || date.isNull() )
171 mDates.remove( type );
172 else
173 mDates[type] = date;
174}
175
177{
178 return mLanguage;
179}
180
181void QgsAbstractMetadataBase::setLanguage( const QString &language )
182{
184}
185
186bool QgsAbstractMetadataBase::readMetadataXml( const QDomElement &metadataElement )
187{
188 QDomNode mnl;
189 QDomElement mne;
190
191 // set identifier
192 mnl = metadataElement.namedItem( QStringLiteral( "identifier" ) );
193 mIdentifier = mnl.toElement().text();
194
195 // set parent identifier
196 mnl = metadataElement.namedItem( QStringLiteral( "parentidentifier" ) );
197 mParentIdentifier = mnl.toElement().text();
198
199 // set language
200 mnl = metadataElement.namedItem( QStringLiteral( "language" ) );
201 mLanguage = mnl.toElement().text();
202
203 // set type
204 mnl = metadataElement.namedItem( QStringLiteral( "type" ) );
205 mType = mnl.toElement().text();
206
207 // set title
208 mnl = metadataElement.namedItem( QStringLiteral( "title" ) );
209 mTitle = mnl.toElement().text();
210
211 // set abstract
212 mnl = metadataElement.namedItem( QStringLiteral( "abstract" ) );
213 mAbstract = mnl.toElement().text();
214
215 // set keywords
216 const QDomNodeList keywords = metadataElement.elementsByTagName( QStringLiteral( "keywords" ) );
217 mKeywords.clear();
218 for ( int i = 0; i < keywords.size(); i++ )
219 {
220 QStringList keywordsList;
221 mnl = keywords.at( i );
222 mne = mnl.toElement();
223
224 const QDomNodeList el = mne.elementsByTagName( QStringLiteral( "keyword" ) );
225 for ( int j = 0; j < el.size(); j++ )
226 {
227 keywordsList.append( el.at( j ).toElement().text() );
228 }
229 addKeywords( mne.attribute( QStringLiteral( "vocabulary" ) ), keywordsList );
230 }
231
232 // contact
233 const QDomNodeList contactsList = metadataElement.elementsByTagName( QStringLiteral( "contact" ) );
234 mContacts.clear();
235 for ( int i = 0; i < contactsList.size(); i++ )
236 {
237 mnl = contactsList.at( i );
238 mne = mnl.toElement();
239
241 oneContact.name = mne.namedItem( QStringLiteral( "name" ) ).toElement().text();
242 oneContact.organization = mne.namedItem( QStringLiteral( "organization" ) ).toElement().text();
243 oneContact.position = mne.namedItem( QStringLiteral( "position" ) ).toElement().text();
244 oneContact.voice = mne.namedItem( QStringLiteral( "voice" ) ).toElement().text();
245 oneContact.fax = mne.namedItem( QStringLiteral( "fax" ) ).toElement().text();
246 oneContact.email = mne.namedItem( QStringLiteral( "email" ) ).toElement().text();
247 oneContact.role = mne.namedItem( QStringLiteral( "role" ) ).toElement().text();
248
249 QList< QgsAbstractMetadataBase::Address > addresses;
250 const QDomNodeList addressList = mne.elementsByTagName( QStringLiteral( "contactAddress" ) );
251 for ( int j = 0; j < addressList.size(); j++ )
252 {
253 const QDomElement addressElement = addressList.at( j ).toElement();
255 oneAddress.address = addressElement.namedItem( QStringLiteral( "address" ) ).toElement().text();
256 oneAddress.administrativeArea = addressElement.namedItem( QStringLiteral( "administrativearea" ) ).toElement().text();
257 oneAddress.city = addressElement.namedItem( QStringLiteral( "city" ) ).toElement().text();
258 oneAddress.country = addressElement.namedItem( QStringLiteral( "country" ) ).toElement().text();
259 oneAddress.postalCode = addressElement.namedItem( QStringLiteral( "postalcode" ) ).toElement().text();
260 oneAddress.type = addressElement.namedItem( QStringLiteral( "type" ) ).toElement().text();
261 addresses << oneAddress;
262 }
263 oneContact.addresses = addresses;
264 addContact( oneContact );
265 }
266
267 // links
268 mnl = metadataElement.namedItem( QStringLiteral( "links" ) );
269 mne = mnl.toElement();
270 mLinks.clear();
271 const QDomNodeList el = mne.elementsByTagName( QStringLiteral( "link" ) );
272 for ( int i = 0; i < el.size(); i++ )
273 {
274 mne = el.at( i ).toElement();
276 oneLink.name = mne.attribute( QStringLiteral( "name" ) );
277 oneLink.type = mne.attribute( QStringLiteral( "type" ) );
278 oneLink.url = mne.attribute( QStringLiteral( "url" ) );
279 oneLink.description = mne.attribute( QStringLiteral( "description" ) );
280 oneLink.format = mne.attribute( QStringLiteral( "format" ) );
281 oneLink.mimeType = mne.attribute( QStringLiteral( "mimeType" ) );
282 oneLink.size = mne.attribute( QStringLiteral( "size" ) );
283 addLink( oneLink );
284 }
285
286 // history
287 const QDomNodeList historyNodeList = metadataElement.elementsByTagName( QStringLiteral( "history" ) );
288 QStringList historyList;
289 for ( int i = 0; i < historyNodeList.size(); i++ )
290 {
291 mnl = historyNodeList.at( i );
292 mne = mnl.toElement();
293 historyList.append( mne.text() );
294 }
295 setHistory( historyList );
296
297 {
298 mDates.clear();
299 const QDomElement dateElement = metadataElement.firstChildElement( QStringLiteral( "dates" ) );
300 if ( !dateElement.isNull() )
301 {
302 const QDomNodeList dateNodeList = dateElement.elementsByTagName( QStringLiteral( "date" ) );
303 const QMetaEnum dateEnum = QMetaEnum::fromType<Qgis::MetadataDateType>();
304 for ( int i = 0; i < dateNodeList.size(); i++ )
305 {
306 const QDomElement dateElement = dateNodeList.at( i ).toElement();
307 const Qgis::MetadataDateType type = static_cast< Qgis::MetadataDateType >( dateEnum.keyToValue( dateElement.attribute( QStringLiteral( "type" ) ).toStdString().c_str() ) );
308 const QDateTime value = QDateTime::fromString( dateElement.attribute( QStringLiteral( "value" ) ), Qt::ISODate );
309 if ( value.isValid() && !value.isNull() )
310 mDates.insert( type, value );
311 }
312 }
313 }
314
315 return true;
316}
317
318bool QgsAbstractMetadataBase::writeMetadataXml( QDomElement &metadataElement, QDomDocument &document ) const
319{
320 // identifier
321 QDomElement identifier = document.createElement( QStringLiteral( "identifier" ) );
322 const QDomText identifierText = document.createTextNode( mIdentifier );
323 identifier.appendChild( identifierText );
324 metadataElement.appendChild( identifier );
325
326 // parent identifier
327 QDomElement parentIdentifier = document.createElement( QStringLiteral( "parentidentifier" ) );
328 const QDomText parentIdentifierText = document.createTextNode( mParentIdentifier );
329 parentIdentifier.appendChild( parentIdentifierText );
330 metadataElement.appendChild( parentIdentifier );
331
332 // language
333 QDomElement language = document.createElement( QStringLiteral( "language" ) );
334 const QDomText languageText = document.createTextNode( mLanguage );
335 language.appendChild( languageText );
336 metadataElement.appendChild( language );
337
338 // type
339 QDomElement type = document.createElement( QStringLiteral( "type" ) );
340 const QDomText typeText = document.createTextNode( mType );
341 type.appendChild( typeText );
342 metadataElement.appendChild( type );
343
344 // title
345 QDomElement title = document.createElement( QStringLiteral( "title" ) );
346 const QDomText titleText = document.createTextNode( mTitle );
347 title.appendChild( titleText );
348 metadataElement.appendChild( title );
349
350 // abstract
351 QDomElement abstract = document.createElement( QStringLiteral( "abstract" ) );
352 const QDomText abstractText = document.createTextNode( mAbstract );
353 abstract.appendChild( abstractText );
354 metadataElement.appendChild( abstract );
355
356 // keywords
357 QMapIterator<QString, QStringList> i( mKeywords );
358 while ( i.hasNext() )
359 {
360 i.next();
361 QDomElement keywordsElement = document.createElement( QStringLiteral( "keywords" ) );
362 keywordsElement.setAttribute( QStringLiteral( "vocabulary" ), i.key() );
363 const QStringList values = i.value();
364 for ( const QString &kw : values )
365 {
366 QDomElement keyword = document.createElement( QStringLiteral( "keyword" ) );
367 const QDomText keywordText = document.createTextNode( kw );
368 keyword.appendChild( keywordText );
369 keywordsElement.appendChild( keyword );
370 }
371 metadataElement.appendChild( keywordsElement );
372 }
373
374 // contact
375 for ( const QgsAbstractMetadataBase::Contact &contact : mContacts )
376 {
377 QDomElement contactElement = document.createElement( QStringLiteral( "contact" ) );
378 QDomElement nameElement = document.createElement( QStringLiteral( "name" ) );
379 QDomElement organizationElement = document.createElement( QStringLiteral( "organization" ) );
380 QDomElement positionElement = document.createElement( QStringLiteral( "position" ) );
381 QDomElement voiceElement = document.createElement( QStringLiteral( "voice" ) );
382 QDomElement faxElement = document.createElement( QStringLiteral( "fax" ) );
383 QDomElement emailElement = document.createElement( QStringLiteral( "email" ) );
384 QDomElement roleElement = document.createElement( QStringLiteral( "role" ) );
385
386 const QDomText nameText = document.createTextNode( contact.name );
387 const QDomText orgaText = document.createTextNode( contact.organization );
388 const QDomText positionText = document.createTextNode( contact.position );
389 const QDomText voiceText = document.createTextNode( contact.voice );
390 const QDomText faxText = document.createTextNode( contact.fax );
391 const QDomText emailText = document.createTextNode( contact.email );
392 const QDomText roleText = document.createTextNode( contact.role );
393
394 for ( const QgsAbstractMetadataBase::Address &oneAddress : contact.addresses )
395 {
396 QDomElement addressElement = document.createElement( QStringLiteral( "contactAddress" ) );
397 QDomElement typeElement = document.createElement( QStringLiteral( "type" ) );
398 QDomElement addressDetailedElement = document.createElement( QStringLiteral( "address" ) );
399 QDomElement cityElement = document.createElement( QStringLiteral( "city" ) );
400 QDomElement administrativeAreaElement = document.createElement( QStringLiteral( "administrativearea" ) );
401 QDomElement postalCodeElement = document.createElement( QStringLiteral( "postalcode" ) );
402 QDomElement countryElement = document.createElement( QStringLiteral( "country" ) );
403
404 typeElement.appendChild( document.createTextNode( oneAddress.type ) );
405 addressDetailedElement.appendChild( document.createTextNode( oneAddress.address ) );
406 cityElement.appendChild( document.createTextNode( oneAddress.city ) );
407 administrativeAreaElement.appendChild( document.createTextNode( oneAddress.administrativeArea ) );
408 postalCodeElement.appendChild( document.createTextNode( oneAddress.postalCode ) );
409 countryElement.appendChild( document.createTextNode( oneAddress.country ) );
410
411 addressElement.appendChild( typeElement );
412 addressElement.appendChild( addressDetailedElement );
413 addressElement.appendChild( cityElement );
414 addressElement.appendChild( administrativeAreaElement );
415 addressElement.appendChild( postalCodeElement );
416 addressElement.appendChild( countryElement );
417 contactElement.appendChild( addressElement );
418 }
419
420 nameElement.appendChild( nameText );
421 organizationElement.appendChild( orgaText );
422 positionElement.appendChild( positionText );
423 voiceElement.appendChild( voiceText );
424 faxElement.appendChild( faxText );
425 emailElement.appendChild( emailText );
426 roleElement.appendChild( roleText );
427
428 contactElement.appendChild( nameElement );
429 contactElement.appendChild( organizationElement );
430 contactElement.appendChild( positionElement );
431 contactElement.appendChild( voiceElement );
432 contactElement.appendChild( faxElement );
433 contactElement.appendChild( emailElement );
434 contactElement.appendChild( roleElement );
435 metadataElement.appendChild( contactElement );
436 }
437
438 // links
439 QDomElement links = document.createElement( QStringLiteral( "links" ) );
440 for ( const QgsAbstractMetadataBase::Link &link : mLinks )
441 {
442 QDomElement linkElement = document.createElement( QStringLiteral( "link" ) );
443 linkElement.setAttribute( QStringLiteral( "name" ), link.name );
444 linkElement.setAttribute( QStringLiteral( "type" ), link.type );
445 linkElement.setAttribute( QStringLiteral( "url" ), link.url );
446 linkElement.setAttribute( QStringLiteral( "description" ), link.description );
447 linkElement.setAttribute( QStringLiteral( "format" ), link.format );
448 linkElement.setAttribute( QStringLiteral( "mimeType" ), link.mimeType );
449 linkElement.setAttribute( QStringLiteral( "size" ), link.size );
450 links.appendChild( linkElement );
451 }
452 metadataElement.appendChild( links );
453
454 // history
455 for ( const QString &history : mHistory )
456 {
457 QDomElement historyElement = document.createElement( QStringLiteral( "history" ) );
458 const QDomText historyText = document.createTextNode( history );
459 historyElement.appendChild( historyText );
460 metadataElement.appendChild( historyElement );
461 }
462
463 // dates
464 {
465 const QMetaEnum dateEnum = QMetaEnum::fromType<Qgis::MetadataDateType>();
466 QDomElement datesElement = document.createElement( QStringLiteral( "dates" ) );
467 for ( int k = 0; k < dateEnum.keyCount(); k++ )
468 {
469 const Qgis::MetadataDateType type = static_cast< Qgis::MetadataDateType >( dateEnum.value( k ) );
470 if ( mDates.contains( type ) && mDates.value( type ).isValid() )
471 {
472 QDomElement dateElement = document.createElement( QStringLiteral( "date" ) );
473 dateElement.setAttribute( QStringLiteral( "type" ), dateEnum.valueToKey( static_cast< int >( type ) ) );
474 dateElement.setAttribute( QStringLiteral( "value" ), mDates.value( type ).toString( Qt::ISODate ) );
475 datesElement.appendChild( dateElement );
476 }
477 }
478 metadataElement.appendChild( datesElement );
479 }
480
481 return true;
482}
483
485{
486 if ( !other )
487 return;
488
489 if ( !other->identifier().isEmpty() )
490 mIdentifier = other->identifier();
491
492 if ( !other->parentIdentifier().isEmpty() )
494
495 if ( !other->language().isEmpty() )
496 mLanguage = other->language();
497
498 if ( !other->type().isEmpty() )
499 mType = other->type();
500
501 if ( !other->title().isEmpty() )
502 mTitle = other->title();
503
504 if ( !other->abstract().isEmpty() )
505 mAbstract = other->abstract();
506
507 if ( !other->history().isEmpty() )
508 mHistory = other->history();
509
510 if ( !other->keywords().isEmpty() )
511 mKeywords = other->keywords();
512
513 if ( !other->contacts().isEmpty() )
514 mContacts = other->contacts();
515
516 if ( !other->links().isEmpty() )
517 mLinks = other->links();
518
519 const QMetaEnum dateEnum = QMetaEnum::fromType<Qgis::MetadataDateType>();
520 for ( int k = 0; k < dateEnum.keyCount(); k++ )
521 {
522 const Qgis::MetadataDateType type = static_cast< Qgis::MetadataDateType >( dateEnum.value( k ) );
523 if ( other->mDates.contains( type ) && other->mDates.value( type ).isValid() )
524 {
525 mDates.insert( type, other->mDates[type] );
526 }
527 }
528
529}
530
532{
533 return ( ( mIdentifier == metadataOther.mIdentifier ) &&
534 ( mParentIdentifier == metadataOther.mParentIdentifier ) &&
535 ( mLanguage == metadataOther.mLanguage ) &&
536 ( mType == metadataOther.mType ) &&
537 ( mTitle == metadataOther.mTitle ) &&
538 ( mAbstract == metadataOther.mAbstract ) &&
539 ( mHistory == metadataOther.mHistory ) &&
540 ( mKeywords == metadataOther.mKeywords ) &&
541 ( mContacts == metadataOther.mContacts ) &&
542 ( mLinks == metadataOther.mLinks ) &&
543 ( mDates == metadataOther.mDates ) );
544}
545
546
548{
549 return name == other.name &&
550 organization == other.organization &&
551 position == other.position &&
552 addresses == other.addresses &&
553 voice == other.voice &&
554 fax == other.fax &&
555 email == other.email &&
556 role == other.role;
557}
558
560{
561 return name == other.name &&
562 type == other.type &&
563 description == other.description &&
564 url == other.url &&
565 format == other.format &&
566 mimeType == other.mimeType &&
567 size == other.size;
568}
569
571{
572 return type == other.type &&
573 address == other.address &&
574 city == other.city &&
575 administrativeArea == other.administrativeArea &&
576 postalCode == other.postalCode &&
577 country == other.country;
578}
MetadataDateType
Date types for metadata.
Definition qgis.h:4349
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.
QMap< Qgis::MetadataDateType, QDateTime > mDates
Metadata dates.
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.
void setDateTime(Qgis::MetadataDateType type, QDateTime date)
Sets a date value for the specified date type.
QDateTime dateTime(Qgis::MetadataDateType type) const
Returns the date for the specified date type.
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 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