2012-01-13 7 views
8

私はユーザーから2つの地理座標を取得し、それらの間の距離を計算する(地球の曲率を考慮して)小さなプログラムを設定しています。だから私は式がhereであることをウィキペディアで調べました。地理的距離を計算するのに助けが必要です

私は基本的にそれに基づいて、私のPythonの関数を設定し、これは私が思い付いたものです:

def geocalc(start_lat, start_long, end_lat, end_long): 
    start_lat = math.radians(start_lat) 
    start_long = math.radians(start_long) 
    end_lat = math.radians(end_long) 
    end_long = math.radians(end_long) 

    d_lat = start_lat - end_lat 
    d_long = start_long - end_long 

    EARTH_R = 6372.8 

    c = math.atan((math.sqrt((math.cos(end_lat)*d_long)**2 +((math.cos(start_lat)*math.sin(end_lat)) - (math.sin(start_lat)*math.cos(end_lat)*math.cos(d_long)))**2))/((math.sin(start_lat)*math.sin(end_lat)) + (math.cos(start_lat)*math.cos(end_lat)*math.cos(d_long)))) 

    return EARTH_R*c 

問題は結果が本当に不正確出てくるということです。私はPythonの初心者ですので、いくつかの助けやアドバイスをいただければ幸いです!私は(d_long)あなたはmath.sinを逃したと思います https://pypi.python.org/pypi/geopy

+2

は、具体的な例(入力、期待される出力、実際の出力)を与えてください。 –

+0

これらの座標(-6.508,55.071)と(-8.886,51.622)を入力しました。 予想される出力は414Kmでした。実際の結果は6473Kmでした – Darkphenom

+0

これは間違っています、これは間違っています><不正確かもしれません420km – hochl

答えて

10

(1)end_lat = math.radians(end_long)end_lat = math.radians(end_lat)

でなければなりません(2)あなたが誰かのようにいくつかのものが欠けています

(3)あなたのコードが読みにくくなるため、すでにおそらく、言及した(ラインあまりにも長い間、冗長な括弧の17の無意味なインスタンス「数学。」)

(4)あなたは(6)delta(latitude)が不必要に計算され

をあなたの座標を入力するとき(5)あなたは緯度と経度を交換された可能性がありatan2()

の使用に関するWikipediaの記事に発言に気付きませんでした。それはすべて一緒にそれを置く式

には表示されません。

from math import radians, sqrt, sin, cos, atan2 

def geocalc(lat1, lon1, lat2, lon2): 
    lat1 = radians(lat1) 
    lon1 = radians(lon1) 
    lat2 = radians(lat2) 
    lon2 = radians(lon2) 

    dlon = lon1 - lon2 

    EARTH_R = 6372.8 

    y = sqrt(
     (cos(lat2) * sin(dlon)) ** 2 
     + (cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dlon)) ** 2 
     ) 
    x = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(dlon) 
    c = atan2(y, x) 
    return EARTH_R * c 



>>> geocalc(36.12, -86.67, 33.94, -118.40) 
2887.2599506071115 
>>> geocalc(-6.508, 55.071, -8.886, 51.622) 
463.09798886300376 
>>> geocalc(55.071, -6.508, 51.622, -8.886) 
414.7830891822618 
+0

これらの問題をすべて指摘していただきありがとうございます。それは確かに私がやったことよりもはるかにクリーンに見え、それは動作します! – Darkphenom

4

あなたは下のリンクで「計算距離」までスクロールし、距離計算のための組み込み関数を持っているgeopyモジュールを使用することができます

c = math.atan((math.sqrt((math.cos(end_lat)*math.sin(d_long))**2 +((math.cos(start_lat)*math.sin(end_lat)) - (math.sin(start_lat)*math.cos(end_lat)*math.cos(d_long)))**2))/((math.sin(start_lat)*math.sin(end_lat)) + (math.cos(start_lat)*math.cos(end_lat)*math.cos(d_long)))) 
+0

これは良い提案ですが、OPの目標を達成する方法についていくつかのコードを含めることができれば。 – MERose

3

+0

あなたは正しいです。しかし、私はまだ完全に間違った答えを得ています。 – Darkphenom

4

これは動作します(印刷fはhttp://en.wikipedia.org/wiki/Great-circle_distance @加工した例に従って2887.26キロを返す):

import math 

def geocalc(start_lat, start_long, end_lat, end_long): 

    start_lat = math.radians(start_lat) 
    start_long = math.radians(start_long) 
    end_lat = math.radians(end_lat) 
    end_long = math.radians(end_long) 

    d_lat = math.fabs(start_lat - end_lat) 
    d_long = math.fabs(start_long - end_long) 

    EARTH_R = 6372.8 

    y = ((math.sin(start_lat)*math.sin(end_lat)) + (math.cos(start_lat)*math.cos(end_lat)*math.cos(d_long))) 

    x = math.sqrt((math.cos(end_lat)*math.sin(d_long))**2 + ((math.cos(start_lat)*math.sin(end_lat)) - (math.sin(start_lat)*math.cos(end_lat)*math.cos(d_long)))**2) 

    c = math.atan(x/y) 

    return EARTH_R*c 

f = geocalc(36.12, -86.67, 33.94, -118.40) 
print f 
最初の方に、多分これでなければなりませんあなたの投稿で

お知らせこの行を:あなたは4つのまたは5または6の問題を持っているend_lat = math.radians(end_long)

+0

'fabs()'は不要です。 'cos(-x)== cos(x)' –

+0

-1 'atan2'の代わりに' atan'を使っているので、これは非常に間違っています。例:南緯45度から-45度までの南緯は10010キロ(OK)ですが、45.1度から-45.1度(わずかに長い距離)はマイナス9988キロメートルです。北極から南極への旅はゼロ・キロを生む! –

+0

@ JohnMachinは、この特定の実装には正しいというfabs()のコメントについては言及していますが、デルタの概念を教えるときには、絶対値の使用は簡単な減算ではなく正しいものです。 – sgallen

関連する問題