GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
units.py
Go to the documentation of this file.
1 """!
2 @package core.units
3 
4 @brief Units management
5 
6 @todo Probably will be replaced by Python ctypes fns in the near
7 future(?)
8 
9 Usage:
10 @code
11 from core.units import Units
12 @endcode
13 
14 Classes:
15  - units::BaseUnits
16 
17 (C) 2009, 2011 by the GRASS Development Team
18 
19 This program is free software under the GNU General Public License
20 (>=v2). Read the file COPYING that comes with GRASS for details.
21 
22 @author Martin Landa <landa.martin gmail.com>
23 """
24 
25 class BaseUnits:
26  def __init__(self):
27  self._units = dict()
28  self._units['length'] = { 0 : { 'key' : 'mu', 'label' : _('map units') },
29  1 : { 'key' : 'me', 'label' : _('meters') },
30  2 : { 'key' : 'km', 'label' : _('kilometers') },
31  3 : { 'key' : 'mi', 'label' : _('miles') },
32  4 : { 'key' : 'ft', 'label' : _('feet') } }
33 
34  self._units['area'] = { 0 : { 'key' : 'mu', 'label' : _('sq map units') },
35  1 : { 'key' : 'me', 'label' : _('sq meters') },
36  2 : { 'key' : 'km', 'label' : _('sq kilometers') },
37  3 : { 'key' : 'ar', 'label' : _('acres') },
38  4 : { 'key' : 'ht', 'label' : _('hectares') } }
39 
40  def GetUnitsList(self, type):
41  """!Get list of units (their labels)
42 
43  @param type units type ('length' or 'area')
44 
45  @return list of units labels
46  """
47  result = list()
48  try:
49  keys = self._units[type].keys()
50  keys.sort()
51  for idx in keys:
52  result.append(self._units[type][idx]['label'])
53  except KeyError:
54  pass
55 
56  return result
57 
58  def GetUnitsKey(self, type, index):
59  """!Get units key based on index
60 
61  @param type units type ('length' or 'area')
62  @param index units index
63  """
64  return self._units[type][index]['key']
65 
66  def GetUnitsIndex(self, type, key):
67  """!Get units index based on key
68 
69  @param type units type ('length' or 'area')
70  @param key units key, e.g. 'me' for meters
71 
72  @return index
73  """
74  for k, u in self._units[type].iteritems():
75  if u['key'] == key:
76  return k
77  return 0
78 
79 Units = BaseUnits()
80 
81 def ConvertValue(value, type, units):
82  """!Convert value from map units to given units
83 
84  Inspired by vector/v.to.db/units.c
85 
86  @param value value to be converted
87  @param type units type ('length', 'area')
88  @param unit destination units
89  """
90  # get map units
91  # TODO
92 
93  f = 1
94  if type == 'length':
95  if units == 'me':
96  f = 1.0
97  elif units == 'km':
98  f = 1.0e-3
99  elif units == 'mi':
100  f = 6.21371192237334e-4
101  elif units == 'ft':
102  f = 3.28083989501312
103  else: # -> area
104  if units == 'me':
105  f = 1.0
106  elif units == 'km':
107  f = 1.0e-6
108  elif units == 'mi':
109  f = 3.86102158542446e-7
110  elif units == 'ft':
111  f = 10.7639104167097
112  elif units == 'ar':
113  f = 2.47105381467165e-4
114  elif units == 'ht':
115  f = 1.0e-4
116 
117  return f * value
def __init__
Definition: units.py:26
def GetUnitsIndex
Get units index based on key.
Definition: units.py:66
def GetUnitsList
Get list of units (their labels)
Definition: units.py:40
def GetUnitsKey
Get units key based on index.
Definition: units.py:58
def ConvertValue
Convert value from map units to given units.
Definition: units.py:81