From 519c76db373b2007729ce2843b6618211d5c5a43 Mon Sep 17 00:00:00 2001 From: Craig Stephenson Date: Mon, 18 Nov 2024 18:09:39 -0900 Subject: [PATCH] Validate CMIP6 lat/lons against CMIP6-specific BBOX. --- config.py | 2 +- routes/cmip6.py | 8 +++--- routes/indicators.py | 8 +++--- templates/422/invalid_cmip6_latlon.html | 36 +++++++++++++++++++++++++ validate_request.py | 10 +++++-- 5 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 templates/422/invalid_cmip6_latlon.html diff --git a/config.py b/config.py index 1c16eca0..627ad8a1 100644 --- a/config.py +++ b/config.py @@ -9,7 +9,7 @@ WEST_BBOX = [-180, 51.3492, -122.8098, 71.3694] EAST_BBOX = [172.4201, 51.3492, 180, 71.3694] SEAICE_BBOX = [-180, 30.98, 180, 90] -INDICATORS_BBOX = [0, 49.94, 359.37, 90] +CMIP6_BBOX = [-180, 49.95, 180, 90] WEB_APP_URL = os.getenv("WEB_APP_URL") or "https://northernclimatereports.org/" if os.getenv("SITE_OFFLINE"): diff --git a/routes/cmip6.py b/routes/cmip6.py index 2b3653dc..f627d24f 100644 --- a/routes/cmip6.py +++ b/routes/cmip6.py @@ -11,7 +11,7 @@ from postprocessing import postprocess, prune_nulls_with_max_intensity from csv_functions import create_csv from . import routes -from config import WEST_BBOX, EAST_BBOX +from config import CMIP6_BBOX cmip6_api = Blueprint("cmip6_api", __name__) @@ -127,14 +127,12 @@ def run_fetch_cmip6_monthly_point_data(lat, lon): example request: http://localhost:5000/cmip6/point/65.06/-146.16?vars=tas,pr """ # Validate the lat/lon values - validation = validate_latlon(lat, lon) + validation = validate_latlon(lat, lon, CMIP6_BBOX) if validation == 400: return render_template("400/bad_request.html"), 400 if validation == 422: return ( - render_template( - "422/invalid_latlon.html", west_bbox=WEST_BBOX, east_bbox=EAST_BBOX - ), + render_template("422/invalid_cmip6_latlon.html", bbox=CMIP6_BBOX), 422, ) try: diff --git a/routes/indicators.py b/routes/indicators.py index 9b3de3e4..5558787b 100644 --- a/routes/indicators.py +++ b/routes/indicators.py @@ -29,7 +29,7 @@ from postprocessing import nullify_and_prune, postprocess from csv_functions import create_csv from . import routes -from config import WEST_BBOX, EAST_BBOX +from config import WEST_BBOX, EAST_BBOX, CMIP6_BBOX indicators_api = Blueprint("indicators_api", __name__) # Rasdaman targets @@ -329,14 +329,12 @@ def run_fetch_cmip6_indicators_point_data(lat, lon): """ # Validate the lat/lon values - validation = validate_latlon(lat, lon) + validation = validate_latlon(lat, lon, CMIP6_BBOX) if validation == 400: return render_template("400/bad_request.html"), 400 if validation == 422: return ( - render_template( - "422/invalid_latlon.html", west_bbox=WEST_BBOX, east_bbox=EAST_BBOX - ), + render_template("422/invalid_cmip6_latlon.html", bbox=CMIP6_BBOX), 422, ) try: diff --git a/templates/422/invalid_cmip6_latlon.html b/templates/422/invalid_cmip6_latlon.html new file mode 100644 index 00000000..fe9e63fd --- /dev/null +++ b/templates/422/invalid_cmip6_latlon.html @@ -0,0 +1,36 @@ +{% extends 'base.html' %} +{% block content %} +

Invalid coordinates

+ +
+ +

+ Provided coordinates are outside of the valid range. Coordinates must be + within the following bounding box: +

+

Bounding box:

+

+ Latitude: {{ bbox[1] }} to {{ bbox[3] }} +

+

+ Longitude: {{ bbox[0] }} to {{ bbox[2] }} +

+ + + + +{% endblock %} \ No newline at end of file diff --git a/validate_request.py b/validate_request.py index f945a7c9..24f3fea3 100644 --- a/validate_request.py +++ b/validate_request.py @@ -12,7 +12,7 @@ from fetch_data import fetch_data -def validate_latlon(lat, lon): +def validate_latlon(lat, lon, bbox=None): """Validate the lat and lon values. Return True if valid or HTTP status code if validation failed """ @@ -26,8 +26,14 @@ def validate_latlon(lat, lon): if not lat_in_world or not lon_in_world: return 400 # HTTP status code + # Use bbox if provided, otherwise default to WEST_BBOX and EAST_BBOX + if bbox: + bboxes = [bbox] + else: + bboxes = [WEST_BBOX, EAST_BBOX] + # Validate against two different BBOXes to deal with antimeridian issues - for bbox in [WEST_BBOX, EAST_BBOX]: + for bbox in bboxes: valid_lat = bbox[1] <= lat_float <= bbox[3] valid_lon = bbox[0] <= lon_float <= bbox[2] if valid_lat and valid_lon: