All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SpatialReferenceSystemManager.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 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 SpatialReferenceSystemManager.h
22 
23  \brief A class to manage Coordinate Systems representations.
24  */
25 
26 #ifndef __TERRALIB_SRS_INTERNAL_SPATIALREFERENCESYSTEMMANAGER_H
27 #define __TERRALIB_SRS_INTERNAL_SPATIALREFERENCESYSTEMMANAGER_H
28 
29 // TerraLib
30 #include "../common/Singleton.h"
31 #include "../common/UnitOfMeasure.h"
32 #include "Config.h"
33 #include "SpatialReferenceSystem.h"
34 
35 // STL
36 #include <map>
37 #include <string>
38 #include <vector>
39 #include <memory>
40 
41 // Boost
42 #include <boost/lexical_cast.hpp>
43 #include <boost/multi_index_container.hpp>
44 #include <boost/multi_index/member.hpp>
45 #include <boost/multi_index/mem_fun.hpp>
46 #include <boost/multi_index/ordered_index.hpp>
47 
48 namespace te
49 {
50  namespace srs
51  {
52  /*!
53  \class SpatialReferenceSystemManager
54 
55  \brief A class to manage Coordinate Systems representations within TerraLib environment.
56 
57  A Coordinate System used to describe coordinates are also known as Spatial Reference System.
58  It can be represented by either one of these properties:
59  - A pair <id,authority>: a unique numeric id given by a particular authority;
60  - A simple, human-readable, interface-friendly, name;
61  - An OGC's WKT description;
62  - A PROJ4 text: to support coordinate conversion.
63 
64  Refer to the <a href="http://www.spatialreference.org">Spatial Reference website</a> for more information about EPSG codes for SRS.
65 
66  \ingroup srs
67  */
68  class TESRSEXPORT SpatialReferenceSystemManager : public te::common::Singleton<SpatialReferenceSystemManager>
69  {
71 
72  private:
73 
74  /*! This is a private struct to handle the set of SRS descriptions in the manager.
75  It is not visible outside this class. */
76  struct srs_desc
77  {
78  srs_desc(const std::string& name, unsigned int auth_id, const std::string& auth_name, const std::string& p4txt, const std::string& wkt);
79 
80  std::string srid() const;
81 
82  std::string m_name;
83  unsigned int m_auth_id;
84  std::string m_auth_name;
85  std::string m_p4txt;
86  std::string m_wkt;
87  };
88 
89  /*! A mult-index container with the following indexes:
90  1) an unique index by <auth_name, id>;
91  2) a non-unique index by name;
92  3) a non-unique index by p4txt;
93  4) a non unuque index by wkt;
94  */
95  typedef boost::multi_index_container<
96  srs_desc,
97  boost::multi_index::indexed_by<
98  boost::multi_index::ordered_unique<BOOST_MULTI_INDEX_CONST_MEM_FUN(srs_desc,std::string,srid)>,
99  boost::multi_index::ordered_non_unique<BOOST_MULTI_INDEX_MEMBER(srs_desc,std::string,m_name)>,
100  boost::multi_index::ordered_non_unique<BOOST_MULTI_INDEX_MEMBER(srs_desc,std::string,m_p4txt)>,
101  boost::multi_index::ordered_non_unique<BOOST_MULTI_INDEX_MEMBER(srs_desc,std::string,m_wkt)>
102  >
104 
105  private:
106 
108 
109  public:
110 
111  //! An iterator by SRS <id,authority>
112  typedef boost::multi_index::nth_index<srs_set,0>::type::iterator iterator;
113 
114  //! Destructor.
116 
117  /*!
118  \brief Inializes the manager from a JSON file containing instances of SRSs
119 
120  This methods reads the file "TE_JSON_FILES_LOCATION/srs.json" for SRSs definitions and insert them on the manager if it is empty.
121  \exception te::srs::Exception if the JSON file is not well formed.
122  */
123  void init();
124 
125  /*!
126  \brief Inializes the manager from a JSON file containing instances of SRSs.
127 
128  \param fileName Name of the JSON file.
129  \exception te::srs::Exception if the JSON file is not well formed.
130  */
131  void init(const std::string& fileName);
132 
133  /*!
134  \brief Adds a <id, authority> to the manager.
135  \param name A simple, human-readable, interface-friendly, name;
136  \param p4Txt a PROJ4 library description of coordinate system;
137  \param wkt An OGC's WKT description for the coordinate system;
138  \param id SRS id.
139  \param authName The authority responsible for the id. Default "EPSG".
140  \exception te::srs::Exception if the coordinate system id is already registered in the manager.
141  */
142  void add(const std::string& name, const std::string& p4Txt, const std::string& wkt, unsigned int id, const std::string& authName="EPSG");
143 
144  /*!
145  \brief Returns true is a pair <id, authority> is recognized by the manager.
146  \param id The coordinate system identification.
147  \param authName The authority responsible for the id. Default "EPSG".
148  \return True if the pair <id,authority> is already registered in the manager and false otherwise.
149  */
150  bool recognizes(unsigned int id, const std::string& authName="EPSG") const;
151 
152  /*!
153  \brief Returns a coordinate system name given an identification.
154  \param id The coordinate system identification.
155  \param authName The authority responsible for the id. Default "EPSG".
156  \return the Coordinate System name if the identification is recognized or and empty string otherwise.
157  */
158  std::string getName (unsigned int id, const std::string& authName="EPSG") const;
159 
160  /*!
161  \brief Returns a coordinate system PROJ4 description given an identification.
162  \param id The coordinate system identification.
163  \param authName The authority responsible for the id. Default "EPSG".
164  \return The coordinate system PROJ4 description if the identification is recognized or an empty string otherwise.
165  */
166  std::string getP4Txt(unsigned int id, const std::string& authName="EPSG") const;
167 
168  /*!
169  \brief Returns a coordinate system WKT description given an id.
170  \param id The coordinate system identification.
171  \param authName The authority responsible for the id. Default "EPSG".
172  \return The coordinate system OGC WKT if the identification is recognized or an empty string otherwise.
173  */
174  std::string getWkt (unsigned int id, const std::string& authName="EPSG") const;
175 
176  /*!
177  \brief Returns a coordinate system identification given a name.
178  \param name The coordinate system name.
179  \return A pair composed by the coordinate system identification and the name of the authority responsible for it.
180  \exception te::srs::Exception if the coordinate system name is not found in the manager.
181  */
182  std::pair<std::string,unsigned int> getIdFromName (const std::string& name) const;
183 
184  /*!
185  \brief Returns a coordinate system identification given a PROJ4 description.
186  \param p4Txt The coordinate system PROJ4 description.
187  \return A pair composed by the coordinate system identification and the name of the authority responsible for it.
188  \exception te::srs::Exception if the coordinate system PROJ4 description is not found in the manager.
189  */
190  std::pair<std::string,unsigned int> getIdFromP4Txt(const std::string& p4Txt) const;
191 
192  /*!
193  \brief Returns a coordinate system identification given a WKT description.
194  \param wkt The coordinate system WKT.
195  \return A pair composed by the coordinate system identification and the name of the authority responsible for it.
196  \exception te::srs::Exception if the coordinate system WKT is not found in the manager.
197  */
198  std::pair<std::string,unsigned int> getIdFromWkt (const std::string& wkt) const;
199 
200  /*!
201  \brief Returns a pointer to a coordinate system given an identification.
202  \param id The coordinate system identification.
203  \param authName The authority responsible for the id. Default "EPSG".
204  \return A pointer to a coordinate system if the identification is recognized or a null pointer otherwise. Caller is responsible for deleting the returned pointer.
205  */
206  SpatialReferenceSystemPtr getSpatialReferenceSystem(unsigned int id, const std::string& authName="EPSG") const;
207 
208  /*!
209  \brief Removes a coordinate system representation from the manager, given its identification.
210  \param id The coordinate system identification.
211  \param authName The authority responsible for the id. Default "EPSG".
212  */
213  void remove (unsigned int id, const std::string& authName="EPSG");
214 
215  //! Removes all coordinate system representations from the manager.
216  void clear();
217 
218  //! Returns the number of objects in the manager.
219  size_t size() const;
220 
221  /*!
222  \brief Returns an iterator mechanism over the coordinate system descriptions in the manager.
223 
224  The first iterator of the returned pair points to first coordinate system description.
225  The second iterator of the returned pair points to the last plus one ccoordinate system description.
226  \return a pair of iterators pointing to the first and last coordnate system representation in the manager.
227  */
228  std::pair<te::srs::SpatialReferenceSystemManager::iterator,te::srs::SpatialReferenceSystemManager::iterator> getIterators() const;
229 
230  /*!
231  \brief Returns the unit of measure for a SRS with a given id.
232  \return A pointer to the unit of measure or a null pointer if it the SRS could not be founded.
233  */
234  te::common::UnitOfMeasurePtr getUnit(unsigned int id, const std::string& authName="EPSG");
235 
236  /*!
237  \brief Checks if a SRS with a given id refers to a geographic spatial reference system.
238  \return True if the SRS with a given id refers to a geographic spatial reference system or false if not or not founded in the Manager.
239  */
240  bool isGeographic(unsigned int id, const std::string& authName);
241 
242  protected:
243 
244  /*! \brief Constructor. */
246 
247  private:
248 
249  /*!
250  \brief Copy constructor not allowed.
251 
252  \param rhs The right-hand-side copy that would be used to copy from.
253  */
255 
256  /*!
257  \brief Assignment operator not allowed.
258 
259  \param rhs The right-hand-side copy that would be used to copy from.
260 
261  \return A reference to this object.
262  */
264 
265  };
266  }
267 }
268 #endif // __TERRALIB_SRS_INTERNAL_SPATIALREFERENCESYSTEMMANAGER_H
boost::multi_index_container< srs_desc, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< BOOST_MULTI_INDEX_CONST_MEM_FUN(srs_desc, std::string, srid)>, boost::multi_index::ordered_non_unique< BOOST_MULTI_INDEX_MEMBER(srs_desc, std::string, m_name)>, boost::multi_index::ordered_non_unique< BOOST_MULTI_INDEX_MEMBER(srs_desc, std::string, m_p4txt)>, boost::multi_index::ordered_non_unique< BOOST_MULTI_INDEX_MEMBER(srs_desc, std::string, m_wkt)> > > srs_set
boost::multi_index::nth_index< srs_set, 0 >::type::iterator iterator
An iterator by SRS
std::auto_ptr< SpatialReferenceSystem > SpatialReferenceSystemPtr
This file contains the structs necessary to represent a Spatial Reference System. ...
A class to manage Coordinate Systems representations within TerraLib environment. ...
boost::shared_ptr< UnitOfMeasure > UnitOfMeasurePtr
#define TESRSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:367
Template support for singleton pattern.
Definition: Singleton.h:100