add category in QgsLayerMetadata using keywords

This commit is contained in:
Etienne Trimaille 2017-08-04 22:20:04 +02:00
parent c3ae3bfc74
commit 5100d85c49
5 changed files with 85 additions and 2 deletions

View File

@ -492,7 +492,7 @@ class QgsLayerMetadata
CRS which is actually used to display and manipulate the layer within QGIS. 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 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. and a user has manually overridden the layer's CRS within QGIS.
.. seealso:: setCrs() .. seealso:: crs()
%End %End
KeywordMap keywords() const; KeywordMap keywords() const;
@ -535,6 +535,15 @@ class QgsLayerMetadata
.. seealso:: setKeywords() .. seealso:: setKeywords()
%End %End
bool removeKeywords( const QString &vocabulary );
%Docstring
Remove a vocabulary from the list.
.. seealso:: setKeywords()
.. seealso:: addKeywords()
:rtype: bool
%End
QStringList keywordVocabularies() const; QStringList keywordVocabularies() const;
%Docstring %Docstring
Returns a list of keyword vocabularies contained in the metadata. Returns a list of keyword vocabularies contained in the metadata.
@ -559,6 +568,23 @@ class QgsLayerMetadata
:rtype: list of str :rtype: list of str
%End %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; QgsLayerMetadata::ContactList contacts() const;
%Docstring %Docstring
Returns a list of contact persons or entities associated with the resource. Returns a list of contact persons or entities associated with the resource.

View File

@ -6,6 +6,9 @@
<type>dataset</type> <type>dataset</type>
<title>roads</title> <title>roads</title>
<abstract>my roads</abstract> <abstract>my roads</abstract>
<keywords vocabulary="gmd:topicCategory">
<keyword>natural</keyword>
</keywords>
<keywords vocabulary="GEMET"> <keywords vocabulary="GEMET">
<keyword>kw1</keyword> <keyword>kw1</keyword>
<keyword>kw2</keyword> <keyword>kw2</keyword>

View File

@ -158,6 +158,11 @@ void QgsLayerMetadata::addKeywords( const QString &vocabulary, const QStringList
mKeywords.insert( vocabulary, keywords ); mKeywords.insert( vocabulary, keywords );
} }
bool QgsLayerMetadata::removeKeywords( const QString &vocabulary )
{
return mKeywords.remove( vocabulary );
}
QStringList QgsLayerMetadata::keywordVocabularies() const QStringList QgsLayerMetadata::keywordVocabularies() const
{ {
return mKeywords.keys(); return mKeywords.keys();
@ -168,6 +173,23 @@ QStringList QgsLayerMetadata::keywords( const QString &vocabulary ) const
return mKeywords.value( vocabulary ); 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 QList<QgsLayerMetadata::Contact> QgsLayerMetadata::contacts() const
{ {
return mContacts; return mContacts;

View File

@ -545,7 +545,7 @@ class CORE_EXPORT QgsLayerMetadata
* CRS which is actually used to display and manipulate the layer within QGIS. * 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 * 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. * and a user has manually overridden the layer's CRS within QGIS.
* \see setCrs() * \see crs()
*/ */
void setCrs( const QgsCoordinateReferenceSystem &crs ); void setCrs( const QgsCoordinateReferenceSystem &crs );
@ -588,6 +588,14 @@ class CORE_EXPORT QgsLayerMetadata
*/ */
void addKeywords( const QString &vocabulary, const QStringList &keywords ); 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. * Returns a list of keyword vocabularies contained in the metadata.
* *
@ -610,6 +618,22 @@ class CORE_EXPORT QgsLayerMetadata
*/ */
QStringList keywords( const QString &vocabulary ) const; 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. * Returns a list of contact persons or entities associated with the resource.
* \see setContacts() * \see setContacts()

View File

@ -48,6 +48,9 @@ class TestQgsLayerMetadata(unittest.TestCase):
m.setTitle('title') m.setTitle('title')
self.assertEqual(m.title(), 'title') self.assertEqual(m.title(), 'title')
m.setCategories(['category'])
self.assertEqual(m.categories(), ['category'])
m.setAbstract('abstract') m.setAbstract('abstract')
self.assertEqual(m.abstract(), 'abstract') self.assertEqual(m.abstract(), 'abstract')
@ -102,6 +105,11 @@ class TestQgsLayerMetadata(unittest.TestCase):
def testKeywords(self): def testKeywords(self):
m = QgsLayerMetadata() 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'], m.setKeywords({'vocab a': ['keyword a', 'other a'],
'vocab b': ['keyword b', 'other b']}) 'vocab b': ['keyword b', 'other b']})
self.assertEqual(m.keywords(), {'vocab a': ['keyword a', 'other a'], self.assertEqual(m.keywords(), {'vocab a': ['keyword a', 'other a'],