Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 105 deal with multiple locations #108

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,5 @@ datastore_pb2.py
datastore_pb2_grpc.py
# Ignore all the copied protobuf directories, as the root contains the source of truth.
**/protobuf
# Ignore nvim styling files
.stylua.toml
39 changes: 25 additions & 14 deletions api/routers/edr.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from utilities import split_and_strip
from utilities import validate_bbox
from utilities import verify_parameter_names
from utilities import calculate_largest_postition_deviation

router = APIRouter(prefix="/collections/observations")

Expand Down Expand Up @@ -125,6 +126,7 @@ async def get_locations(
platform_coordinates[obs.ts_mdata.platform].add(
(obs.obs_mdata[-1].geo_point.lon, obs.obs_mdata[-1].geo_point.lat)
)

# Check for inconsistent parameter definitions between stations
# TODO: How to handle those?
if obs.ts_mdata.parameter_name in all_parameters and all_parameters[obs.ts_mdata.parameter_name] != parameter:
Expand All @@ -137,25 +139,34 @@ async def get_locations(
)
all_parameters[obs.ts_mdata.parameter_name] = parameter

# Check for multiple coordinates or names on one station
# Check for multiple coordinates or names on one station'
errors = defaultdict(list)
for station_id in platform_parameters.keys():
if len(platform_coordinates[station_id]) > 1:
raise HTTPException(
status_code=500,
detail={
"coordinates": f"Station with id `{station_id} "
f"has multiple coordinates: {platform_coordinates[station_id]}"
},
)
if (
calculate_largest_postition_deviation(platform_coordinates[station_id]) < 1e-4
): # all coordinates are within 1e-4 degrees (roughly 10 m)
platform_coordinates[station_id] = {
sorted(
[i for i in platform_coordinates[station_id]],
key=lambda x: (len(str(x[0])), len(str(x[1])), x[0], x[1]),
Teddy-1000 marked this conversation as resolved.
Show resolved Hide resolved
# keys sort on number of decimals, then value of coordinates.
# This is to enshure we just coordiantes with the most precision and the same ones every time
)[-1]
}
else:
errors["coordinates"].append(
f"Station with id `{station_id} "
f"has multiple incompatible coordinates: {platform_coordinates[station_id]}"
)
if len(platform_names[station_id]) > 1:
raise HTTPException(
status_code=500,
detail={
"platform_name": f"Station with id `{station_id} "
f"has multiple names: {platform_names[station_id]}"
},
errors["platform_name"].append(
[f"Station with id `{station_id} has multiple names: {platform_names[station_id]}"]
)

if errors:
raise HTTPException(status_code=500, detail=errors)

features = [
Feature(
type="Feature",
Expand Down
9 changes: 9 additions & 0 deletions api/utilities.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime
from datetime import timedelta
from typing import Tuple
from itertools import combinations

import datastore_pb2 as dstore
from fastapi import HTTPException
Expand Down Expand Up @@ -121,3 +122,11 @@ def validate_bbox(bbox: str) -> Tuple[float, float, float, float]:
raise HTTPException(status_code=400, detail=errors)

return left, bottom, right, top


def calculate_largest_postition_deviation(positions: list) -> float:
largest_deviation = 0
for i, j in combinations(positions, 2):
if (dist := ((i[0] - j[0]) ** 2) + ((i[1] - j[1]) ** 2) ** 0.5) > largest_deviation:
largest_deviation = dist
return largest_deviation
5 changes: 5 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
ignore = E221,E226,E228,E241,W503
max-line-length = 120
exclude = ingest/src/esoh/*pb2*.py
[isort]
profile = black
[black]
line-length = 120
Teddy-1000 marked this conversation as resolved.
Show resolved Hide resolved
exclude = *pb2*.py