QGIS API Documentation  3.0.2-Girona (307d082)
qgslayermetadatavalidator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayermetadatavalidator.cpp
3  -----------------------------
4  begin : April 2017
5  copyright : (C) 2017 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 "qgslayermetadata.h"
20 
21 bool QgsNativeMetadataValidator::validate( const QgsLayerMetadata &metadata, QList<ValidationResult> &results ) const
22 {
23  results.clear();
24 
25  bool result = true;
26  if ( metadata.identifier().isEmpty() )
27  {
28  result = false;
29  results << ValidationResult( QObject::tr( "identifier" ), QObject::tr( "Identifier element is required." ) );
30  }
31 
32  if ( metadata.language().isEmpty() )
33  {
34  result = false;
35  results << ValidationResult( QObject::tr( "language" ), QObject::tr( "Language element is required." ) );
36  }
37 
38  if ( metadata.type().isEmpty() )
39  {
40  result = false;
41  results << ValidationResult( QObject::tr( "type" ), QObject::tr( "Type element is required." ) );
42  }
43 
44  if ( metadata.title().isEmpty() )
45  {
46  result = false;
47  results << ValidationResult( QObject::tr( "title" ), QObject::tr( "Title element is required." ) );
48  }
49 
50  if ( metadata.abstract().isEmpty() )
51  {
52  result = false;
53  results << ValidationResult( QObject::tr( "abstract" ), QObject::tr( "Abstract element is required." ) );
54  }
55 
56  if ( metadata.licenses().isEmpty() )
57  {
58  result = false;
59  results << ValidationResult( QObject::tr( "license" ), QObject::tr( "At least one license is required." ) );
60  }
61 
62  if ( !metadata.crs().isValid() )
63  {
64  result = false;
65  results << ValidationResult( QObject::tr( "crs" ), QObject::tr( "A valid CRS element is required." ) );
66  }
67 
68  int index = 0;
69  Q_FOREACH ( const QgsLayerMetadata::SpatialExtent &extent, metadata.extent().spatialExtents() )
70  {
71  if ( !extent.extentCrs.isValid() )
72  {
73  result = false;
74  results << ValidationResult( QObject::tr( "extent" ), QObject::tr( "A valid CRS element for the spatial extent is required." ), index );
75  }
76 
77  if ( extent.bounds.width() == 0.0 || extent.bounds.height() == 0.0 )
78  {
79  result = false;
80  results << ValidationResult( QObject::tr( "extent" ), QObject::tr( "A valid spatial extent is required." ), index );
81  }
82  index++;
83  }
84 
85  if ( metadata.contacts().isEmpty() )
86  {
87  result = false;
88  results << ValidationResult( QObject::tr( "contacts" ), QObject::tr( "At least one contact is required." ) );
89  }
90 
91  if ( metadata.links().isEmpty() )
92  {
93  result = false;
94  results << ValidationResult( QObject::tr( "links" ), QObject::tr( "At least one link is required." ) );
95  }
96 
97  // validate keywords
98  QgsLayerMetadata::KeywordMap keywords = metadata.keywords();
99  QgsLayerMetadata::KeywordMap::const_iterator keywordIt = keywords.constBegin();
100  index = 0;
101  for ( ; keywordIt != keywords.constEnd(); ++keywordIt )
102  {
103  if ( keywordIt.key().isEmpty() )
104  {
105  result = false;
106  results << ValidationResult( QObject::tr( "keywords" ), QObject::tr( "Keyword vocabulary cannot be empty." ), index );
107  }
108  if ( keywordIt.value().isEmpty() )
109  {
110  result = false;
111  results << ValidationResult( QObject::tr( "keywords" ), QObject::tr( "Keyword list cannot be empty." ), index );
112  }
113  index++;
114  }
115 
116  // validate contacts
117  index = 0;
118  Q_FOREACH ( const QgsLayerMetadata::Contact &contact, metadata.contacts() )
119  {
120  if ( contact.name.isEmpty() )
121  {
122  result = false;
123  results << ValidationResult( QObject::tr( "contacts" ), QObject::tr( "Contact name cannot be empty." ), index );
124  }
125  index++;
126  }
127 
128  // validate links
129  index = 0;
130  Q_FOREACH ( const QgsLayerMetadata::Link &link, metadata.links() )
131  {
132  if ( link.name.isEmpty() )
133  {
134  result = false;
135  results << ValidationResult( QObject::tr( "links" ), QObject::tr( "Link name cannot be empty." ), index );
136  }
137  if ( link.type.isEmpty() )
138  {
139  result = false;
140  results << ValidationResult( QObject::tr( "links" ), QObject::tr( "Link type cannot be empty." ), index );
141  }
142  if ( link.url.isEmpty() )
143  {
144  result = false;
145  results << ValidationResult( QObject::tr( "links" ), QObject::tr( "Link url cannot be empty." ), index );
146  }
147  index++;
148  }
149 
150  return result;
151 }
Contains the parameters describing a metadata validation failure.
double height() const
Returns the height of the box.
Definition: qgsbox3d.h:157
QString name
Name of contact.
QString title() const
Returns the human readable name of the resource, typically displayed in search results.
QString abstract() const
Returns a free-form description of the resource.
QString identifier() const
A reference, URI, URL or some other mechanism to identify the resource.
const QgsLayerMetadata::Extent & extent() const
Returns the spatial and temporal extents associated with the resource.
QgsCoordinateReferenceSystem extentCrs
Coordinate reference system for spatial extent.
QgsLayerMetadata::ContactList contacts() const
Returns a list of contact persons or entities associated with the resource.
QString language() const
Returns the human language associated with the resource.
bool validate(const QgsLayerMetadata &metadata, QList< QgsMetadataValidator::ValidationResult > &results) const override
Validates a metadata object, and returns true if the metadata is considered valid.
QString type() const
Returns the nature of the resource.
KeywordMap keywords() const
Returns the keywords map, which is a set of descriptive keywords associated with the resource...
QMap< QString, QStringList > KeywordMap
Map of vocabulary string to keyword list.
A structured metadata store for a map layer.
double width() const
Returns the width of the box.
Definition: qgsbox3d.h:150
QgsLayerMetadata::LinkList links() const
Returns a list of online resources associated with the resource.
Metadata contact structure.
QgsBox3d bounds
Geospatial extent of the resource.
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system described by the layer&#39;s metadata.
QStringList licenses() const
Returns a list of licenses associated with the resource (examples: http://opendefinition.org/licenses/).
QList< QgsLayerMetadata::SpatialExtent > spatialExtents() const
Spatial extents of the resource.
Metadata spatial extent structure.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.