短距離條件下,由經緯度獲取距離的近似計算公式

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)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章