GRASS Programmer's Manual
6.4.2(2012)
|
00001 """! 00002 @package units 00003 00004 @brief Units management 00005 00006 Probably will be replaced by Python SWIG fns in the near future(?) 00007 00008 Usage: 00009 00010 @code 00011 from units import Units 00012 @endcode 00013 00014 Classes: 00015 - BaseUnits 00016 00017 (C) 2009 by the GRASS Development Team 00018 00019 This program is free software under the GNU General Public 00020 License (>=v2). Read the file COPYING that comes with GRASS 00021 for details. 00022 00023 @author Martin Landa <landa.martin gmail.com> 00024 """ 00025 00026 class BaseUnits: 00027 def __init__(self): 00028 self._units = dict() 00029 self._units['length'] = { 0 : { 'key' : 'mu', 'label' : _('map units') }, 00030 1 : { 'key' : 'me', 'label' : _('meters') }, 00031 2 : { 'key' : 'km', 'label' : _('kilometers') }, 00032 3 : { 'key' : 'mi', 'label' : _('miles') }, 00033 4 : { 'key' : 'ft', 'label' : _('feet') } } 00034 00035 self._units['area'] = { 0 : { 'key' : 'mu', 'label' : _('sq map units') }, 00036 1 : { 'key' : 'me', 'label' : _('sq meters') }, 00037 2 : { 'key' : 'km', 'label' : _('sq kilometers') }, 00038 3 : { 'key' : 'ar', 'label' : _('acres') }, 00039 4 : { 'key' : 'ht', 'label' : _('hectares') } } 00040 00041 def GetUnitsList(self, type): 00042 """!Get list of units (their labels) 00043 00044 @param type units type ('length' or 'area') 00045 00046 @return list of units labels 00047 """ 00048 result = list() 00049 try: 00050 keys = self._units[type].keys() 00051 keys.sort() 00052 for idx in keys: 00053 result.append(self._units[type][idx]['label']) 00054 except KeyError: 00055 pass 00056 00057 return result 00058 00059 def GetUnitsKey(self, type, index): 00060 """!Get units key based on index 00061 00062 @param type units type ('length' or 'area') 00063 @param index units index 00064 """ 00065 return self._units[type][index]['key'] 00066 00067 def GetUnitsIndex(self, type, key): 00068 """!Get units index based on key 00069 00070 @param type units type ('length' or 'area') 00071 @param key units key, e.g. 'me' for meters 00072 00073 @return index 00074 """ 00075 for k, u in self._units[type].iteritems(): 00076 if u['key'] == key: 00077 return k 00078 return 0 00079 00080 Units = BaseUnits() 00081 00082 def ConvertValue(value, type, units): 00083 """!Convert value from map units to given units 00084 00085 Inspired by vector/v.to.db/units.c 00086 00087 @param value value to be converted 00088 @param type units type ('length', 'area') 00089 @param unit destination units 00090 """ 00091 # get map units 00092 # TODO 00093 00094 f = 1 00095 if type == 'length': 00096 if units == 'me': 00097 f = 1.0 00098 elif units == 'km': 00099 f = 1.0e-3 00100 elif units == 'mi': 00101 f = 6.21371192237334e-4 00102 elif units == 'ft': 00103 f = 3.28083989501312 00104 else: # -> area 00105 if units == 'me': 00106 f = 1.0 00107 elif units == 'km': 00108 f = 1.0e-6 00109 elif units == 'mi': 00110 f = 3.86102158542446e-7 00111 elif units == 'ft': 00112 f = 10.7639104167097 00113 elif units == 'ar': 00114 f = 2.47105381467165e-4 00115 elif units == 'ht': 00116 f = 1.0e-4 00117 00118 return f * value