_matchblock.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:matchtools 作者: matchtools 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号