88from typing import *
99
1010# # Installed # #
11+ import pydantic
1112from pydantic import BaseModel
1213
1314# # Project # #
2425GOOGLE_STREETVIEW_STATIC_API_URL = "https://maps.googleapis.com/maps/api/streetview"
2526
2627
28+ def get_labelled_icon_url (label : str ) -> str :
29+ """Get an URL pointing to a picture of a map marker, with a custom label on top of it"""
30+ # TODO self-hosted generation, and/or cache of generated labels
31+ return f"https://cdn.mapmarker.io/api/v1/font-awesome/v5/pin?text={ label } &size=40&background=D94B43&color=000000&hoffset=-1"
32+
33+
2734class _GoogleMapsBaseRequest (BaseModel , ChecksumableClass ):
2835 location_x : float
2936 location_y : float
@@ -60,13 +67,23 @@ class Tag(BaseModel, ChecksumableClass):
6067 __ALLOWED_LABELS = [* [str (i ) for i in range (1 , 10 )], * [c for c in string .ascii_uppercase ]]
6168
6269 label : Optional [str ] = None # TODO constrain values accepted (avoid enum?)
70+ icon_url : Optional [str ] = None
6371 location_x : float
6472 location_y : float
6573
6674 @classmethod
6775 def get_allowed_labels (cls ):
6876 return cls .__ALLOWED_LABELS
6977
78+ @pydantic .root_validator (pre = True )
79+ def label_to_icon (cls , kwargs : dict ):
80+ """If label is not an "allowed label", generate an icon for it and set it as "icon_url"."""
81+ label = kwargs .get ("label" )
82+ if label not in cls .__ALLOWED_LABELS :
83+ kwargs ["label" ] = None
84+ kwargs ["icon_url" ] = get_labelled_icon_url (label )
85+ return kwargs
86+
7087 @property
7188 def location_str (self ):
7289 return f"{ self .location_x } ,{ self .location_y } "
@@ -154,15 +171,23 @@ async def _request(url: str, params: Union[dict, ListOfTuples], expect_http_erro
154171 )
155172
156173
157- async def get_map (request : GoogleMapRequest ) -> bytes :
158- """Get a static Map picture from the Google Maps Static API. Return the acquired PNG picture as bytes.
174+ def _get_map_tags_params (request_tags : List [GoogleMapRequest .Tag ]) -> ListOfTuples :
175+ params = list ()
176+ for tag in request_tags :
177+ tag_param_values = [tag .location_str ] # Location always at the end
159178
160- References:
161- https://developers.google.com/maps/documentation/maps-static/overview
162- https://developers.google.com/maps/documentation/maps-static/start
163- """
164- logger .bind (map_request = request .dict ()).debug ("Requesting Google Static Map picture..." )
165- # TODO cache loaded pictures
179+ if tag .label :
180+ tag_param_values .insert (0 , "label:" + tag .label )
181+ if tag .icon_url :
182+ tag_param_values .insert (0 , "icon:" + tag .icon_url )
183+
184+ tag_param = "|" .join (tag_param_values )
185+ params .append (("markers" , tag_param ))
186+
187+ return params
188+
189+
190+ def _get_map_params (request : GoogleMapRequest ) -> ListOfTuples :
166191 params = [
167192 ("size" , request .size_str ),
168193 ("maptype" , request .map_type .value ),
@@ -176,15 +201,22 @@ async def get_map(request: GoogleMapRequest) -> bytes:
176201 params .append (("zoom" , str (request .zoom )))
177202
178203 if request .tags :
179- for tag in request .tags :
180- tag_param_values = [tag .location_str ] # Location always at the end
204+ params .extend (_get_map_tags_params (request .tags ))
205+
206+ return params
181207
182- if tag .label :
183- tag_param_values .insert (0 , "label:" + tag .label )
184208
185- tag_param = "|" .join (tag_param_values )
186- params .append (("markers" , tag_param ))
209+ async def get_map (request : GoogleMapRequest ) -> bytes :
210+ """Get a static Map picture from the Google Maps Static API. Return the acquired PNG picture as bytes.
211+
212+ References:
213+ https://developers.google.com/maps/documentation/maps-static/overview
214+ https://developers.google.com/maps/documentation/maps-static/start
215+ """
216+ logger .bind (map_request = request .dict ()).debug ("Requesting Google Static Map picture..." )
217+ # TODO cache loaded pictures
187218
219+ params = _get_map_params (request )
188220 return (await _request (url = GOOGLE_MAPS_STATIC_API_URL , params = params )).content
189221
190222
0 commit comments