Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added an optional buffer-zone to nearest_station #16

Merged
merged 1 commit into from
Oct 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions dwdweather/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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):
Expand Down