Skip to content

Commit

Permalink
Updating inundations to call with same signature as gauges
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher J Lowrie authored and Christopher J Lowrie committed Aug 23, 2024
1 parent 2e4bf07 commit d358674
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
50 changes: 30 additions & 20 deletions api/app/googleflood.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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(
Expand All @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions api/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
8 changes: 2 additions & 6 deletions api/app/tests/test_google_floods_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@

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")
assert len(gauges) > 0


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


Expand All @@ -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

0 comments on commit d358674

Please sign in to comment.