Skip to content
This repository was archived by the owner on Sep 5, 2023. It is now read-only.

Commit 1788335

Browse files
fix issues (#23)
* fix typing error * fix default value error * fix response type * fixes Signed-off-by: Aayush Jain <f20180320@hyderabad.bits-pilani.ac.in>
1 parent b2d59c1 commit 1788335

File tree

5 files changed

+65
-20
lines changed

5 files changed

+65
-20
lines changed

docs/source/dest_weather.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ Attributes
8585
==================== =============================================================================================================== ===
8686
Attribute Type Doc
8787
==================== =============================================================================================================== ===
88-
geometry Point or LineString or Polygon or MultiPolygon Point or LineString or Polygon or MultiPolygon defining the route or a single location
88+
geometry Point or LineString Point or LineString defining the route or a single location
8989
start_time :func:`datetime.datetime` Start time of the event
9090
id str optional Unique weather alert id.
9191
weather_severity :class:`WeatherSeverity <here_location_services.config.dest_weather_config.WeatherSeverity>` optional Defines the severity of the weather event
9292
weather_type :class:`WeatherType <here_location_services.config.dest_weather_config.WeatherType>` optional Defines the type of the weather event
9393
country str optional String for ISO-3166-1 2-letter country code.
9494
end_time :func:`datetime.datetime` optional End time of the event. If not present, warning is valid until it is not removed from the feed by national weather institutes (valid until warning is present in the response)
95-
width int optional int. default 50000
95+
width int optional int.
9696
==================== =============================================================================================================== ===

here_location_services/destination_weather_api.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from datetime import date, datetime
99
from typing import Any, Dict, List, Optional, Union
1010

11-
from geojson import Feature, FeatureCollection, LineString, MultiPolygon, Point, Polygon
11+
from geojson import Feature, FeatureCollection, LineString, Point
1212

1313
from here_location_services.platform.auth import Auth
1414

@@ -93,21 +93,20 @@ def get_dest_weather(
9393

9494
def get_weather_alerts(
9595
self,
96-
geometry: Union[Point, LineString, Polygon, MultiPolygon],
96+
geometry: Union[Point, LineString],
9797
start_time: datetime,
9898
id: Optional[str] = None,
9999
weather_severity: Optional[int] = None,
100100
weather_type: Optional[str] = None,
101101
country: Optional[str] = None,
102102
end_time: Optional[datetime] = None,
103-
width: Optional[int] = 50000,
103+
width: Optional[int] = None,
104104
):
105105
"""Retrieves weather reports, weather forecasts, severe weather alerts and moon and sun rise and set information.
106106
107107
See further information `Here Destination Weather API <https://developer.here.com/documentation/destination-weather/dev_guide/topics/overview.html>_`.
108108
109-
:param geometry: Point or LineString or Polygon or MultiPolygon defining the route or
110-
a single location
109+
:param geometry: Point or LineString defining the route or a single location
111110
:param start_time: Start time of the event
112111
:param id: Unique weather alert id.
113112
:param weather_severity: Defines the severity of the weather event as defined
@@ -118,7 +117,7 @@ def get_weather_alerts(
118117
:param end_time: End time of the event. If not present, warning is valid until
119118
it is not removed from the feed by national weather institutes
120119
(valid until warning is present in the response)
121-
:param width: int. default 50000
120+
:param width: int
122121
:return: :class:`requests.Response` object.
123122
:raises ApiError: If ``status_code`` of API response is not 200.
124123
""" # noqa E501

here_location_services/ls.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from time import sleep
1111
from typing import Dict, List, Optional, Tuple, Union
1212

13-
from geojson import LineString, MultiPolygon, Point, Polygon
13+
from geojson import LineString, Point
1414

1515
from here_location_services.config.routing_config import Scooter, Via
1616
from here_location_services.platform.apis.aaa_oauth2_api import AAAOauth2Api
@@ -44,6 +44,7 @@
4444
MatrixRoutingResponse,
4545
ReverseGeocoderResponse,
4646
RoutingResponse,
47+
WeatherAlertsResponse,
4748
)
4849
from .routing_api import RoutingApi
4950

@@ -373,20 +374,19 @@ def get_dest_weather(
373374

374375
def get_weather_alerts(
375376
self,
376-
geometry: Union[Point, LineString, Polygon, MultiPolygon],
377+
geometry: Union[Point, LineString],
377378
start_time: datetime,
378379
id: Optional[str] = None,
379380
weather_severity: Optional[int] = None,
380381
weather_type: Optional[str] = None,
381382
country: Optional[str] = None,
382383
end_time: Optional[datetime] = None,
383-
width: Optional[int] = 50000,
384-
) -> DestinationWeatherResponse:
384+
width: Optional[int] = None,
385+
) -> WeatherAlertsResponse:
385386
"""Retrieves weather reports, weather forecasts, severe weather alerts
386387
and moon and sun rise and set information.
387388
388-
:param geometry: Point or LineString or Polygon or MultiPolygon defining the route or
389-
a single location
389+
:param geometry: Point or LineString defining the route or a single location
390390
:param start_time: Start time of the event
391391
:param id: Unique weather alert id.
392392
:param weather_severity: Defines the severity of the weather event as defined
@@ -397,10 +397,17 @@ def get_weather_alerts(
397397
:param end_time: End time of the event. If not present, warning is valid until
398398
it is not removed from the feed by national weather institutes
399399
(valid until warning is present in the response)
400-
:param width: int. default 50000
401-
:return: :class:`DestinationWeatherResponse` object.
400+
:param width: int
401+
:raises ValueError: If maximum width exceeds 100000 for point type geometry
402+
or width exceeds 25000 for LineString geometry
403+
:return: :class:`WeatherAlertsResponse` object.
402404
"""
403405

406+
if type(geometry) is Point and width and width > 100000:
407+
raise ValueError("Maximum width is 100000 for Point geometry")
408+
if type(geometry) is LineString and width and width > 25000:
409+
raise ValueError("Maximum width is 25000 for LineString geometry")
410+
404411
resp = self.destination_weather_api.get_weather_alerts(
405412
geometry=geometry,
406413
id=id,
@@ -411,7 +418,7 @@ def get_weather_alerts(
411418
end_time=end_time,
412419
width=width,
413420
)
414-
response = DestinationWeatherResponse.new(resp.json())
421+
response = WeatherAlertsResponse.new(resp.json())
415422
return response
416423

417424
def discover(

here_location_services/responses.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,28 @@ class DestinationWeatherResponse(ApiResponse):
200200

201201
def __init__(self, **kwargs):
202202
super().__init__()
203-
self._filters = {"places": None, "features": None}
203+
self._filters = {"places": None}
204204
for param, default in self._filters.items():
205205
setattr(self, param, kwargs.get(param, default))
206+
207+
def to_geojson(self):
208+
"""Return API response as GeoJSON."""
209+
raise NotImplementedError("This method is not valid for DestinationWeatherResponse.")
210+
211+
212+
class WeatherAlertsResponse(ApiResponse):
213+
"""A class representing the Destination Weather API response data."""
214+
215+
def __init__(self, **kwargs):
216+
super().__init__()
217+
self._filters = {"features": None}
218+
for param, default in self._filters.items():
219+
setattr(self, param, kwargs.get(param, default))
220+
221+
def to_geojson(self):
222+
"""Return API response as GeoJSON."""
223+
feature_collection = FeatureCollection([])
224+
for feature in self.response["features"]:
225+
f = Feature(geometry=feature["geometry"], properties=feature["properties"])
226+
feature_collection.features.append(f)
227+
return feature_collection

tests/test_ls.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99
import pytz
1010
from geojson import FeatureCollection, Point
11+
from geojson.geometry import LineString
1112

1213
from here_location_services import LS
1314
from here_location_services.config.autosuggest_config import POLITICAL_VIEW, SHOW, SearchCircle
@@ -63,11 +64,13 @@ def test_ls_weather_alerts():
6364
"""Test weather alerts endpoint of destination weather api."""
6465
ls = LS(api_key=LS_API_KEY)
6566
resp = ls.get_weather_alerts(
66-
geometry=Point(coordinates=[15.256, 23.456]),
67+
geometry=LineString([(8.919, 44.4074), (8.923, 44.4075)]),
6768
start_time=datetime.now(),
68-
width=3000,
69+
width=25000,
6970
)
7071
assert resp
72+
geo_json = resp.to_geojson()
73+
assert geo_json.type == "FeatureCollection"
7174

7275
resp2 = ls.get_weather_alerts(
7376
geometry=Point(coordinates=[15.256, 23.456]),
@@ -79,6 +82,20 @@ def test_ls_weather_alerts():
7982
)
8083
assert resp2
8184

85+
with pytest.raises(ValueError):
86+
ls.get_weather_alerts(
87+
geometry=LineString([(8.919, 44.4074), (8.923, 44.4075)]),
88+
start_time=datetime.now(),
89+
width=50000,
90+
)
91+
92+
with pytest.raises(ValueError):
93+
ls.get_weather_alerts(
94+
geometry=Point(coordinates=[15.256, 23.456]),
95+
start_time=datetime.now(),
96+
width=1000000,
97+
)
98+
8299
with pytest.raises(ApiError):
83100
ls2 = LS(api_key="dummy")
84101
ls2.get_weather_alerts(

0 commit comments

Comments
 (0)