5
5
""" # noqa E501
6
6
7
7
import time
8
- from datetime import datetime
9
- from typing import Any , Dict , List , Optional
8
+ from datetime import date , datetime
9
+ from typing import Any , Dict , List , Optional , Union
10
+
11
+ from geojson import Feature , FeatureCollection , LineString , MultiPolygon , Point , Polygon
10
12
11
13
from here_location_services .platform .auth import Auth
12
14
@@ -33,7 +35,7 @@ def get_dest_weather(
33
35
at : Optional [List ] = None ,
34
36
query : Optional [str ] = None ,
35
37
zipcode : Optional [str ] = None ,
36
- hourly_date : Optional [Any ] = None ,
38
+ hourly_date : Optional [Union [ date , datetime ] ] = None ,
37
39
one_observation : Optional [bool ] = None ,
38
40
language : Optional [str ] = None ,
39
41
units : Optional [str ] = None ,
@@ -49,7 +51,8 @@ def get_dest_weather(
49
51
:param query: Free text query. Examples: "125, Berliner, berlin", "Beacon, Boston"
50
52
:param zipcode: ZIP code of the location. This parameter is supported only for locations in
51
53
the United States of America.
52
- :param hourly_date: Date for which hourly forecasts are to be retrieved.
54
+ :param hourly_date: Date for which hourly forecasts are to be retrieved. Can be either a `date` or
55
+ `datetime` object
53
56
:param one_observation: Boolean, if set to true, the response only includes the closest
54
57
location. Only available when the `product` parameter is set to
55
58
`DEST_WEATHER_PRODUCT.observation`.
@@ -71,7 +74,10 @@ def get_dest_weather(
71
74
if zipcode :
72
75
params ["zipCode" ] = zipcode
73
76
if hourly_date :
74
- params ["hourlyDate" ] = hourly_date .strftime ("%Y-%m-%dT%H:%M:%S" )
77
+ if type (hourly_date ) is datetime .date :
78
+ params ["hourlyDate" ] = hourly_date .strftime ("%Y-%m-%d" )
79
+ else :
80
+ params ["hourlyDate" ] = hourly_date .strftime ("%Y-%m-%dT%H:%M:%S" )
75
81
if one_observation :
76
82
params ["oneObservation" ] = "true" if one_observation else "false"
77
83
if language :
@@ -87,9 +93,7 @@ def get_dest_weather(
87
93
88
94
def get_weather_alerts (
89
95
self ,
90
- feature_type : str ,
91
- geometry_type : str ,
92
- geometry_coordinates : List ,
96
+ geometry : Union [Point , LineString , Polygon , MultiPolygon ],
93
97
start_time : datetime ,
94
98
id : Optional [str ] = None ,
95
99
weather_severity : Optional [int ] = None ,
@@ -102,9 +106,8 @@ def get_weather_alerts(
102
106
103
107
See further information `Here Destination Weather API <https://developer.here.com/documentation/destination-weather/dev_guide/topics/overview.html>_`.
104
108
105
- :param feature_type: String to define feature type
106
- :param geometry_type: Point or LineString or Polygon or MultiPolygon
107
- :param geometry_coordinates: Array of coordinates corressponding to type provided
109
+ :param geometry: Point or LineString or Polygon or MultiPolygon defining the route or
110
+ a single location
108
111
:param start_time: Start time of the event
109
112
:param id: Unique weather alert id.
110
113
:param weather_severity: Defines the severity of the weather event as defined
@@ -122,17 +125,6 @@ def get_weather_alerts(
122
125
123
126
path = "v3/alerts"
124
127
url = f"{ self ._base_url } /{ path } "
125
- data : Dict [str , Any ] = {
126
- "type" : "FeatureCollection" ,
127
- }
128
- features : Dict [str , Any ] = {
129
- "type" : feature_type ,
130
- }
131
-
132
- if id :
133
- features ["id" ] = id
134
-
135
- geometry : Dict [str , Any ] = {"type" : geometry_type , "coordinates" : geometry_coordinates }
136
128
137
129
properties : Dict [str , Any ] = {
138
130
"width" : width ,
@@ -151,10 +143,19 @@ def get_weather_alerts(
151
143
weather_warnings ["endTime" ] = time .mktime (end_time .timetuple ())
152
144
153
145
properties ["warnings" ] = [weather_warnings ]
154
- features ["properties" ] = properties
155
- features ["geometry" ] = geometry
156
- data ["features" ] = [features ]
157
- resp = self .post (url , data = data )
146
+
147
+ f = Feature (
148
+ geometry = geometry ,
149
+ properties = properties ,
150
+ )
151
+
152
+ if id :
153
+ f .id = id
154
+
155
+ feature_collection = FeatureCollection ([])
156
+ feature_collection .features .append (f )
157
+
158
+ resp = self .post (url , data = feature_collection )
158
159
if resp .status_code == 200 :
159
160
return resp
160
161
else :
0 commit comments