units.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. """
  2. @package core.units
  3. @brief Units management
  4. .. todo::
  5. Probably will be replaced by Python ctypes fns in the near future(?)
  6. Usage:
  7. from core.units import Units
  8. Classes:
  9. - units::BaseUnits
  10. (C) 2009, 2011 by the GRASS Development Team
  11. This program is free software under the GNU General Public License
  12. (>=v2). Read the file COPYING that comes with GRASS for details.
  13. @author Martin Landa <landa.martin gmail.com>
  14. """
  15. import math
  16. if __name__ == '__main__':
  17. import os
  18. import sys
  19. from core.utils import _
  20. class BaseUnits:
  21. def __init__(self):
  22. self._units = dict()
  23. self._units['length'] = { 0 : { 'key' : 'mu', 'label' : _('map units') },
  24. 1 : { 'key' : 'me', 'label' : _('meters') },
  25. 2 : { 'key' : 'km', 'label' : _('kilometers') },
  26. 3 : { 'key' : 'mi', 'label' : _('miles') },
  27. 4 : { 'key' : 'ft', 'label' : _('feet') } }
  28. self._units['area'] = { 0 : { 'key' : 'mu', 'label' : _('sq map units') },
  29. 1 : { 'key' : 'me', 'label' : _('sq meters') },
  30. 2 : { 'key' : 'km', 'label' : _('sq kilometers') },
  31. 3 : { 'key' : 'ar', 'label' : _('acres') },
  32. 4 : { 'key' : 'ht', 'label' : _('hectares') } }
  33. def GetUnitsList(self, type):
  34. """Get list of units (their labels)
  35. :param type: units type ('length' or 'area')
  36. :return: list of units labels
  37. """
  38. result = list()
  39. try:
  40. keys = self._units[type].keys()
  41. keys.sort()
  42. for idx in keys:
  43. result.append(self._units[type][idx]['label'])
  44. except KeyError:
  45. pass
  46. return result
  47. def GetUnitsKey(self, type, index):
  48. """Get units key based on index
  49. :param type: units type ('length' or 'area')
  50. :param index: units index
  51. """
  52. return self._units[type][index]['key']
  53. def GetUnitsIndex(self, type, key):
  54. """Get units index based on key
  55. :param type: units type ('length' or 'area')
  56. :param key: units key, e.g. 'me' for meters
  57. :return: index
  58. """
  59. for k, u in self._units[type].iteritems():
  60. if u['key'] == key:
  61. return k
  62. return 0
  63. Units = BaseUnits()
  64. def ConvertValue(value, type, units):
  65. """Convert value from map units to given units
  66. Inspired by vector/v.to.db/units.c
  67. :param value: value to be converted
  68. :param type: units type ('length', 'area')
  69. :param unit: destination units
  70. """
  71. # get map units
  72. # TODO
  73. f = 1
  74. if type == 'length':
  75. if units == 'me':
  76. f = 1.0
  77. elif units == 'km':
  78. f = 1.0e-3
  79. elif units == 'mi':
  80. f = 6.21371192237334e-4
  81. elif units == 'ft':
  82. f = 3.28083989501312
  83. else: # -> area
  84. if units == 'me':
  85. f = 1.0
  86. elif units == 'km':
  87. f = 1.0e-6
  88. elif units == 'mi':
  89. f = 3.86102158542446e-7
  90. elif units == 'ft':
  91. f = 10.7639104167097
  92. elif units == 'ar':
  93. f = 2.47105381467165e-4
  94. elif units == 'ht':
  95. f = 1.0e-4
  96. return f * value
  97. def formatDist(distance, mapunits):
  98. """Formats length numbers and units in a nice way.
  99. Formats length numbers and units as a function of length.
  100. >>> formatDist(20.56915, 'metres')
  101. (20.57, 'm')
  102. >>> formatDist(6983.4591, 'metres')
  103. (6.983, 'km')
  104. >>> formatDist(0.59, 'feet')
  105. (0.59, 'ft')
  106. >>> formatDist(8562, 'feet')
  107. (1.622, 'miles')
  108. >>> formatDist(0.48963, 'degrees')
  109. (29.38, 'min')
  110. >>> formatDist(20.2546, 'degrees')
  111. (20.25, 'deg')
  112. >>> formatDist(82.146, 'unknown')
  113. (82.15, 'units')
  114. Accepted map units are 'meters', 'metres', 'feet', 'degree'.
  115. Returns 'units' instead of unrecognized units.
  116. :param distance: map units
  117. :param mapunits: map units
  118. From code by Hamish Bowman Grass Development Team 2006.
  119. """
  120. if mapunits == 'metres':
  121. mapunits = 'meters'
  122. outunits = mapunits
  123. distance = float(distance)
  124. divisor = 1.0
  125. # figure out which units to use
  126. if mapunits == 'meters':
  127. if distance > 2500.0:
  128. outunits = 'km'
  129. divisor = 1000.0
  130. else:
  131. outunits = 'm'
  132. elif mapunits == 'feet':
  133. # nano-bug: we match any "feet", but US Survey feet is really
  134. # 5279.9894 per statute mile, or 10.6' per 1000 miles. As >1000
  135. # miles the tick markers are rounded to the nearest 10th of a
  136. # mile (528'), the difference in foot flavours is ignored.
  137. if distance > 5280.0:
  138. outunits = 'miles'
  139. divisor = 5280.0
  140. else:
  141. outunits = 'ft'
  142. elif 'degree' in mapunits:
  143. # was: 'degree' in mapunits and not haveCtypes (for unknown reason)
  144. if distance < 1:
  145. outunits = 'min'
  146. divisor = (1 / 60.0)
  147. else:
  148. outunits = 'deg'
  149. else:
  150. return (distance, 'units')
  151. # format numbers in a nice way
  152. if (distance / divisor) >= 2500.0:
  153. outdistance = round(distance / divisor)
  154. elif (distance / divisor) >= 1000.0:
  155. outdistance = round(distance / divisor, 1)
  156. elif (distance / divisor) > 0.0:
  157. outdistance = round(distance / divisor,
  158. int(math.ceil(3 - math.log10(distance / divisor))))
  159. else:
  160. outdistance = float(distance / divisor)
  161. return (outdistance, outunits)
  162. def doc_test():
  163. """Tests the module using doctest
  164. :return: a number of failed tests
  165. """
  166. import doctest
  167. from core.utils import do_doctest_gettext_workaround
  168. do_doctest_gettext_workaround()
  169. return doctest.testmod().failed
  170. if __name__ == '__main__':
  171. sys.exit(doc_test())