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