短距离条件下,由经纬度获取距离的近似计算公式

import math
def rad(d):
  return (d * math.pi) / 180

def distance(longitude1, latitude1, longitude2, latitude2):
  l1 = rad(latitude1)
  l2 = rad(latitude2)
  a = l1 - l2
  b = rad(longitude1) - rad(longitude2)
  s = 2 * math.asin(math.sqrt((math.sin(a / 2) ** 2) + (math.cos(l1) * math.cos(l2) * (math.sin(b / 2) ** 2))))
  s *= 6378137.0
  return s


def distance2(longitude1, latitude1):
  longitude2=longitude1+0.2 
  latitude2=latitude1+0.2
  return distance(longitude1, latitude1, longitude2, latitude2)

print distance(113,39,114,39.5)
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

# 以北京为例,观察经纬度与距离的关系,10公里范围内,基本可以认为近似为线性关系
def bjdis():
    X0 = 113
    Y0 = 39
    X = np.arange(112.98, 113.02, 0.001)
    Y = np.arange(38.98, 39.02, 0.001)
    X, Y = np.meshgrid(X, Y)
    dfn=np.frompyfunc(distance, 4, 1)
    Z = dfn(X0, Y0, X,Y)
    return X,Y,Z

# 中国经纬范围,经纬度相差0.2的情况下,不同经纬度的距离差值,近似为2次线性
def chinadis():
    X = np.arange(73, 135, 0.1)
    Y = np.arange(3, 54, 0.1)
    X, Y = np.meshgrid(X, Y)
    dfn=np.frompyfunc(distance2, 2, 1)
    Z = dfn(X,Y)
    return X,Y,Z

X,Y,Z=chinadis()
#X,Y,Z=bjdis()
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)

ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

# Add a color bar which maps values to colors.
plt.show()

# 获取线性系数
print "fit..."
Y = np.arange(3, 54, 0.1)
dfn=np.frompyfunc(distance2, 2, 1)
Z=dfn(113,Y)
print np.polyfit(Y, Z, 3)

X = np.arange(113, 113.05, 0.00001)
dfn=np.frompyfunc(distance, 4, 1)
Z=dfn(113,39,X,39)
print np.polyfit(X-113, Z, 3)

 

你可能感兴趣的:(算法,python)