QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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  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  Q_FOREACH ( const QgsAbstractMetadataBase::Contact &contact, metadata->contacts() )
98  {
99  if ( contact.name.isEmpty() )
100  {
101  result = false;
102  results << ValidationResult( QObject::tr( "contacts" ), QObject::tr( "Contact name cannot be empty." ), index );
103  }
104  index++;
105  }
106 
107  // validate links
108  index = 0;
109  Q_FOREACH ( const QgsAbstractMetadataBase::Link &link, metadata->links() )
110  {
111  if ( link.name.isEmpty() )
112  {
113  result = false;
114  results << ValidationResult( QObject::tr( "links" ), QObject::tr( "Link name cannot be empty." ), index );
115  }
116  if ( link.type.isEmpty() )
117  {
118  result = false;
119  results << ValidationResult( QObject::tr( "links" ), QObject::tr( "Link type cannot be empty." ), index );
120  }
121  if ( link.url.isEmpty() )
122  {
123  result = false;
124  results << ValidationResult( QObject::tr( "links" ), QObject::tr( "Link url cannot be empty." ), index );
125  }
126  index++;
127  }
128 
129  return result;
130 }
131 
132 //
133 // QgsNativeMetadataValidator
134 //
135 
136 bool QgsNativeMetadataValidator::validate( const QgsAbstractMetadataBase *baseMetadata, QList<ValidationResult> &results ) const
137 {
138  results.clear();
139 
140  const QgsLayerMetadata *metadata = dynamic_cast< const QgsLayerMetadata * >( baseMetadata );
141  if ( !metadata )
142  return false;
143 
144  bool result = true;
145  if ( !QgsNativeMetadataBaseValidator::validate( metadata, results ) )
146  result = false;
147 
148  if ( metadata->licenses().isEmpty() )
149  {
150  result = false;
151  results << ValidationResult( QObject::tr( "license" ), QObject::tr( "At least one license is required." ) );
152  }
153 
154  if ( !metadata->crs().isValid() )
155  {
156  result = false;
157  results << ValidationResult( QObject::tr( "crs" ), QObject::tr( "A valid CRS element is required." ) );
158  }
159 
160  int index = 0;
161  Q_FOREACH ( const QgsLayerMetadata::SpatialExtent &extent, metadata->extent().spatialExtents() )
162  {
163  if ( !extent.extentCrs.isValid() )
164  {
165  result = false;
166  results << ValidationResult( QObject::tr( "extent" ), QObject::tr( "A valid CRS element for the spatial extent is required." ), index );
167  }
168 
169  if ( extent.bounds.width() == 0.0 || extent.bounds.height() == 0.0 )
170  {
171  result = false;
172  results << ValidationResult( QObject::tr( "extent" ), QObject::tr( "A valid spatial extent is required." ), index );
173  }
174  index++;
175  }
176 
177  return result;
178 }
179 
180 
181 //
182 // QgsNativeProjectMetadataValidator
183 //
184 
185 bool QgsNativeProjectMetadataValidator::validate( const QgsAbstractMetadataBase *baseMetadata, QList<QgsAbstractMetadataBaseValidator::ValidationResult> &results ) const
186 {
187  results.clear();
188 
189  const QgsProjectMetadata *metadata = dynamic_cast< const QgsProjectMetadata * >( baseMetadata );
190  if ( !metadata )
191  return false;
192 
193  bool result = true;
194  if ( !QgsNativeMetadataBaseValidator::validate( metadata, results ) )
195  result = false;
196 
197  if ( metadata->author().isEmpty() )
198  {
199  result = false;
200  results << ValidationResult( QObject::tr( "author" ), QObject::tr( "A project author is required." ) );
201  }
202 
203  if ( !metadata->creationDateTime().isValid() )
204  {
205  result = false;
206  results << ValidationResult( QObject::tr( "creation" ), QObject::tr( "The project creation date/time is required." ) );
207  }
208 
209  return result;
210 }
QString type() const
Returns the nature of the resource.
QList< QgsLayerMetadata::SpatialExtent > spatialExtents() const
Spatial extents of the resource.
const QgsLayerMetadata::Extent & extent() const
Returns the spatial and temporal extents associated with the resource.
bool validate(const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results) const override
Validates a metadata object, and returns true if the metadata is considered valid.
QString identifier() const
A reference, URI, URL or some other mechanism to identify the resource.
QgsAbstractMetadataBase::ContactList contacts() const
Returns a list of contact persons or entities associated with the resource.
QgsAbstractMetadataBase::KeywordMap keywords() const
Returns the keywords map, which is a set of descriptive keywords associated with the resource...
QString language() const
Returns the human language associated with the resource.
QgsAbstractMetadataBase::LinkList links() const
Returns a list of online resources associated with the resource.
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system described by the layer&#39;s metadata.
QgsCoordinateReferenceSystem extentCrs
Coordinate reference system for spatial extent.
QString title() const
Returns the human readable name of the resource, typically displayed in search results.
double height() const
Returns the height of the box.
Definition: qgsbox3d.h:158
double width() const
Returns the width of the box.
Definition: qgsbox3d.h:151
bool validate(const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results) const override
Validates a metadata object, and returns true if the metadata is considered valid.
Contains the parameters describing a metadata validation failure.
QString abstract() const
Returns a free-form description of the resource.
QDateTime creationDateTime() const
Returns the project&#39;s creation date/timestamp.
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.
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
QString author() const
Returns the project author string.
QStringList licenses() const
Returns a list of licenses associated with the resource (examples: http://opendefinition.org/licenses/).
QMap< QString, QStringList > KeywordMap
Map of vocabulary string to keyword list.
QgsBox3d bounds
Geospatial extent of the resource.
An abstract base class for metadata stores.
Metadata spatial extent structure.
A structured metadata store for a map layer.