UtilsXML.h
1 /*
2  * Copyright © (2011-2013) Institut national de l'information
3  * géographique et forestière
4  *
5  * Géoportail SAV <contact.geoservices@ign.fr>
6  *
7  * This software is a computer program whose purpose is to publish geographic
8  * data using OGC WMS and WMTS protocol.
9  *
10  * This software is governed by the CeCILL-C license under French law and
11  * abiding by the rules of distribution of free software. You can use,
12  * modify and/ or redistribute the software under the terms of the CeCILL-C
13  * license as circulated by CEA, CNRS and INRIA at the following URL
14  * "http://www.cecill.info".
15  *
16  * As a counterpart to the access to the source code and rights to copy,
17  * modify and redistribute granted by the license, users are provided only
18  * with a limited warranty and the software's author, the holder of the
19  * economic rights, and the successive licensors have only limited
20  * liability.
21  *
22  * In this respect, the user's attention is drawn to the risks associated
23  * with loading, using, modifying and/or developing or reproducing the
24  * software by the user in light of its specific status of free software,
25  * that may mean that it is complicated to manipulate, and that also
26  * therefore means that it is reserved for developers and experienced
27  * professionals having in-depth computer knowledge. Users are therefore
28  * encouraged to load and test the software's suitability as regards their
29  * requirements in conditions enabling the security of their systems and/or
30  * data to be ensured and, more generally, to use and operate it in the
31  * same conditions as regards security.
32  *
33  * The fact that you are presently reading this means that you have had
34  *
35  * knowledge of the CeCILL-C license and that you accept its terms.
36  */
37 
38 class Attribute;
39 
40 #ifndef UTILSXML
41 #define UTILSXML
42 
43 #include <vector>
44 #include <string>
45 #include <tinyxml.h>
46 #include <iomanip>
47 
48 #include <rok4/utils/Keyword.h>
49 #include <rok4/utils/TileMatrixLimits.h>
50 
51 class UtilsXML
52 {
53 
54  public:
55 
60  static TiXmlElement* buildTextNode ( std::string elementName, std::string value ) {
61  TiXmlElement * elem = new TiXmlElement ( elementName );
62  TiXmlText * text = new TiXmlText ( value );
63  elem->LinkEndChild ( text );
64  return elem;
65  }
66 
71  static const std::string getTextStrFromElem(TiXmlElement* pElem) {
72  const TiXmlNode* child = pElem->FirstChild();
73  if ( child ) {
74  const TiXmlText* childText = child->ToText();
75  if ( childText ) {
76  return childText->ValueStr();
77  }
78  }
79  return "";
80  }
81 
88  static TiXmlElement* getXml(std::string elName, Keyword k) {
89  TiXmlElement* el = new TiXmlElement ( elName );
90  el->LinkEndChild ( new TiXmlText ( k.getContent() ) );
91 
92  for ( std::map<std::string,std::string>::const_iterator it = k.getAttributes()->begin(); it != k.getAttributes()->end(); it++ ) {
93  el->SetAttribute ( ( *it ).first, ( *it ).second );
94  }
95 
96  return el;
97  }
98 
111  static int GetDecimalPlaces ( double dbVal ) {
112  dbVal = fmod(dbVal, 1);
113  static const int MAX_DP = 10;
114  double THRES = pow ( 0.1, MAX_DP );
115  if ( dbVal == 0.0 )
116  return 0;
117  int nDecimal = 0;
118  while ( dbVal - floor ( dbVal ) > THRES && nDecimal < MAX_DP && ceil(dbVal)-dbVal > THRES) {
119  dbVal *= 10.0;
120  THRES *= 10.0;
121  nDecimal++;
122  }
123  return nDecimal;
124  }
125 
132  static TiXmlElement* getXml(TileMatrixLimits tml) {
133  TiXmlElement* tmLimitsEl = new TiXmlElement ( "TileMatrixLimits" );
134  tmLimitsEl->LinkEndChild ( buildTextNode ( "TileMatrix", tml.tileMatrixId ) );
135  tmLimitsEl->LinkEndChild ( buildTextNode ( "MinTileRow", std::to_string(tml.minTileRow) ) );
136  tmLimitsEl->LinkEndChild ( buildTextNode ( "MaxTileRow", std::to_string(tml.maxTileRow) ) );
137  tmLimitsEl->LinkEndChild ( buildTextNode ( "MinTileCol", std::to_string(tml.minTileCol) ) );
138  tmLimitsEl->LinkEndChild ( buildTextNode ( "MaxTileCol", std::to_string(tml.maxTileCol) ) );
139 
140  return tmLimitsEl;
141  }
142 
143 
154  static TiXmlElement* getXml(BoundingBox<double> bbox, std::string crs = "") {
155 
156  std::ostringstream os;
157 
158  if (crs == "") {
159  TiXmlElement * el = new TiXmlElement ( "EX_GeographicBoundingBox" );
160 
161  os.str ( "" );
162  os<<bbox.xmin;
163  el->LinkEndChild ( UtilsXML::buildTextNode ( "westBoundLongitude", os.str() ) );
164  os.str ( "" );
165  os<<bbox.xmax;
166  el->LinkEndChild ( UtilsXML::buildTextNode ( "eastBoundLongitude", os.str() ) );
167  os.str ( "" );
168  os<<bbox.ymin;
169  el->LinkEndChild ( UtilsXML::buildTextNode ( "southBoundLatitude", os.str() ) );
170  os.str ( "" );
171  os<<bbox.ymax;
172  el->LinkEndChild ( UtilsXML::buildTextNode ( "northBoundLatitude", os.str() ) );
173  os.str ( "" );
174 
175  return el;
176  } else {
177 
178  TiXmlElement * el = new TiXmlElement ( "BoundingBox" );
179 
180  el->SetAttribute ( "CRS", crs );
181  int floatprecision = GetDecimalPlaces ( bbox.xmin );
182  floatprecision = std::max ( floatprecision,GetDecimalPlaces ( bbox.xmax ) );
183  floatprecision = std::max ( floatprecision,GetDecimalPlaces ( bbox.ymin ) );
184  floatprecision = std::max ( floatprecision,GetDecimalPlaces ( bbox.ymax ) );
185  floatprecision = std::min ( floatprecision,9 ); //FIXME gestion du nombre maximal de décimal.
186 
187  os.str ( "" );
188  os<< std::fixed << std::setprecision ( floatprecision );
189  os<<bbox.xmin;
190  el->SetAttribute ( "minx",os.str() );
191  os.str ( "" );
192  os<<bbox.ymin;
193  el->SetAttribute ( "miny",os.str() );
194  os.str ( "" );
195  os<<bbox.xmax;
196  el->SetAttribute ( "maxx",os.str() );
197  os.str ( "" );
198  os<<bbox.ymax;
199  el->SetAttribute ( "maxy",os.str() );
200  os.str ( "" );
201 
202  return el;
203  }
204 
205  }
206 
207 };
208 
209 #endif // UTILSXML_H
210 
UtilsXML::getTextStrFromElem
static const std::string getTextStrFromElem(TiXmlElement *pElem)
Definition: UtilsXML.h:71
UtilsXML
Definition: UtilsXML.h:51
UtilsXML::buildTextNode
static TiXmlElement * buildTextNode(std::string elementName, std::string value)
Definition: UtilsXML.h:60
UtilsXML::getXml
static TiXmlElement * getXml(std::string elName, Keyword k)
Export XML du mot clé pour le GetCapabilities.
Definition: UtilsXML.h:88
UtilsXML::GetDecimalPlaces
static int GetDecimalPlaces(double dbVal)
Donne le nombre de chiffres après la virgule.
Definition: UtilsXML.h:111
UtilsXML::getXml
static TiXmlElement * getXml(BoundingBox< double > bbox, std::string crs="")
Export XML d'une bbox, en géographique ou dans le CRS fourni.
Definition: UtilsXML.h:154
UtilsXML::getXml
static TiXmlElement * getXml(TileMatrixLimits tml)
Export XML des tuiles limites pour un niveau.
Definition: UtilsXML.h:132