Skip to content

Commit

Permalink
Merge pull request #77 from EURODEO/issue_71-get_extent
Browse files Browse the repository at this point in the history
Issue 71 - Get Extent from the datastore.
  • Loading branch information
Jeffrey-Vervoort-KNMI authored Mar 26, 2024
2 parents 9452b90 + 80558ef commit aaecdcf
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 24 deletions.
2 changes: 1 addition & 1 deletion api/grpc_getter.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async def get_ts_ag_request(request):
return response


async def get_spatial_extent(request):
async def get_extents_request(request):
grpc_stub = get_grpc_stub()
response = await grpc_stub.GetExtents(request)
return response
44 changes: 43 additions & 1 deletion api/metadata_endpoints.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import logging
from datetime import datetime
from datetime import timezone
from typing import Dict

import datastore_pb2 as dstore
Expand All @@ -11,20 +13,44 @@
from edr_pydantic.data_queries import EDRQuery
from edr_pydantic.extent import Extent
from edr_pydantic.extent import Spatial
from edr_pydantic.extent import Temporal
from edr_pydantic.link import EDRQueryLink
from edr_pydantic.link import Link
from edr_pydantic.observed_property import ObservedProperty
from edr_pydantic.parameter import Parameter
from edr_pydantic.unit import Unit
from edr_pydantic.variables import Variables
from fastapi import HTTPException
from grpc_getter import get_extents_request
from grpc_getter import get_ts_ag_request


logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


def datetime_to_iso_string(value: datetime) -> str:
"""Returns the datetime as ISO 8601 string.
Changes timezone +00:00 to the military time zone indicator (Z).
Keyword arguments:
value -- A datetime
Returns:
datetime string -- Returns the datetime as an ISO 8601 string with the military indicator.
"""
if value.tzinfo is None:
# This sort of replicates the functionality of Pydantic's AwareDatetime type
raise ValueError("Datetime object is not timezone aware")

iso_8601_str = value.isoformat()
tz_offset_utc = "+00:00"
if iso_8601_str.endswith(tz_offset_utc):
return f"{iso_8601_str[:-len(tz_offset_utc)]}Z"
else:
return iso_8601_str


