2008-08-06 02:13:06 +08:00
|
|
|
# Copyright (c) 2007, Robert Coup <robert.coup@onetrackmind.co.nz>
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
# are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer.
|
|
|
|
#
|
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
#
|
|
|
|
# 3. Neither the name of Distance nor the names of its contributors may be used
|
|
|
|
# to endorse or promote products derived from this software without
|
|
|
|
# specific prior written permission.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
#
|
|
|
|
"""
|
2014-03-02 22:25:53 +08:00
|
|
|
Distance and Area objects to allow for sensible and convenient calculation
|
2008-08-06 02:13:06 +08:00
|
|
|
and conversions.
|
|
|
|
|
2012-06-14 21:24:47 +08:00
|
|
|
Authors: Robert Coup, Justin Bronn, Riccardo Di Virgilio
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2016-06-16 09:20:23 +08:00
|
|
|
Inspired by GeoPy (https://github.com/geopy/geopy)
|
2008-08-06 02:13:06 +08:00
|
|
|
and Geoff Biggs' PhD work on dimensioned units for robotics.
|
|
|
|
"""
|
|
|
|
from decimal import Decimal
|
2015-06-15 22:10:40 +08:00
|
|
|
from functools import total_ordering
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2016-04-04 09:41:37 +08:00
|
|
|
__all__ = ['A', 'Area', 'D', 'Distance']
|
|
|
|
|
2016-12-29 23:27:49 +08:00
|
|
|
NUMERIC_TYPES = (int, float, Decimal)
|
2012-06-14 21:24:47 +08:00
|
|
|
AREA_PREFIX = "sq_"
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2012-06-14 21:24:47 +08:00
|
|
|
def pretty_name(obj):
|
|
|
|
return obj.__name__ if obj.__class__ == type else obj.__class__.__name__
|
|
|
|
|
|
|
|
|
|
|
|
@total_ordering
|
2017-01-19 15:39:46 +08:00
|
|
|
class MeasureBase:
|
2012-06-14 21:24:47 +08:00
|
|
|
STANDARD_UNIT = None
|
2013-10-22 21:31:43 +08:00
|
|
|
ALIAS = {}
|
|
|
|
UNITS = {}
|
2012-06-14 21:24:47 +08:00
|
|
|
LALIAS = {}
|
|
|
|
|
|
|
|
def __init__(self, default_unit=None, **kwargs):
|
|
|
|
value, self._default_unit = self.default_units(kwargs)
|
|
|
|
setattr(self, self.STANDARD_UNIT, value)
|
2016-12-29 23:27:49 +08:00
|
|
|
if default_unit and isinstance(default_unit, str):
|
2012-06-14 21:24:47 +08:00
|
|
|
self._default_unit = default_unit
|
|
|
|
|
|
|
|
def _get_standard(self):
|
|
|
|
return getattr(self, self.STANDARD_UNIT)
|
|
|
|
|
|
|
|
def _set_standard(self, value):
|
|
|
|
setattr(self, self.STANDARD_UNIT, value)
|
|
|
|
|
|
|
|
standard = property(_get_standard, _set_standard)
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
if name in self.UNITS:
|
|
|
|
return self.standard / self.UNITS[name]
|
|
|
|
else:
|
|
|
|
raise AttributeError('Unknown unit type: %s' % name)
|
|
|
|
|
|
|
|
def __repr__(self):
|
2016-03-29 06:33:29 +08:00
|
|
|
return '%s(%s=%s)' % (pretty_name(self), self._default_unit, getattr(self, self._default_unit))
|
2012-06-14 21:24:47 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '%s %s' % (getattr(self, self._default_unit), self._default_unit)
|
|
|
|
|
|
|
|
# **** Comparison methods ****
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if isinstance(other, self.__class__):
|
|
|
|
return self.standard == other.standard
|
|
|
|
else:
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
if isinstance(other, self.__class__):
|
|
|
|
return self.standard < other.standard
|
|
|
|
else:
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
# **** Operators methods ****
|
|
|
|
|
|
|
|
def __add__(self, other):
|
|
|
|
if isinstance(other, self.__class__):
|
2016-03-29 06:33:29 +08:00
|
|
|
return self.__class__(
|
|
|
|
default_unit=self._default_unit,
|
|
|
|
**{self.STANDARD_UNIT: (self.standard + other.standard)}
|
|
|
|
)
|
2012-06-14 21:24:47 +08:00
|
|
|
else:
|
2013-10-25 01:30:03 +08:00
|
|
|
raise TypeError('%(class)s must be added with %(class)s' % {"class": pretty_name(self)})
|
2012-06-14 21:24:47 +08:00
|
|
|
|
|
|
|
def __iadd__(self, other):
|
|
|
|
if isinstance(other, self.__class__):
|
|
|
|
self.standard += other.standard
|
|
|
|
return self
|
|
|
|
else:
|
2013-10-25 01:30:03 +08:00
|
|
|
raise TypeError('%(class)s must be added with %(class)s' % {"class": pretty_name(self)})
|
2012-06-14 21:24:47 +08:00
|
|
|
|
|
|
|
def __sub__(self, other):
|
|
|
|
if isinstance(other, self.__class__):
|
2016-03-29 06:33:29 +08:00
|
|
|
return self.__class__(
|
|
|
|
default_unit=self._default_unit,
|
|
|
|
**{self.STANDARD_UNIT: (self.standard - other.standard)}
|
|
|
|
)
|
2012-06-14 21:24:47 +08:00
|
|
|
else:
|
2013-10-25 01:30:03 +08:00
|
|
|
raise TypeError('%(class)s must be subtracted from %(class)s' % {"class": pretty_name(self)})
|
2012-06-14 21:24:47 +08:00
|
|
|
|
|
|
|
def __isub__(self, other):
|
|
|
|
if isinstance(other, self.__class__):
|
|
|
|
self.standard -= other.standard
|
|
|
|
return self
|
|
|
|
else:
|
2013-10-25 01:30:03 +08:00
|
|
|
raise TypeError('%(class)s must be subtracted from %(class)s' % {"class": pretty_name(self)})
|
2012-06-14 21:24:47 +08:00
|
|
|
|
|
|
|
def __mul__(self, other):
|
|
|
|
if isinstance(other, NUMERIC_TYPES):
|
2016-03-29 06:33:29 +08:00
|
|
|
return self.__class__(
|
|
|
|
default_unit=self._default_unit,
|
|
|
|
**{self.STANDARD_UNIT: (self.standard * other)}
|
|
|
|
)
|
2012-06-14 21:24:47 +08:00
|
|
|
else:
|
2013-10-25 01:30:03 +08:00
|
|
|
raise TypeError('%(class)s must be multiplied with number' % {"class": pretty_name(self)})
|
2012-06-14 21:24:47 +08:00
|
|
|
|
|
|
|
def __imul__(self, other):
|
|
|
|
if isinstance(other, NUMERIC_TYPES):
|
|
|
|
self.standard *= float(other)
|
|
|
|
return self
|
|
|
|
else:
|
2013-10-25 01:30:03 +08:00
|
|
|
raise TypeError('%(class)s must be multiplied with number' % {"class": pretty_name(self)})
|
2012-06-14 21:24:47 +08:00
|
|
|
|
|
|
|
def __rmul__(self, other):
|
|
|
|
return self * other
|
|
|
|
|
2012-08-08 19:43:21 +08:00
|
|
|
def __truediv__(self, other):
|
2012-06-14 21:24:47 +08:00
|
|
|
if isinstance(other, self.__class__):
|
|
|
|
return self.standard / other.standard
|
|
|
|
if isinstance(other, NUMERIC_TYPES):
|
2016-03-29 06:33:29 +08:00
|
|
|
return self.__class__(
|
|
|
|
default_unit=self._default_unit,
|
|
|
|
**{self.STANDARD_UNIT: (self.standard / other)}
|
|
|
|
)
|
2012-06-14 21:24:47 +08:00
|
|
|
else:
|
2013-10-25 01:30:03 +08:00
|
|
|
raise TypeError('%(class)s must be divided with number or %(class)s' % {"class": pretty_name(self)})
|
2012-11-04 04:43:11 +08:00
|
|
|
|
2012-08-08 19:43:21 +08:00
|
|
|
def __itruediv__(self, other):
|
2012-06-14 21:24:47 +08:00
|
|
|
if isinstance(other, NUMERIC_TYPES):
|
|
|
|
self.standard /= float(other)
|
|
|
|
return self
|
|
|
|
else:
|
2013-10-25 01:30:03 +08:00
|
|
|
raise TypeError('%(class)s must be divided with number' % {"class": pretty_name(self)})
|
2012-11-04 04:43:11 +08:00
|
|
|
|
2012-08-08 19:43:21 +08:00
|
|
|
def __bool__(self):
|
2012-06-14 21:24:47 +08:00
|
|
|
return bool(self.standard)
|
2012-11-04 04:43:11 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def default_units(self, kwargs):
|
|
|
|
"""
|
2009-05-18 00:45:28 +08:00
|
|
|
Return the unit value and the default units specified
|
2008-08-06 02:13:06 +08:00
|
|
|
from the given keyword arguments dictionary.
|
|
|
|
"""
|
|
|
|
val = 0.0
|
2012-06-14 21:24:47 +08:00
|
|
|
default_unit = self.STANDARD_UNIT
|
2017-01-07 19:11:46 +08:00
|
|
|
for unit, value in kwargs.items():
|
2013-10-17 16:17:41 +08:00
|
|
|
if not isinstance(value, float):
|
|
|
|
value = float(value)
|
2008-08-06 02:13:06 +08:00
|
|
|
if unit in self.UNITS:
|
|
|
|
val += self.UNITS[unit] * value
|
|
|
|
default_unit = unit
|
|
|
|
elif unit in self.ALIAS:
|
|
|
|
u = self.ALIAS[unit]
|
|
|
|
val += self.UNITS[u] * value
|
|
|
|
default_unit = u
|
|
|
|
else:
|
|
|
|
lower = unit.lower()
|
|
|
|
if lower in self.UNITS:
|
|
|
|
val += self.UNITS[lower] * value
|
|
|
|
default_unit = lower
|
|
|
|
elif lower in self.LALIAS:
|
|
|
|
u = self.LALIAS[lower]
|
|
|
|
val += self.UNITS[u] * value
|
|
|
|
default_unit = u
|
|
|
|
else:
|
|
|
|
raise AttributeError('Unknown unit type: %s' % unit)
|
|
|
|
return val, default_unit
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def unit_attname(cls, unit_str):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Retrieve the unit attribute name for the given unit string.
|
|
|
|
For example, if the given unit string is 'metre', return 'm'.
|
|
|
|
Raise an exception if an attribute cannot be found.
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
|
|
|
lower = unit_str.lower()
|
|
|
|
if unit_str in cls.UNITS:
|
|
|
|
return unit_str
|
|
|
|
elif lower in cls.UNITS:
|
|
|
|
return lower
|
|
|
|
elif lower in cls.LALIAS:
|
|
|
|
return cls.LALIAS[lower]
|
|
|
|
else:
|
|
|
|
raise Exception('Could not find a unit keyword associated with "%s"' % unit_str)
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
class Distance(MeasureBase):
|
2012-06-14 21:24:47 +08:00
|
|
|
STANDARD_UNIT = "m"
|
2008-08-06 02:13:06 +08:00
|
|
|
UNITS = {
|
2013-10-27 09:27:42 +08:00
|
|
|
'chain': 20.1168,
|
|
|
|
'chain_benoit': 20.116782,
|
|
|
|
'chain_sears': 20.1167645,
|
|
|
|
'british_chain_benoit': 20.1167824944,
|
|
|
|
'british_chain_sears': 20.1167651216,
|
|
|
|
'british_chain_sears_truncated': 20.116756,
|
|
|
|
'cm': 0.01,
|
|
|
|
'british_ft': 0.304799471539,
|
|
|
|
'british_yd': 0.914398414616,
|
|
|
|
'clarke_ft': 0.3047972654,
|
|
|
|
'clarke_link': 0.201166195164,
|
2013-11-03 03:37:48 +08:00
|
|
|
'fathom': 1.8288,
|
2008-08-06 02:13:06 +08:00
|
|
|
'ft': 0.3048,
|
2019-04-09 02:17:05 +08:00
|
|
|
'furlong': 201.168,
|
2013-10-27 09:27:42 +08:00
|
|
|
'german_m': 1.0000135965,
|
|
|
|
'gold_coast_ft': 0.304799710181508,
|
|
|
|
'indian_yd': 0.914398530744,
|
|
|
|
'inch': 0.0254,
|
2008-08-06 02:13:06 +08:00
|
|
|
'km': 1000.0,
|
2013-10-27 09:27:42 +08:00
|
|
|
'link': 0.201168,
|
|
|
|
'link_benoit': 0.20116782,
|
|
|
|
'link_sears': 0.20116765,
|
2008-08-06 02:13:06 +08:00
|
|
|
'm': 1.0,
|
|
|
|
'mi': 1609.344,
|
2013-10-27 09:27:42 +08:00
|
|
|
'mm': 0.001,
|
2008-08-06 02:13:06 +08:00
|
|
|
'nm': 1852.0,
|
2013-10-27 09:27:42 +08:00
|
|
|
'nm_uk': 1853.184,
|
|
|
|
'rod': 5.0292,
|
|
|
|
'sears_yd': 0.91439841,
|
|
|
|
'survey_ft': 0.304800609601,
|
|
|
|
'um': 0.000001,
|
2008-08-06 02:13:06 +08:00
|
|
|
'yd': 0.9144,
|
2013-10-18 17:02:43 +08:00
|
|
|
}
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Unit aliases for `UNIT` terms encountered in Spatial Reference WKT.
|
|
|
|
ALIAS = {
|
2013-10-27 09:27:42 +08:00
|
|
|
'centimeter': 'cm',
|
|
|
|
'foot': 'ft',
|
|
|
|
'inches': 'inch',
|
|
|
|
'kilometer': 'km',
|
|
|
|
'kilometre': 'km',
|
|
|
|
'meter': 'm',
|
|
|
|
'metre': 'm',
|
|
|
|
'micrometer': 'um',
|
|
|
|
'micrometre': 'um',
|
|
|
|
'millimeter': 'mm',
|
|
|
|
'millimetre': 'mm',
|
|
|
|
'mile': 'mi',
|
|
|
|
'yard': 'yd',
|
|
|
|
'British chain (Benoit 1895 B)': 'british_chain_benoit',
|
|
|
|
'British chain (Sears 1922)': 'british_chain_sears',
|
|
|
|
'British chain (Sears 1922 truncated)': 'british_chain_sears_truncated',
|
|
|
|
'British foot (Sears 1922)': 'british_ft',
|
|
|
|
'British foot': 'british_ft',
|
|
|
|
'British yard (Sears 1922)': 'british_yd',
|
|
|
|
'British yard': 'british_yd',
|
|
|
|
"Clarke's Foot": 'clarke_ft',
|
|
|
|
"Clarke's link": 'clarke_link',
|
|
|
|
'Chain (Benoit)': 'chain_benoit',
|
|
|
|
'Chain (Sears)': 'chain_sears',
|
|
|
|
'Foot (International)': 'ft',
|
2019-04-09 02:17:05 +08:00
|
|
|
'Furrow Long': 'furlong',
|
2013-10-27 09:27:42 +08:00
|
|
|
'German legal metre': 'german_m',
|
|
|
|
'Gold Coast foot': 'gold_coast_ft',
|
|
|
|
'Indian yard': 'indian_yd',
|
2008-08-06 02:13:06 +08:00
|
|
|
'Link (Benoit)': 'link_benoit',
|
|
|
|
'Link (Sears)': 'link_sears',
|
2013-10-27 09:27:42 +08:00
|
|
|
'Nautical Mile': 'nm',
|
|
|
|
'Nautical Mile (UK)': 'nm_uk',
|
|
|
|
'US survey foot': 'survey_ft',
|
|
|
|
'U.S. Foot': 'survey_ft',
|
|
|
|
'Yard (Indian)': 'indian_yd',
|
|
|
|
'Yard (Sears)': 'sears_yd'
|
2013-10-18 17:02:43 +08:00
|
|
|
}
|
2014-12-07 05:00:09 +08:00
|
|
|
LALIAS = {k.lower(): v for k, v in ALIAS.items()}
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def __mul__(self, other):
|
2012-06-14 21:24:47 +08:00
|
|
|
if isinstance(other, self.__class__):
|
2016-03-29 06:33:29 +08:00
|
|
|
return Area(
|
|
|
|
default_unit=AREA_PREFIX + self._default_unit,
|
|
|
|
**{AREA_PREFIX + self.STANDARD_UNIT: (self.standard * other.standard)}
|
|
|
|
)
|
2012-06-14 21:24:47 +08:00
|
|
|
elif isinstance(other, NUMERIC_TYPES):
|
2016-03-29 06:33:29 +08:00
|
|
|
return self.__class__(
|
|
|
|
default_unit=self._default_unit,
|
|
|
|
**{self.STANDARD_UNIT: (self.standard * other)}
|
|
|
|
)
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
2012-06-14 21:24:47 +08:00
|
|
|
raise TypeError('%(distance)s must be multiplied with number or %(distance)s' % {
|
2013-10-25 01:30:03 +08:00
|
|
|
"distance": pretty_name(self.__class__),
|
2013-10-18 17:02:43 +08:00
|
|
|
})
|
2009-12-18 02:21:30 +08:00
|
|
|
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
class Area(MeasureBase):
|
2012-06-14 21:24:47 +08:00
|
|
|
STANDARD_UNIT = AREA_PREFIX + Distance.STANDARD_UNIT
|
2008-08-06 02:13:06 +08:00
|
|
|
# Getting the square units values and the alias dictionary.
|
2014-12-07 05:00:09 +08:00
|
|
|
UNITS = {'%s%s' % (AREA_PREFIX, k): v ** 2 for k, v in Distance.UNITS.items()}
|
|
|
|
ALIAS = {k: '%s%s' % (AREA_PREFIX, v) for k, v in Distance.ALIAS.items()}
|
|
|
|
LALIAS = {k.lower(): v for k, v in ALIAS.items()}
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-08-08 19:43:21 +08:00
|
|
|
def __truediv__(self, other):
|
2012-06-14 21:24:47 +08:00
|
|
|
if isinstance(other, NUMERIC_TYPES):
|
2016-03-29 06:33:29 +08:00
|
|
|
return self.__class__(
|
|
|
|
default_unit=self._default_unit,
|
|
|
|
**{self.STANDARD_UNIT: (self.standard / other)}
|
|
|
|
)
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
2013-10-25 01:30:03 +08:00
|
|
|
raise TypeError('%(class)s must be divided by a number' % {"class": pretty_name(self)})
|
2012-11-04 04:43:11 +08:00
|
|
|
|
2009-12-18 02:21:30 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Shortcuts
|
|
|
|
D = Distance
|
|
|
|
A = Area
|