From 5b4947c9d386b76d023a89822847c7522663f4bc Mon Sep 17 00:00:00 2001 From: Adrian Letchford Date: Sun, 23 Jul 2023 18:26:36 +0100 Subject: [PATCH] Fix error on missing pitch and roll (#29) --- streetview/download.py | 12 ++++++++---- streetview/search.py | 8 ++++---- tests/test_search.py | 12 ++++++++++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/streetview/download.py b/streetview/download.py index e1a63b3..1706e65 100644 --- a/streetview/download.py +++ b/streetview/download.py @@ -2,7 +2,7 @@ import time from dataclasses import dataclass from io import BytesIO -from typing import Generator +from typing import Generator, Tuple import requests from PIL import Image @@ -22,9 +22,10 @@ class Tile: image: Image.Image -def get_width_and_height_from_zoom(zoom: int) -> (int, int): +def get_width_and_height_from_zoom(zoom: int) -> Tuple[int, int]: """ - Returns the width and height of a panorama at a given zoom level, depends on the zoom level. + Returns the width and height of a panorama at a given zoom level, depends on the + zoom level. """ return 2**zoom, 2 ** (zoom - 1) @@ -33,7 +34,10 @@ def make_download_url(pano_id: str, zoom: int, x: int, y: int) -> str: """ Returns the URL to download a tile. """ - return f"https://cbk0.google.com/cbk?output=tile&panoid={pano_id}&zoom={zoom}&x={x}&y={y}" + return ( + "https://cbk0.google.com/cbk" + f"?output=tile&panoid={pano_id}&zoom={zoom}&x={x}&y={y}" + ) def fetch_panorama_tile(tile_info: TileInfo) -> Image.Image: diff --git a/streetview/search.py b/streetview/search.py index 9a85444..e6ecbe2 100644 --- a/streetview/search.py +++ b/streetview/search.py @@ -12,8 +12,8 @@ class Panorama(BaseModel): lat: float lon: float heading: float - pitch: float - roll: float + pitch: Optional[float] + roll: Optional[float] date: Optional[str] @@ -79,8 +79,8 @@ def extract_panoramas(text: str) -> List[Panorama]: lat=pano[2][0][2], lon=pano[2][0][3], heading=pano[2][2][0], - pitch=pano[2][2][1], - roll=pano[2][2][2], + pitch=pano[2][2][1] if len(pano[2][2]) >= 2 else None, + roll=pano[2][2][2] if len(pano[2][2]) >= 3 else None, date=dates[i] if i < len(dates) else None, ) for i, pano in enumerate(raw_panos) diff --git a/tests/test_search.py b/tests/test_search.py index 55f9014..22a11a7 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -104,3 +104,15 @@ def test_search_where_there_are_no_dates(): dates = [p.date for p in result] assert dates == [None] * len(dates) + + +def test_coordinates_with_missing_pitch(): + panos = search_panoramas(35.658353457849685, 139.6920989241623) + is_pitch_none = [p.pitch is None for p in panos] + assert any(is_pitch_none) + + +def test_coordinates_with_missing_roll(): + panos = search_panoramas(35.658353457849685, 139.6920989241623) + is_roll_none = [p.roll is None for p in panos] + assert any(is_roll_none)