units.py 3.2 KB

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