|
6 | 6 |
|
7 | 7 | #pylint: skip-file
|
8 | 8 |
|
9 |
| -from __future__ import absolute_import, division, print_function |
10 |
| - |
| 9 | +import math |
11 | 10 | from datetime import datetime, timedelta, timezone
|
12 | 11 |
|
13 | 12 | import datacube
|
14 | 13 | from psycopg2.extras import Json
|
15 | 14 |
|
16 | 15 | 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 |
19 | 17 | from datacube_ows.utils import get_sqlconn
|
20 | 18 |
|
21 | 19 |
|
@@ -270,25 +268,46 @@ def create_range_entry(dc, product, crses, summary_product=False):
|
270 | 268 | float(r[1]),
|
271 | 269 | epsg4326)
|
272 | 270 |
|
| 271 | + all_bboxes = bbox_projections(box, crses) |
| 272 | + |
273 | 273 | conn.execute("""
|
274 | 274 | UPDATE wms.product_ranges
|
275 | 275 | SET bboxes = %(bbox)s::jsonb
|
276 | 276 | WHERE id=%(p_id)s
|
277 | 277 | """, {
|
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), |
286 | 279 | "p_id": product.id})
|
287 | 280 |
|
288 | 281 | txn.commit()
|
289 | 282 | conn.close()
|
290 | 283 |
|
291 | 284 |
|
| 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 | + |
292 | 311 | def datasets_exist(dc, product_name):
|
293 | 312 | conn = get_sqlconn(dc)
|
294 | 313 |
|
|
0 commit comments