31 #include "../core/translator/Translator.h" 32 #include "../core/utils/Platform.h" 41 #include <boost/algorithm/string.hpp> 42 #include <boost/format.hpp> 43 #include <boost/property_tree/json_parser.hpp> 44 #include <boost/property_tree/ptree.hpp> 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()));
96 if (itn->second == uom->getId())
116 std::string upstr = boost::to_upper_copy(name);
118 std::map<std::string, unsigned int>::const_iterator it =
m_uomsIdxByName.find(upstr);
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();
148 if (it->second == uom->getId())
149 names.push_back(it->first);
159 if (uFrom->getType() != uTo->getType())
160 throw Exception(
TE_TR(
"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 if (uFrom->getType() != uTo->getType())
184 if ((uFrom->getBaseUnitId() == uTo->getId()) ||
185 (uTo->getBaseUnitId() == uFrom->getId()))
194 throw Exception(
TE_TR(
"The unit of measure manager is already initialized!"));
199 throw Exception(
TE_TR(
"The unit of measure JSON file could not be found!"));
201 boost::property_tree::ptree pt;
203 boost::property_tree::json_parser::read_json(uom_file, pt);
205 for(boost::property_tree::ptree::value_type& v: pt.get_child(
"units"))
207 unsigned int id = v.second.get<
unsigned int>(
"id");
208 std::string name = v.second.get<std::string>(
"name");
209 std::string symbol = v.second.get<std::string>(
"symbol");
210 std::string stype = v.second.get<std::string>(
"type");
211 std::string description = v.second.get<std::string>(
"description");
212 unsigned int targetUOM = v.second.get<
unsigned int>(
"target_uom");
213 double a = v.second.get<
double>(
"factor_a");
214 double b = v.second.get<
double>(
"factor_b");
215 double c = v.second.get<
double>(
"factor_c");
216 double d = v.second.get<
double>(
"factor_d");
220 if(stype ==
"length")
222 else if(stype ==
"area")
224 else if(stype ==
"volume")
226 else if(stype ==
"angle")
228 else if(stype ==
"scale")
230 else if(stype ==
"time")
232 else if(stype ==
"speed")
236 throw Exception((boost::format(
TE_TR(
"Invalid unit of measure type: %1%!")) % stype).str());
246 bool initialized = !
m_uoms.empty();
void remove(UnitOfMeasurePtr &uom)
Removes a unit of measure from the manager.
std::map< std::string, unsigned int > m_uomsIdxByName
void insert(UnitOfMeasurePtr &uom)
Inserts a new unit of measure to be managed.
void init()
It initializes the list of well kown units of measure.
A singleton class for dealing with units-of-measure.
UnitOfMeasurePtr findBySymbol(const std::string &symbol) const
Returns a unit of measure identified by its symbol.
void clear()
Removes all units from the catalogue.
#define TE_TR(message)
It marks a string in order to get translated.
std::map< unsigned int, UnitOfMeasurePtr > m_uoms
void getNames(UnitOfMeasurePtr &uom, std::vector< std::string > &names) const
Retrieves the alternative names for a unit of measure.
std::map< unsigned int, UnitOfMeasurePtr >::const_iterator const_iterator
A class for representing a unit of measure.
static te::dt::DateTime d(2010, 8, 9, 15, 58, 39)
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...
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
boost::shared_ptr< UnitOfMeasure > UnitOfMeasurePtr
MeasureType
Defines the possible types of unit of measurements.
A class to represent units of measure.
TECOREEXPORT std::string FindInTerraLibPath(const std::string &path)
Returns the path relative to a directory or file in the context of TerraLib.
bool areConvertible(const std::string &unitFromName, const std::string &unitToName) const
Check if two units of measure are compatible.
Utility functions for dealing with strings.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
UnitOfMeasurePtr find(unsigned int id) const
Returns a unit of measure identified by its identificaton.
UnitsOfMeasureManager()
It initializes the Singleton.
~UnitsOfMeasureManager()
Destructor.
This file contains several utility functions for dealing with STL containers.
std::map< unsigned int, UnitOfMeasurePtr >::iterator iterator