QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsxmlutils.cpp
Go to the documentation of this file.
1 #include "qgsxmlutils.h"
2 
3 #include <QDomElement>
4 
5 #include "qgslogger.h"
6 #include "qgsrectangle.h"
7 
8 
9 QGis::UnitType QgsXmlUtils::readMapUnits( const QDomElement& element )
10 {
11  if ( "meters" == element.text() )
12  {
13  return QGis::Meters;
14  }
15  else if ( "feet" == element.text() )
16  {
17  return QGis::Feet;
18  }
19  else if ( "nautical miles" == element.text() )
20  {
21  return QGis::NauticalMiles;
22  }
23  else if ( "degrees" == element.text() )
24  {
25  return QGis::Degrees;
26  }
27  else if ( "unknown" == element.text() )
28  {
29  return QGis::UnknownUnit;
30  }
31  else
32  {
33  QgsDebugMsg( "Unknown map unit type " + element.text() );
34  return QGis::Degrees;
35  }
36 }
37 
38 QgsRectangle QgsXmlUtils::readRectangle( const QDomElement& element )
39 {
40  QgsRectangle aoi;
41 
42  QDomNode xminNode = element.namedItem( "xmin" );
43  QDomNode yminNode = element.namedItem( "ymin" );
44  QDomNode xmaxNode = element.namedItem( "xmax" );
45  QDomNode ymaxNode = element.namedItem( "ymax" );
46 
47  QDomElement exElement = xminNode.toElement();
48  double xmin = exElement.text().toDouble();
49  aoi.setXMinimum( xmin );
50 
51  exElement = yminNode.toElement();
52  double ymin = exElement.text().toDouble();
53  aoi.setYMinimum( ymin );
54 
55  exElement = xmaxNode.toElement();
56  double xmax = exElement.text().toDouble();
57  aoi.setXMaximum( xmax );
58 
59  exElement = ymaxNode.toElement();
60  double ymax = exElement.text().toDouble();
61  aoi.setYMaximum( ymax );
62 
63  return aoi;
64 }
65 
66 
67 
68 QDomElement QgsXmlUtils::writeMapUnits( QGis::UnitType units, QDomDocument& doc )
69 {
70  QString unitsString;
71  switch ( units )
72  {
73  case QGis::Meters:
74  unitsString = "meters";
75  break;
76  case QGis::Feet:
77  unitsString = "feet";
78  break;
80  unitsString = "nautical miles";
81  break;
82  case QGis::Degrees:
83  unitsString = "degrees";
84  break;
85  case QGis::UnknownUnit:
86  default:
87  unitsString = "unknown";
88  break;
89  }
90 
91  QDomElement unitsNode = doc.createElement( "units" );
92  unitsNode.appendChild( doc.createTextNode( unitsString ) );
93  return unitsNode;
94 }
95 
96 QDomElement QgsXmlUtils::writeRectangle( const QgsRectangle& rect, QDomDocument& doc )
97 {
98  QDomElement xMin = doc.createElement( "xmin" );
99  QDomElement yMin = doc.createElement( "ymin" );
100  QDomElement xMax = doc.createElement( "xmax" );
101  QDomElement yMax = doc.createElement( "ymax" );
102 
103  QDomText xMinText = doc.createTextNode( qgsDoubleToString( rect.xMinimum() ) );
104  QDomText yMinText = doc.createTextNode( qgsDoubleToString( rect.yMinimum() ) );
105  QDomText xMaxText = doc.createTextNode( qgsDoubleToString( rect.xMaximum() ) );
106  QDomText yMaxText = doc.createTextNode( qgsDoubleToString( rect.yMaximum() ) );
107 
108  xMin.appendChild( xMinText );
109  yMin.appendChild( yMinText );
110  xMax.appendChild( xMaxText );
111  yMax.appendChild( yMaxText );
112 
113  QDomElement extentNode = doc.createElement( "extent" );
114  extentNode.appendChild( xMin );
115  extentNode.appendChild( yMin );
116  extentNode.appendChild( xMax );
117  extentNode.appendChild( yMax );
118  return extentNode;
119 }