Skip to content

Commit

Permalink
Rollback custom markers on multi-stop maps; Refactor google_maps modu…
Browse files Browse the repository at this point in the history
…le to package; Validate at least one stop with location when getting multi-stop map
  • Loading branch information
David-Lor committed Sep 26, 2021
1 parent 4644f2f commit 0c9ac9f
Show file tree
Hide file tree
Showing 8 changed files with 306 additions and 254 deletions.
34 changes: 26 additions & 8 deletions vigobusapi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# # Native # #
import io
import json
from typing import Optional, Set

# # Installed # #
Expand Down Expand Up @@ -108,7 +109,7 @@ async def endpoint_get_stop_map(
):
"""Get a picture of a map with the stop location marked on it."""
stop = await get_stop(stop_id)
if (stop.lat, stop.lon) == (None, None):
if not stop.has_location:
raise HTTPException(status_code=409, detail="The stop does not have information about the location")

map_request = GoogleMapRequest(
Expand All @@ -126,14 +127,30 @@ async def endpoint_get_stop_map(

@app.get("/stops/map")
async def endpoint_get_stops_map(
stops_ids: Set[int] = Query(None, alias="stop_id", min_items=1, max_items=35),
stops_ids: Set[int] = Query(None, alias="stop_id",
min_items=1, max_items=len(GoogleMapRequest.Tag.get_allowed_labels())),
map_params: MapQueryParams = Depends(),
):
"""Get a picture of a map with the locations of the given stops marked on it."""
stops = await get_stops(stops_ids)
"""Get a picture of a map with the locations of the given stops marked on it.
Non existing stops, or those without location available, are ignored,
but if none of the given stops are valid, returns 404.
# noinspection PyTypeChecker
stops_tags = [GoogleMapRequest.Tag(label=stop.stop_id, location_x=stop.lat, location_y=stop.lon) for stop in stops]
A header "X-Stops-Tags" is returned, being a JSON associating the Stops IDs with the tag label on the map,
with the format: {"<stop id>" : "<tag label>"}
"""
stops = await get_stops(stops_ids)
stops = [stop for stop in stops if stop.has_location]
if not stops:
raise HTTPException(status_code=404, detail="None of the stops exist or have location available")

stops_tags = list()
stops_tags_relation = dict()
for i, stop in enumerate(stops):
tag_label = GoogleMapRequest.Tag.get_allowed_labels()[i]
tag = GoogleMapRequest.Tag(label=tag_label, location_x=stop.lat, location_y=stop.lon)
stops_tags.append(tag)
stops_tags_relation[stop.stop_id] = tag_label

map_request = GoogleMapRequest(
size_x=map_params.size_x,
Expand All @@ -146,7 +163,8 @@ async def endpoint_get_stops_map(

return StreamingResponse(
content=io.BytesIO(map_data),
media_type="image/png"
media_type="image/png",
headers={"X-Stops-Tags": json.dumps(stops_tags_relation)}
)


Expand All @@ -157,7 +175,7 @@ async def endpoint_get_stop_photo(
size_y: int = google_maps_settings.stop_photo_default_size_y,
):
stop = await get_stop(stop_id)
if (stop.lat, stop.lon) == (None, None):
if not stop.has_location:
raise HTTPException(status_code=409, detail="The stop does not have information about the location")

photo_request = GoogleStreetviewRequest(
Expand Down
4 changes: 4 additions & 0 deletions vigobusapi/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def get_mongo_dict(self):
d.pop("source")
return d

@property
def has_location(self):
return self.lat is not None and self.lon is not None


OptionalStop = Optional[Stop]
StopOrNotExist = Union[Stop, StopNotExist]
Expand Down
246 changes: 0 additions & 246 deletions vigobusapi/services/google_maps.py

This file was deleted.

7 changes: 7 additions & 0 deletions vigobusapi/services/google_maps/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""GOOGLE MAPS
Classes and functions for acquiring static maps and photos using Google Maps & Street View Static APIs
"""

from ._entities import *
from ._getter_maps import *
from ._getter_streetview import *
Loading

0 comments on commit 0c9ac9f

Please sign in to comment.