Skip to content

Commit

Permalink
Fixes station distance and bundeslaender
Browse files Browse the repository at this point in the history
  • Loading branch information
FL550 committed Oct 18, 2023
1 parent 8674b96 commit 359de2a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 14 deletions.
59 changes: 54 additions & 5 deletions development/generate_stations.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
import datetime
import requests
from requests.exceptions import Timeout
from bs4 import BeautifulSoup
import json
import re
import pandas as pd
import math


def get_distance(lat, lon, _lat, _lon):
"""Calculate the distance between two points. Result is returned in km."""

lon_diff = 111.3 * math.cos((lat + _lat) / 2 * 0.01745) * (lon - _lon)
lat_diff = 111.3 * (lat - _lat)

return round(math.sqrt(math.pow(lon_diff, 2) + math.pow(lat_diff, 2)), 1)


def get_bundesland_with_smallest_distance(station, bundeslaender):
smallest_distance = float("inf")
smallest_distance_bundesland = ""
for key in bundeslaender:
coords = key.split(";")
lat_points = coords[0].split(".")
lat = float(lat_points[0])
if len(lat_points) > 1:
lat = lat + float(lat_points[1]) / 60
lat = round(lat, 3)

lon_points = coords[1].split(".")
lon = float(lon_points[0])
if len(lon_points) > 1:
lon = lon + float(lon_points[1]) / 60
lon = round(lon, 3)

temp_distance = get_distance(station["lat"], station["lon"], lat, lon)
if temp_distance < smallest_distance:
smallest_distance = temp_distance
smallest_distance_bundesland = bundeslaender[key]
return smallest_distance_bundesland if smallest_distance < 50 else ""


print("Retrieving MOSMIX stations catalogue...")
while True:
Expand Down Expand Up @@ -34,6 +66,7 @@
with open("development/bundeslaender.json", "r", encoding="utf-8") as f:
bundeslaender = json.load(f)

print("Parsing MOSMIX...")
for i in range_iter:
if first_run:
mosmix_data[i] += f" POI REGION"
Expand All @@ -50,18 +83,34 @@
"report_available": 1 if group_id in poi_links else 0,
"bundesland": "",
}
lat = round(
float(group("lat").split(".")[0]) + float(group("lat").split(".")[1]) / 60, 3
)
lon = round(
float(group("lon").split(".")[0]) + float(group("lon").split(".")[1]) / 60, 3
)
station.update(
{
"name": group("name").title(),
"lat": group("lat"),
"lon": group("lon"),
"lat": lat,
"lon": lon,
"elev": group("elev"),
"bundesland": bundeslaender[f'{group("lat")};{group("lon")}']
if f'{group("lat")};{group("lon")}' in bundeslaender
else "",
}
)
stations[group_id] = station
print("Done.")

print("Adding missing bundeslaender...")
# Add bundesland for those missing
for key, station in stations.items():
if station["bundesland"] == "":
station["bundesland"] = get_bundesland_with_smallest_distance(
station, bundeslaender
)
print("Done.")

with open("simple_dwd_weatherforecast/stations.json", "w", encoding="utf-8") as f:
json.dump(stations, f, ensure_ascii=False)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="simple_dwd_weatherforecast",
version="2.0.20",
version="2.0.21",
author="Max Fermor",
description="A simple tool to retrieve a weather forecast from DWD OpenData",
long_description=long_description,
Expand Down
2 changes: 1 addition & 1 deletion simple_dwd_weatherforecast/stations.json

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions tests/test_location_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ class LocationToolsTestCase(unittest.TestCase):
def test_get_nearest_station_id(self):
self.assertEqual(
dwdforecast.get_nearest_station_id(50.291472, 8.607336),
"L643",
"L732",
"Wrong nearest station",
)
self.assertEqual(
dwdforecast.get_nearest_station_id(50.357, 8.751),
"L543",
"L635",
"Wrong nearest station",
)
self.assertEqual(
dwdforecast.get_nearest_station_id(51.290303, 6.763528),
"10400",
"Wrong nearest station",
)

Expand Down
12 changes: 7 additions & 5 deletions tests/test_stationsfile.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import random
import unittest

from simple_dwd_weatherforecast import dwdforecast
import json


Expand Down Expand Up @@ -29,6 +27,10 @@ def test_contains(self):
self.assertTrue("48564" in self.stations)

def test_correct_mapping(self):
self.assertTrue(self.stations["H721"]["name"] == "Bedburg-Weiler Hohen")
self.assertTrue(self.stations["78458"]["name"] == "Puerto Plata")
self.assertTrue(self.stations["H361"]["name"] == "Beckum-Unterberg")
self.assertEqual(self.stations["H721"]["name"], "Bedburg-Weiler Hohen")
self.assertEqual(self.stations["78458"]["name"], "Puerto Plata")
self.assertEqual(self.stations["H361"]["name"], "Beckum-Unterberg")

def test_bundesland(self):
self.assertEqual(self.stations["10452"]["bundesland"], "NI")
self.assertEqual(self.stations["10147"]["bundesland"], "HH")

0 comments on commit 359de2a

Please sign in to comment.