27 #include "../core/translator/Translator.h" 28 #include "../core/utils/Platform.h" 29 #include "../common/UnitsOfMeasureManager.h" 30 #include "../common/StringUtils.h" 36 #include <boost/property_tree/ptree.hpp> 37 #include <boost/property_tree/json_parser.hpp> 52 f.open(fileName.c_str());
57 boost::property_tree::ptree pt;
58 boost::property_tree::json_parser::read_json(f,pt);
59 for(boost::property_tree::ptree::value_type &v: pt.get_child(
"SRSs"))
61 if (v.second.get<
unsigned int>(
"srid") > 100000)
62 mger->
add(v.second.get<std::string>(
"name"), v.second.get<std::string>(
"pj4txt"),
63 v.second.get<std::string>(
"wkt"), v.second.get<
unsigned int>(
"srid"),
"USER");
65 mger->
add(v.second.get<std::string>(
"name"), v.second.get<std::string>(
"pj4txt"),
66 v.second.get<std::string>(
"wkt"), v.second.get<
unsigned int>(
"srid"));
76 m_auth_name(auth_name),
86 ssrid += boost::lexical_cast<std::string>(
m_auth_id);
101 throw Exception(
TE_TR(
"The spatial reference system manager is already initialized!"));
121 catch(boost::property_tree::json_parser::json_parser_error &je)
123 std::string errmsg =
"Error parsing: " + je.filename() +
": " + je.message();
124 te::srs::Exception ex(
TE_TR(errmsg));
127 catch (std::exception
const& e)
129 std::cerr << e.what() << std::endl;
136 std::string key = authName;
138 key += boost::lexical_cast<std::string>(id);
140 boost::multi_index::nth_index<srs_set,0>::type::iterator it = boost::multi_index::get<0>(
m_set).find(key);
141 if (it != boost::multi_index::get<0>(
m_set).end())
142 throw te::srs::Exception(
TE_TR(
"The CS identification already exists in the manager."));
144 srs_desc record(name,
id, authName, p4Txt, wkt);
145 m_set.insert(record);
150 std::string key = authName;
152 key += boost::lexical_cast<std::string>(id);
154 boost::multi_index::nth_index<srs_set,0>::type::iterator it = boost::multi_index::get<0>(
m_set).find(key);
155 if (it == boost::multi_index::get<0>(
m_set).end())
159 key += boost::lexical_cast<std::string>(id);
160 it = boost::multi_index::get<0>(
m_set).find(key);
161 return (it != boost::multi_index::get<0>(
m_set).end());
169 std::string wkt =
getWkt(
id,authName);
171 return std::unique_ptr<te::srs::SpatialReferenceSystem>();
179 throw te::srs::Exception(
TE_TR(
"Error parsing the registered CS WKT."));
181 return std::unique_ptr<te::srs::SpatialReferenceSystem>();
187 std::string key = authName;
189 key += boost::lexical_cast<std::string>(id);
191 boost::multi_index::nth_index<srs_set,0>::type::iterator it = boost::multi_index::get<0>(
m_set).find(key);
192 if (it == boost::multi_index::get<0>(
m_set).end())
196 key += boost::lexical_cast<std::string>(id);
197 it = boost::multi_index::get<0>(
m_set).find(key);
198 if (it!=boost::multi_index::get<0>(
m_set).end())
209 std::string key = authName;
211 key += boost::lexical_cast<std::string>(id);
213 boost::multi_index::nth_index<srs_set,0>::type::iterator it = boost::multi_index::get<0>(
m_set).find(key);
214 if (it==boost::multi_index::get<0>(
m_set).end())
218 key += boost::lexical_cast<std::string>(id);
219 it = boost::multi_index::get<0>(
m_set).find(key);
220 if (it!=boost::multi_index::get<0>(
m_set).end())
231 std::string key = authName;
233 key += boost::lexical_cast<std::string>(id);
235 boost::multi_index::nth_index<srs_set,0>::type::iterator it = boost::multi_index::get<0>(
m_set).find(key);
236 if (it==boost::multi_index::get<0>(
m_set).end())
240 key += boost::lexical_cast<std::string>(id);
241 it = boost::multi_index::get<0>(
m_set).find(key);
242 if (it!=boost::multi_index::get<0>(
m_set).end())
253 std::pair< srs_set::nth_index< 1 >::type::const_iterator,
254 srs_set::nth_index< 1 >::type::const_iterator > iterators =
255 m_set.get< 1 >().equal_range(name);
257 bool resultDeprecated =
true;
258 bool currentDeprecated =
false;
259 std::string upperCaseName;
261 std::pair<std::string,unsigned int> result;
262 result.second = (std::numeric_limits<unsigned int>::max)();
264 while( iterators.first != iterators.second )
266 assert( name == iterators.first->m_name );
269 currentDeprecated = ( upperCaseName.find(
"DEPRECATED" ) != std::string::npos );
272 currentDeprecated = currentDeprecated ||
273 ( upperCaseName.find(
"DEPRECATED" ) != std::string::npos );
281 ( iterators.first->m_auth_id < result.second )
285 ( !resultDeprecated )
287 ( !currentDeprecated )
289 ( iterators.first->m_auth_id < result.second )
295 ( !currentDeprecated )
299 result.first = iterators.first->m_auth_name;
300 result.second = iterators.first->m_auth_id;
301 resultDeprecated = currentDeprecated;
307 if( result.first.empty() )
309 throw te::srs::Exception(
TE_TR(
"CS name not recognized."));
318 std::pair< srs_set::nth_index< 2 >::type::const_iterator,
319 srs_set::nth_index< 2 >::type::const_iterator > iterators =
320 m_set.get< 2 >().equal_range(p4Txt);
322 bool resultDeprecated =
true;
323 bool currentDeprecated =
false;
324 std::string upperCaseName;
326 std::pair<std::string,unsigned int> result;
327 result.second = (std::numeric_limits<unsigned int>::max)();
329 while( iterators.first != iterators.second )
331 assert( p4Txt == iterators.first->m_p4txt );
334 currentDeprecated = ( upperCaseName.find(
"DEPRECATED" ) != std::string::npos );
337 currentDeprecated = currentDeprecated ||
338 ( upperCaseName.find(
"DEPRECATED" ) != std::string::npos );
346 ( iterators.first->m_auth_id < result.second )
350 ( !resultDeprecated )
352 ( !currentDeprecated )
354 ( iterators.first->m_auth_id < result.second )
360 ( !currentDeprecated )
364 result.first = iterators.first->m_auth_name;
365 result.second = iterators.first->m_auth_id;
366 resultDeprecated = currentDeprecated;
372 if( result.first.empty() )
374 throw te::srs::Exception(
TE_TR(
"CS name not recognized."));
382 std::pair< srs_set::nth_index< 3 >::type::const_iterator,
383 srs_set::nth_index< 3 >::type::const_iterator > iterators =
384 m_set.get< 3 >().equal_range(wkt);
386 bool resultDeprecated =
true;
387 bool currentDeprecated =
false;
388 std::string upperCaseName;
390 std::pair<std::string,unsigned int> result;
391 result.second = (std::numeric_limits<unsigned int>::max)();
393 while( iterators.first != iterators.second )
395 assert( wkt == iterators.first->m_wkt );
398 currentDeprecated = ( upperCaseName.find(
"DEPRECATED" ) != std::string::npos );
401 currentDeprecated = currentDeprecated ||
402 ( upperCaseName.find(
"DEPRECATED" ) != std::string::npos );
410 ( iterators.first->m_auth_id < result.second )
414 ( !resultDeprecated )
416 ( !currentDeprecated )
418 ( iterators.first->m_auth_id < result.second )
424 ( !currentDeprecated )
428 result.first = iterators.first->m_auth_name;
429 result.second = iterators.first->m_auth_id;
430 resultDeprecated = currentDeprecated;
436 if( result.first.empty() )
438 throw te::srs::Exception(
TE_TR(
"CS name not recognized."));
446 std::string key = authName;
448 key += boost::lexical_cast<std::string>(id);
450 boost::multi_index::nth_index<srs_set,0>::type::iterator it = boost::multi_index::get<0>(
m_set).find(key);
451 if (it==boost::multi_index::get<0>(
m_set).end())
455 key += boost::lexical_cast<std::string>(id);
456 it = boost::multi_index::get<0>(
m_set).find(key);
457 if (it!=boost::multi_index::get<0>(
m_set).end())
469 std::pair<te::srs::SpatialReferenceSystemManager::iterator,te::srs::SpatialReferenceSystemManager::iterator>
473 te::srs::SpatialReferenceSystemManager::iterator>(boost::multi_index::get<0>(
m_set).begin(), boost::multi_index::get<0>(
m_set).end());
483 std::string unitName=
"metre";
487 std::string pjstr =
getP4Txt(
id,authName);
491 std::size_t found = pjstr.find(
"+units=");
492 if (found!=std::string::npos)
494 std::size_t aux = pjstr.find(
" ", found);
495 std::string unitsymbol = pjstr.substr(found+7,aux-(found+7));
505 std::string pjstr =
getP4Txt(
id,authName);
506 return (pjstr.find(
"+proj=longlat")!=std::string::npos);
511 bool initialized = !
m_set.empty();
518 boost::multi_index::nth_index<srs_set,4>::type::iterator it = boost::multi_index::get<4>(
m_set).find(
"USER");
519 if (it==boost::multi_index::get<4>(
m_set).end())
522 unsigned int val = it->m_auth_id;
524 while (it != boost::multi_index::get<4>(
m_set).end())
526 if (it->m_auth_id > val)
530 return boost::lexical_cast<std::string>(val+1);
std::pair< std::string, unsigned int > getIdFromWkt(const std::string &wkt) const
Returns a coordinate system identification given a WKT description.
A class to manage Coordinate Systems representations.
Base exception class for plugin module.
SpatialReferenceSystemManager()
Constructor.
std::pair< te::srs::SpatialReferenceSystemManager::iterator, te::srs::SpatialReferenceSystemManager::iterator > getIterators() const
Returns an iterator mechanism over the coordinate system descriptions in the manager.
size_t size() const
Returns the number of objects in the manager.
UnitOfMeasurePtr findBySymbol(const std::string &symbol) const
Returns a unit of measure identified by its symbol.
An exception class for the SRS module.
bool isInitialized()
Checks if the System Manager is already initialized.
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.
void clear()
Removes all coordinate system representations from the manager.
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
boost::multi_index::nth_index< srs_set, 0 >::type::iterator iterator
An iterator by SRS <id,authority>
std::string getP4Txt(unsigned int id, const std::string &authName="EPSG") const
Returns a coordinate system PROJ4 description given an identification.
#define TE_TR(message)
It marks a string in order to get translated.
static SpatialReferenceSystem * read(const char *wkt)
It returns a valid SRS from a given WKT.
std::string getNewUserDefinedSRID()
Returns a SRID, not yet used, to identify an SRS created by an user.
SpatialReferenceSystemPtr getSpatialReferenceSystem(unsigned int id, const std::string &authName="EPSG") const
Returns a pointer to a coordinate system given an identification.
static UnitsOfMeasureManager & getInstance()
It returns a reference to the singleton instance.
std::pair< std::string, unsigned int > getIdFromName(const std::string &name) const
Returns a coordinate system identification given a name.
te::common::UnitOfMeasurePtr getUnit(unsigned int id, const std::string &authName="EPSG")
Returns the unit of measure for a SRS with a given id.
srs_desc(const std::string &name, unsigned int auth_id, const std::string &auth_name, const std::string &p4txt, const std::string &wkt)
A class to manage Coordinate Systems representations within TerraLib environment. ...
void remove(unsigned int id, const std::string &authName="EPSG")
Removes a coordinate system representation from the manager, given its identification.
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.
void init()
Inializes the manager from a JSON file containing instances of SRSs.
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.
std::string getName(unsigned int id, const std::string &authName="EPSG") const
Returns a coordinate system name given an identification.
boost::shared_ptr< UnitOfMeasure > UnitOfMeasurePtr
#define TE_SRS_USER_DEFINED_START_ID
std::string getWkt(unsigned int id, const std::string &authName="EPSG") const
Returns a coordinate system WKT description given an id.
TECOREEXPORT std::string FindInTerraLibPath(const std::string &path)
Returns the path relative to a directory or file in the context of TerraLib.
void LoadSpatialReferenceSystemManager(const std::string fileName, te::srs::SpatialReferenceSystemManager *mger)
UnitOfMeasurePtr find(unsigned int id) const
Returns a unit of measure identified by its identificaton.
std::pair< std::string, unsigned int > getIdFromP4Txt(const std::string &p4Txt) const
Returns a coordinate system identification given a PROJ4 description.