Skip to content

Commit

Permalink
Merge pull request #4963 from Gustry/category
Browse files Browse the repository at this point in the history
add category in QgsLayerMetadata using keywords
  • Loading branch information
nyalldawson authored Aug 5, 2017
2 parents 0854e79 + 5100d85 commit 19fd3e8
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
28 changes: 27 additions & 1 deletion python/core/metadata/qgslayermetadata.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions resources/qgis-resource-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<type>dataset</type>
<title>roads</title>
<abstract>my roads</abstract>
<keywords vocabulary="gmd:topicCategory">
<keyword>natural</keyword>
</keywords>
<keywords vocabulary="GEMET">
<keyword>kw1</keyword>
<keyword>kw2</keyword>
Expand Down
22 changes: 22 additions & 0 deletions src/core/metadata/qgslayermetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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::Contact> QgsLayerMetadata::contacts() const
{
return mContacts;
Expand Down
26 changes: 25 additions & 1 deletion src/core/metadata/qgslayermetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -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.
*
Expand All @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions tests/src/python/test_qgslayermetadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ def testGettersSetters(self):
m.setTitle('title')
self.assertEqual(m.title(), 'title')

m.setCategories(['category'])
self.assertEqual(m.categories(), ['category'])

m.setAbstract('abstract')
self.assertEqual(m.abstract(), 'abstract')

Expand Down Expand Up @@ -102,6 +105,11 @@ def testExtent(self):
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'],
Expand Down

0 comments on commit 19fd3e8

Please sign in to comment.