27 #include <boost/foreach.hpp>
28 #include <boost/property_tree/ptree.hpp>
29 #include <boost/property_tree/json_parser.hpp>
32 #include "../common/Translator.h"
33 #include "../common/UnitsOfMeasureManager.h"
45 m_auth_name(auth_name),
53 std::string ssrid = m_auth_name;
55 ssrid += boost::lexical_cast<std::string>(m_auth_id);
71 throw Exception(
TR_SRS(
"The spatial reference system manager is already initialized!"));
73 const char* te_env = getenv(
"TERRALIB_DIR");
76 throw Exception(
TR_SRS(
"Environment variable \"TERRALIB_DIR\" not found.\nTry to set it before run the application."));
82 std::string jsonf(te_env);
83 jsonf +=
"/resources/json/srs.json";
85 f.open(jsonf.c_str());
89 boost::property_tree::ptree pt;
90 boost::property_tree::json_parser::read_json(f,pt);
91 BOOST_FOREACH(boost::property_tree::ptree::value_type &v, pt.get_child(
"SRSs"))
93 if (v.second.get<
unsigned int>(
"srid") > 100000)
94 add(v.second.get<std::string>(
"name"), v.second.get<std::string>(
"pj4txt"),
95 v.second.get<std::string>(
"wkt"), v.second.get<
unsigned int>(
"srid"),
"USER");
97 add(v.second.get<std::string>(
"name"), v.second.get<std::string>(
"pj4txt"),
98 v.second.get<std::string>(
"wkt"), v.second.get<
unsigned int>(
"srid"));
102 catch(boost::property_tree::json_parser::json_parser_error &je)
104 std::string errmsg =
"Error parsing: " + je.filename() +
": " + je.message();
105 te::srs::Exception ex(
TR_SRS(errmsg));
108 catch (std::exception
const& e)
110 std::cerr << e.what() << std::endl;
117 std::string key = authName;
119 key += boost::lexical_cast<std::string>(id);
121 boost::multi_index::nth_index<srs_set,0>::type::iterator it = boost::multi_index::get<0>(
m_set).find(key);
122 if (it != boost::multi_index::get<0>(
m_set).end())
123 throw te::srs::Exception(
TR_SRS(
"The CS identification already exists in the manager."));
125 srs_desc record(name,
id, authName, p4Txt, wkt);
126 m_set.insert(record);
131 std::string key = authName;
133 key += boost::lexical_cast<std::string>(id);
135 boost::multi_index::nth_index<srs_set,0>::type::iterator it = boost::multi_index::get<0>(
m_set).find(key);
136 if (it == boost::multi_index::get<0>(
m_set).end())
140 key += boost::lexical_cast<std::string>(id);
141 it = boost::multi_index::get<0>(
m_set).find(key);
142 return (it != boost::multi_index::get<0>(
m_set).end());
150 std::string wkt =
getWkt(
id,authName);
152 return std::auto_ptr<te::srs::SpatialReferenceSystem>();
160 throw te::srs::Exception(
TR_SRS(
"Error parsing the registered CS WKT."));
162 return std::auto_ptr<te::srs::SpatialReferenceSystem>();
168 std::string key = authName;
170 key += boost::lexical_cast<std::string>(id);
172 boost::multi_index::nth_index<srs_set,0>::type::iterator it = boost::multi_index::get<0>(
m_set).find(key);
173 if (it == boost::multi_index::get<0>(
m_set).end())
177 key += boost::lexical_cast<std::string>(id);
178 it = boost::multi_index::get<0>(
m_set).find(key);
179 if (it!=boost::multi_index::get<0>(
m_set).end())
190 std::string key = authName;
192 key += boost::lexical_cast<std::string>(id);
194 boost::multi_index::nth_index<srs_set,0>::type::iterator it = boost::multi_index::get<0>(
m_set).find(key);
195 if (it==boost::multi_index::get<0>(
m_set).end())
199 key += boost::lexical_cast<std::string>(id);
200 it = boost::multi_index::get<0>(
m_set).find(key);
201 if (it!=boost::multi_index::get<0>(
m_set).end())
212 std::string key = authName;
214 key += boost::lexical_cast<std::string>(id);
216 boost::multi_index::nth_index<srs_set,0>::type::iterator it = boost::multi_index::get<0>(
m_set).find(key);
217 if (it==boost::multi_index::get<0>(
m_set).end())
221 key += boost::lexical_cast<std::string>(id);
222 it = boost::multi_index::get<0>(
m_set).find(key);
223 if (it!=boost::multi_index::get<0>(
m_set).end())
234 boost::multi_index::nth_index<srs_set,1>::type::iterator it = boost::multi_index::get<1>(
m_set).find(name);
235 if (it==boost::multi_index::get<1>(
m_set).end())
236 throw te::srs::Exception(
TR_SRS(
"CS name not recognized."));
238 return std::pair<std::string,unsigned int>(it->m_auth_name, it->m_auth_id);
244 boost::multi_index::nth_index<srs_set,2>::type::iterator it = boost::multi_index::get<2>(
m_set).find(p4Txt);
245 if (it==boost::multi_index::get<2>(
m_set).end())
246 throw te::srs::Exception(
TR_SRS(
"CS name not recognized."));
248 return std::pair<std::string,unsigned int>(it->m_auth_name, it->m_auth_id);
253 boost::multi_index::nth_index<srs_set,3>::type::iterator it = boost::multi_index::get<3>(
m_set).find(wkt);
254 if (it==boost::multi_index::get<3>(
m_set).end())
255 throw te::srs::Exception(
TR_SRS(
"CS name not recognized."));
257 return std::pair<std::string,unsigned int>(it->m_auth_name, it->m_auth_id);
262 std::string key = authName;
264 key += boost::lexical_cast<std::string>(id);
266 boost::multi_index::nth_index<srs_set,0>::type::iterator it = boost::multi_index::get<0>(
m_set).find(key);
267 if (it==boost::multi_index::get<0>(
m_set).end())
271 key += boost::lexical_cast<std::string>(id);
272 it = boost::multi_index::get<0>(
m_set).find(key);
273 if (it!=boost::multi_index::get<0>(
m_set).end())
285 std::pair<te::srs::SpatialReferenceSystemManager::iterator,te::srs::SpatialReferenceSystemManager::iterator>
289 te::srs::SpatialReferenceSystemManager::iterator>(boost::multi_index::get<0>(
m_set).begin(), boost::multi_index::get<0>(
m_set).end());
299 std::string unitName=
"metre";
303 std::string pjstr =
getP4Txt(
id,authName);
307 std::size_t found = pjstr.find(
"+units=");
308 if (found!=std::string::npos)
310 std::size_t aux = pjstr.find(
" ", found);
311 std::string unitsymbol = pjstr.substr(found+7,aux-(found+7));
321 std::string pjstr =
getP4Txt(
id,authName);
322 return (pjstr.find(
"+proj=longlat")!=std::string::npos);
An exception class for the SRS module.
std::pair< std::string, unsigned int > getIdFromName(const std::string &name) const
Returns a coordinate system identification given a name.
void clear()
Removes all coordinate system representations from the manager.
bool isGeographic(unsigned int id, const std::string &authName)
Checks if a SRS with a given id refers to a geographic spatial reference system.
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="EPSG") const
Returns a coordinate system WKT description given an id.
static SpatialReferenceSystem * read(const char *wkt)
It returns a valid SRS from a given WKT.
size_t size() const
Returns the number of objects in the manager.
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.
A class to manage Coordinate Systems representations.
UnitOfMeasurePtr find(unsigned int id) const
Returns a unit of measure identified by its identificaton.
SpatialReferenceSystemManager()
Constructor.
boost::shared_ptr< UnitOfMeasure > UnitOfMeasurePtr
#define TR_SRS(message)
It marks a string in order to get translated. This is a special mark used in the SRS module of TerraL...
std::pair< te::srs::SpatialReferenceSystemManager::iterator, te::srs::SpatialReferenceSystemManager::iterator > getIterators() const
Returns an iterator mechanism over the coordinate system descriptions in the manager.
srs_desc(const std::string &name, unsigned int auth_id, const std::string &auth_name, const std::string &p4txt, const std::string &wkt)
std::string getP4Txt(unsigned int id, const std::string &authName="EPSG") const
Returns a coordinate system PROJ4 description given an identification.
SpatialReferenceSystemPtr getSpatialReferenceSystem(unsigned int id, const std::string &authName="EPSG") const
Returns a pointer to a coordinate system given an identification.
void remove(unsigned int id, const std::string &authName="EPSG")
Removes a coordinate system representation from the manager, given its identification.
boost::multi_index::nth_index< srs_set, 0 >::type::iterator iterator
An iterator by SRS <id,authority>
te::common::UnitOfMeasurePtr getUnit(unsigned int id, const std::string &authName="EPSG")
Returns the unit of measure for a SRS with a given id.
bool recognizes(unsigned int id, const std::string &authName="EPSG") const
Returns true is a pair <id, authority> is recognized by the manager.
~SpatialReferenceSystemManager()
Destructor.
std::pair< std::string, unsigned int > getIdFromWkt(const std::string &wkt) const
Returns a coordinate system identification given a WKT description.
UnitOfMeasurePtr findBySymbol(const std::string &symbol) const
Returns a unit of measure identified by its symbol.
std::string getName(unsigned int id, const std::string &authName="EPSG") const
Returns a coordinate system name given an identification.
static UnitsOfMeasureManager & getInstance()
It returns a reference to the singleton instance.
void init()
Inializes the manager from a JSON file containing instances of SRSs.