地理空间计算, geographical computation

GPS坐标

国际惯用 (维度,经度).degree 格式、

两点间的距离计算

三方库

from geopy import distance


class gps:
    def __init__(self, latitude, longitude):
        self.latitude = latitude
        self.longitude = longitude

    def to_tuple(self):
        return self.latitude, self.longitude


阿里巴巴西溪园区 = gps(30.27884, 120.02631)
杭州东站 = gps(30.29018, 120.21398)

# 18.09941369613151 km
print(distance.distance(阿里巴巴西溪园区.to_tuple(), 杭州东站.to_tuple()))

自己实现

import numpy as np


class StarGEO:
    def __init__(self):
        self.EARTH_RADIUS = 6378.137

    def rad(self, degree):
        """
        converts degree to radian
        :param degree:
        :return:
        """
        return degree * np.pi / 180

    def get_geodesic_distance(self, gps_A, gps_B):
        """

        :param gps_A: a tuple,(latitude,longitude)
        :param gps_B: same as above
        :return:
        """
        rad_lat_A = self.rad(gps_A[0])
        rad_lat_B = self.rad(gps_B[0])
        rad_lon_A = self.rad(gps_A[1])
        rad_lon_B = self.rad(gps_B[1])
        delta_lat = rad_lat_A - rad_lat_B
        delta_lon = rad_lon_A - rad_lon_B
        dis = 2 * np.arcsin(
            np.sqrt(
                np.power(
                    np.sin(delta_lat / 2),
                    2) + np.cos(rad_lat_A) * np.cos(rad_lat_B) * np.power(np.sin(delta_lon / 2),
                                                                          2)
            )
        )
        return dis * self.EARTH_RADIUS


alibaba = (30.27884, 120.02631)
railway_station = (30.29018, 120.21398)
# 18.084439851016995 Km
print(StarGEO().get_geodesic_distance(alibaba, railway_station),'Km')

你可能感兴趣的:(其他)