def compare_coordinates(cls, coords1, coords2, *args, tolerance=None,
unit='km', **kwargs):
"""
Check if a distance between the pairs of coordinates provided is
within the specified tolerance.
Return True if yes, otherwise return False.
Use geopy (https://pypi.python.org/pypi/geopy).
Try to use Vincenty formula, if error occurs use Great Circle formula.
:param coords1: pair of coordinates - a tuple of two numbers
:param coords2: pair of coordinates - a tuple of two numbers
:param tolerance: number
:param unit: str, one of: 'kilometers', 'km', 'meters',
'm', 'miles', 'mi', 'feet',
'ft', 'nautical', 'nm'
:rtype: bool
:Example:
>>> a, b = (36.1332600, -5.4505100), (35.8893300, -5.3197900)
>>> MatchBlock.compare_coordinates(a, b, tolerance=20, unit='mi')
True
>>> a, b = (36.1332600, -5.4505100), (35.8893300, -5.3197900)
>>> MatchBlock.compare_coordinates(a, b, tolerance=1, unit='mi')
False
"""
if tolerance is None:
tolerance = cls.coordinates_tolerance
units = {'kilometers', 'km', 'meters', 'm', 'miles', 'mi',
'feet', 'ft', 'nautical', 'nm'}
unit = unit.strip().lower()
if unit not in units:
raise ValueError('unsupported unit')
try:
length = getattr(vincenty(coords1, coords2, *args, **kwargs), unit)
except ValueError as e:
if 'Vincenty formula failed to converge!' in e.args:
warnings.warn(
'vincenty formula failed, using great circle formula')
length = getattr(great_circle(coords1, coords2, *args), unit)
else:
raise
return length <= tolerance
评论列表
文章目录