GRASS Programmer's Manual  6.5.svn(2012)-r51648
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
units.py
Go to the documentation of this file.
00001 """!
00002 @package core.units
00003 
00004 @brief Units management
00005 
00006 @todo Probably will be replaced by Python ctypes fns in the near
00007 future(?)
00008 
00009 Usage:
00010 @code
00011 from core.units import Units
00012 @endcode
00013 
00014 Classes:
00015  - units::BaseUnits
00016 
00017 (C) 2009, 2011 by the GRASS Development Team
00018 
00019 This program is free software under the GNU General Public License
00020 (>=v2). Read the file COPYING that comes with GRASS for details.
00021 
00022 @author Martin Landa <landa.martin gmail.com>
00023 """
00024 
00025 class BaseUnits:
00026     def __init__(self):
00027         self._units = dict()
00028         self._units['length'] = { 0 : { 'key' : 'mu', 'label' : _('map units') },
00029                              1 : { 'key' : 'me', 'label' : _('meters') },
00030                              2 : { 'key' : 'km', 'label' : _('kilometers') },
00031                              3 : { 'key' : 'mi', 'label' : _('miles') },
00032                              4 : { 'key' : 'ft', 'label' : _('feet') } }
00033         
00034         self._units['area']   = { 0 : { 'key' : 'mu', 'label' : _('sq map units') },
00035                              1 : { 'key' : 'me', 'label' : _('sq meters') },
00036                              2 : { 'key' : 'km', 'label' : _('sq kilometers') },
00037                              3 : { 'key' : 'ar', 'label' : _('acres') },
00038                              4 : { 'key' : 'ht', 'label' : _('hectares') } }
00039 
00040     def GetUnitsList(self, type):
00041         """!Get list of units (their labels)
00042         
00043         @param type units type ('length' or 'area')
00044         
00045         @return list of units labels
00046         """
00047         result = list()
00048         try:
00049             keys = self._units[type].keys()
00050             keys.sort()
00051             for idx in keys:
00052                 result.append(self._units[type][idx]['label'])
00053         except KeyError:
00054             pass
00055         
00056         return result
00057 
00058     def GetUnitsKey(self, type, index):
00059         """!Get units key based on index
00060         
00061         @param type units type ('length' or 'area')
00062         @param index units index
00063         """
00064         return self._units[type][index]['key']
00065 
00066     def GetUnitsIndex(self, type, key):
00067         """!Get units index based on key
00068         
00069         @param type units type ('length' or 'area')
00070         @param key units key, e.g. 'me' for meters
00071 
00072         @return index
00073         """
00074         for k, u in self._units[type].iteritems():
00075             if u['key'] == key:
00076                 return k
00077         return 0
00078 
00079 Units = BaseUnits()
00080 
00081 def ConvertValue(value, type, units):
00082     """!Convert value from map units to given units
00083 
00084     Inspired by vector/v.to.db/units.c
00085 
00086     @param value value to be converted
00087     @param type units type ('length', 'area')
00088     @param unit  destination units
00089     """
00090     # get map units
00091     # TODO
00092     
00093     f = 1
00094     if type == 'length':
00095         if units == 'me':
00096             f = 1.0
00097         elif units == 'km':
00098             f = 1.0e-3
00099         elif units == 'mi':
00100             f = 6.21371192237334e-4
00101         elif units == 'ft':
00102             f = 3.28083989501312
00103     else: # -> area
00104         if units == 'me':
00105             f = 1.0
00106         elif units == 'km':
00107             f = 1.0e-6
00108         elif units == 'mi':
00109             f = 3.86102158542446e-7
00110         elif units == 'ft':
00111             f = 10.7639104167097
00112         elif units == 'ar':
00113             f = 2.47105381467165e-4
00114         elif units == 'ht':
00115             f = 1.0e-4
00116 
00117     return f * value