QGIS API Documentation 4.1.0-Master (31622b25bb0)
Loading...
Searching...
No Matches
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
20#include "qgslayermetadata.h"
21#include "qgsprojectmetadata.h"
22
23//
24// QgsNativeMetadataBaseValidator
25//
26
27bool QgsNativeMetadataBaseValidator::validate( const QgsAbstractMetadataBase *metadata, QList<QgsAbstractMetadataBaseValidator::ValidationResult> &results ) const
28{
29 results.clear();
30 if ( !metadata )
31 return false;
32
33 int index = 0;
34 bool result = true;
35 if ( metadata->identifier().isEmpty() )
36 {
37 result = false;
38 results << ValidationResult( QObject::tr( "identifier" ), QObject::tr( "Identifier element is required." ) );
39 }
40
41 if ( metadata->language().isEmpty() )
42 {
43 result = false;
44 results << ValidationResult( QObject::tr( "language" ), QObject::tr( "Language element is required." ) );
45 }
46
47 if ( metadata->type().isEmpty() )
48 {
49 result = false;
50 results << ValidationResult( QObject::tr( "type" ), QObject::tr( "Type element is required." ) );
51 }
52
53 if ( metadata->title().isEmpty() )
54 {
55 result = false;
56 results << ValidationResult( QObject::tr( "title" ), QObject::tr( "Title element is required." ) );
57 }
58
59 if ( metadata->abstract().isEmpty() )
60 {
61 result = false;
62 results << ValidationResult( QObject::tr( "abstract" ), QObject::tr( "Abstract element is required." ) );
63 }
64
65 if ( metadata->contacts().isEmpty() )
66 {
67 result = false;
68 results << ValidationResult( QObject::tr( "contacts" ), QObject::tr( "At least one contact is required." ) );
69 }
70
71 if ( metadata->links().isEmpty() )
72 {
73 result = false;
74 results << ValidationResult( QObject::tr( "links" ), QObject::tr( "At least one link is required." ) );
75 }
76
77 // validate keywords
78 const QgsAbstractMetadataBase::KeywordMap keywords = metadata->keywords();
79 QgsAbstractMetadataBase::KeywordMap::const_iterator keywordIt = keywords.constBegin();
80 index = 0;
81 for ( ; keywordIt != keywords.constEnd(); ++keywordIt )
82 {
83 if ( keywordIt.key().isEmpty() )
84 {
85 result = false;
86 results << ValidationResult( QObject::tr( "keywords" ), QObject::tr( "Keyword vocabulary cannot be empty." ), index );
87 }
88 if ( keywordIt.value().isEmpty() )
89 {
90 result = false;
91 results << ValidationResult( QObject::tr( "keywords" ), QObject::tr( "Keyword list cannot be empty." ), index );
92 }
93 index++;
94 }
95
96 // validate contacts
97 index = 0;
98 const auto constContacts = metadata->contacts();
99 for ( const QgsAbstractMetadataBase::Contact &contact : constContacts )
100 {
101 if ( contact.name.isEmpty() )
102 {
103 result = false;
104 results << ValidationResult( QObject::tr( "contacts" ), QObject::tr( "Contact name cannot be empty." ), index );
105 }
106 index++;
107 }
108
109 // validate links
110 index = 0;
111 const auto constLinks = metadata->links();
112 for ( const QgsAbstractMetadataBase::Link &link : constLinks )
113 {
114 if ( link.name.isEmpty() )
115 {
116 result = false;
117 results << ValidationResult( QObject::tr( "links" ), QObject::tr( "Link name cannot be empty." ), index );
118 }
119 if ( link.type.isEmpty() )
120 {
121 result = false;
122 results << ValidationResult( QObject::tr( "links" ), QObject::tr( "Link type cannot be empty." ), index );
123 }
124 if ( link.url.isEmpty() )
125 {
126 result = false;
127 results << ValidationResult( QObject::tr( "links" ), QObject::tr( "Link url cannot be empty." ), index );
128 }
129 index++;
130 }
131
132 return result;
133}
134
135//
136// QgsNativeMetadataValidator
137//
138
139bool QgsNativeMetadataValidator::validate( const QgsAbstractMetadataBase *baseMetadata, QList<ValidationResult> &results ) const
140{
141 results.clear();
142
143 const QgsLayerMetadata *metadata = dynamic_cast< const QgsLayerMetadata * >( baseMetadata );
144 if ( !metadata )
145 return false;
146
147 bool result = true;
148 if ( !QgsNativeMetadataBaseValidator::validate( metadata, results ) )
149 result = false;
150
151 if ( metadata->licenses().isEmpty() )
152 {
153 result = false;
154 results << ValidationResult( QObject::tr( "license" ), QObject::tr( "At least one license is required." ) );
155 }
156
157 if ( !metadata->crs().isValid() )
158 {
159 result = false;
160 results << ValidationResult( QObject::tr( "crs" ), QObject::tr( "A valid CRS element is required." ) );
161 }
162
163 int index = 0;
164 const auto constSpatialExtents = metadata->extent().spatialExtents();
165 for ( const QgsLayerMetadata::SpatialExtent &extent : constSpatialExtents )
166 {
167 if ( !extent.extentCrs.isValid() )
168 {
169 result = false;
170 results << ValidationResult( QObject::tr( "extent" ), QObject::tr( "A valid CRS element for the spatial extent is required." ), index );
171 }
172
173 if ( extent.bounds.width() == 0.0 || extent.bounds.height() == 0.0 )
174 {
175 result = false;
176 results << ValidationResult( QObject::tr( "extent" ), QObject::tr( "A valid spatial extent is required." ), index );
177 }
178 index++;
179 }
180
181 return result;
182}
183
184
185//
186// QgsNativeProjectMetadataValidator
187//
188
189bool QgsNativeProjectMetadataValidator::validate( const QgsAbstractMetadataBase *baseMetadata, QList<QgsAbstractMetadataBaseValidator::ValidationResult> &results ) const
190{
191 results.clear();
192
193 const QgsProjectMetadata *metadata = dynamic_cast< const QgsProjectMetadata * >( baseMetadata );
194 if ( !metadata )
195 return false;
196
197 bool result = true;
198 if ( !QgsNativeMetadataBaseValidator::validate( metadata, results ) )
199 result = false;
200
201 if ( metadata->author().isEmpty() )
202 {
203 result = false;
204 results << ValidationResult( QObject::tr( "author" ), QObject::tr( "A project author is required." ) );
205 }
206
207 if ( !metadata->creationDateTime().isValid() )
208 {
209 result = false;
210 results << ValidationResult( QObject::tr( "creation" ), QObject::tr( "The project creation date/time is required." ) );
211 }
212
213 return result;
214}
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.
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 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.
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 project.
QDateTime creationDateTime() const
Returns the project's creation date/timestamp.
QList< QgsLayerMetadata::SpatialExtent > spatialExtents() const
Spatial extents of the resource.
Metadata spatial extent structure.