Skip to content

Commit

Permalink
Fix distance calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
FL550 committed Jul 1, 2023
1 parent c53abe1 commit b659127
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
22 changes: 12 additions & 10 deletions simple_dwd_weatherforecast/dwdforecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,18 @@ def get_stations_sorted_by_distance(lat: float, lon: float):
"""
result = []
for station in stations.items():
_lat = station[1]["lat"].split(".")
if len(_lat) == 2:
_lat = round(float(_lat[0]) + float(_lat[1]) / 60, 2)
else:
_lat = float(_lat[0])
_lon = station[1]["lon"].split(".")
if len(_lon) == 2:
_lon = round(float(_lon[0]) + float(_lon[1]) / 60, 2)
else:
_lon = float(_lon[0])
_lat = float(station[1]["lat"])
_lon = float(station[1]["lon"])
# _lat = station[1]["lat"].split(".")
# if len(_lat) == 2:
# _lat = round(float(_lat[0]) + float(_lat[1]) / 60, 2)
# else:
# _lat = float(_lat[0])
# _lon = station[1]["lon"].split(".")
# if len(_lon) == 2:
# _lon = round(float(_lon[0]) + float(_lon[1]) / 60, 2)
# else:
# _lon = float(_lon[0])
distance_temp = get_distance(lat, lon, _lat, _lon)
result.append([station[0], distance_temp])
result.sort(key=lambda x: x[1])
Expand Down
11 changes: 8 additions & 3 deletions tests/test_location_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
class LocationToolsTestCase(unittest.TestCase):
def test_get_nearest_station_id(self):
self.assertEqual(
dwdforecast.get_nearest_station_id(50.272388, 8.645408),
"N4333",
dwdforecast.get_nearest_station_id(50.291472, 8.607336),
"L732",
"Wrong nearest station",
)
self.assertEqual(
dwdforecast.get_nearest_station_id(50.357, 8.751),
"L635",
"Wrong nearest station",
)

def test_stations_ordered_by_distance(self):
list = dwdforecast.get_stations_sorted_by_distance(51.272388, 8.645408)
list = dwdforecast.get_stations_sorted_by_distance(51.291472, 8.607336)
self.assertGreater(
list[1][1],
list[0][1],
Expand Down

0 comments on commit b659127

Please sign in to comment.