From b65496f64eb2b2ac92b3c70600af8c85ed627f1d Mon Sep 17 00:00:00 2001 From: stianchris Date: Mon, 21 Oct 2019 14:15:14 +0200 Subject: [PATCH] added an optional buffer-zone to nearest_station The buffer zone let's you get more stations, that are nearby and not just the nearest. It's optional and won't change existing methods. Output is a list of stations. --- dwdweather/core.py | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/dwdweather/core.py b/dwdweather/core.py index 93a707c..cad847e 100644 --- a/dwdweather/core.py +++ b/dwdweather/core.py @@ -562,8 +562,31 @@ def station_info(self, station_id): c.execute(sql, (station_id,)) return c.fetchone() - def nearest_station(self, lon, lat): - # Select most current stations datasets. + def nearest_station(self, lon, lat, surrounding=False): + """ + Select most current stations datasets. + + Parameters: + ---------- + + lon : float + + lat : float + + surrounding : float + adds a buffer-zone in meter on top of the most closest + distance, and returns a list with all stations inside this zone + (instead of just one station) + + Example: + -------- + + >>> from dwdweather import DwdWeather + >>> dw = DwdWeather(resolution="hourly") + >>> dw.nearest_station(lon=7.0, lat=51.0, surrounding=10000) + + """ + closest = None closest_distance = 99999999999 for station in self.stations(): @@ -573,6 +596,19 @@ def nearest_station(self, lon, lat): if d < closest_distance: closest = station closest_distance = d + + if surrounding: + closest1 = [] + closest_distance = closest_distance+surrounding + i = 0 + for station in self.stations(): + d = self.haversine_distance( + (lon, lat), (station["geo_lon"], station["geo_lat"]) + ) + if d < closest_distance: + closest1.append(station) + i += 1 + closest = closest1 return closest def stations_geojson(self):