def get_landing_page(request):
return LandingPageModel(
title="E-SOH EDR API",
Expand Down Expand Up @@ -73,12 +99,28 @@ async def get_collection_metadata(base_url: str, is_self) -> Collection:

all_parameters[ts.parameter_name] = parameter

extent_request = dstore.GetExtentsRequest()
extent_response = await get_extents_request(extent_request)
spatial_extent = extent_response.spatial_extent
interval_start = extent_response.temporal_extent.start.ToDatetime(tzinfo=timezone.utc)
interval_end = extent_response.temporal_extent.end.ToDatetime(tzinfo=timezone.utc)

collection = Collection(
id="observations",
links=[
Link(href=f"{base_url}/observations", rel="self" if is_self else "data"),
],
extent=Extent(spatial=Spatial(bbox=[[3.0, 50.0, 8.0, 55.0]], crs="WGS84")), # TODO: Get this from database
extent=Extent(
spatial=Spatial(
bbox=[[spatial_extent.left, spatial_extent.bottom, spatial_extent.right, spatial_extent.top]],
crs="EPSG:4326",
),
temporal=Temporal(
interval=[[interval_start, interval_end]],
values=[f"{datetime_to_iso_string(interval_start)}/{datetime_to_iso_string(interval_end)}"],
trs="datetime",
),
),
data_queries=DataQueries(
position=EDRQuery(
link=EDRQueryLink(
Expand Down
30 changes: 15 additions & 15 deletions api/routers/edr.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ async def get_data_location_id(
str | None,
Query(
alias="parameter-name",
example="wind_from_direction_2.0_mean_PT10M,"
"wind_speed_2.0_mean_PT10M,"
"relative_humidity_2.0_mean_PT1M,"
"air_pressure_at_sea_level_2.0_mean_PT1M,"
"air_temperature_2.0_minimum_PT10M",
example="wind_from_direction:2.0:mean:PT10M,"
"wind_speed:10:mean:PT10M,"
"relative_humidity:2.0:mean:PT1M,"
"air_pressure_at_sea_level:1:mean:PT1M,"
"air_temperature:1.5:maximum:PT10M",
),
] = None,
datetime: Annotated[str | None, Query(example="2022-12-31T00:00Z/2023-01-01T00:00Z")] = None,
Expand Down Expand Up @@ -186,11 +186,11 @@ async def get_data_position(
str | None,
Query(
alias="parameter-name",
example="wind_from_direction_2.0_mean_PT10M,"
"wind_speed_2.0_mean_PT10M,"
"relative_humidity_2.0_mean_PT1M,"
"air_pressure_at_sea_level_2.0_mean_PT1M,"
"air_temperature_2.0_minimum_PT10M",
example="wind_from_direction:2.0:mean:PT10M,"
"wind_speed:10:mean:PT10M,"
"relative_humidity:2.0:mean:PT1M,"
"air_pressure_at_sea_level:1:mean:PT1M,"
"air_temperature:1.5:maximum:PT10M",
),
] = None,
datetime: Annotated[str | None, Query(example="2022-12-31T00:00Z/2023-01-01T00:00Z")] = None,
Expand Down Expand Up @@ -232,11 +232,11 @@ async def get_data_area(
str | None,
Query(
alias="parameter-name",
example="wind_from_direction_2.0_mean_PT10M,"
"wind_speed_2.0_mean_PT10M,"
"relative_humidity_2.0_mean_PT1M,"
"air_pressure_at_sea_level_2.0_mean_PT1M,"
"air_temperature_2.0_minimum_PT10M",
example="wind_from_direction:2.0:mean:PT10M,"
"wind_speed:10:mean:PT10M,"
"relative_humidity:2.0:mean:PT1M,"
"air_pressure_at_sea_level:1:mean:PT1M,"
"air_temperature:1.5:maximum:PT10M",
),
] = None,
datetime: Annotated[str | None, Query(example="2022-12-31T00:00Z/2023-01-01T00:00Z")] = None,
Expand Down
4 changes: 2 additions & 2 deletions api/routers/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from fastapi import Query
from geojson_pydantic import Feature
from geojson_pydantic import FeatureCollection
from grpc_getter import get_extents_request
from grpc_getter import get_obs_request
from grpc_getter import get_spatial_extent
from jinja2 import Environment
from jinja2 import FileSystemLoader
from jinja2 import select_autoescape
Expand Down Expand Up @@ -122,7 +122,7 @@ async def get_time_series_by_id(
async def get_dataset_metadata():
# need to get spatial extent.
spatial_request = dstore.GetExtentsRequest()
extent = await get_spatial_extent(spatial_request)
extent = await get_extents_request(spatial_request)
dynamic_fields = {
"spatial_extents": [
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,25 @@
"spatial": {
"bbox": [
[
3.0,
50.0,
8.0,
55.0
-68.2758333,
12.13,
7.1493220605216,
55.399166666667
]
],
"crs": "WGS84"
"crs": "EPSG:4326"
},
"temporal": {
"interval": [
[
"2022-12-31T00:00:00Z",
"2022-12-31T23:50:00Z"
]
],
"values": [
"2022-12-31T00:00:00Z/2022-12-31T23:50:00Z"
],
"trs": "datetime"
}
},
"data_queries": {
Expand Down

1 comment on commit aaecdcf

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API Unit Test Coverage Report
FileStmtsMissCoverMissing
\_\_init\_\_.py00100% 
datastore_pb2.py584621%24–69
datastore_pb2_grpc.py432347%37–52, 85–87, 92–94, 99–101, 106–108, 112–136, 174, 191, 208, 225
grpc_getter.py201145%15–16, 20–23, 27–29, 33–35
locustfile.py15150%1–31
main.py34585%41, 51–52, 62–63
metadata_endpoints.py552555%42–51, 55, 72–151, 155
utilities.py683154%33, 40, 62–65, 73–80, 85–92, 104–123
custom_geo_json
   edr_feature_collection.py60100% 
formatters
   \_\_init\_\_.py110100% 
   covjson.py50198%73
   geojson.py15193%42
routers
   \_\_init\_\_.py00100% 
   edr.py972673%59–134, 214–215, 259–260
   feature.py451958%70–103, 113–118, 124–146
TOTAL51720361% 

API Unit Test Coverage Summary

Tests Skipped Failures Errors Time
17 0 💤 0 ❌ 0 🔥 1.834s ⏱️

Please sign in to comment.