Skip to content

Commit

Permalink
Formating
Browse files Browse the repository at this point in the history
  • Loading branch information
FL550 committed Jul 27, 2024
1 parent 46244c8 commit 8457110
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions simple_dwd_weatherforecast/dwdmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class WeatherMapType(Enum):
WARNUNGEN_GEMEINDEN = "dwd:Warnungen_Gemeinden"
WARNUNGEN_KREISE = "dwd:Warnungen_Landkreise"


class WeatherBackgroundMapType(Enum):
LAENDER = "dwd:Laender"
BUNDESLAENDER = "dwd:Warngebiete_Bundeslaender"
Expand All @@ -23,25 +24,63 @@ class WeatherBackgroundMapType(Enum):
SATELLIT = "dwd:bluemarble"
GEWAESSER = "dwd:Gewaesser"

def get_from_location(longitude, latitude, radius_km, map_type: WeatherMapType, background_type: WeatherBackgroundMapType = WeatherBackgroundMapType.BUNDESLAENDER, image_width=520, image_height=580):

def get_from_location(
longitude,
latitude,
radius_km,
map_type: WeatherMapType,
background_type: WeatherBackgroundMapType = WeatherBackgroundMapType.BUNDESLAENDER,
image_width=520,
image_height=580,
):
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)))
return get_map(latitude-radius, longitude-radius, latitude+radius, longitude+radius, map_type, background_type, image_width, image_height)
return get_map(
latitude - radius,
longitude - radius,
latitude + radius,
longitude + radius,
map_type,
background_type,
image_width,
image_height,
)


def get_germany(
map_type: WeatherMapType,
background_type: WeatherBackgroundMapType = WeatherBackgroundMapType.BUNDESLAENDER,
image_width=520,
image_height=580,
):
return get_map(
4.4, 46.4, 16.1, 55.6, map_type, background_type, image_width, image_height
)

def get_germany(map_type: WeatherMapType, background_type: WeatherBackgroundMapType = WeatherBackgroundMapType.BUNDESLAENDER, image_width=520, image_height=580):
return get_map(4.4, 46.4, 16.1, 55.6, map_type, background_type, image_width, image_height)

def get_map(minx,miny,maxx,maxy, map_type: WeatherMapType, background_type: WeatherBackgroundMapType, image_width=520, image_height=580):
def get_map(
minx,
miny,
maxx,
maxy,
map_type: WeatherMapType,
background_type: WeatherBackgroundMapType,
image_width=520,
image_height=580,
):
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.")
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))
return image
return image

0 comments on commit 8457110

Please sign in to comment.