Re: Haversine-Formel liefert falsche Werte
Verfasst: Donnerstag 9. Mai 2019, 12:50
@Strawk: Wo kommt denn die zweite Zeile her? Oder wo sind die Zeilen dazwischen geblieben?
Seit 2002 Diskussionen rund um die Programmiersprache Python
https://www.python-forum.de/
Code: Alles auswählen
coords = get_airport_positions("data\\airports.dat", "All")
counter = 0
# print(len(coords))
for i in range(7184):
coord0 = coords[i]
coords = np.array(coords[i+1:]) # slicing von i+1 bis Ende Array
Mehr ist da nicht, alles andere ist auskommentiert.IndexError: index 119 is out of bounds for axis 0 with size 44
Grüße(7184, 2)
Code: Alles auswählen
coords = get_airport_positions("data\\airports.dat", "All")
coords = np.array(coords)
counter = 0
# print(len(coords))
print(coords.shape)
for i in range(10):
coord0 = coords[i]
coords = coords[i+1:] # slicing von i+1 bis Ende Array
print(len(coords))
Verkürzt sich in immer größeren Schritten, statt 1er-Schritten. Ich habe wohl ein größeres Brett vorm Kopf.(7184, 2)
7183
7181
7178
7174
7169
7163
7156
7148
7139
7129
Code: Alles auswählen
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 26 10:45:34 2019
@author: Karl Kraft
"""
from __future__ import print_function, division
import numpy as np
from distances_airports_worldwide import get_airport_positions
import time
def haversine(lat1, lat2, lon1, lon2):
""" calculates the distance in meters between geographical points
"""
# in: 2 pairs of coordinates
# out: distance in meters of them
# Haversine formula
lat1 = np.radians(lat1)
lat2 = np.radians(lat2)
lon1 = np.radians(lon1)
lon2 = np.radians(lon2)
leftParenthesis = ((lat2 - lat1)/2)
rightParenthesis = ((lon2 - lon1)/2)
allUnderSquareRoot = ((np.sin(leftParenthesis))**2) + np.cos(lat1)*np.cos(lat2)*(np.sin(rightParenthesis))**2
squareRootResult = np.sqrt(allUnderSquareRoot)
arcSin = np.arcsin(squareRootResult)
middleEarthRadiusKm = 6371.009
middleEarthRadiusM = middleEarthRadiusKm * 1000
distancesMeters = 2 * middleEarthRadiusM * arcSin
return distancesMeters
def haversine_vectorized(coord0, coords):
""" prepares data vor vectorized use of haversine-function
"""
# in: one tupel of first 2 coordinates, list of all remainung coordinates
# out: vectorized result, which is list of kilometers
lat0, lon0 = coord0
lats = coords[:,0] # 1. Spalte von coords (= array von latitudes)
lons = coords[:,1] # 2. Spalte von coords (= array von longitudes)
result_vectorized = haversine(lat0, lats, lon0, lons)
return result_vectorized
def calc_dist_all2all_airports(airport_file, country):
""" calculates the distances of each airport to each and adds them
"""
# in: the file where the airport data are stored, the desired country
# out: added kilometers of all the distances, number of calculations done
coords = get_airport_positions(airport_file, country)
coords = np.array(coords)
coordsOneValue = coords
multipleKilometers = []
counter = 0
for i in range(len(coords)):
coord0 = coordsOneValue[i]
coords = coords[i+1:] # slicing from i+1 until end array
result_vectorized = haversine_vectorized(coord0, coords) / 1000
multipleKilometers.extend(result_vectorized)
counter += 1
sumKilometers = sum(multipleKilometers)
nCalculations = counter
return sumKilometers, nCalculations
# number of airports: 7184
if __name__ == "__main__":
t0 = time.clock()
sumKilometers, nCalculations = calc_dist_all2all_airports("data\\airports.dat", country="All")
print("sum kilometers: %10.1f" % (sumKilometers))
print("number of calculations done: %10.f" % (nCalculations))
print("Computation of distances took %6.2f secs" % (time.clock()-t0))
Code: Alles auswählen
EARTH_RADIUS_IN_M = 63710090
def haversine(lat1, lat2, lon1, lon2):
""" calculates the distance in meters between geographical points
in: 2 pairs of coordinates in radians
out: distance in meters of them
"""
mean_lat = (lat2 - lat1) * 0.5
mean_lon = (lon2 - lon1) * 0.5
a = np.sin(mean_lat)**2 + np.cos(lat1)*np.cos(lat2)* np.sin(mean_lon)**2
return 2 * EARTH_RADIUS_IN_M * np.arcsin(a**0.5)
def calc_dist_all2all_airports(airport_file, country):
""" calculates the distances of each airport to each and adds them
in: the file where the airport data are stored, the desired country
out: added kilometers of all the distances, number of calculations done
"""
coords = get_airport_positions(airport_file, country)
lat, lon = np.radians(coords).T
distances = haversine(lat[:, None], lon[:, None], lat[None, :], lon[None, :])
return distances.sum()/2, len(coords)
# number of airports: 7184
if __name__ == "__main__":
t0 = time.clock()
total_distance, number_of_airports = calc_dist_all2all_airports("data\\airports.dat", country="All")
print("sum of kilometers: %10.1f" % total_distance)
print("number of airports: %10d" % number_of_airports)
print("Computation of distances took %6.2f secs" % (time.clock()-t0))