|
| 1 | +"""STAC helpers.""" |
| 2 | + |
| 3 | +# Copyright 2025, European Union. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import datetime |
| 18 | + |
| 19 | +import pydantic |
| 20 | +import stac_pydantic |
| 21 | +import structlog |
| 22 | + |
| 23 | +from . import database |
| 24 | + |
| 25 | +logger = structlog.getLogger(__name__) |
| 26 | + |
| 27 | + |
| 28 | +WRONG_BBOX_LOGGED_IDS: set[str] = set() |
| 29 | + |
| 30 | + |
| 31 | +def get_extent( |
| 32 | + record: database.Resource, |
| 33 | +) -> dict: |
| 34 | + """Get extent from model.""" |
| 35 | + spatial = record.geo_extent or {} |
| 36 | + |
| 37 | + west = float(spatial.get("bboxW", -180)) |
| 38 | + south = float(spatial.get("bboxS", -90)) |
| 39 | + east = float(spatial.get("bboxE", 180)) |
| 40 | + north = float(spatial.get("bboxN", 90)) |
| 41 | + |
| 42 | + try: |
| 43 | + spatial_extent = stac_pydantic.collection.SpatialExtent( |
| 44 | + bbox=[(west, south, east, north)], |
| 45 | + ) |
| 46 | + except pydantic.ValidationError as e: |
| 47 | + # 0-360 longitude values are considered valid |
| 48 | + if ( |
| 49 | + 0 <= west <= 360 or 0 <= east <= 360 |
| 50 | + ) and "Bounding box must be within (-180, -90, 180, 90)" in str(e): |
| 51 | + spatial_extent = stac_pydantic.collection.SpatialExtent.model_construct( |
| 52 | + bbox=[(west, south, east, north)] |
| 53 | + ) |
| 54 | + else: |
| 55 | + if record.resource_uid not in WRONG_BBOX_LOGGED_IDS: |
| 56 | + # this log is important, but we don't want to flood the logs |
| 57 | + WRONG_BBOX_LOGGED_IDS.add(str(record.resource_uid)) |
| 58 | + logger.warning( |
| 59 | + "Bbox stac_pydantic validation failed, fallback to whole world", |
| 60 | + error=e, |
| 61 | + id=record.resource_uid, |
| 62 | + ) |
| 63 | + spatial_extent = stac_pydantic.collection.SpatialExtent( |
| 64 | + bbox=[(-180, -90, 180, 90)] |
| 65 | + ) |
| 66 | + |
| 67 | + begin_date_value = getattr(record, "begin_date", None) |
| 68 | + end_date_value = getattr(record, "end_date", None) |
| 69 | + |
| 70 | + return stac_pydantic.collection.Extent( |
| 71 | + spatial=spatial_extent, |
| 72 | + temporal=stac_pydantic.collection.TimeInterval( |
| 73 | + interval=[ |
| 74 | + [ |
| 75 | + ( |
| 76 | + # We have datetime.date on DB, while STAC requires datetime with timezone |
| 77 | + datetime.datetime.combine( |
| 78 | + begin_date_value, datetime.datetime.min.time() |
| 79 | + ).replace(tzinfo=datetime.timezone.utc) |
| 80 | + if begin_date_value |
| 81 | + else None |
| 82 | + ), |
| 83 | + ( |
| 84 | + # We have datetime.date on DB, while STAC requires datetime with timezone |
| 85 | + datetime.datetime.combine( |
| 86 | + end_date_value, datetime.datetime.min.time() |
| 87 | + ).replace(tzinfo=datetime.timezone.utc) |
| 88 | + if end_date_value |
| 89 | + else None |
| 90 | + ), |
| 91 | + ] |
| 92 | + ], |
| 93 | + ), |
| 94 | + ).model_dump() |
0 commit comments