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 
111  public:
112 
113  //! An iterator by SRS <id,authority>
114  typedef boost::multi_index::nth_index<srs_set,0>::type::iterator iterator;
115 
116  //! Destructor.
118 
119  /*!
120  \brief Inializes the manager from a JSON file containing instances of SRSs
121 
122  This methods reads the file "TE_JSON_FILES_LOCATION/srs.json" for SRSs definitions and insert them on the manager if it is empty.
123  \exception te::srs::Exception if the JSON file is not well formed.
124  */
125  void init();
126 
127  /*!
128  \brief Inializes the manager from a JSON file containing instances of SRSs.
129 
130  \param fileName Name of the JSON file.
131  \exception te::srs::Exception if the JSON file is not well formed.
132  */
133  void init(const std::string& fileName);
134 
135  /*!
136  \brief Adds a <id, authority> to the manager.
137  \param name A simple, human-readable, interface-friendly, name;
138  \param p4Txt a PROJ4 library description of coordinate system;
139  \param wkt An OGC's WKT description for the coordinate system;
140  \param id SRS id.
141  \param authName The authority responsible for the id. Default "EPSG".
142  \exception te::srs::Exception if the coordinate system id is already registered in the manager.
143  */
144  void add(const std::string& name, const std::string& p4Txt, const std::string& wkt, unsigned int id, const std::string& authName="EPSG");
145 
146  /*!
147  \brief Returns true is a pair <id, authority> is recognized by the manager.
148  \param id The coordinate system identification.
149  \param authName The authority responsible for the id. Default "EPSG".
150  \return True if the pair <id,authority> is already registered in the manager and false otherwise.
151  */
152  bool recognizes(unsigned int id, const std::string& authName="EPSG") const;
153 
154  /*!
155  \brief Returns a coordinate system name given an identification.
156  \param id The coordinate system identification.
157  \param authName The authority responsible for the id. Default "EPSG".
158  \return the Coordinate System name if the identification is recognized or and empty string otherwise.
159  */
160  std::string getName (unsigned int id, const std::string& authName="EPSG") const;
161 
162  /*!
163  \brief Returns a coordinate system PROJ4 description given an identification.
164  \param id The coordinate system identification.
165  \param authName The authority responsible for the id. Default "EPSG".
166  \return The coordinate system PROJ4 description if the identification is recognized or an empty string otherwise.
167  */
168  std::string getP4Txt(unsigned int id, const std::string& authName="EPSG") const;
169 
170  /*!
171  \brief Returns a coordinate system WKT description given an id.
172  \param id The coordinate system identification.
173  \param authName The authority responsible for the id. Default "EPSG".
174  \return The coordinate system OGC WKT if the identification is recognized or an empty string otherwise.
175  */
176  std::string getWkt (unsigned int id, const std::string& authName="EPSG") const;
177 
178  /*!
179  \brief Returns a coordinate system identification given a name.
180  \param name The coordinate system name.
181  \return A pair composed by the coordinate system identification and the name of the authority responsible for it.
182  \exception te::srs::Exception if the coordinate system name is not found in the manager.
183  */
184  std::pair<std::string,unsigned int> getIdFromName (const std::string& name) const;
185 
186  /*!
187  \brief Returns a coordinate system identification given a PROJ4 description.
188  \param p4Txt The coordinate system PROJ4 description.
189  \return A pair composed by the coordinate system identification and the name of the authority responsible for it.
190  \exception te::srs::Exception if the coordinate system PROJ4 description is not found in the manager.
191  */
192  std::pair<std::string,unsigned int> getIdFromP4Txt(const std::string& p4Txt) const;
193 
194  /*!
195  \brief Returns a coordinate system identification given a WKT description.
196  \param wkt The coordinate system WKT.
197  \return A pair composed by the coordinate system identification and the name of the authority responsible for it.
198  \exception te::srs::Exception if the coordinate system WKT is not found in the manager.
199  */
200  std::pair<std::string,unsigned int> getIdFromWkt (const std::string& wkt) const;
201 
202  /*!
203  \brief Returns a pointer to a coordinate system given an identification.
204  \param id The coordinate system identification.
205  \param authName The authority responsible for the id. Default "EPSG".
206  \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.
207  */
208  SpatialReferenceSystemPtr getSpatialReferenceSystem(unsigned int id, const std::string& authName="EPSG") const;
209 
210  /*!
211  \brief Removes a coordinate system representation from the manager, given its identification.
212  \param id The coordinate system identification.
213  \param authName The authority responsible for the id. Default "EPSG".
214  */
215  void remove (unsigned int id, const std::string& authName="EPSG");
216 
217  //! Removes all coordinate system representations from the manager.
218  void clear();
219 
220  //! Returns the number of objects in the manager.
221  size_t size() const;
222 
223  /*!
224  \brief Returns an iterator mechanism over the coordinate system descriptions in the manager.
225 
226  The first iterator of the returned pair points to first coordinate system description.
227  The second iterator of the returned pair points to the last plus one ccoordinate system description.
228  \return a pair of iterators pointing to the first and last coordnate system representation in the manager.
229  */
230  std::pair<te::srs::SpatialReferenceSystemManager::iterator,te::srs::SpatialReferenceSystemManager::iterator> getIterators() const;
231 
232  /*!
233  \brief Returns the unit of measure for a SRS with a given id.
234  \return A pointer to the unit of measure or a null pointer if it the SRS could not be founded.
235  */
236  te::common::UnitOfMeasurePtr getUnit(unsigned int id, const std::string& authName="EPSG");
237 
238  /*!
239  \brief Checks if a SRS with a given id refers to a geographic spatial reference system.
240  \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.
241  */
242  bool isGeographic(unsigned int id, const std::string& authName="EPSG");
243 
244  /*!
245  \brief Checks if the System Manager is already initialized.
246  */
247  bool isInitialized();
248 
249  /*!
250  \brief Returns a SRID, not yet used, to identify an SRS created by an user.
251  \return a string represent a SRID, not yet used, to identify an SRS created by an user.
252  */
253  std::string getNewUserDefinedSRID();
254 
255  protected:
256 
257  /*! \brief Constructor. */
259 
260  private:
261 
262  /*!
263  \brief Copy constructor not allowed.
264 
265  \param rhs The right-hand-side copy that would be used to copy from.
266  */
268 
269  /*!
270  \brief Assignment operator not allowed.
271 
272  \param rhs The right-hand-side copy that would be used to copy from.
273 
274  \return A reference to this object.
275  */
277 
278  };
279  }
280 }
281 #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)>, boost::multi_index::ordered_non_unique< BOOST_MULTI_INDEX_MEMBER(srs_desc, std::string, m_auth_name)> > > 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. ...
URI C++ Library.
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:364
Template support for singleton pattern.
Definition: Singleton.h:100