QGIS API Documentation  2.99.0-Master (716ff6c)
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 <QDomElement>
18 
19 #include "qgslogger.h"
20 #include "qgsrectangle.h"
21 
22 
24 {
25  if ( "unknown" == element.text() )
26  {
28  }
29  else
30  {
33  }
34 }
35 
36 QgsRectangle QgsXmlUtils::readRectangle( const QDomElement &element )
37 {
38  QgsRectangle aoi;
39 
40  QDomNode xminNode = element.namedItem( QStringLiteral( "xmin" ) );
41  QDomNode yminNode = element.namedItem( QStringLiteral( "ymin" ) );
42  QDomNode xmaxNode = element.namedItem( QStringLiteral( "xmax" ) );
43  QDomNode ymaxNode = element.namedItem( QStringLiteral( "ymax" ) );
44 
45  QDomElement exElement = xminNode.toElement();
46  double xmin = exElement.text().toDouble();
47  aoi.setXMinimum( xmin );
48 
49  exElement = yminNode.toElement();
50  double ymin = exElement.text().toDouble();
51  aoi.setYMinimum( ymin );
52 
53  exElement = xmaxNode.toElement();
54  double xmax = exElement.text().toDouble();
55  aoi.setXMaximum( xmax );
56 
57  exElement = ymaxNode.toElement();
58  double ymax = exElement.text().toDouble();
59  aoi.setYMaximum( ymax );
60 
61  return aoi;
62 }
63 
64 
65 
66 QDomElement QgsXmlUtils::writeMapUnits( QgsUnitTypes::DistanceUnit units, QDomDocument &doc )
67 {
68  QString unitsString = QgsUnitTypes::encodeUnit( units );
69  // maintain compatibility with old projects
70  if ( units == QgsUnitTypes::DistanceUnknownUnit )
71  unitsString = QStringLiteral( "unknown" );
72 
73  QDomElement unitsNode = doc.createElement( QStringLiteral( "units" ) );
74  unitsNode.appendChild( doc.createTextNode( unitsString ) );
75  return unitsNode;
76 }
77 
78 QDomElement QgsXmlUtils::writeRectangle( const QgsRectangle &rect, QDomDocument &doc )
79 {
80  QDomElement xMin = doc.createElement( QStringLiteral( "xmin" ) );
81  QDomElement yMin = doc.createElement( QStringLiteral( "ymin" ) );
82  QDomElement xMax = doc.createElement( QStringLiteral( "xmax" ) );
83  QDomElement yMax = doc.createElement( QStringLiteral( "ymax" ) );
84 
85  QDomText xMinText = doc.createTextNode( qgsDoubleToString( rect.xMinimum() ) );
86  QDomText yMinText = doc.createTextNode( qgsDoubleToString( rect.yMinimum() ) );
87  QDomText xMaxText = doc.createTextNode( qgsDoubleToString( rect.xMaximum() ) );
88  QDomText yMaxText = doc.createTextNode( qgsDoubleToString( rect.yMaximum() ) );
89 
90  xMin.appendChild( xMinText );
91  yMin.appendChild( yMinText );
92  xMax.appendChild( xMaxText );
93  yMax.appendChild( yMaxText );
94 
95  QDomElement extentNode = doc.createElement( QStringLiteral( "extent" ) );
96  extentNode.appendChild( xMin );
97  extentNode.appendChild( yMin );
98  extentNode.appendChild( xMax );
99  extentNode.appendChild( yMax );
100  return extentNode;
101 }
102 
103 QDomElement QgsXmlUtils::writeVariant( const QVariant &value, QDomDocument &doc )
104 {
105  QDomElement element = doc.createElement( QStringLiteral( "Option" ) );
106  switch ( value.type() )
107  {
108  case QVariant::Map:
109  {
110  QVariantMap map = value.toMap();
111 
112  for ( auto option = map.constBegin(); option != map.constEnd(); ++option )
113  {
114  QDomElement optionElement = writeVariant( option.value(), doc );
115  optionElement.setAttribute( QStringLiteral( "name" ), option.key() );
116  element.appendChild( optionElement );
117  element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "Map" ) );
118  }
119  break;
120  }
121 
122  case QVariant::List:
123  {
124  QVariantList list = value.toList();
125 
126  Q_FOREACH ( const QVariant &value, list )
127  {
128  QDomElement valueElement = writeVariant( value, doc );
129  element.appendChild( valueElement );
130  element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "List" ) );
131  }
132  break;
133  }
134 
135  case QVariant::Int:
136  case QVariant::Bool:
137  case QVariant::Double:
138  case QVariant::String:
139  element.setAttribute( QStringLiteral( "type" ), QVariant::typeToName( value.type() ) );
140  element.setAttribute( QStringLiteral( "value" ), value.toString() );
141  break;
142 
143  default:
144  element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "Unknown" ) );
145  element.setAttribute( QStringLiteral( "value" ), value.toString() );
146  break;
147  }
148 
149  return element;
150 }
151 
152 QVariant QgsXmlUtils::readVariant( const QDomElement &element )
153 {
154  QString type = element.attribute( QStringLiteral( "type" ) );
155 
156  if ( type == QLatin1String( "int" ) )
157  {
158  return element.attribute( QStringLiteral( "value" ) ).toInt();
159  }
160  else if ( type == QLatin1String( "double" ) )
161  {
162  return element.attribute( QStringLiteral( "value" ) ).toDouble();
163  }
164  else if ( type == QLatin1String( "QString" ) )
165  {
166  return element.attribute( QStringLiteral( "value" ) );
167  }
168  else if ( type == QLatin1String( "bool" ) )
169  {
170  return element.attribute( QStringLiteral( "value" ) ) == QLatin1String( "true" );
171  }
172  else if ( type == QLatin1String( "Map" ) )
173  {
174  QVariantMap map;
175  QDomNodeList options = element.childNodes();
176 
177  for ( int i = 0; i < options.count(); ++i )
178  {
179  QDomElement elem = options.at( i ).toElement();
180  if ( elem.tagName() == QLatin1String( "Option" ) )
181  map.insert( elem.attribute( QStringLiteral( "name" ) ), readVariant( elem ) );
182  }
183  return map;
184  }
185  else if ( type == QLatin1String( "List" ) )
186  {
187  QVariantList list;
188  QDomNodeList values = element.childNodes();
189  for ( int i = 0; i < values.count(); ++i )
190  {
191  QDomElement elem = values.at( i ).toElement();
192  list.append( readVariant( elem ) );
193  }
194  return list;
195  }
196  else
197  {
198  return QVariant();
199  }
200 }
A rectangle specified with double values.
Definition: qgsrectangle.h:38
void setXMaximum(double x)
Set the maximum x value.
Definition: qgsrectangle.h:70
static QDomElement writeRectangle(const QgsRectangle &rect, QDomDocument &doc)
Definition: qgsxmlutils.cpp:78
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
static QgsUnitTypes::DistanceUnit readMapUnits(const QDomElement &element)
Decodes a distance unit from a DOM element.
Definition: qgsxmlutils.cpp:23
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
Definition: qgis.h:186
void setYMinimum(double y)
Set the minimum y value.
Definition: qgsrectangle.h:75
static QDomElement writeMapUnits(QgsUnitTypes::DistanceUnit units, QDomDocument &doc)
Encodes a distance unit to a DOM element.
Definition: qgsxmlutils.cpp:66
Degrees, for planar geographic CRS distance measurements.
Definition: qgsunittypes.h:50
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:106
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:42
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:91
Unknown distance unit.
Definition: qgsunittypes.h:53
void setYMaximum(double y)
Set the maximum y value.
Definition: qgsrectangle.h:80
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:96
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:101
static QgsRectangle readRectangle(const QDomElement &element)
Definition: qgsxmlutils.cpp:36
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
void setXMinimum(double x)
Set the minimum x value.
Definition: qgsrectangle.h:65
static Q_INVOKABLE QgsUnitTypes::DistanceUnit decodeDistanceUnit(const QString &string, bool *ok=0)
Decodes a distance unit from a string.