Skip to content

Commit 94e075f

Browse files
authored
Merge pull request #828 from opendatacube/bounding_boxes_for_global_datasets
Attempt to sanity-check and sanitise bounding box ranges for global datasets
2 parents 4a3a4d6 + d772d1f commit 94e075f

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

datacube_ows/product_ranges.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@
66

77
#pylint: skip-file
88

9-
from __future__ import absolute_import, division, print_function
10-
9+
import math
1110
from datetime import datetime, timedelta, timezone
1211

1312
import datacube
1413
from psycopg2.extras import Json
1514

1615
from datacube_ows.ogc_utils import NoTimezoneException, tz_for_coord
17-
from datacube_ows.ows_configuration import \
18-
get_config # , get_layers, ProductLayerDef
16+
from datacube_ows.ows_configuration import get_config
1917
from datacube_ows.utils import get_sqlconn
2018

2119

@@ -270,25 +268,46 @@ def create_range_entry(dc, product, crses, summary_product=False):
270268
float(r[1]),
271269
epsg4326)
272270

271+
all_bboxes = bbox_projections(box, crses)
272+
273273
conn.execute("""
274274
UPDATE wms.product_ranges
275275
SET bboxes = %(bbox)s::jsonb
276276
WHERE id=%(p_id)s
277277
""", {
278-
"bbox": Json(
279-
{crsid: {"top": box.to_crs(crs).boundingbox.top,
280-
"bottom": box.to_crs(crs).boundingbox.bottom,
281-
"left": box.to_crs(crs).boundingbox.left,
282-
"right": box.to_crs(crs).boundingbox.right}
283-
for crsid, crs in crses.items()
284-
}
285-
),
278+
"bbox": Json(all_bboxes),
286279
"p_id": product.id})
287280

288281
txn.commit()
289282
conn.close()
290283

291284

285+
def bbox_projections(starting_box, crses):
286+
result = {}
287+
for crsid, crs in crses.items():
288+
if crs.valid_region is not None:
289+
clipped_crs_bbox = (starting_box & crs.valid_region).to_crs(crs).boundingbox
290+
else:
291+
clipped_crs_bbox = None
292+
if clipped_crs_bbox is not None:
293+
result[crsid] = jsonise_bbox(clipped_crs_bbox)
294+
else:
295+
projbbox = starting_box.to_crs(crs).boundingbox
296+
result[crsid] = sanitise_bbox(projbbox)
297+
return result
298+
299+
300+
def sanitise_bbox(bbox):
301+
def sanitise_coordinate(coord, fallback):
302+
return coord if math.isfinite(coord) else fallback
303+
return {
304+
"top": sanitise_coordinate(bbox.top, float("9.999999999e99")),
305+
"bottom": sanitise_coordinate(bbox.bottom, float("-9.999999999e99")),
306+
"left": sanitise_coordinate(bbox.left, float("-9.999999999e99")),
307+
"right": sanitise_coordinate(bbox.right, float("9.999999999e99")),
308+
}
309+
310+
292311
def datasets_exist(dc, product_name):
293312
conn = get_sqlconn(dc)
294313

0 commit comments

Comments
 (0)