Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate CMIP6 lat/lons against CMIP6-specific BBOX #494

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
8 changes: 3 additions & 5 deletions routes/cmip6.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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:
Expand Down
8 changes: 3 additions & 5 deletions routes/indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
36 changes: 36 additions & 0 deletions templates/422/invalid_cmip6_latlon.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{% extends 'base.html' %}
{% block content %}
<h3>Invalid coordinates</h3>

<div id="map"></div>

<p>
Provided coordinates are outside of the valid range. Coordinates must be
within the following bounding box:
</p>
<h4 class="mt-4">Bounding box:</h4>
<p>
<strong>Latitude:</strong> <code>{{ bbox[1] }}</code> to <code>{{ bbox[3] }}</code>
</p>
<p>
<strong>Longitude:</strong> <code>{{ bbox[0] }}</code> to <code>{{ bbox[2] }}</code>
</p>


<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
<script>
var map = L.map('map', { zoomControl: false, scrollWheelZoom: false }).setView([61.0, -151.505], 5);
map.attributionControl.setPrefix('');

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);

var bbox = {{ bbox }}
var southWest = L.latLng(bbox[1], bbox[0]),
northEast = L.latLng(bbox[3], bbox[2]),
bounds = L.latLngBounds(southWest, northEast);

L.rectangle(bounds, { color: "#F1891E", weight: 3 }).addTo(map);

map.fitBounds(bounds);
</script>
{% endblock %}
10 changes: 8 additions & 2 deletions validate_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand All @@ -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:
Expand Down