-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
119 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ init | |
dist | ||
htmlcov | ||
__init__.pyc | ||
development/secret.py | ||
development/secret.py | ||
custom_test.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import requests | ||
import math | ||
from io import BytesIO | ||
from PIL import Image | ||
from enum import Enum | ||
|
||
|
||
class WeatherMapType(Enum): | ||
NIEDERSCHLAGSRADAR = "dwd:Niederschlagsradar" | ||
MAXTEMP = "dwd:GefuehlteTempMax" | ||
UVINDEX = "dwd:UVI_CS" | ||
POLLENFLUG = "dwd:Pollenflug" | ||
SATELLITE_RGB = "dwd:Satellite_meteosat_1km_euat_rgb_day_hrv_and_night_ir108_3h" | ||
SATELLITE_IR = "dwd:Satellite_worldmosaic_3km_world_ir108_3h" | ||
WARNUNGEN_GEMEINDEN = "dwd:Warnungen_Gemeinden" | ||
WARNUNGEN_KREISE = "dwd:Warnungen_Landkreise" | ||
|
||
class WeatherBackgroundMapType(Enum): | ||
LAENDER = "dwd:Laender" | ||
BUNDESLAENDER = "dwd:Warngebiete_Bundeslaender" | ||
KREISE = "dwd:Warngebiete_Kreise" | ||
GEMEINDEN = "dwd:Warngebiete_Gemeinden" | ||
SATELLIT = "dwd:bluemarble" | ||
|
||
def get_from_location(longitude, latitude, radius_km, map_type: WeatherMapType, background_type: WeatherBackgroundMapType = WeatherBackgroundMapType.BUNDESLAENDER, image_width=520, image_height=580, filename="map.png"): | ||
if radius_km <= 0: | ||
raise ValueError("Radius must be greater than 0") | ||
if latitude < -90 or latitude > 90: | ||
raise ValueError("Latitude must be between -90 and 90") | ||
if longitude < -180 or longitude > 180: | ||
raise ValueError("Longitude must be between -180 and 180") | ||
radius = math.fabs(radius_km / (111.3 * math.cos(latitude))) | ||
get_map(latitude-radius, longitude-radius, latitude+radius, longitude+radius, map_type, background_type, image_width, image_height, filename) | ||
|
||
def get_germany(map_type: WeatherMapType, image_width=520, image_height=580, filename="map.png"): | ||
get_map(4.4, 46.4, 16.1, 55.6, map_type, WeatherBackgroundMapType.BUNDESLAENDER, image_width, image_height, filename) | ||
|
||
def get_map(minx,miny,maxx,maxy, map_type: WeatherMapType, background_type: WeatherBackgroundMapType, image_width=520, image_height=580, filename="map.png"): | ||
if image_width > 1200 or image_height > 1400: | ||
raise ValueError("Width and height must not exceed 1200 and 1400 respectively. Please be kind to the DWD servers.") | ||
|
||
url = f"https://maps.dwd.de/geoserver/dwd/wms?service=WMS&version=1.1.0&request=GetMap&layers={map_type.value},{background_type.value}&bbox={minx},{miny},{maxx},{maxy}&width={image_width}&height={image_height}&srs=EPSG:4326&styles=&format=image/png" | ||
request = requests.get(url, stream=True) | ||
if request.status_code == 200: | ||
image = Image.open(BytesIO(request.content)) | ||
image.save(filename) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import unittest | ||
from simple_dwd_weatherforecast import dwdmap | ||
|
||
|
||
class MapTestCase(unittest.TestCase): | ||
def test_prevent_too_large_height(self): | ||
with self.assertRaises(ValueError): | ||
dwdmap.get_map(4.4, 46.4, 16.1, 55.6, dwdmap.WeatherMapType.NIEDERSCHLAGSRADAR, dwdmap.WeatherBackgroundMapType.BUNDESLAENDER, 1300, 700) | ||
|
||
def test_prevent_too_large_width(self): | ||
with self.assertRaises(ValueError): | ||
dwdmap.get_map(4.4, 46.4, 16.1, 55.6, dwdmap.WeatherMapType.NIEDERSCHLAGSRADAR, dwdmap.WeatherBackgroundMapType.BUNDESLAENDER, 300, 1700) | ||
|
||
def test_prevent_too_large(self): | ||
with self.assertRaises(ValueError): | ||
dwdmap.get_map(4.4, 46.4, 16.1, 55.6, dwdmap.WeatherMapType.NIEDERSCHLAGSRADAR, dwdmap.WeatherBackgroundMapType.BUNDESLAENDER, 1300, 1700) |