All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WKTParser.h
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file WKTParser.h
22 
23  \brief A class that implements the grammar rules for well known text (WKT) of Spatial Reference Systems.
24  It is based on boost::spirit V2 and WKT BNF definition available in the OGC Coordinate Transformation
25  Services Specification - Revision 1.00 - pages 18 and 19.
26 
27  \warning Do not use this class. It is for TerraLib internal use. Try the te::srs::WKTReader instead.
28  */
29 
30 #ifndef __TERRALIB_SRS_INTERNAL_WKTPARSER_H
31 #define __TERRALIB_SRS_INTERNAL_WKTPARSER_H
32 
33 // TerraLib
34 #include "WKTActions.h"
35 
36 // boost
37 #include <boost/bind/bind.hpp>
38 #include <boost/spirit/include/qi.hpp>
39 
40 // STL
41 #include <iostream>
42 #include <string>
43 
44 // Auxiliaries namespaces
45 namespace qi = boost::spirit::qi;
46 namespace ascii = boost::spirit::ascii;
47 
48 // Using the following boost::spirit definitions
49 using qi::as_string;
50 using qi::char_;
51 using qi::double_;
52 using ascii::no_case;
53 
54 namespace te
55 {
56  namespace srs
57  {
58  /*!
59  \class WKTParser
60 
61  \brief A class that implements the Grammar Rules for well known text (WKT) format of Spatial Reference Systems.
62  */
63  template<typename Iterator>
64  class WKTParser : public qi::grammar<Iterator, ascii::space_type>
65  {
66  public:
67 
68  /** @name Constructor
69  * Initilizer methods.
70  */
71  //@{
72 
73  /*! \brief Default constructor. */
74  WKTParser() : WKTParser::base_type(srs)
75  {
77 
78  // Initializing all rules
79  initTags();
81  initDatum();
82  initToWGS84();
84  initParameter();
85  initAxis();
86  initSpheroid();
88  initUnits();
91  initAuthority();
93  }
94 
95  //@}
96 
97  private:
98 
99  /** @name Initializer methods.
100  * Methods to initialize the grammar rules.
101  */
102  //@{
103 
104  void initTags()
105  {
106  projectedCSTag = "PROJCS";
107  geographicCSTag = "GEOGCS";
108  datumTag = "DATUM";
109  toWGS84Tag = "TOWGS84";
110  spheroidTag = "SPHEROID";
111  projectionTag = "PROJECTION";
112  parameterTag = "PARAMETER";
113  primeMeridianTag = "PRIMEM";
114  unitTag = "UNIT";
115  authorityTag = "AUTHORITY";
116  axisTag = "AXIS";
117  northTag = "NORTH";
118  southTag = "SOUTH";
119  eastTag = "EAST";
120  westTag = "WEST";
121  upTag = "UP";
122  downTag = "DOWN";
123  otherTag = "OTHER";
124  }
125 
127  {
129  >> leftDelimiter
130  >> csName >> ','
131  >> geographicCS >> ','
132  >> projection >> *(',' >> parameter) >> *(',' >> axis)
133  >> ',' >> unit >> -(',' >> authority)
135 
137  >> leftDelimiter
138  >> csName >> ','
139  >> datum >> ','
140  >> primeMeridian >> ','
141  >> unit >> -(',' >> authority)
143  }
144 
145  void initDatum()
146  {
147  datum = (datumTag[boost::bind(&WKTActions::createDatum, &m_a)]
148  >> leftDelimiter
149  >> datumName >> ','
150  >> spheroid >> -(',' >> toWGS84) >> -(',' >> authority)
151  >> rigthDelimiter)[boost::bind(&WKTActions::endDatum, &m_a)];
152  }
153 
154  void initToWGS84()
155  {
157  sevenParams = dx >> ',' >> dy >> ',' >> dz >> ',' >> ex >> ',' >> ey >> ',' >> ez >> ',' >> ppm;
158  dx = double_[boost::bind(&WKTActions::setDx, &m_a, ::_1)];
159  dy = double_[boost::bind(&WKTActions::setDy, &m_a, ::_1)];
160  dz = double_[boost::bind(&WKTActions::setDz, &m_a, ::_1)];
161  ex = double_[boost::bind(&WKTActions::setEx, &m_a, ::_1)];
162  ey = double_[boost::bind(&WKTActions::setEy, &m_a, ::_1)];
163  ez = double_[boost::bind(&WKTActions::setEz, &m_a, ::_1)];
164  ppm = double_[boost::bind(&WKTActions::setPPM, &m_a, ::_1)];
165  }
166 
168  {
170  >> leftDelimiter
171  >> projectionName >> -(',' >> authority)
172  >> rigthDelimiter;
173  }
174 
176  {
178  >> leftDelimiter
179  >> parameterName >> ','
180  >> value
181  >> rigthDelimiter;
182  }
183 
185  {
187  >> leftDelimiter
188  >> spheroidName >> ','
189  >> semiMajorAxis >> ','
190  >> invFlattening >> -(',' >> authority)
191  >> rigthDelimiter)[boost::bind(&WKTActions::endSpheroid, &m_a)];
192  }
193 
195  {
197  >> leftDelimiter
198  >> primeMeridianName >> ','
199  >> longitude >> -(',' >> authority)
200  >> rigthDelimiter;
201  }
202 
203  void initUnits()
204  {
205  unit = unitTag
206  >> leftDelimiter
207  >> unitName >> ','
208  >> conversionFactor >> -(',' >> authority)
209  >> rigthDelimiter;
210  }
211 
213  {
214  unitName = '"' >> as_string[(+(char_ - '"'))][boost::bind(&WKTActions::setUnitName, &m_a, ::_1)] >> '"';
215 
216  spheroidName = '"' >> as_string[(+(char_ - '"'))][boost::bind(&WKTActions::setSpheroidName, &m_a, ::_1)] >> '"';
217 
218  projectionName = '"' >> as_string[(+(char_ - '"'))][boost::bind(&WKTActions::setProjectionName, &m_a, ::_1)] >> '"';
219 
220  datumName = '"' >> as_string[(+(char_ - '"'))][boost::bind(&WKTActions::setDatumName, &m_a, ::_1)] >> '"';
221 
222  axisName = '"' >> as_string[(+(char_ - '"'))][boost::bind(&WKTActions::setAxisName, &m_a, ::_1)] >> '"';
223 
224  axisValue = as_string[(+(char_ - ']'))][boost::bind(&WKTActions::setAxisValue, &m_a, ::_1)];
225 
226  csName = '"' >> as_string[(+(char_ - '"'))][boost::bind(&WKTActions::setName, &m_a, ::_1)] >> '"';
227 
228  parameterName = '"' >> as_string[(+(char_ - '"'))][boost::bind(&WKTActions::setParameter, &m_a, ::_1)] >> '"';
229 
230  primeMeridianName = '"' >> (+(char_ - '"')) >> '"';
231  }
232 
234  {
235  value = double_[boost::bind(&WKTActions::setParameterValue, &m_a, ::_1)];
236 
237  semiMajorAxis = double_[boost::bind(&WKTActions::setSemiMajorAxis, &m_a, ::_1)];
238 
239  longitude = double_[boost::bind(&WKTActions::setPrimeMeridian, &m_a, ::_1)];
240 
241  invFlattening = double_[boost::bind(&WKTActions::setInverseFlattening, &m_a, ::_1)];
242 
243  conversionFactor = double_[boost::bind(&WKTActions::setConversionFactor, &m_a, ::_1)];
244  }
245 
247  {
249  authorityName = '"' >> as_string[(+(char_ - '"'))][boost::bind(&WKTActions::setAuthorityName, &m_a, ::_1)] >> '"';
250  authorityCode = '"' >> as_string[(+(char_ - '"'))][boost::bind(&WKTActions::setAuthorityCode, &m_a, ::_1)] >> '"';
251  }
252 
253  void initAxis()
254  {
256 
257  axis = axisTag
258  >> leftDelimiter
259  >> axisName >> ','
260  >> axisValue
261  >> rigthDelimiter;
262  }
263 
264 
266  {
267  leftDelimiter = char_('(') | char_('[');
268  rigthDelimiter = char_(')') | char_(']');
269  }
270 
271  //@}
272 
273  public:
274 
275  /** @name Access method.
276  * Method to access the SRS generated.
277  */
278  //@{
279 
280  /*!
281  \brief It returns the SRS generated by the parser process.
282 
283  \note The caller of this method will take the ownership of the SRS.
284  */
286 
287  //@}
288 
289  /*!
290  \brief This method resets the Parser to original state.
291 
292  \note Should be called case the parser processing faill.
293  \note Basically, it is responsable to free the memory.
294  */
295  void reset() { m_a.reset(); }
296 
297  private:
298 
299  WKTActions m_a; //<! Semanthic actions to grammar rules.
300 
301  /** @name Rules of WKT Grammar.
302  */
303  //@{
304 
305  qi::rule<Iterator, ascii::space_type> srs;
306 
307  qi::rule<Iterator, ascii::space_type> projectedCS, geographicCS;
308 
309  qi::rule<Iterator, ascii::space_type> projectedCSTag, geographicCSTag, datumTag, spheroidTag, projectionTag,
312 
313  qi::rule<Iterator, ascii::space_type> datum;
314 
315  qi::rule<Iterator, ascii::space_type> toWGS84, sevenParams;
316 
317  qi::rule<Iterator, ascii::space_type> dx, dy, dz, ex, ey, ez, ppm;
318 
319  qi::rule<Iterator, ascii::space_type> projection;
320 
321  qi::rule<Iterator, ascii::space_type> parameter;
322 
323  qi::rule<Iterator, ascii::space_type> spheroid;
324 
325  qi::rule<Iterator, ascii::space_type> primeMeridian;
326 
327  qi::rule<Iterator, ascii::space_type> unit;
328 
329  qi::rule<Iterator, ascii::space_type> axis;
330 
331  qi::rule<Iterator, ascii::space_type> axisValue;
332 
333  qi::rule<Iterator, ascii::space_type> value, semiMajorAxis, longitude, invFlattening, conversionFactor;
334 
336 
337  qi::rule<Iterator, ascii::space_type> authority, authorityCode;
338 
339  qi::rule<Iterator> authorityName;
340 
341  qi::rule<Iterator, ascii::space_type> leftDelimiter, rigthDelimiter;
342 
343  //@}
344 
345  }; // WKTParser
346 
347  } // namespace srs
348 } // namespace te
349 
350 #endif // __TERRALIB_SRS_INTERNAL_WKTPARSER_H
qi::rule< Iterator, ascii::space_type > downTag
Definition: WKTParser.h:309
qi::rule< Iterator, ascii::space_type > geographicCS
Definition: WKTParser.h:307
qi::rule< Iterator, ascii::space_type > longitude
Definition: WKTParser.h:333
void reset()
This method resets the Parser to original state.
Definition: WKTParser.h:295
void initDelimiters()
Definition: WKTParser.h:265
SpatialReferenceSystem * getSRS()
It returns the SRS generated by the parser process.
Definition: WKTParser.h:285
qi::rule< Iterator, ascii::space_type > northTag
Definition: WKTParser.h:309
qi::rule< Iterator > primeMeridianName
Definition: WKTParser.h:335
void setEy(const double &ey)
Definition: WKTActions.cpp:135
void setPPM(const double &ppm)
Definition: WKTActions.cpp:145
qi::rule< Iterator, ascii::space_type > unitTag
Definition: WKTParser.h:309
qi::rule< Iterator, ascii::space_type > primeMeridian
Definition: WKTParser.h:325
qi::rule< Iterator, ascii::space_type > value
Definition: WKTParser.h:333
qi::rule< Iterator > csName
Definition: WKTParser.h:335
qi::rule< Iterator, ascii::space_type > upTag
Definition: WKTParser.h:309
void initAuthority()
Definition: WKTParser.h:246
qi::rule< Iterator > spheroidName
Definition: WKTParser.h:335
void setSemiMajorAxis(const double &rad)
Definition: WKTActions.cpp:170
void initSpatialReferenceSystems()
Definition: WKTParser.h:126
qi::rule< Iterator, ascii::space_type > projectionTag
Definition: WKTParser.h:309
void initParameter()
Definition: WKTParser.h:175
qi::rule< Iterator, ascii::space_type > projection
Definition: WKTParser.h:319
void setAuthorityCode(const std::string &code)
Definition: WKTActions.cpp:160
qi::rule< Iterator, ascii::space_type > projectedCS
Definition: WKTParser.h:307
void setAxisName(const std::string &name)
Definition: WKTActions.cpp:196
void initProjection()
Definition: WKTParser.h:167
qi::rule< Iterator, ascii::space_type > ez
Definition: WKTParser.h:317
qi::rule< Iterator, ascii::space_type > axis
Definition: WKTParser.h:329
void setDz(const double &dz)
Definition: WKTActions.cpp:125
qi::rule< Iterator, ascii::space_type > ex
Definition: WKTParser.h:317
qi::rule< Iterator, ascii::space_type > sevenParams
Definition: WKTParser.h:315
void setDatumName(const std::string &name)
Definition: WKTActions.cpp:110
qi::rule< Iterator, ascii::space_type > authorityCode
Definition: WKTParser.h:337
void setPrimeMeridian(const double &primem)
Definition: WKTActions.cpp:97
void endProjectedCoordinateSystem()
Definition: WKTActions.cpp:240
A Spatial Reference System, also known as a Coordinate System.
qi::rule< Iterator, ascii::space_type > parameterTag
Definition: WKTParser.h:309
A class that implements the Grammar Rules for well known text (WKT) format of Spatial Reference Syste...
Definition: WKTParser.h:64
qi::rule< Iterator, ascii::space_type > datumTag
Definition: WKTParser.h:309
void initDoubleValues()
Definition: WKTParser.h:233
qi::rule< Iterator, ascii::space_type > dy
Definition: WKTParser.h:317
qi::rule< Iterator, ascii::space_type > conversionFactor
Definition: WKTParser.h:333
void endGeographicCoordinateSystem()
Definition: WKTActions.cpp:219
void createProjectedCoordinateSystem()
Definition: WKTActions.cpp:70
qi::rule< Iterator, ascii::space_type > otherTag
Definition: WKTParser.h:309
WKTActions m_a
Definition: WKTParser.h:299
void initPrimeMeridian()
Definition: WKTParser.h:194
qi::rule< Iterator > parameterName
Definition: WKTParser.h:335
void setName(const std::string &name)
Definition: WKTActions.cpp:92
qi::rule< Iterator, ascii::space_type > dz
Definition: WKTParser.h:317
qi::rule< Iterator, ascii::space_type > leftDelimiter
Definition: WKTParser.h:341
qi::rule< Iterator, ascii::space_type > westTag
Definition: WKTParser.h:309
qi::rule< Iterator > projectionName
Definition: WKTParser.h:335
qi::rule< Iterator, ascii::space_type > southTag
Definition: WKTParser.h:309
void setDy(const double &dy)
Definition: WKTActions.cpp:120
void setAxisValue(const std::string &value)
Definition: WKTActions.cpp:201
qi::rule< Iterator, ascii::space_type > spheroidTag
Definition: WKTParser.h:309
qi::rule< Iterator, ascii::space_type > unit
Definition: WKTParser.h:327
void setConversionFactor(const double &f)
Definition: WKTActions.cpp:107
void setAuthorityName(const std::string &name)
Definition: WKTActions.cpp:155
SpatialReferenceSystem * getSRS()
It returns the SRS generated by the parser process.
Definition: WKTActions.cpp:254
void setParameterValue(const double &value)
Definition: WKTActions.cpp:190
qi::rule< Iterator > axisName
Definition: WKTParser.h:335
void setParameter(const std::string &name)
Definition: WKTActions.cpp:185
void setDx(const double &dx)
Definition: WKTActions.cpp:115
qi::rule< Iterator, ascii::space_type > spheroid
Definition: WKTParser.h:323
void setEx(const double &ex)
Definition: WKTActions.cpp:130
qi::rule< Iterator, ascii::space_type > toWGS84
Definition: WKTParser.h:315
A class that implements the Grammar Rules for well known text (WKT) format for Spatial Reference Syst...
Definition: WKTActions.h:53
void createGeographicCoordinateSystem()
Definition: WKTActions.cpp:76
qi::rule< Iterator, ascii::space_type > geographicCSTag
Definition: WKTParser.h:309
qi::rule< Iterator > unitName
Definition: WKTParser.h:335
qi::rule< Iterator, ascii::space_type > axisValue
Definition: WKTParser.h:331
WKTParser()
Default constructor.
Definition: WKTParser.h:74
qi::rule< Iterator, ascii::space_type > invFlattening
Definition: WKTParser.h:333
void setProjectionName(const std::string &name)
Definition: WKTActions.cpp:180
void setEz(const double &ez)
Definition: WKTActions.cpp:140
qi::rule< Iterator > datumName
Definition: WKTParser.h:335
qi::rule< Iterator, ascii::space_type > authority
Definition: WKTParser.h:337
void setUnitName(const std::string &name)
Definition: WKTActions.cpp:102
qi::rule< Iterator, ascii::space_type > parameter
Definition: WKTParser.h:321
void setInverseFlattening(const double &invflat)
Definition: WKTActions.cpp:175
qi::rule< Iterator, ascii::space_type > ppm
Definition: WKTParser.h:317
qi::rule< Iterator, ascii::space_type > srs
Definition: WKTParser.h:305
void setSpheroidName(const std::string &name)
Definition: WKTActions.cpp:165
void initQuotedNames()
Definition: WKTParser.h:212
qi::rule< Iterator, ascii::space_type > authorityTag
Definition: WKTParser.h:309
qi::rule< Iterator, ascii::space_type > ey
Definition: WKTParser.h:317
qi::rule< Iterator, ascii::space_type > semiMajorAxis
Definition: WKTParser.h:333
qi::rule< Iterator, ascii::space_type > axisTag
Definition: WKTParser.h:309
void reset()
This method resets the Action class to original state.
Definition: WKTActions.cpp:53
qi::rule< Iterator, ascii::space_type > rigthDelimiter
Definition: WKTParser.h:341
qi::rule< Iterator > authorityName
Definition: WKTParser.h:339
qi::rule< Iterator, ascii::space_type > projectedCSTag
Definition: WKTParser.h:309
qi::rule< Iterator, ascii::space_type > dx
Definition: WKTParser.h:317
qi::rule< Iterator, ascii::space_type > datum
Definition: WKTParser.h:313
qi::rule< Iterator, ascii::space_type > eastTag
Definition: WKTParser.h:309
qi::rule< Iterator, ascii::space_type > toWGS84Tag
Definition: WKTParser.h:309
qi::rule< Iterator, ascii::space_type > primeMeridianTag
Definition: WKTParser.h:309