39 #include <boost/algorithm/string.hpp>
40 #include <boost/foreach.hpp>
41 #include <boost/format.hpp>
42 #include <boost/property_tree/json_parser.hpp>
43 #include <boost/property_tree/ptree.hpp>
56 m_uomsIdxByName.clear();
64 if(it != m_uoms.end())
67 m_uoms.insert(std::pair<unsigned int, UnitOfMeasurePtr>(uom->getId(),uom));
69 std::string upstr = boost::to_upper_copy(uom->getName());
70 m_uomsIdxByName.insert(std::pair<std::string, unsigned int>(upstr,uom->getId()));
77 for(std::size_t i=0; i<alternativeNames.size(); ++i)
79 std::string upstr = boost::to_upper_copy(alternativeNames[i]);
80 m_uomsIdxByName.insert(std::pair<std::string, unsigned int>(upstr,uom->getId()));
88 iterator it = m_uoms.find(uom->getId());
89 if (it == m_uoms.end())
94 for (std::map<std::string, unsigned int>::iterator itn = m_uomsIdxByName.begin(); itn != m_uomsIdxByName.end();)
96 if (itn->second == uom->getId())
97 m_uomsIdxByName.erase(itn++);
107 if (it == m_uoms.end())
116 std::string upstr = boost::to_upper_copy(name);
118 std::map<std::string, unsigned int>::const_iterator it = m_uomsIdxByName.find(upstr);
120 if(it != m_uomsIdxByName.end())
123 if (it2 != m_uoms.end())
134 while (it != m_uoms.end())
136 if (it->second->getSymbol() == symbol)
145 std::map<std::string, unsigned int>::const_iterator it = m_uomsIdxByName.begin();
146 while (it != m_uomsIdxByName.end())
148 if (it->second == uom->getId())
149 names.push_back(it->first);
159 if (uFrom->getType() != uTo->getType())
160 throw Exception(
TR_COMMON(
"There is not conversion between units for different types of measures."));
162 if (uFrom->getBaseUnitId() == uTo->getId())
164 return (uFrom->getConversionValue());
166 else if (uTo->getBaseUnitId() == uFrom->getId())
170 uTo->getConversionFactors(a,b,c,d);
171 return ((b-d)/(c-a));
181 boost::property_tree::ptree pt;
183 const char* te_env = getenv(
"TERRALIB_DIR");
186 throw Exception(
TR_COMMON(
"Environment variable \"TERRALIB_DIR\" not found.\nTry to set it before run the application."));
188 std::string uom_file(te_env);
189 uom_file +=
"/resources/json/uom.json";
191 boost::property_tree::json_parser::read_json(uom_file, pt);
193 BOOST_FOREACH(boost::property_tree::ptree::value_type& v, pt.get_child(
"units"))
195 unsigned int id = v.second.get<
unsigned int>(
"id");
196 std::string name = v.second.get<std::string>(
"name");
197 std::string symbol = v.second.get<std::string>(
"symbol");
198 std::string stype = v.second.get<std::string>(
"type");
199 std::string description = v.second.get<std::string>(
"description");
200 unsigned int targetUOM = v.second.get<
unsigned int>(
"target_uom");
201 double a = v.second.get<
double>(
"factor_a");
202 double b = v.second.get<
double>(
"factor_b");
203 double c = v.second.get<
double>(
"factor_c");
204 double d = v.second.get<
double>(
"factor_d");
208 if(stype ==
"length")
210 else if(stype ==
"area")
212 else if(stype ==
"volume")
214 else if(stype ==
"angle")
216 else if(stype ==
"scale")
218 else if(stype ==
"time")
220 else if(stype ==
"speed")
224 throw Exception((boost::format(
TR_COMMON(
"Invalid unit of measure type: %1%!")) % stype).str());
Utility functions for dealing with strings.
This file contains several utility functions for dealing with STL containers.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
std::map< unsigned int, UnitOfMeasurePtr >::iterator iterator
mydialect insert("+", new te::da::BinaryOpEncoder("+"))
void getNames(UnitOfMeasurePtr &uom, std::vector< std::string > &names) const
Retrieves the alternative names for a unit of measure.
UnitOfMeasurePtr find(unsigned int id) const
Returns a unit of measure identified by its identificaton.
A class for representing a unit of measure.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
boost::shared_ptr< UnitOfMeasure > UnitOfMeasurePtr
double getConversion(const std::string &unitFromName, const std::string &unitToName) const
Calculates a multiplicative factor to convert from a given unit to its base unit and vice-versa...
void remove(UnitOfMeasurePtr &uom)
Removes a unit of measure from the manager.
A class to represent units of measure.
std::map< unsigned int, UnitOfMeasurePtr >::const_iterator const_iterator
void clear()
Removes all units from the catalogue.
UnitsOfMeasureManager()
It initializes the Singleton.
~UnitsOfMeasureManager()
Destructor.
MeasureType
Defines the possible types of unit of measurements.
#define TR_COMMON(message)
It marks a string in order to get translated. This is the mark used in the Common module of TerraLib...
void init()
It initializes the list of well kown units of measure.
UnitOfMeasurePtr findBySymbol(const std::string &symbol) const
Returns a unit of measure identified by its symbol.
A singleton class for dealing with units-of-measure.
void insert(UnitOfMeasurePtr &uom)
Inserts a new unit of measure to be managed.
This class is designed for dealing with multi-language text translation in TerraLib.