QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgsxmlutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsxmlutils.cpp
3 ---------------------
4 begin : December 2013
5 copyright : (C) 2013 by Martin Dobias
6 email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15#include "qgsxmlutils.h"
16
17#include "qgscolorutils.h"
19#include "qgsproperty.h"
20#include "qgsrectangle.h"
22#include "qgsunittypes.h"
23
24#include <QDomElement>
25
26Qgis::DistanceUnit QgsXmlUtils::readMapUnits( const QDomElement &element )
27{
28 if ( "unknown" == element.text() )
29 {
31 }
32 else
33 {
34 const Qgis::DistanceUnit unit = QgsUnitTypes::decodeDistanceUnit( element.text() );
36 }
37}
38
39QgsBox3D QgsXmlUtils::readBox3D( const QDomElement &element )
40{
41 QgsBox3D aoi;
42
43 const double xmin = element.attribute( QStringLiteral( "xmin" ) ).toDouble();
44 aoi.setXMinimum( xmin );
45
46 const double ymin = element.attribute( QStringLiteral( "ymin" ) ).toDouble();
47 aoi.setYMinimum( ymin );
48
49 const double zmin = element.attribute( QStringLiteral( "zmin" ) ).toDouble();
50 aoi.setZMinimum( zmin );
51
52 const double xmax = element.attribute( QStringLiteral( "xmax" ) ).toDouble();
53 aoi.setXMaximum( xmax );
54
55 const double ymax = element.attribute( QStringLiteral( "ymax" ) ).toDouble();
56 aoi.setYMaximum( ymax );
57
58 const double zmax = element.attribute( QStringLiteral( "zmax" ) ).toDouble();
59 aoi.setZMaximum( zmax );
60
61 return aoi;
62}
63
64QgsRectangle QgsXmlUtils::readRectangle( const QDomElement &element )
65{
66 QgsRectangle aoi;
67
68 const QDomNode xminNode = element.namedItem( QStringLiteral( "xmin" ) );
69 const QDomNode yminNode = element.namedItem( QStringLiteral( "ymin" ) );
70 const QDomNode xmaxNode = element.namedItem( QStringLiteral( "xmax" ) );
71 const QDomNode ymaxNode = element.namedItem( QStringLiteral( "ymax" ) );
72
73 QDomElement exElement = xminNode.toElement();
74 const double xmin = exElement.text().toDouble();
75 aoi.setXMinimum( xmin );
76
77 exElement = yminNode.toElement();
78 const double ymin = exElement.text().toDouble();
79 aoi.setYMinimum( ymin );
80
81 exElement = xmaxNode.toElement();
82 const double xmax = exElement.text().toDouble();
83 aoi.setXMaximum( xmax );
84
85 exElement = ymaxNode.toElement();
86 const double ymax = exElement.text().toDouble();
87 aoi.setYMaximum( ymax );
88
89 return aoi;
90}
91
92
93
94QDomElement QgsXmlUtils::writeMapUnits( Qgis::DistanceUnit units, QDomDocument &doc )
95{
96 QString unitsString = QgsUnitTypes::encodeUnit( units );
97 // maintain compatibility with old projects
98 if ( units == Qgis::DistanceUnit::Unknown )
99 unitsString = QStringLiteral( "unknown" );
100
101 QDomElement unitsNode = doc.createElement( QStringLiteral( "units" ) );
102 unitsNode.appendChild( doc.createTextNode( unitsString ) );
103 return unitsNode;
104}
105
106QDomElement QgsXmlUtils::writeBox3D( const QgsBox3D &box, QDomDocument &doc, const QString &elementName )
107{
108 QDomElement elemExtent3D = doc.createElement( elementName );
109 elemExtent3D.setAttribute( QStringLiteral( "xMin" ), box.xMinimum() );
110 elemExtent3D.setAttribute( QStringLiteral( "yMin" ), box.yMinimum() );
111 elemExtent3D.setAttribute( QStringLiteral( "zMin" ), box.zMinimum() );
112 elemExtent3D.setAttribute( QStringLiteral( "xMax" ), box.xMaximum() );
113 elemExtent3D.setAttribute( QStringLiteral( "yMax" ), box.yMaximum() );
114 elemExtent3D.setAttribute( QStringLiteral( "zMax" ), box.zMaximum() );
115
116 return elemExtent3D;
117}
118
119QDomElement QgsXmlUtils::writeRectangle( const QgsRectangle &rect, QDomDocument &doc, const QString &elementName )
120{
121 QDomElement xMin = doc.createElement( QStringLiteral( "xmin" ) );
122 QDomElement yMin = doc.createElement( QStringLiteral( "ymin" ) );
123 QDomElement xMax = doc.createElement( QStringLiteral( "xmax" ) );
124 QDomElement yMax = doc.createElement( QStringLiteral( "ymax" ) );
125
126 const QDomText xMinText = doc.createTextNode( qgsDoubleToString( rect.xMinimum() ) );
127 const QDomText yMinText = doc.createTextNode( qgsDoubleToString( rect.yMinimum() ) );
128 const QDomText xMaxText = doc.createTextNode( qgsDoubleToString( rect.xMaximum() ) );
129 const QDomText yMaxText = doc.createTextNode( qgsDoubleToString( rect.yMaximum() ) );
130
131 xMin.appendChild( xMinText );
132 yMin.appendChild( yMinText );
133 xMax.appendChild( xMaxText );
134 yMax.appendChild( yMaxText );
135
136 QDomElement extentNode = doc.createElement( elementName );
137 extentNode.appendChild( xMin );
138 extentNode.appendChild( yMin );
139 extentNode.appendChild( xMax );
140 extentNode.appendChild( yMax );
141 return extentNode;
142}
143
144QDomElement QgsXmlUtils::writeVariant( const QVariant &value, QDomDocument &doc )
145{
146 QDomElement element = doc.createElement( QStringLiteral( "Option" ) );
147 switch ( value.userType() )
148 {
149 case QMetaType::Type::UnknownType:
150 {
151 element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "invalid" ) );
152 break;
153 }
154
155 case QMetaType::Type::QVariantMap:
156 {
157 const QVariantMap map = value.toMap();
158
159 for ( auto option = map.constBegin(); option != map.constEnd(); ++option )
160 {
161 QDomElement optionElement = writeVariant( option.value(), doc );
162 optionElement.setAttribute( QStringLiteral( "name" ), option.key() );
163 element.appendChild( optionElement );
164 element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "Map" ) );
165 }
166 break;
167 }
168
169 case QMetaType::Type::QVariantList:
170 {
171 const QVariantList list = value.toList();
172
173 const auto constList = list;
174 for ( const QVariant &value : constList )
175 {
176 const QDomElement valueElement = writeVariant( value, doc );
177 element.appendChild( valueElement );
178 element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "List" ) );
179 }
180 break;
181 }
182
183 case QMetaType::Type::QStringList:
184 {
185 const QStringList list = value.toStringList();
186
187 const auto constList = list;
188 for ( const QString &value : constList )
189 {
190 const QDomElement valueElement = writeVariant( value, doc );
191 element.appendChild( valueElement );
192 element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "StringList" ) );
193 }
194 break;
195 }
196
197 case QMetaType::Type::QRect:
198 {
199 element.setAttribute( QStringLiteral( "type" ), "QRect" );
200
201 const QRect rect = value.toRect();
202 element.setAttribute( QStringLiteral( "x" ), rect.x() );
203 element.setAttribute( QStringLiteral( "y" ), rect.y() );
204 element.setAttribute( QStringLiteral( "width" ), rect.width() );
205 element.setAttribute( QStringLiteral( "height" ), rect.height() );
206 break;
207 }
208
209 case QMetaType::Type::Int:
210 case QMetaType::Type::UInt:
211 case QMetaType::Type::Bool:
212 case QMetaType::Type::Double:
213 case QMetaType::Type::LongLong:
214 case QMetaType::Type::ULongLong:
215 case QMetaType::Type::QString:
216 element.setAttribute( QStringLiteral( "type" ), QVariant::typeToName( static_cast<QMetaType::Type>( value.userType() ) ) );
217 element.setAttribute( QStringLiteral( "value" ), value.toString() );
218 break;
219
220 case QMetaType::Type::QChar:
221 element.setAttribute( QStringLiteral( "type" ), QVariant::typeToName( static_cast<QMetaType::Type>( value.userType() ) ) );
222 element.setAttribute( QStringLiteral( "value" ), QgsVariantUtils::isNull( value ) ? QString() : QString( value.toChar() ) );
223 break;
224
225 case QMetaType::Type::QColor:
226 element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "color" ) );
227 element.setAttribute( QStringLiteral( "value" ), value.value< QColor >().isValid() ? QgsColorUtils::colorToString( value.value< QColor >() ) : QString() );
228 break;
229
230 case QMetaType::Type::QDateTime:
231 element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "datetime" ) );
232 element.setAttribute( QStringLiteral( "value" ), value.value< QDateTime >().isValid() ? value.toDateTime().toString( Qt::ISODate ) : QString() );
233 break;
234
235 case QMetaType::Type::QDate:
236 element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "date" ) );
237 element.setAttribute( QStringLiteral( "value" ), value.value< QDate >().isValid() ? value.toDate().toString( Qt::ISODate ) : QString() );
238 break;
239
240 case QMetaType::Type::QTime:
241 element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "time" ) );
242 element.setAttribute( QStringLiteral( "value" ), value.value< QTime >().isValid() ? value.toTime().toString( Qt::ISODate ) : QString() );
243 break;
244
245 default:
246
247 if ( value.userType() == qMetaTypeId<QgsProperty>() )
248 {
249 element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "QgsProperty" ) );
250 const QDomElement propertyElem = QgsXmlUtils::writeVariant( value.value< QgsProperty >().toVariant(), doc );
251 element.appendChild( propertyElem );
252 break;
253 }
254 else if ( value.userType() == qMetaTypeId<QgsCoordinateReferenceSystem>() )
255 {
256 element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "QgsCoordinateReferenceSystem" ) );
258 crs.writeXml( element, doc );
259 break;
260 }
261 else if ( value.userType() == qMetaTypeId< QgsGeometry>() )
262 {
263 element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "QgsGeometry" ) );
264 const QgsGeometry geom = value.value< QgsGeometry >();
265 element.setAttribute( QStringLiteral( "value" ), geom.asWkt() );
266 break;
267 }
268 else if ( value.userType() == qMetaTypeId<QgsProcessingOutputLayerDefinition>() )
269 {
270 const QDomElement valueElement = writeVariant( value.value< QgsProcessingOutputLayerDefinition >().toVariant(), doc );
271 element.appendChild( valueElement );
272 element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "QgsProcessingOutputLayerDefinition" ) );
273 break;
274 }
275 else if ( value.userType() == qMetaTypeId<QgsProcessingFeatureSourceDefinition>() )
276 {
277 const QDomElement valueElement = writeVariant( value.value< QgsProcessingFeatureSourceDefinition >().toVariant(), doc );
278 element.appendChild( valueElement );
279 element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "QgsProcessingFeatureSourceDefinition" ) );
280 break;
281 }
282 else if ( value.userType() == qMetaTypeId<QgsRemappingSinkDefinition>() )
283 {
284 const QDomElement valueElement = writeVariant( value.value< QgsRemappingSinkDefinition >().toVariant(), doc );
285 element.appendChild( valueElement );
286 element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "QgsRemappingSinkDefinition" ) );
287 break;
288 }
289 else
290 {
291#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
292 Q_ASSERT_X( false, "QgsXmlUtils::writeVariant", QStringLiteral( "unsupported %1variant type %2" )
293 .arg( value.userType() >= QMetaType::Type::User ? "user " : QString() ).arg( QMetaType::typeName( value.userType() ) ).toLocal8Bit() );
294#else
295 Q_ASSERT_X( false, "QgsXmlUtils::writeVariant", QStringLiteral( "unsupported %1variant type %2" )
296 .arg( value.userType() >= QMetaType::Type::User ? "user " : QString() ).arg( value.metaType().name() ).toLocal8Bit() );
297#endif
298 }
299 break;
300 }
301
302 return element;
303}
304
305QVariant QgsXmlUtils::readVariant( const QDomElement &element )
306{
307 const QString type = element.attribute( QStringLiteral( "type" ) );
308
309 if ( type == QLatin1String( "invalid" ) )
310 {
311 return QVariant();
312 }
313 else if ( type == QLatin1String( "int" ) )
314 {
315 return element.attribute( QStringLiteral( "value" ) ).toInt();
316 }
317 else if ( type == QLatin1String( "uint" ) )
318 {
319 return element.attribute( QStringLiteral( "value" ) ).toUInt();
320 }
321 else if ( type == QLatin1String( "qlonglong" ) )
322 {
323 return element.attribute( QStringLiteral( "value" ) ).toLongLong();
324 }
325 else if ( type == QLatin1String( "qulonglong" ) )
326 {
327 return element.attribute( QStringLiteral( "value" ) ).toULongLong();
328 }
329 else if ( type == QLatin1String( "double" ) )
330 {
331 return element.attribute( QStringLiteral( "value" ) ).toDouble();
332 }
333 else if ( type == QLatin1String( "QString" ) )
334 {
335 const QString res = element.attribute( QStringLiteral( "value" ) );
336 return res.isEmpty() ? QVariant() : res;
337 }
338 else if ( type == QLatin1String( "QChar" ) )
339 {
340 const QString res = element.attribute( QStringLiteral( "value" ) );
341 return res.isEmpty() ? QVariant() : res.at( 0 );
342 }
343 else if ( type == QLatin1String( "bool" ) )
344 {
345 return element.attribute( QStringLiteral( "value" ) ) == QLatin1String( "true" );
346 }
347 else if ( type == QLatin1String( "color" ) )
348 {
349 return element.attribute( QStringLiteral( "value" ) ).isEmpty() ? QVariant() : QgsColorUtils::colorFromString( element.attribute( QStringLiteral( "value" ) ) );
350 }
351 else if ( type == QLatin1String( "datetime" ) )
352 {
353 return element.attribute( QStringLiteral( "value" ) ).isEmpty() ? QVariant() : QDateTime::fromString( element.attribute( QStringLiteral( "value" ) ), Qt::ISODate );
354 }
355 else if ( type == QLatin1String( "date" ) )
356 {
357 return element.attribute( QStringLiteral( "value" ) ).isEmpty() ? QVariant() : QDate::fromString( element.attribute( QStringLiteral( "value" ) ), Qt::ISODate );
358 }
359 else if ( type == QLatin1String( "time" ) )
360 {
361 return element.attribute( QStringLiteral( "value" ) ).isEmpty() ? QVariant() : QTime::fromString( element.attribute( QStringLiteral( "value" ) ), Qt::ISODate );
362 }
363 else if ( type == QLatin1String( "Map" ) )
364 {
365 QVariantMap map;
366 const QDomNodeList options = element.childNodes();
367
368 for ( int i = 0; i < options.count(); ++i )
369 {
370 const QDomElement elem = options.at( i ).toElement();
371 if ( elem.tagName() == QLatin1String( "Option" ) )
372 map.insert( elem.attribute( QStringLiteral( "name" ) ), readVariant( elem ) );
373 }
374 return map;
375 }
376 else if ( type == QLatin1String( "List" ) )
377 {
378 QVariantList list;
379 const QDomNodeList values = element.childNodes();
380 for ( int i = 0; i < values.count(); ++i )
381 {
382 const QDomElement elem = values.at( i ).toElement();
383 list.append( readVariant( elem ) );
384 }
385 return list;
386 }
387 else if ( type == QLatin1String( "StringList" ) )
388 {
389 QStringList list;
390 const QDomNodeList values = element.childNodes();
391 for ( int i = 0; i < values.count(); ++i )
392 {
393 const QDomElement elem = values.at( i ).toElement();
394 list.append( readVariant( elem ).toString() );
395 }
396 return list;
397 }
398 else if ( type == QLatin1String( "QRect" ) )
399 {
400 const int x = element.attribute( "x" ).toInt();
401 const int y = element.attribute( "y" ).toInt();
402 const int width = element.attribute( "width" ).toInt();
403 const int height = element.attribute( "height" ).toInt();
404
405 return QRect( x, y, width, height );
406 }
407 else if ( type == QLatin1String( "QgsProperty" ) )
408 {
409 const QDomNodeList values = element.childNodes();
410 if ( values.isEmpty() )
411 return QVariant();
412
413 QgsProperty p;
414 if ( p.loadVariant( QgsXmlUtils::readVariant( values.at( 0 ).toElement() ) ) )
415 return p;
416
417 return QVariant();
418 }
419 else if ( type == QLatin1String( "QgsCoordinateReferenceSystem" ) )
420 {
422 crs.readXml( element );
423 return crs.isValid() ? crs : QVariant();
424 }
425 else if ( type == QLatin1String( "QgsGeometry" ) )
426 {
427 const QgsGeometry g = QgsGeometry::fromWkt( element.attribute( "value" ) );
428 return !g.isNull() ? g : QVariant();
429 }
430 else if ( type == QLatin1String( "QgsProcessingOutputLayerDefinition" ) )
431 {
433 const QDomNodeList values = element.childNodes();
434 if ( values.isEmpty() )
435 return QVariant();
436
437 if ( res.loadVariant( QgsXmlUtils::readVariant( values.at( 0 ).toElement() ).toMap() ) )
438 return res;
439
440 return QVariant();
441 }
442 else if ( type == QLatin1String( "QgsProcessingFeatureSourceDefinition" ) )
443 {
445 const QDomNodeList values = element.childNodes();
446 if ( values.isEmpty() )
447 return QVariant();
448
449 if ( res.loadVariant( QgsXmlUtils::readVariant( values.at( 0 ).toElement() ).toMap() ) )
450 return res;
451
452 return QVariant();
453 }
454 else if ( type == QLatin1String( "QgsRemappingSinkDefinition" ) )
455 {
457 const QDomNodeList values = element.childNodes();
458 if ( values.isEmpty() )
459 return QVariant();
460
461 if ( res.loadVariant( QgsXmlUtils::readVariant( values.at( 0 ).toElement() ).toMap() ) )
462 return QVariant::fromValue( res );
463
464 return QVariant();
465 }
466 else
467 {
468 return QVariant();
469 }
470}
DistanceUnit
Units of distance.
Definition qgis.h:5013
@ Unknown
Unknown distance unit.
Definition qgis.h:5063
@ Degrees
Degrees, for planar geographic CRS distance measurements.
Definition qgis.h:5020
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:42
double yMaximum() const
Returns the maximum y value.
Definition qgsbox3d.h:230
void setZMinimum(double z)
Sets the minimum z value.
Definition qgsbox3d.cpp:90
void setYMaximum(double y)
Sets the maximum y value.
Definition qgsbox3d.cpp:85
void setZMaximum(double z)
Sets the maximum z value.
Definition qgsbox3d.cpp:95
double xMinimum() const
Returns the minimum x value.
Definition qgsbox3d.h:195
double zMaximum() const
Returns the maximum z value.
Definition qgsbox3d.h:258
double xMaximum() const
Returns the maximum x value.
Definition qgsbox3d.h:202
void setXMaximum(double x)
Sets the maximum x value.
Definition qgsbox3d.cpp:75
void setYMinimum(double y)
Sets the minimum y value.
Definition qgsbox3d.cpp:80
double zMinimum() const
Returns the minimum z value.
Definition qgsbox3d.h:251
double yMinimum() const
Returns the minimum y value.
Definition qgsbox3d.h:223
void setXMinimum(double x)
Sets the minimum x value.
Definition qgsbox3d.cpp:70
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
Represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
bool readXml(const QDomNode &node)
Restores state from the given DOM node.
bool writeXml(QDomNode &node, QDomDocument &doc) const
Stores state to the given Dom node in the given document.
A geometry is the spatial representation of a feature.
static Q_INVOKABLE QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
Q_INVOKABLE QString asWkt(int precision=17) const
Exports the geometry to WKT.
Encapsulates settings relating to a feature source input to a processing algorithm.
bool loadVariant(const QVariantMap &map)
Loads this source definition from a QVariantMap, wrapped in a QVariant.
QVariant toVariant() const
Saves this source definition to a QVariantMap, wrapped in a QVariant.
Encapsulates settings relating to a feature sink or output raster layer for a processing algorithm.
bool loadVariant(const QVariantMap &map)
Loads this output layer definition from a QVariantMap, wrapped in a QVariant.
QVariant toVariant() const
Saves this output layer definition to a QVariantMap, wrapped in a QVariant.
A store for object properties.
QVariant toVariant() const
Saves this property to a QVariantMap, wrapped in a QVariant.
bool loadVariant(const QVariant &property)
Loads this property from a QVariantMap, wrapped in a QVariant.
A rectangle specified with double values.
double xMinimum
double yMinimum
double xMaximum
void setYMinimum(double y)
Set the minimum y value.
void setXMinimum(double x)
Set the minimum x value.
void setYMaximum(double y)
Set the maximum y value.
void setXMaximum(double x)
Set the maximum x value.
double yMaximum
Defines the parameters used to remap features when creating a QgsRemappingProxyFeatureSink.
QVariant toVariant() const
Saves this remapping definition to a QVariantMap, wrapped in a QVariant.
bool loadVariant(const QVariantMap &map)
Loads this remapping definition from a QVariantMap, wrapped in a QVariant.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
static Q_INVOKABLE Qgis::DistanceUnit decodeDistanceUnit(const QString &string, bool *ok=nullptr)
Decodes a distance unit from a string.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
static QDomElement writeBox3D(const QgsBox3D &box, QDomDocument &doc, const QString &elementName=QStringLiteral("extent3D"))
Encodes a 3D box to a DOM element.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
static QgsBox3D readBox3D(const QDomElement &element)
Decodes a DOM element to a 3D box.
static QDomElement writeRectangle(const QgsRectangle &rect, QDomDocument &doc, const QString &elementName=QStringLiteral("extent"))
Encodes a rectangle to a DOM element.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
static Qgis::DistanceUnit readMapUnits(const QDomElement &element)
Decodes a distance unit from a DOM element.
static QgsRectangle readRectangle(const QDomElement &element)
static QDomElement writeMapUnits(Qgis::DistanceUnit units, QDomDocument &doc)
Encodes a distance unit to a DOM element.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition qgis.h:6524