Skip to content

Commit

Permalink
Removes save file to be handled by the calling function
Browse files Browse the repository at this point in the history
  • Loading branch information
FL550 committed Jan 5, 2024
1 parent 527a04b commit 9ba7ba4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,13 @@ You can download weather maps from the DWD GeoServer with this package. There ar
```python
from simple_dwd_weatherforecast import dwdmap

dwdmap.get_from_location(51.272, 8.84, radius_km=100, map_type=dwdmap.WeatherMapType.NIEDERSCHLAGSRADAR, background_type=dwdmap.WeatherBackgroundMapType.BUNDESLAENDER, file_name="map.png")
image = dwdmap.get_from_location(51.272, 8.84, radius_km=100, map_type=dwdmap.WeatherMapType.NIEDERSCHLAGSRADAR, background_type=dwdmap.WeatherBackgroundMapType.BUNDESLAENDER)

dwdmap.get_germany(map_type=dwdmap.WeatherMapType.UVINDEX, width=520, height=580, filename="germany.png")
image.save("niederschlag.png")

image = dwdmap.get_germany(map_type=dwdmap.WeatherMapType.UVINDEX, image_width=520, image_height=580)

image.save("uvindex.png")
```

#### Available methods
Expand All @@ -175,11 +179,11 @@ class WeatherBackgroundMapType(Enum):
GEMEINDEN = "dwd:Warngebiete_Gemeinden"
SATELLIT = "dwd:bluemarble"

def get_from_location(longitude, latitude, radius_km, map_type: WeatherMapType, background_type: WeatherBackgroundMapType, optional integer image_width, optional integer image_height, optional string filename (default:"map.png")) #Saves map with given radius from coordinates
def get_from_location(longitude, latitude, radius_km, map_type: WeatherMapType, background_type: WeatherBackgroundMapType, optional integer image_width, optional integer image_height) #Returns map as pillow image with given radius from coordinates

get_germany(map_type: WeatherMapType, optional integer image_width, optional integer image_height, optional string filename (default:"map.png")) #Saves map of whole germany
get_germany(map_type: WeatherMapType, optional integer image_width, optional integer image_height, optional string save_to_filename) #Returns map as pillow image of whole germany

get_map(minx,miny,maxx,maxy, map_type: WeatherMapType, background_type: WeatherBackgroundMapType, optional integer image_width, optional integer image_height, optional string filename (default:"map.png")) #Map retrieval
get_map(minx,miny,maxx,maxy, map_type: WeatherMapType, background_type: WeatherBackgroundMapType, optional integer image_width, optional integer image_height, optional string save_to_filename) #Returns map as pillow image
```

## Help and Contribution
Expand Down
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.26",
version="2.0.27",
author="Max Fermor",
description="A simple tool to retrieve a weather forecast from DWD OpenData",
long_description=long_description,
Expand Down
12 changes: 6 additions & 6 deletions simple_dwd_weatherforecast/dwdmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ 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, filename="map.png"):
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)))
get_map(latitude-radius, longitude-radius, latitude+radius, longitude+radius, map_type, background_type, image_width, image_height, filename)
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, 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_germany(map_type: WeatherMapType, image_width=520, image_height=580):
return get_map(4.4, 46.4, 16.1, 55.6, map_type, WeatherBackgroundMapType.BUNDESLAENDER, image_width, image_height)

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

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)
return image

0 comments on commit 9ba7ba4

Please sign in to comment.