2017-04-10 12 views
4

からPythonは - だから私はこのコードを持っている...私は一体これと間違っているかわからないが、私は全くそれを把握することはできませんGPSの経度と緯度

を総距離を取得します。

from model.Formulas import Formulas 

f = open("coords_data.txt", "r") 
line1 = f.readline() 
line2 = f.readline() 

orig = line1.split(';') 
dest = line2.split(';') 

origin = (orig[0] + ", " + orig[1].strip("\n")) 
destination = (dest[0] + ", " + dest[1].strip("\n")) 

print("Orig: " + str(origin)) 
print("Dest: " + str(destination)) 

total_dist = Formulas.calculateDistance(str(origin), str(destination)) 

# Formulas.calculateDistance() 

[インポートコードはこれです:

import math 

class Formulas: 
    # 3959 # radius of the great circle in miles...some algorithms use 3956 
    # 6371 # radius in kilometers...some algorithms use 6367 
    # 3959 * 5280 # radius in feet 
    # 6371 * 1000 # radius in meters 
    @staticmethod 
    def calculateDistance(origin, destination, rounding=0): 
     lat1, lon1 = origin 
     lat2, lon2 = destination 
     radius = 6371 # km 

     dlat = math.radians(lat2 - lat1) 
     dlon = math.radians(lon2 - lon1) 
     a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2) 
     c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a)) 
     d = radius * c 
     return round(d, rounding) 

だから今、私は正確な総距離を持っている座標の大規模なリスト(5057行)から取得したいです。したがって、距離間のすべての差を数え、1つの大きな数値(たとえば150km)を返す必要があります。

私が手にエラーがある。このようなファイルを見て内

ValueError: too many values to unpack (expected 2)

座標:

5114.8268;00457.9847 
5114.8271;00457.9845 
5114.8271;00457.9845 
5114.8271;00457.9845 
5114.8270;00457.9846 
5114.8271;00457.9846 
5114.8272;00457.9847 
5114.8272;00457.9847 
5114.8274;00457.9843 
5114.8272;00457.9846 
5114.8274;00457.9843 
5114.8277;00457.9837 
5114.8287;00457.9835 
5114.8274;00457.9843 
5114.8288;00457.9831 
5114.8287;00457.9835 
5114.8286;00457.9813 
5114.8274;00457.9843 
5114.8287;00457.9815 
5114.8286;00457.9813 
5114.8270;00457.9846 
5114.8286;00457.9813 
5114.8355;00457.9784 
5114.8292;00457.9814 
5114.8274;00457.9843 
5114.8376;00457.9776 
5114.8395;00457.9769 

これは、ファイルに今だが、このデータはデータベースに保存されます。

どうすればこの問題を解決できますか?そして、私はどのようにエラーを取り除くべきですか?

+0

どのようなエラーがありますか?入力/出力と予想出力の簡単な例を挙げてください。 – Scheme

+0

更新しました。申し訳ありませんが、エラーを配置するのを忘れてしまいました。 0秒前に – Robin

答えて

3

Formulas.calculateDistance()は、山車のタプルを期待:

line1 = "5114.8268;00457.9847" 
line2 = "5114.8271;00457.9845" 

def strip_line(line_str): 
    x, y = line_str.split(';') 
    return float(x), float(y.strip()) 

total_dist = Formulas.calculateDistance(
    strip_line(line1), strip_line(line2)) 

機能strip_line()は、使用しているのと同じ基本的なロジックを使用していますが、関数内の論理アップをラップ、そして最も重要なこと:

はこれを試してみてください値を浮動小数点として保持します。

+0

に回答しました。ダン、私の編集者はスタートアップが遅かった。 – Scheme

+0

Okej、それは動作しますが、どのようにしてすべての値をループすることができますか?それで、いつもうまくいくでしょうし、正しい距離を教えてくれますか? – Robin

関連する問題