units.py 3.2 KB

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