-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidate_request.py
More file actions
618 lines (509 loc) · 22.4 KB
/
validate_request.py
File metadata and controls
618 lines (509 loc) · 22.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
"""
A module to validate request parameters such as latitude and longitude for use across multiple endpoints.
"""
import asyncio
import ast
import rasterio
import os.path
import os
from flask import render_template
from pyproj import Transformer
import numpy as np
import pandas as pd
import geopandas as gpd
from shapely.geometry import shape
from rasterio.crs import CRS
from config import WEST_BBOX, EAST_BBOX, SEAICE_BBOX
from generate_urls import generate_wfs_places_url
from fetch_data import fetch_data
from luts import geotiff_projections
def check_poly_in_geotiffs(polygon, coverages):
"""Check if a polygon is completely within the data footprint of binary GeoTIFFs.
Args:
polygon (GeoDataFrame): GeoDataFrame representing the polygon to check; if multiple polygons are present, only the first will be evaluated.
coverages (list): List of coverages to check for data availability.
Returns:
True if valid or if there is a problem processing the geotiff; HTTP 404 status code if no data was found.
"""
for coverage in coverages:
reference_geotiff = "geotiffs/" + coverage + ".tif"
# Skip if the GeoTIFF file does not exist.
if not os.path.isfile(reference_geotiff):
return True
try:
with rasterio.open(reference_geotiff) as dataset:
# reproject polygon to match geotiff CRS
if coverage in geotiff_projections:
crs = geotiff_projections[coverage]
else:
crs = "EPSG:3338"
polygon_proj = polygon.to_crs(crs)
# read binary mask from geotiff, convert to uint8 for rasterio
mask = dataset.read(1)
mask = mask.astype("uint8")
transform = dataset.transform
# convert binary mask to geometries
mask_shapes = []
for geom, value in rasterio.features.shapes(mask, transform=transform):
if value == 1:
mask_shapes.append(shape(geom))
# create a unified geometry from the mask shapes
mask_geometry = gpd.GeoSeries(mask_shapes, crs=crs).unary_union
# check if the polygon is completely within the mask geometry
if polygon_proj.geometry.iloc[0].within(mask_geometry):
return True
else:
return 404
except Exception as e:
print(f"Error processing GeoTIFF {reference_geotiff}: {e}")
return True
def check_geotiffs(lat, lon, coverages):
"""Load a binary GeoTIFF mask corresponding to the coverage(s) requested, then use this to check if lat/lon has data available.
Args:
lat (int or float): latitude
lon (int or float): longitude
coverages (list): list of coverages to check for data availability
Returns:
True if valid, or HTTP 404 status code if no data was found
"""
for coverage in coverages:
# Use the same GeoTIFF for all CMIP6 downscaled coverages.
if coverage.startswith("cmip6_downscaled_"):
coverage = "cmip6_downscaled"
reference_geotiff = "geotiffs/" + coverage + ".tif"
# Do not perform GeoTIFF check if the file does not exist.
if not os.path.isfile(reference_geotiff):
return True
# Do not perform GeoTIFF check if the file does not open properly.
# This seems safer than the alternative of hiding data due to a corrupt file.
try:
with rasterio.open(reference_geotiff) as dataset:
if coverage in geotiff_projections:
crs = geotiff_projections[coverage]
else:
crs = "EPSG:3338"
if crs == "EPSG:4326":
# check if lat/lon is within the dataset's geographic bounds
if (
dataset.bounds.left <= lon <= dataset.bounds.right
and dataset.bounds.bottom <= lat <= dataset.bounds.top
):
row, col = dataset.index(lon, lat)
if dataset.read(1)[row, col] == 1:
return True
else:
x, y = project_latlon(lat, lon, crs)
row, col = dataset.index(x, y)
if 0 <= row < dataset.height and 0 <= col < dataset.width:
if dataset.read(1)[row, col] == 1:
return True
except:
return True
return 404
def latlon_is_numeric_and_in_geodetic_range(lat, lon):
"""Validate that the lat and lon values are numeric and within the decimal degree boundaries of +- 90 (lat) and +- 180 (lon).
Args:
lat (int or float): latitude
lon (int or float): longitude
Returns:
True if valid, or HTTP 400 status code if validation failed
"""
try:
lat_float = float(lat)
lon_float = float(lon)
except:
return 400
lat_in_world = -90 <= lat_float <= 90
lon_in_world = -180 <= lon_float <= 180
if not lat_in_world or not lon_in_world:
return 400
return True
def validate_latlon(lat, lon, coverages=[]):
"""Validate the lat and lon values.
Return True if valid or HTTP status code if validation failed
"""
try:
lat_float = float(lat)
lon_float = float(lon)
except:
return 400 # HTTP status code
lat_in_world = -90 <= lat_float <= 90
lon_in_world = -180 <= lon_float <= 180
if not lat_in_world or not lon_in_world:
return 400 # HTTP status code
# Validate against two different BBOXes to deal with antimeridian issues
within_a_bbox = False
for bbox in [WEST_BBOX, EAST_BBOX]:
valid_lat = bbox[1] <= lat_float <= bbox[3]
valid_lon = bbox[0] <= lon_float <= bbox[2]
if valid_lat and valid_lon:
within_a_bbox = True
if not within_a_bbox:
return 422
if len(coverages) > 0:
return check_geotiffs(lat, lon, coverages)
return True
def validate_seaice_latlon(lat, lon, coverages):
"""Validate the lat and lon values for pan arctic sea ice.
Return True if valid or HTTP status code if validation failed
"""
try:
lat_float = float(lat)
lon_float = float(lon)
except:
return 400 # HTTP status code
lat_in_world = -90 <= lat_float <= 90
lon_in_world = -180 <= lon_float <= 180
if not lat_in_world or not lon_in_world:
return 400 # HTTP status code
# Validate against two different BBOXes to deal with antimeridian issues
within_a_bbox = False
for bbox in [SEAICE_BBOX]:
valid_lat = bbox[1] <= lat_float <= bbox[3]
valid_lon = bbox[0] <= lon_float <= bbox[2]
if valid_lat and valid_lon:
within_a_bbox = True
if not within_a_bbox:
return 422
if len(coverages) > 0:
return check_geotiffs(lat, lon, coverages)
return True
def validate_bbox(lat1, lon1, lat2, lon2):
"""Validate a bounding box given lat lon values
LL: (lat1, lon), UR: (lat2, lon2)
"""
lat_valid = lat1 < lat2
validations = [lat_valid]
# validate all four corners
for lat in [lat1, lat2]:
for lon in [lon1, lon2]:
validations.append(validate_latlon(lat, lon))
# Prioritize HTTP 400 errors over HTTP 422 errors
if 400 in validations:
return 400
if 422 in validations:
return 422
valid = np.all(validations)
return valid
def validate_seaice_year(start_year, end_year):
if start_year is not None:
if int(start_year) < 1850 or int(start_year) > 2021:
return 400
if end_year is not None:
if int(end_year) < 1850 or int(end_year) > 2021:
return 400
return True
def validate_year(start_year, end_year):
if (
1900 < int(start_year) <= 2100
and 1900 < int(end_year) <= 2100
and int(start_year) <= int(end_year)
):
return True
else:
return 400
def validate_var_id(var_id):
if not var_id.isalnum():
return render_template("400/bad_request.html"), 400
var_id_check = asyncio.run(
fetch_data(
[generate_wfs_places_url("all_boundaries:all_areas", "type", var_id, "id")]
)
)
if var_id_check["numberMatched"] > 0:
return var_id_check["features"][0]["properties"]["type"]
else:
return render_template("422/invalid_area.html"), 400
def project_latlon(lat1, lon1, dst_crs, lat2=None, lon2=None):
"""Reproject lat lon coords
Args:
lat1 (float): latitude (single point) or southern bound (bbox)
lon1 (float): longitude (single point) or western bound (bbox)
dst_crs (int): EPSG code for the destination
coordinate reference system
lat2 (float): northern bound (bbox)
lon2 (float): eastern bound (bbox)
Returns:
Reprojected coordinates in order x, y
"""
transformer = Transformer.from_crs(4326, dst_crs)
if lat2 is None:
projected_coords = transformer.transform(lat1, lon1)
else:
x1, y1 = transformer.transform(lat1, lon1)
x2, y2 = transformer.transform(lat2, lon2)
projected_coords = (x1, y1, x2, y2)
return projected_coords
def get_x_y_axes(coverage_metadata):
"""Extract the X and Y axes from the coverage metadata.
We're doing this because we won't always know the axis ordering and position that come from Rasdaman. They are usually the last two axes, but their exact numbering might depend on on how many axes the coverage has. So we can iterate through the axes and find the ones with the axisLabel "X" and "Y" or "lon" and "lat" and grab them with `next()`.
Args:
coverage_metadata (dict): JSON-like dictionary containing coverage metadata
Returns:
tuple: A tuple containing the X and Y axes metadata
"""
# CP note: you'll notice that we reverse the order of the return when lat-lon labels are found - this is a follow-on impact of how coordinate transformers work when we don't specify the ordering, e.g., always x-y
try:
x_axis = next(
axis
for axis in coverage_metadata["domainSet"]["generalGrid"]["axis"]
if axis["axisLabel"] == "X" or axis["axisLabel"] == "lon"
)
y_axis = next(
axis
for axis in coverage_metadata["domainSet"]["generalGrid"]["axis"]
if axis["axisLabel"] == "Y" or axis["axisLabel"] == "lat"
)
if x_axis["axisLabel"] == "lon" and y_axis["axisLabel"] == "lat":
return y_axis, x_axis
return x_axis, y_axis
except (KeyError, StopIteration):
raise ValueError("Unexpected coverage metadata: 'X' or 'Y' axis not found")
def generate_time_index_from_coverage_metadata(meta):
"""Generate a pandas DatetimeIndex from the ansi/time axis coordinates
in the coverage description metadata.
Args:
meta (dict): JSON-like dictionary containing coverage metadata
Returns:
pd.DatetimeIndex: corresponding to the ansi (i.e. time) axis coordinates
"""
try:
# we won't always know the axis positioning / ordering
ansi_axis = next(
axis
for axis in meta["domainSet"]["generalGrid"]["axis"]
if axis["axisLabel"] == "ansi" or axis["axisLabel"] == "time"
)
# this is a list of dates formatted like "1996-11-03T00:00:00.000Z"
ansi_coordinates = ansi_axis["coordinate"]
date_index = pd.DatetimeIndex(ansi_coordinates)
return date_index
except (KeyError, StopIteration):
raise ValueError(
"Unexpected coverage metadata: 'ansi' or 'time' axis not found"
)
def validate_xy_in_coverage_extent(
x,
y,
coverage_metadata,
east_tolerance=None,
west_tolerance=None,
north_tolerance=None,
south_tolerance=None,
):
"""Validate if the x and y coordinates are within the bounding box of the coverage.
Args:
x (float): x-coordinate
y (float): y-coordinate
coverage_metadata (dict): JSON-like dictionary containing coverage metadata
east_tolerance (float): Optional tolerance to expand the bounding box to the east, will be in units native to the coverage, e.g. meters for EPSG:3338. This parameter is included to hedge against the edge: query locations where the query is within the geographic bounding box, but not the projected bounding box due to distortion at the edges of conical projections. Without this, users may experience errors because it is possible for a geographic point to be within the geographic bounding box, but the same point, projected, to be outside the projected bounding box.
west_tolerance (float): Optional tolerance to expand the bounding box to the west
north_tolerance (float): Optional tolerance to expand the bounding box to the north
south_tolerance (float): Optional tolerance to expand the bounding box to the south
Returns:
bool: True if the coordinates are within the bounding box, False otherwise
"""
try:
x_axis, y_axis = get_x_y_axes(coverage_metadata)
if west_tolerance:
x_axis["lowerBound"] -= west_tolerance
if east_tolerance:
x_axis["upperBound"] += east_tolerance
if north_tolerance:
y_axis["upperBound"] += north_tolerance
if south_tolerance:
y_axis["upperBound"] -= south_tolerance
x_in_bounds = x_axis["lowerBound"] <= x <= x_axis["upperBound"]
y_in_bounds = y_axis["lowerBound"] <= y <= y_axis["upperBound"]
return x_in_bounds and y_in_bounds
except ValueError:
return False
def construct_latlon_bbox_from_coverage_bounds(coverage_metadata):
"""Construct a bounding box from the coverage metadata.
We use this to trigger a 422 error if the user's lat/lon is outside the coverage bounds and avoid polluting the config with hardcoded bounding boxes for each coverage.
Args:
coverage_metadata (dict): JSON-like dictionary containing coverage metadata
Returns:
list: containing the bounding box [lon_min, lat_min, lon_max, lat_max]
"""
try:
transformer = Transformer.from_crs(
coverage_metadata["envelope"]["srsName"].split("/")[-1], 4326
)
except KeyError:
raise ValueError(
"Unexpected coverage metadata: Could not parse CRS via `srsName` key."
)
try:
x_axis, y_axis = get_x_y_axes(coverage_metadata)
# CP note: seems like the tuple unpacking should be reversed, but I'm matching the coordinate ordering of the example BBOXES in config.py
lat_min, lon_min = transformer.transform(
x_axis["lowerBound"], y_axis["lowerBound"]
)
lat_max, lon_max = transformer.transform(
x_axis["upperBound"], y_axis["upperBound"]
)
bbox = [
round(lon_min, 4),
round(lat_min, 4),
round(lon_max, 4),
round(lat_max, 4),
]
return bbox
except ValueError:
raise ValueError(
"Unexpected coverage metadata: lower or upper spatial bounds not found."
)
def validate_latlon_in_bboxes(lat, lon, bboxes, coverages=[]):
"""Validate if a lat and lon are within a list of bounding boxes.
Args:
lat (float): latitude
lon (float): longitude
bboxes (list): list of bounding boxes in the format [lon_min, lat_min, lon_max, lat_max]
Returns:
bool: True if the coordinates are within the bounding boxes, else 422
"""
lat = float(lat)
lon = float(lon)
within_a_bbox = False
for bbox in bboxes:
valid_lat = bbox[1] <= lat <= bbox[3]
valid_lon = bbox[0] <= lon <= bbox[2]
if valid_lat and valid_lon:
within_a_bbox = True
if not within_a_bbox:
return 422
if len(coverages) > 0:
return check_geotiffs(lat, lon, coverages)
return True
def get_coverage_encodings(coverage_metadata):
"""Extract the encoding dictionary from a coverage's metadata obtained via describe_via_wcps.
This function extracts the "Encoding" component from a coverage's metadata, which typically matches the integer values used for axis labels and positions to descriptive strings (e.g., mapping coordinate values to model names, scenarios, variables, etc.)
Args:
coverage_metadata (dict): JSON-like dictionary containing coverage metadata from describe_via_wcps()
Returns:
dict: A dictionary mapping axis names to their encoding dictionaries. Each encoding dictionary maps integer values to their descriptive strings.
Raises:
ValueError: If the coverage metadata doesn't contain the expected encoding information
Example:
>>> metadata = await describe_via_wcps("alfresco_relative_flammability_30yr")
>>> encodings = get_coverage_encodings(metadata)
>>> print(encodings)
{
'era': {0: '1950-1979', 1: '1980-2008', ...},
'model': {0: 'MODEL-SPINUP', 2: 'GFDL-CM3', ...},
'scenario': {0: 'historical', 1: 'rcp45', ...}
}
"""
try:
# encoding **should** be in the metadata in zeroth slice
metadata = coverage_metadata.get("metadata", {})
slices = metadata.get("slices", {}).get("slice", [])
if not slices:
encoding_str = metadata.get("Encoding")
else:
# get encoding string from first slice (all slices contain the same encoding)
encoding_str = slices[0].get("Encoding")
if not encoding_str:
raise ValueError("No encoding information found in coverage metadata")
# convert the string representation of dict to actual dict
try:
encodings = ast.literal_eval(encoding_str)
except (SyntaxError, ValueError) as e:
raise ValueError(f"Failed to parse encoding string: {str(e)}")
# convert string keys to ints
for dim in encodings:
if isinstance(encodings[dim], dict):
encodings[dim] = {int(k): v for k, v in encodings[dim].items()}
return encodings
except (KeyError, TypeError) as e:
raise ValueError(f"Invalid coverage metadata format: {str(e)}")
def get_axis_coordinate_values(coverage_metadata):
"""
Get axis coordinate values from the JSON output describing a coverage. These are the literal
values defining each axis, and are usually integers e.g., [0, 1, 2, 3, 4] for an axis with 5 positions.
This function extracts these coordinate values for each axis defined in the coverage metadata,
and returns them in a dictionary format with axis names as keys.
Args:
coverage_axis_metadata (dict): JSON-like dictionary containing the coverage axes description.
Returns:
dict: A dictionary where each axis (key) maps to its respective coordinates (value).
Raises:
ValueError: If required information is missing in the JSON data.
Example:
>>> metadata = await describe_via_wcps("alfresco_relative_flammability_30yr")
>>> coord_values = get_axis_coordinate_values(metadata)
>>> print(coord_values)
{
"era": [0, 1, 2, 3, 4],
"model": [0, 1, 2, 3, 4, 5, 6],
"scenario": [0, 1, 2, 3]
}
"""
try:
# Navigate to the generalGrid section which contains the axis coordinate values
domain_set = coverage_metadata.get("domainSet", {})
general_grid = domain_set.get("generalGrid", {})
axes = general_grid.get("axis", [])
# Extract coordinate values for each axis
axis_coords = {}
for axis in axes:
axis_label = axis.get("axisLabel")
coordinates = axis.get("coordinate", [])
if axis_label and coordinates:
axis_coords[axis_label] = coordinates
if not axis_coords:
raise ValueError(
"No axis coordinate values found in the coverage metadata."
)
return axis_coords
except (KeyError, TypeError) as e:
raise ValueError(f"Invalid coverage metadata format: {str(e)}")
def get_axis_encodings(coverage_metadata, encoding_attr="encoding"):
"""
Get axis encodings from the JSON output describing a coverage. This is the dictionary that maps
integer values used for axis labels and positions to descriptive strings, which can be used
to decode the meaning of each axis value.
Args:
coverage_axis_metadata (dict): JSON-like dictionary containing the coverage axes description.
encoding_attr (str): The axis attribute that stores the encoding dictionary.
Returns:
dict: A dictionary with axis names as keys and their encoding dictionaries as values.
Encoding attributes are coverted from string to dict.
If an axis does not have the specified encoding attribute, it will map to None.
Raises:
ValueError: If required information is missing in the JSON data.
"""
try:
metadata = coverage_metadata.get("metadata", {})
axes = metadata.get("axes", {})
axis_encodings = {}
for axis in axes:
encoding_str = metadata.get("axes").get(axis).get(encoding_attr, None)
# convert the string representation of dict to actual dict
try:
encodings = ast.literal_eval(encoding_str)
except (SyntaxError, ValueError) as e:
raise ValueError(f"Failed to parse encoding string: {str(e)}")
# convert string keys to ints
for dim in encodings:
if isinstance(encodings[dim], dict):
encodings[dim] = {int(k): v for k, v in encodings[dim].items()}
axis_encodings[axis] = encodings
except (KeyError, TypeError) as e:
raise ValueError(f"Invalid coverage metadata format: {str(e)}")
return axis_encodings
def get_coverage_crs_str(coverage_metadata):
try:
# Navigate to the generalGrid section which contains the CRS information
domain_set = coverage_metadata.get("domainSet", {})
general_grid = domain_set.get("generalGrid", {})
srs_string = general_grid.get("srsName", [])
epsg_code_str = srs_string.split("EPSG/0/")[1]
# get rasterio CRS object from EPSG code
crs = CRS.from_epsg(int(epsg_code_str))
except:
raise ValueError(
"Unexpected coverage metadata: 'srsName' not found or metadata otherwise invalid."
)
return crs.to_string()