From 5100d85c4930eeeff880a657bfe44d6a6fe55b52 Mon Sep 17 00:00:00 2001 From: Etienne Trimaille Date: Fri, 4 Aug 2017 22:20:04 +0200 Subject: [PATCH] add category in QgsLayerMetadata using keywords --- python/core/metadata/qgslayermetadata.sip | 28 ++++++++++++++++++++++- resources/qgis-resource-metadata.xml | 3 +++ src/core/metadata/qgslayermetadata.cpp | 22 ++++++++++++++++++ src/core/metadata/qgslayermetadata.h | 26 ++++++++++++++++++++- tests/src/python/test_qgslayermetadata.py | 8 +++++++ 5 files changed, 85 insertions(+), 2 deletions(-) diff --git a/python/core/metadata/qgslayermetadata.sip b/python/core/metadata/qgslayermetadata.sip index 6f495bc1952..379114a1ff4 100644 --- a/python/core/metadata/qgslayermetadata.sip +++ b/python/core/metadata/qgslayermetadata.sip @@ -492,7 +492,7 @@ class QgsLayerMetadata CRS which is actually used to display and manipulate the layer within QGIS. This may be the case when a layer has an incorrect CRS within its metadata and a user has manually overridden the layer's CRS within QGIS. -.. seealso:: setCrs() +.. seealso:: crs() %End KeywordMap keywords() const; @@ -535,6 +535,15 @@ class QgsLayerMetadata .. seealso:: setKeywords() %End + bool removeKeywords( const QString &vocabulary ); +%Docstring + Remove a vocabulary from the list. + +.. seealso:: setKeywords() +.. seealso:: addKeywords() + :rtype: bool +%End + QStringList keywordVocabularies() const; %Docstring Returns a list of keyword vocabularies contained in the metadata. @@ -559,6 +568,23 @@ class QgsLayerMetadata :rtype: list of str %End + QStringList categories() const; +%Docstring + Returns categories of the resource. + Categories are stored using a special vocabulary 'gmd:topicCategory' in keywords. + +.. seealso:: keywords() + :rtype: list of str +%End + + void setCategories( const QStringList &categories ); +%Docstring + Sets categories of the resource. + Categories are stored using a special vocabulary 'gmd:topicCategory' in keywords. + +.. seealso:: keywords() +%End + QgsLayerMetadata::ContactList contacts() const; %Docstring Returns a list of contact persons or entities associated with the resource. diff --git a/resources/qgis-resource-metadata.xml b/resources/qgis-resource-metadata.xml index e07675b62b8..3aade4a51b8 100644 --- a/resources/qgis-resource-metadata.xml +++ b/resources/qgis-resource-metadata.xml @@ -6,6 +6,9 @@ dataset roads my roads + + natural + kw1 kw2 diff --git a/src/core/metadata/qgslayermetadata.cpp b/src/core/metadata/qgslayermetadata.cpp index f771620616d..7ed06461168 100644 --- a/src/core/metadata/qgslayermetadata.cpp +++ b/src/core/metadata/qgslayermetadata.cpp @@ -158,6 +158,11 @@ void QgsLayerMetadata::addKeywords( const QString &vocabulary, const QStringList mKeywords.insert( vocabulary, keywords ); } +bool QgsLayerMetadata::removeKeywords( const QString &vocabulary ) +{ + return mKeywords.remove( vocabulary ); +} + QStringList QgsLayerMetadata::keywordVocabularies() const { return mKeywords.keys(); @@ -168,6 +173,23 @@ QStringList QgsLayerMetadata::keywords( const QString &vocabulary ) const return mKeywords.value( vocabulary ); } +QStringList QgsLayerMetadata::categories() const +{ + if ( mKeywords.contains( "gmd:topicCategory" ) ) + { + return mKeywords.value( "gmd:topicCategory" ); + } + else + { + return QStringList(); + } +} + +void QgsLayerMetadata::setCategories( const QStringList &category ) +{ + mKeywords.insert( "gmd:topicCategory", category ); +} + QList QgsLayerMetadata::contacts() const { return mContacts; diff --git a/src/core/metadata/qgslayermetadata.h b/src/core/metadata/qgslayermetadata.h index 440d2165fd9..564f02e7af4 100644 --- a/src/core/metadata/qgslayermetadata.h +++ b/src/core/metadata/qgslayermetadata.h @@ -545,7 +545,7 @@ class CORE_EXPORT QgsLayerMetadata * CRS which is actually used to display and manipulate the layer within QGIS. * This may be the case when a layer has an incorrect CRS within its metadata * and a user has manually overridden the layer's CRS within QGIS. - * \see setCrs() + * \see crs() */ void setCrs( const QgsCoordinateReferenceSystem &crs ); @@ -588,6 +588,14 @@ class CORE_EXPORT QgsLayerMetadata */ void addKeywords( const QString &vocabulary, const QStringList &keywords ); + /** + * Remove a vocabulary from the list. + * + * \see setKeywords() + * \see addKeywords() + */ + bool removeKeywords( const QString &vocabulary ); + /** * Returns a list of keyword vocabularies contained in the metadata. * @@ -610,6 +618,22 @@ class CORE_EXPORT QgsLayerMetadata */ QStringList keywords( const QString &vocabulary ) const; + /** + * Returns categories of the resource. + * Categories are stored using a special vocabulary 'gmd:topicCategory' in keywords. + * + * \see keywords() + */ + QStringList categories() const; + + /** + * Sets categories of the resource. + * Categories are stored using a special vocabulary 'gmd:topicCategory' in keywords. + * + * \see keywords() + */ + void setCategories( const QStringList &categories ); + /** * Returns a list of contact persons or entities associated with the resource. * \see setContacts() diff --git a/tests/src/python/test_qgslayermetadata.py b/tests/src/python/test_qgslayermetadata.py index da69de019ac..57bf1883b3c 100644 --- a/tests/src/python/test_qgslayermetadata.py +++ b/tests/src/python/test_qgslayermetadata.py @@ -48,6 +48,9 @@ class TestQgsLayerMetadata(unittest.TestCase): m.setTitle('title') self.assertEqual(m.title(), 'title') + m.setCategories(['category']) + self.assertEqual(m.categories(), ['category']) + m.setAbstract('abstract') self.assertEqual(m.abstract(), 'abstract') @@ -102,6 +105,11 @@ class TestQgsLayerMetadata(unittest.TestCase): def testKeywords(self): m = QgsLayerMetadata() + m.setKeywords({'gmd:topicCategory': ['natural']}) + self.assertEqual(m.keywords(), {'gmd:topicCategory': ['natural']}) + self.assertEqual(m.categories(), ['natural']) + self.assertTrue(m.removeKeywords('gmd:topicCategory')) + m.setKeywords({'vocab a': ['keyword a', 'other a'], 'vocab b': ['keyword b', 'other b']}) self.assertEqual(m.keywords(), {'vocab a': ['keyword a', 'other a'],