From d358674d17efa1a6887a31178714fb2706da2c0f Mon Sep 17 00:00:00 2001 From: Christopher J Lowrie Date: Fri, 23 Aug 2024 11:15:51 -0700 Subject: [PATCH] Updating inundations to call with same signature as gauges --- api/app/googleflood.py | 50 +++++++++++++++---------- api/app/main.py | 4 +- api/app/tests/test_google_floods_api.py | 8 +--- 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/api/app/googleflood.py b/api/app/googleflood.py index b3599710d..038677230 100644 --- a/api/app/googleflood.py +++ b/api/app/googleflood.py @@ -5,12 +5,23 @@ import geopandas as gpd import pandas as pd import uuid +from io import StringIO from fiona.drvsupport import supported_drivers supported_drivers['LIBKML'] = 'rw' logger = logging.getLogger(__name__) +from pydantic import BaseModel +from typing import List + +class InundationMap(BaseModel): + level: str + serializedPolygonId: str + +class InundationMapSet(BaseModel): + inundationMaps: List[InundationMap] + GOOGLE_FLOODS_API_KEY = os.getenv("GOOGLE_FLOODS_API_KEY", "") if GOOGLE_FLOODS_API_KEY == "": logger.warning("Missing backend parameter: GOOGLE_FLOODS_API_KEY") @@ -45,39 +56,38 @@ def get_google_floods_gauges( asGeojson: bool = True, ): """Get statistical charts data""" - URL = f'https://floodforecasting.googleapis.com/v1/floodStatus:searchLatestFloodStatusByArea?key={GOOGLE_FLOODS_API_KEY}' response = requests.post( URL, json={'regionCode': iso2} - ).json().get('floodStatuses', []) + ) + assert response.status_code == 200 + + data = response.json().get('floodStatuses', []) if asGeojson: geojson_feature_collection = { "type": "FeatureCollection", - "features": [format_to_geojson(data) for data in response] + "features": [format_to_geojson(d) for d in data] } return geojson_feature_collection - return response - - -from pydantic import BaseModel -from typing import List - -class InundationMap(BaseModel): - level: str - serializedPolygonId: str - -class InundationMapSet(BaseModel): - inundationMaps: List[InundationMap] + return data def get_google_floods_inundations( - inundationMapSet: InundationMapSet, + iso2: str, ) -> gpd.GeoDataFrame: - """Get statistical charts data""" + """Get polygonal floodmap data""" + gauge_data = get_google_floods_gauges(iso2=iso2, asGeojson=False) + inundationMapSet = [] + for gd in gauge_data: + if 'inundationMapSet' in gd.keys(): + inundationMapSet += gd['inundationMapSet']['inundationMaps'] + + # Fetch the polygons level_to_kml = dict() URL = 'https://floodforecasting.googleapis.com/v1/serializedPolygons/{serializedPolygonId}?key={key}' + for inundationMap in inundationMapSet: response = requests.get( URL.format( @@ -94,10 +104,10 @@ def get_google_floods_inundations( gdf_buff = [] for level, kml in level_to_kml.items(): - kml_path = os.path.join(tmp_path, f'{level}.kml') - with open(kml_path, 'w') as f: + with open(os.path.join(tmp_path, f'{level}.kml'), 'w') as f: f.write(kml) - gdf = gpd.read_file(kml_path, driver='KML') + kml_file = os.path.join(tmp_path, f'{level}.kml') + gdf = gpd.read_file(kml_file, driver='KML') gdf['level'] = level gdf_buff.append(gdf) diff --git a/api/app/main.py b/api/app/main.py index ff0e89ad7..e3d1743b0 100644 --- a/api/app/main.py +++ b/api/app/main.py @@ -424,7 +424,7 @@ def get_google_floods_gauges_api( @app.get('/google-floods-inundations') def get_google_floods_inundations_api( - inundationMapSet_JSONString: str, + iso2: str, ): """Get statistical charts data""" - return get_google_floods_inundations(json.loads(inundationMapSet_JSONString)).to_json() + return get_google_floods_inundations(iso2).to_json() diff --git a/api/app/tests/test_google_floods_api.py b/api/app/tests/test_google_floods_api.py index d0ad5973f..846324e2e 100644 --- a/api/app/tests/test_google_floods_api.py +++ b/api/app/tests/test_google_floods_api.py @@ -5,8 +5,6 @@ client = TestClient(app) -test_inundation_map_set = [{'level': 'HIGH', 'serializedPolygonId': '9631a3143f2543ae9664c4fba7298931'}, {'level': 'MEDIUM', 'serializedPolygonId': '93444f067f42409493fd907b2b0323b7'}, {'level': 'LOW', 'serializedPolygonId': '757ac60c06ea481aba384201a107d611'}] -test_inundation_map_set_jsonstr = json.dumps(test_inundation_map_set) def test_get_google_floods_gauges(): gauges = get_google_floods_gauges("BD") @@ -14,7 +12,7 @@ def test_get_google_floods_gauges(): def test_get_google_floods_inundations(): - floods = get_google_floods_inundations(test_inundation_map_set) + floods = get_google_floods_inundations("BD") assert floods.shape[0] > 0 @@ -25,8 +23,6 @@ def test_get_google_floods_gauges_api(): def test_get_google_floods_inundation_api(): - response = client.get(f"/google-floods-inundations?inundationMapSet_JSONString={test_inundation_map_set_jsonstr}") + response = client.get(f"/google-floods-inundations?iso2=BD") assert response.status_code == 200 - logging.info(json.dumps(response.json())) - # logging.info(response.json()) assert len(response.json()) > 0 \ No newline at end of file