QGIS API Documentation  3.0.2-Girona (307d082)
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::StringList:
136  {
137  QStringList list = value.toStringList();
138 
139  Q_FOREACH ( const QString &value, list )
140  {
141  QDomElement valueElement = writeVariant( value, doc );
142  element.appendChild( valueElement );
143  element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "StringList" ) );
144  }
145  break;
146  }
147 
148  case QVariant::Int:
149  case QVariant::Bool:
150  case QVariant::Double:
151  case QVariant::String:
152  element.setAttribute( QStringLiteral( "type" ), QVariant::typeToName( value.type() ) );
153  element.setAttribute( QStringLiteral( "value" ), value.toString() );
154  break;
155 
156  default:
157  element.setAttribute( QStringLiteral( "type" ), QStringLiteral( "Unknown" ) );
158  element.setAttribute( QStringLiteral( "value" ), value.toString() );
159  break;
160  }
161 
162  return element;
163 }
164 
165 QVariant QgsXmlUtils::readVariant( const QDomElement &element )
166 {
167  QString type = element.attribute( QStringLiteral( "type" ) );
168 
169  if ( type == QLatin1String( "int" ) )
170  {
171  return element.attribute( QStringLiteral( "value" ) ).toInt();
172  }
173  else if ( type == QLatin1String( "double" ) )
174  {
175  return element.attribute( QStringLiteral( "value" ) ).toDouble();
176  }
177  else if ( type == QLatin1String( "QString" ) )
178  {
179  return element.attribute( QStringLiteral( "value" ) );
180  }
181  else if ( type == QLatin1String( "bool" ) )
182  {
183  return element.attribute( QStringLiteral( "value" ) ) == QLatin1String( "true" );
184  }
185  else if ( type == QLatin1String( "Map" ) )
186  {
187  QVariantMap map;
188  QDomNodeList options = element.childNodes();
189 
190  for ( int i = 0; i < options.count(); ++i )
191  {
192  QDomElement elem = options.at( i ).toElement();
193  if ( elem.tagName() == QLatin1String( "Option" ) )
194  map.insert( elem.attribute( QStringLiteral( "name" ) ), readVariant( elem ) );
195  }
196  return map;
197  }
198  else if ( type == QLatin1String( "List" ) )
199  {
200  QVariantList list;
201  QDomNodeList values = element.childNodes();
202  for ( int i = 0; i < values.count(); ++i )
203  {
204  QDomElement elem = values.at( i ).toElement();
205  list.append( readVariant( elem ) );
206  }
207  return list;
208  }
209  else if ( type == QLatin1String( "StringList" ) )
210  {
211  QStringList list;
212  QDomNodeList values = element.childNodes();
213  for ( int i = 0; i < values.count(); ++i )
214  {
215  QDomElement elem = values.at( i ).toElement();
216  list.append( readVariant( elem ).toString() );
217  }
218  return list;
219  }
220  else
221  {
222  return QVariant();
223  }
224 }
A rectangle specified with double values.
Definition: qgsrectangle.h:39
void setXMaximum(double x)
Set the maximum x value.
Definition: qgsrectangle.h:94
static QDomElement writeRectangle(const QgsRectangle &rect, QDomDocument &doc)
Definition: qgsxmlutils.cpp:78
static Q_INVOKABLE QgsUnitTypes::DistanceUnit decodeDistanceUnit(const QString &string, bool *ok=nullptr)
Decodes a distance unit from a string.
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:237
void setYMinimum(double y)
Set the minimum y value.
Definition: qgsrectangle.h:99
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:51
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:130
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:43
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:115
Unknown distance unit.
Definition: qgsunittypes.h:54
void setYMaximum(double y)
Set the maximum y value.
Definition: qgsrectangle.h:104
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:120
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:125
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:89