QGIS API Documentation  2.14.0-Essen
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 ( "meters" == element.text() )
26  {
27  return QGis::Meters;
28  }
29  else if ( "feet" == element.text() )
30  {
31  return QGis::Feet;
32  }
33  else if ( "nautical miles" == element.text() )
34  {
35  return QGis::NauticalMiles;
36  }
37  else if ( "degrees" == element.text() )
38  {
39  return QGis::Degrees;
40  }
41  else if ( "unknown" == element.text() )
42  {
43  return QGis::UnknownUnit;
44  }
45  else
46  {
47  QgsDebugMsg( "Unknown map unit type " + element.text() );
48  return QGis::Degrees;
49  }
50 }
51 
53 {
54  QgsRectangle aoi;
55 
56  QDomNode xminNode = element.namedItem( "xmin" );
57  QDomNode yminNode = element.namedItem( "ymin" );
58  QDomNode xmaxNode = element.namedItem( "xmax" );
59  QDomNode ymaxNode = element.namedItem( "ymax" );
60 
61  QDomElement exElement = xminNode.toElement();
62  double xmin = exElement.text().toDouble();
63  aoi.setXMinimum( xmin );
64 
65  exElement = yminNode.toElement();
66  double ymin = exElement.text().toDouble();
67  aoi.setYMinimum( ymin );
68 
69  exElement = xmaxNode.toElement();
70  double xmax = exElement.text().toDouble();
71  aoi.setXMaximum( xmax );
72 
73  exElement = ymaxNode.toElement();
74  double ymax = exElement.text().toDouble();
75  aoi.setYMaximum( ymax );
76 
77  return aoi;
78 }
79 
80 
81 
83 {
84  QString unitsString;
85  switch ( units )
86  {
87  case QGis::Meters:
88  unitsString = "meters";
89  break;
90  case QGis::Feet:
91  unitsString = "feet";
92  break;
94  unitsString = "nautical miles";
95  break;
96  case QGis::Degrees:
97  unitsString = "degrees";
98  break;
99  case QGis::UnknownUnit:
100  default:
101  unitsString = "unknown";
102  break;
103  }
104 
105  QDomElement unitsNode = doc.createElement( "units" );
106  unitsNode.appendChild( doc.createTextNode( unitsString ) );
107  return unitsNode;
108 }
109 
111 {
112  QDomElement xMin = doc.createElement( "xmin" );
113  QDomElement yMin = doc.createElement( "ymin" );
114  QDomElement xMax = doc.createElement( "xmax" );
115  QDomElement yMax = doc.createElement( "ymax" );
116 
117  QDomText xMinText = doc.createTextNode( qgsDoubleToString( rect.xMinimum() ) );
118  QDomText yMinText = doc.createTextNode( qgsDoubleToString( rect.yMinimum() ) );
119  QDomText xMaxText = doc.createTextNode( qgsDoubleToString( rect.xMaximum() ) );
120  QDomText yMaxText = doc.createTextNode( qgsDoubleToString( rect.yMaximum() ) );
121 
122  xMin.appendChild( xMinText );
123  yMin.appendChild( yMinText );
124  xMax.appendChild( xMaxText );
125  yMax.appendChild( yMaxText );
126 
127  QDomElement extentNode = doc.createElement( "extent" );
128  extentNode.appendChild( xMin );
129  extentNode.appendChild( yMin );
130  extentNode.appendChild( xMax );
131  extentNode.appendChild( yMax );
132  return extentNode;
133 }
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QDomNode appendChild(const QDomNode &newChild)
void setXMaximum(double x)
Set the maximum x value.
Definition: qgsrectangle.h:172
double yMaximum() const
Get the y maximum value (top side of rectangle)
Definition: qgsrectangle.h:197
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
static QDomElement writeRectangle(const QgsRectangle &rect, QDomDocument &doc)
double toDouble(bool *ok) const
QDomElement toElement() const
QString text() const
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
Definition: qgsrectangle.h:202
double xMaximum() const
Get the x maximum value (right side of rectangle)
Definition: qgsrectangle.h:187
QString qgsDoubleToString(double a, int precision=17)
Definition: qgis.h:274
void setYMinimum(double y)
Set the minimum y value.
Definition: qgsrectangle.h:177
static QGis::UnitType readMapUnits(const QDomElement &element)
Definition: qgsxmlutils.cpp:23
QDomText createTextNode(const QString &value)
QDomNode namedItem(const QString &name) const
void setYMaximum(double y)
Set the maximum y value.
Definition: qgsrectangle.h:182
UnitType
Map units that qgis supports.
Definition: qgis.h:155
QDomElement createElement(const QString &tagName)
double xMinimum() const
Get the x minimum value (left side of rectangle)
Definition: qgsrectangle.h:192
static QgsRectangle readRectangle(const QDomElement &element)
Definition: qgsxmlutils.cpp:52
void setXMinimum(double x)
Set the minimum x value.
Definition: qgsrectangle.h:167
static QDomElement writeMapUnits(QGis::UnitType units, QDomDocument &doc)
Definition: qgsxmlutils.cpp:82