QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 #include "qgsprojectmetadata.h"
21 
22 //
23 // QgsNativeMetadataBaseValidator
24 //
25 
26 bool QgsNativeMetadataBaseValidator::validate( const QgsAbstractMetadataBase *metadata, QList<QgsAbstractMetadataBaseValidator::ValidationResult> &results ) const
27 {
28  results.clear();
29  if ( !metadata )
30  return false;
31 
32  int index = 0;
33  bool result = true;
34  if ( metadata->identifier().isEmpty() )
35  {
36  result = false;
37  results << ValidationResult( QObject::tr( "identifier" ), QObject::tr( "Identifier element is required." ) );
38  }
39 
40  if ( metadata->language().isEmpty() )
41  {
42  result = false;
43  results << ValidationResult( QObject::tr( "language" ), QObject::tr( "Language element is required." ) );
44  }
45 
46  if ( metadata->type().isEmpty() )
47  {
48  result = false;
49  results << ValidationResult( QObject::tr( "type" ), QObject::tr( "Type element is required." ) );
50  }
51 
52  if ( metadata->title().isEmpty() )
53  {
54  result = false;
55  results << ValidationResult( QObject::tr( "title" ), QObject::tr( "Title element is required." ) );
56  }
57 
58  if ( metadata->abstract().isEmpty() )
59  {
60  result = false;
61  results << ValidationResult( QObject::tr( "abstract" ), QObject::tr( "Abstract element is required." ) );
62  }
63 
64  if ( metadata->contacts().isEmpty() )
65  {
66  result = false;
67  results << ValidationResult( QObject::tr( "contacts" ), QObject::tr( "At least one contact is required." ) );
68  }
69 
70  if ( metadata->links().isEmpty() )
71  {
72  result = false;
73  results << ValidationResult( QObject::tr( "links" ), QObject::tr( "At least one link is required." ) );
74  }
75 
76  // validate keywords
77  const QgsAbstractMetadataBase::KeywordMap keywords = metadata->keywords();
78  QgsAbstractMetadataBase::KeywordMap::const_iterator keywordIt = keywords.constBegin();
79  index = 0;
80  for ( ; keywordIt != keywords.constEnd(); ++keywordIt )
81  {
82  if ( keywordIt.key().isEmpty() )
83  {
84  result = false;
85  results << ValidationResult( QObject::tr( "keywords" ), QObject::tr( "Keyword vocabulary cannot be empty." ), index );
86  }
87  if ( keywordIt.value().isEmpty() )
88  {
89  result = false;
90  results << ValidationResult( QObject::tr( "keywords" ), QObject::tr( "Keyword list cannot be empty." ), index );
91  }
92  index++;
93  }
94 
95  // validate contacts
96  index = 0;
97  const auto constContacts = metadata->contacts();
98  for ( const QgsAbstractMetadataBase::Contact &contact : constContacts )
99  {
100  if ( contact.name.isEmpty() )
101  {
102  result = false;
103  results << ValidationResult( QObject::tr( "contacts" ), QObject::tr( "Contact name cannot be empty." ), index );
104  }
105  index++;
106  }
107 
108  // validate links
109  index = 0;
110  const auto constLinks = metadata->links();
111  for ( const QgsAbstractMetadataBase::Link &link : constLinks )
112  {
113  if ( link.name.isEmpty() )
114  {
115  result = false;
116  results << ValidationResult( QObject::tr( "links" ), QObject::tr( "Link name cannot be empty." ), index );
117  }
118  if ( link.type.isEmpty() )
119  {
120  result = false;
121  results << ValidationResult( QObject::tr( "links" ), QObject::tr( "Link type cannot be empty." ), index );
122  }
123  if ( link.url.isEmpty() )
124  {
125  result = false;
126  results << ValidationResult( QObject::tr( "links" ), QObject::tr( "Link url cannot be empty." ), index );
127  }
128  index++;
129  }
130 
131  return result;
132 }
133 
134 //
135 // QgsNativeMetadataValidator
136 //
137 
138 bool QgsNativeMetadataValidator::validate( const QgsAbstractMetadataBase *baseMetadata, QList<ValidationResult> &results ) const
139 {
140  results.clear();
141 
142  const QgsLayerMetadata *metadata = dynamic_cast< const QgsLayerMetadata * >( baseMetadata );
143  if ( !metadata )
144  return false;
145 
146  bool result = true;
147  if ( !QgsNativeMetadataBaseValidator::validate( metadata, results ) )
148  result = false;
149 
150  if ( metadata->licenses().isEmpty() )
151  {
152  result = false;
153  results << ValidationResult( QObject::tr( "license" ), QObject::tr( "At least one license is required." ) );
154  }
155 
156  if ( !metadata->crs().isValid() )
157  {
158  result = false;
159  results << ValidationResult( QObject::tr( "crs" ), QObject::tr( "A valid CRS element is required." ) );
160  }
161 
162  int index = 0;
163  const auto constSpatialExtents = metadata->extent().spatialExtents();
164  for ( const QgsLayerMetadata::SpatialExtent &extent : constSpatialExtents )
165  {
166  if ( !extent.extentCrs.isValid() )
167  {
168  result = false;
169  results << ValidationResult( QObject::tr( "extent" ), QObject::tr( "A valid CRS element for the spatial extent is required." ), index );
170  }
171 
172  if ( extent.bounds.width() == 0.0 || extent.bounds.height() == 0.0 )
173  {
174  result = false;
175  results << ValidationResult( QObject::tr( "extent" ), QObject::tr( "A valid spatial extent is required." ), index );
176  }
177  index++;
178  }
179 
180  return result;
181 }
182 
183 
184 //
185 // QgsNativeProjectMetadataValidator
186 //
187 
188 bool QgsNativeProjectMetadataValidator::validate( const QgsAbstractMetadataBase *baseMetadata, QList<QgsAbstractMetadataBaseValidator::ValidationResult> &results ) const
189 {
190  results.clear();
191 
192  const QgsProjectMetadata *metadata = dynamic_cast< const QgsProjectMetadata * >( baseMetadata );
193  if ( !metadata )
194  return false;
195 
196  bool result = true;
197  if ( !QgsNativeMetadataBaseValidator::validate( metadata, results ) )
198  result = false;
199 
200  if ( metadata->author().isEmpty() )
201  {
202  result = false;
203  results << ValidationResult( QObject::tr( "author" ), QObject::tr( "A project author is required." ) );
204  }
205 
206  if ( !metadata->creationDateTime().isValid() )
207  {
208  result = false;
209  results << ValidationResult( QObject::tr( "creation" ), QObject::tr( "The project creation date/time is required." ) );
210  }
211 
212  return result;
213 }
Contains the parameters describing a metadata validation failure.
An abstract base class for metadata stores.
QMap< QString, QStringList > KeywordMap
Map of vocabulary string to keyword list.
QgsAbstractMetadataBase::ContactList contacts() const
Returns a list of contact persons or entities associated with the resource.
QString abstract() const
Returns a free-form description of the resource.
QString title() const
Returns the human readable name of the resource, typically displayed in search results.
QgsAbstractMetadataBase::KeywordMap keywords() const
Returns the keywords map, which is a set of descriptive keywords associated with the resource.
QgsAbstractMetadataBase::LinkList links() const
Returns a list of online resources associated with the resource.
QString language() const
Returns the human language associated with the resource.
QString type() const
Returns the nature of the resource.
QString identifier() const
A reference, URI, URL or some other mechanism to identify the resource.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
A structured metadata store for a map layer.
const QgsLayerMetadata::Extent & extent() const
Returns the spatial and temporal extents associated with the resource.
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system described by the layer's metadata.
QStringList licenses() const
Returns a list of licenses associated with the resource (examples: http://opendefinition....
bool validate(const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results) const override
Validates a metadata object, and returns true if the metadata is considered valid.
bool validate(const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results) const override
Validates a metadata object, and returns true if the metadata is considered valid.
bool validate(const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results) const override
Validates a metadata object, and returns true if the metadata is considered valid.
A structured metadata store for a map layer.
QString author() const
Returns the project author string.
QDateTime creationDateTime() const
Returns the project's creation date/timestamp.
QList< QgsLayerMetadata::SpatialExtent > spatialExtents() const
Spatial extents of the resource.
Metadata spatial extent structure.