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