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-unique index by wkt;
94  5) a non-unique index by auth_name;
95  */
96  typedef boost::multi_index_container<
97  srs_desc,
98  boost::multi_index::indexed_by<
99  boost::multi_index::ordered_unique<BOOST_MULTI_INDEX_CONST_MEM_FUN(srs_desc,std::string,srid)>,
100  boost::multi_index::ordered_non_unique<BOOST_MULTI_INDEX_MEMBER(srs_desc,std::string,m_name)>,
101  boost::multi_index::ordered_non_unique<BOOST_MULTI_INDEX_MEMBER(srs_desc,std::string,m_p4txt)>,
102  boost::multi_index::ordered_non_unique<BOOST_MULTI_INDEX_MEMBER(srs_desc,std::string,m_wkt)>,
103  boost::multi_index::ordered_non_unique<BOOST_MULTI_INDEX_MEMBER(srs_desc,std::string,m_auth_name)>
104  >
106 
107  private:
108 
110  std::map<int, te::common::UnitOfMeasurePtr> m_cacheUnitOfMeasure;
111 
112  public:
113 
114  //! An iterator by SRS <id,authority>
115  typedef boost::multi_index::nth_index<srs_set,0>::type::iterator iterator;
116 
117  //! Destructor.
119 
120  /*!
121  \brief Inializes the manager from a JSON file containing instances of SRSs
122 
123  This methods reads the file "TE_JSON_FILES_LOCATION/srs.json" for SRSs definitions and insert them on the manager if it is empty.
124  \exception te::srs::Exception if the JSON file is not well formed.
125  */
126  void init();
127 
128  /*!
129  \brief Inializes the manager from a JSON file containing instances of SRSs.
130 
131  \param fileName Name of the JSON file.
132  \exception te::srs::Exception if the JSON file is not well formed.
133  */
134  void init(const std::string& fileName);
135 
136  /*!
137  \brief Adds a <id, authority> to the manager.
138  \param name A simple, human-readable, interface-friendly, name;
139  \param p4Txt a PROJ4 library description of coordinate system;
140  \param wkt An OGC's WKT description for the coordinate system;
141  \param id SRS id.
142  \param authName The authority responsible for the id. Default "EPSG".
143  \exception te::srs::Exception if the coordinate system id is already registered in the manager.
144  */
145  void add(const std::string& name, const std::string& p4Txt, const std::string& wkt, unsigned int id, const std::string& authName="EPSG");
146 
147  /*!
148  \brief Returns true is a pair <id, authority> is recognized by the manager.
149  \param id The coordinate system identification.
150  \param authName The authority responsible for the id. Default "EPSG".
151  \return True if the pair <id,authority> is already registered in the manager and false otherwise.
152  */
153  bool recognizes(unsigned int id, const std::string& authName="EPSG") const;
154 
155  /*!
156  \brief Returns a coordinate system name given an identification.
157  \param id The coordinate system identification.
158  \param authName The authority responsible for the id. Default "EPSG".
159  \return the Coordinate System name if the identification is recognized or and empty string otherwise.
160  */
161  std::string getName (unsigned int id, const std::string& authName="EPSG") const;
162 
163  /*!
164  \brief Returns a coordinate system PROJ4 description given an identification.
165  \param id The coordinate system identification.
166  \param authName The authority responsible for the id. Default "EPSG".
167  \return The coordinate system PROJ4 description if the identification is recognized or an empty string otherwise.
168  */
169  std::string getP4Txt(unsigned int id, const std::string& authName="EPSG") const;
170 
171  /*!
172  \brief Returns a coordinate system WKT description given an id.
173  \param id The coordinate system identification.
174  \param authName The authority responsible for the id. Default "EPSG".
175  \return The coordinate system OGC WKT if the identification is recognized or an empty string otherwise.
176  */
177  std::string getWkt (unsigned int id, const std::string& authName="EPSG") const;
178 
179  /*!
180  \brief Returns a coordinate system identification given a name.
181  \param name The coordinate system name.
182  \return A pair composed by the coordinate system identification and the name of the authority responsible for it.
183  \exception te::srs::Exception if the coordinate system name is not found in the manager.
184  */
185  std::pair<std::string,unsigned int> getIdFromName (const std::string& name) const;
186 
187  /*!
188  \brief Returns a coordinate system identification given a PROJ4 description.
189  \param p4Txt The coordinate system PROJ4 description.
190  \return A pair composed by the coordinate system identification and the name of the authority responsible for it.
191  \exception te::srs::Exception if the coordinate system PROJ4 description is not found in the manager.
192  */
193  std::pair<std::string,unsigned int> getIdFromP4Txt(const std::string& p4Txt) const;
194 
195  /*!
196  \brief Returns a coordinate system identification given a WKT description.
197  \param wkt The coordinate system WKT.
198  \return A pair composed by the coordinate system identification and the name of the authority responsible for it.
199  \exception te::srs::Exception if the coordinate system WKT is not found in the manager.
200  */
201  std::pair<std::string,unsigned int> getIdFromWkt (const std::string& wkt) const;
202 
203  /*!
204  \brief Returns a pointer to a coordinate system given an identification.
205  \param id The coordinate system identification.
206  \param authName The authority responsible for the id. Default "EPSG".
207  \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.
208  */
209  SpatialReferenceSystemPtr getSpatialReferenceSystem(unsigned int id, const std::string& authName="EPSG") const;
210 
211  /*!
212  \brief Removes a coordinate system representation from the manager, given its identification.
213  \param id The coordinate system identification.
214  \param authName The authority responsible for the id. Default "EPSG".
215  */
216  void remove (unsigned int id, const std::string& authName="EPSG");
217 
218  //! Removes all coordinate system representations from the manager.
219  void clear();
220 
221  //! Returns the number of objects in the manager.
222  size_t size() const;
223 
224  /*!
225  \brief Returns an iterator mechanism over the coordinate system descriptions in the manager.
226 
227  The first iterator of the returned pair points to first coordinate system description.
228  The second iterator of the returned pair points to the last plus one ccoordinate system description.
229  \return a pair of iterators pointing to the first and last coordnate system representation in the manager.
230  */
231  std::pair<te::srs::SpatialReferenceSystemManager::iterator,te::srs::SpatialReferenceSystemManager::iterator> getIterators() const;
232 
233  /*!
234  \brief Returns the unit of measure for a SRS with a given id.
235  \return A pointer to the unit of measure or a null pointer if it the SRS could not be founded.
236  */
237  te::common::UnitOfMeasurePtr getUnit(unsigned int id, const std::string& authName="EPSG");
238 
239  /*!
240  \brief Checks if a SRS with a given id refers to a geographic spatial reference system.
241  \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.
242  */
243  bool isGeographic(unsigned int id, const std::string& authName="EPSG");
244 
245  /*!
246  \brief Checks if the System Manager is already initialized.
247  */
249 
250  bool isValidNameID(const std::string& name);
251 
252  bool isValidP4TxtID(const std::string& p4Txt);
253 
254  bool isValidWktID(const std::string& wkt);
255 
256  /*!
257  \brief Returns a SRID, not yet used, to identify an SRS created by an user.
258  \return a int represent a SRID, not yet used, to identify an SRS created by an user.
259  */
261 
262  //!< Searches for the proj lib directory
263  static std::string getProjLibDir();
264 
265  protected:
266 
267  /*! \brief Constructor. */
269 
270  private:
271 
272  /*!
273  \brief Copy constructor not allowed.
274 
275  \param rhs The right-hand-side copy that would be used to copy from.
276  */
278 
279  /*!
280  \brief Assignment operator not allowed.
281 
282  \param rhs The right-hand-side copy that would be used to copy from.
283 
284  \return A reference to this object.
285  */
287 
288  /*!
289  \brief Returns a coordinate system identification given a PROJ4 description. This function does not throw exceptions
290  \param p4Txt The coordinate system PROJ4 description.
291  \return A pair composed by the coordinate system identification and the name of the authority responsible for it.
292  \exception te::srs::Exception if the coordinate system PROJ4 description is not found in the manager.
293  */
294  std::pair<std::string, unsigned int> getIdFromP4TxtImpl(const std::string& p4Txt) const;
295 
296  /*!
297  \brief Returns a coordinate system identification given a WKT description. This function does not throw exceptions
298  \param wkt The coordinate system WKT.
299  \return A pair composed by the coordinate system identification and the name of the authority responsible for it.
300  \exception te::srs::Exception if the coordinate system WKT is not found in the manager.
301  */
302  std::pair<std::string, unsigned int> getIdFromWktImpl(const std::string& wkt) const;
303 
304  };
305  }
306 }
307 #endif // __TERRALIB_SRS_INTERNAL_SPATIALREFERENCESYSTEMMANAGER_H
te::srs::SpatialReferenceSystemManager::size
size_t size() const
Returns the number of objects in the manager.
te::srs::SpatialReferenceSystemManager::srs_desc::m_auth_id
unsigned int m_auth_id
Definition: SpatialReferenceSystemManager.h:83
te::srs::SpatialReferenceSystemManager::remove
void remove(unsigned int id, const std::string &authName="EPSG")
Removes a coordinate system representation from the manager, given its identification.
te::srs::SpatialReferenceSystemManager::SpatialReferenceSystemManager
SpatialReferenceSystemManager(const SpatialReferenceSystemManager &rhs)
Copy constructor not allowed.
te::srs::SpatialReferenceSystemManager::init
void init(const std::string &fileName)
Inializes the manager from a JSON file containing instances of SRSs.
te
TerraLib.
Definition: AddressGeocodingOp.h:52
te::srs::SpatialReferenceSystemManager::srs_desc::srs_desc
srs_desc(const std::string &name, unsigned int auth_id, const std::string &auth_name, const std::string &p4txt, const std::string &wkt)
te::srs::SpatialReferenceSystemManager::isValidNameID
bool isValidNameID(const std::string &name)
te::srs::SpatialReferenceSystemManager::clear
void clear()
Removes all coordinate system representations from the manager.
te::srs::SpatialReferenceSystemManager::getName
std::string getName(unsigned int id, const std::string &authName="EPSG") const
Returns a coordinate system name given an identification.
te::srs::SpatialReferenceSystemManager::getIdFromWkt
std::pair< std::string, unsigned int > getIdFromWkt(const std::string &wkt) const
Returns a coordinate system identification given a WKT description.
te::common::UnitOfMeasurePtr
boost::shared_ptr< UnitOfMeasure > UnitOfMeasurePtr
Definition: UnitOfMeasure.h:213
te::srs::SpatialReferenceSystemManager::getNewUserDefinedSRID
int getNewUserDefinedSRID()
Returns a SRID, not yet used, to identify an SRS created by an user.
te::srs::SpatialReferenceSystemManager::getIdFromName
std::pair< std::string, unsigned int > getIdFromName(const std::string &name) const
Returns a coordinate system identification given a name.
te::srs::SpatialReferenceSystemManager::isValidWktID
bool isValidWktID(const std::string &wkt)
te::srs::SpatialReferenceSystemManager::srs_desc
Definition: SpatialReferenceSystemManager.h:77
te::srs::SpatialReferenceSystemManager::getWkt
std::string getWkt(unsigned int id, const std::string &authName="EPSG") const
Returns a coordinate system WKT description given an id.
te::srs::SpatialReferenceSystemManager::iterator
boost::multi_index::nth_index< srs_set, 0 >::type::iterator iterator
An iterator by SRS <id,authority>
Definition: SpatialReferenceSystemManager.h:115
te::srs::SpatialReferenceSystemManager::getProjLibDir
static std::string getProjLibDir()
te::srs::SpatialReferenceSystemManager::getSpatialReferenceSystem
SpatialReferenceSystemPtr getSpatialReferenceSystem(unsigned int id, const std::string &authName="EPSG") const
Returns a pointer to a coordinate system given an identification.
te::srs::SpatialReferenceSystemManager::getIdFromP4TxtImpl
std::pair< std::string, unsigned int > getIdFromP4TxtImpl(const std::string &p4Txt) const
Returns a coordinate system identification given a PROJ4 description. This function does not throw ex...
te::srs::SpatialReferenceSystemManager::isGeographic
bool isGeographic(unsigned int id, const std::string &authName="EPSG")
Checks if a SRS with a given id refers to a geographic spatial reference system.
te::srs::SpatialReferenceSystemManager::getUnit
te::common::UnitOfMeasurePtr getUnit(unsigned int id, const std::string &authName="EPSG")
Returns the unit of measure for a SRS with a given id.
te::srs::SpatialReferenceSystemManager::srs_set
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)>, boost::multi_index::ordered_non_unique< BOOST_MULTI_INDEX_MEMBER(srs_desc, std::string, m_auth_name)> > > srs_set
Definition: SpatialReferenceSystemManager.h:105
TESRSEXPORT
#define TESRSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:375
te::srs::SpatialReferenceSystemManager::operator=
SpatialReferenceSystemManager & operator=(const SpatialReferenceSystemManager &rhs)
Assignment operator not allowed.
te::srs::SpatialReferenceSystemManager
A class to manage Coordinate Systems representations within TerraLib environment.
Definition: SpatialReferenceSystemManager.h:69
te::srs::SpatialReferenceSystemManager::srs_desc::m_name
std::string m_name
Definition: SpatialReferenceSystemManager.h:82
te::srs::SpatialReferenceSystemManager::getIdFromP4Txt
std::pair< std::string, unsigned int > getIdFromP4Txt(const std::string &p4Txt) const
Returns a coordinate system identification given a PROJ4 description.
te::srs::SpatialReferenceSystemManager::add
void add(const std::string &name, const std::string &p4Txt, const std::string &wkt, unsigned int id, const std::string &authName="EPSG")
Adds a <id, authority> to the manager.
te::srs::SpatialReferenceSystemManager::getIdFromWktImpl
std::pair< std::string, unsigned int > getIdFromWktImpl(const std::string &wkt) const
Returns a coordinate system identification given a WKT description. This function does not throw exce...
te::srs::SpatialReferenceSystemManager::m_cacheUnitOfMeasure
std::map< int, te::common::UnitOfMeasurePtr > m_cacheUnitOfMeasure
Definition: SpatialReferenceSystemManager.h:110
te::common::Singleton
Template support for singleton pattern.
Definition: Singleton.h:101
te::srs::SpatialReferenceSystemManager::srs_desc::m_auth_name
std::string m_auth_name
Definition: SpatialReferenceSystemManager.h:84
te::srs::SpatialReferenceSystemManager::getIterators
std::pair< te::srs::SpatialReferenceSystemManager::iterator, te::srs::SpatialReferenceSystemManager::iterator > getIterators() const
Returns an iterator mechanism over the coordinate system descriptions in the manager.
te::srs::SpatialReferenceSystemManager::~SpatialReferenceSystemManager
~SpatialReferenceSystemManager()
Destructor.
te::srs::SpatialReferenceSystemManager::srs_desc::srid
std::string srid() const
SpatialReferenceSystem.h
This file contains the structs necessary to represent a Spatial Reference System.
te::srs::SpatialReferenceSystemManager::getP4Txt
std::string getP4Txt(unsigned int id, const std::string &authName="EPSG") const
Returns a coordinate system PROJ4 description given an identification.
te::srs::SpatialReferenceSystemManager::srs_desc::m_p4txt
std::string m_p4txt
Definition: SpatialReferenceSystemManager.h:85
te::srs::SpatialReferenceSystemManager::isValidP4TxtID
bool isValidP4TxtID(const std::string &p4Txt)
te::srs::SpatialReferenceSystemManager::SpatialReferenceSystemManager
SpatialReferenceSystemManager()
Constructor.
te::srs::SpatialReferenceSystemManager::isInitialized
bool isInitialized()
Checks if the System Manager is already initialized.
te::srs::SpatialReferenceSystemManager::m_set
srs_set m_set
Definition: SpatialReferenceSystemManager.h:109
Config.h
Proxy configuration file for TerraView (see terraview_config.h).
te::srs::SpatialReferenceSystemPtr
std::unique_ptr< SpatialReferenceSystem > SpatialReferenceSystemPtr
Definition: SpatialReferenceSystem.h:121
te::srs::SpatialReferenceSystemManager::recognizes
bool recognizes(unsigned int id, const std::string &authName="EPSG") const
Returns true is a pair <id, authority> is recognized by the manager.
te::srs::SpatialReferenceSystemManager::init
void init()
Inializes the manager from a JSON file containing instances of SRSs.
te::srs::SpatialReferenceSystemManager::srs_desc::m_wkt
std::string m_wkt
Definition: SpatialReferenceSystemManager.h:86