44
55# # Native # #
66import io
7+ import json
78from typing import Optional , Set
89
910# # Installed # #
@@ -108,7 +109,7 @@ async def endpoint_get_stop_map(
108109):
109110 """Get a picture of a map with the stop location marked on it."""
110111 stop = await get_stop (stop_id )
111- if ( stop . lat , stop .lon ) == ( None , None ) :
112+ if not stop .has_location :
112113 raise HTTPException (status_code = 409 , detail = "The stop does not have information about the location" )
113114
114115 map_request = GoogleMapRequest (
@@ -126,14 +127,30 @@ async def endpoint_get_stop_map(
126127
127128@app .get ("/stops/map" )
128129async def endpoint_get_stops_map (
129- stops_ids : Set [int ] = Query (None , alias = "stop_id" , min_items = 1 , max_items = 35 ),
130+ stops_ids : Set [int ] = Query (None , alias = "stop_id" ,
131+ min_items = 1 , max_items = len (GoogleMapRequest .Tag .get_allowed_labels ())),
130132 map_params : MapQueryParams = Depends (),
131133):
132- """Get a picture of a map with the locations of the given stops marked on it."""
133- stops = await get_stops (stops_ids )
134+ """Get a picture of a map with the locations of the given stops marked on it.
135+
136+ Non existing stops, or those without location available, are ignored,
137+ but if none of the given stops are valid, returns 404.
134138
135- # noinspection PyTypeChecker
136- stops_tags = [GoogleMapRequest .Tag (label = stop .stop_id , location_x = stop .lat , location_y = stop .lon ) for stop in stops ]
139+ A header "X-Stops-Tags" is returned, being a JSON associating the Stops IDs with the tag label on the map,
140+ with the format: {"<stop id>" : "<tag label>"}
141+ """
142+ stops = await get_stops (stops_ids )
143+ stops = [stop for stop in stops if stop .has_location ]
144+ if not stops :
145+ raise HTTPException (status_code = 404 , detail = "None of the stops exist or have location available" )
146+
147+ stops_tags = list ()
148+ stops_tags_relation = dict ()
149+ for i , stop in enumerate (stops ):
150+ tag_label = GoogleMapRequest .Tag .get_allowed_labels ()[i ]
151+ tag = GoogleMapRequest .Tag (label = tag_label , location_x = stop .lat , location_y = stop .lon )
152+ stops_tags .append (tag )
153+ stops_tags_relation [stop .stop_id ] = tag_label
137154
138155 map_request = GoogleMapRequest (
139156 size_x = map_params .size_x ,
@@ -146,7 +163,8 @@ async def endpoint_get_stops_map(
146163
147164 return StreamingResponse (
148165 content = io .BytesIO (map_data ),
149- media_type = "image/png"
166+ media_type = "image/png" ,
167+ headers = {"X-Stops-Tags" : json .dumps (stops_tags_relation )}
150168 )
151169
152170
@@ -157,7 +175,7 @@ async def endpoint_get_stop_photo(
157175 size_y : int = google_maps_settings .stop_photo_default_size_y ,
158176):
159177 stop = await get_stop (stop_id )
160- if ( stop . lat , stop .lon ) == ( None , None ) :
178+ if not stop .has_location :
161179 raise HTTPException (status_code = 409 , detail = "The stop does not have information about the location" )
162180
163181 photo_request = GoogleStreetviewRequest (
0 commit comments