Skip to content

Commit

Permalink
Merge pull request #12 from dnv-opensource/11-add-domain-specific-dat…
Browse files Browse the repository at this point in the history
…a-types-for-ship-situation-etc-using-pydantic-models

11 add domain specific data types for ship situation etc using pydantic models
  • Loading branch information
tomarnepedersen authored Nov 27, 2023
2 parents 8b41c3e + 509ff30 commit 544ddef
Show file tree
Hide file tree
Showing 18 changed files with 1,157 additions and 700 deletions.
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import sys

try:
import tomli as tomli
import tomli as tomli # pyright: ignore
except ImportError:
# for Python >= 3.11
import tomllib as tomli
import tomllib as tomli # pyright: ignore

with open("../pyproject.toml", "rb") as f:
toml = tomli.load(f)
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ basemap = "^1.3.8"
basemap-data-hires = "^1.3.2"
global-land-mask = "^1.0.0"
folium = "^0.14.0"
pydantic = "^2.5"

[tool.poetry.group.dev]
optional = true
Expand Down Expand Up @@ -128,7 +129,7 @@ extraPaths = ["./src"]
typeCheckingMode = "basic"
useLibraryCodeForTypes = true
# Activate the following rules step by step to (step by step..) improve code quality
# reportMissingParameterType = "error"
reportMissingParameterType = "error"
# reportUnknownParameterType = "warning"
# reportUnknownMemberType = "warning"
# reportMissingTypeArgument = "error"
Expand Down
8 changes: 2 additions & 6 deletions src/trafficgen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from .check_land_crossing import path_crosses_land

from .write_traffic_situation_to_file import write_traffic_situations_to_json_file
from .write_traffic_situation_to_file import clean_traffic_situation_data

from .encounter import generate_encounter
from .encounter import check_encounter_evolvement
Expand All @@ -39,10 +38,9 @@
from .read_files import read_situation_files
from .read_files import read_own_ship_file
from .read_files import read_target_ship_files
from .read_files import read_encounter_setting_file
from .read_files import read_encounter_settings_file

from .ship_traffic_generator import generate_traffic_situations
from .ship_traffic_generator import find_value


__all__ = [
Expand All @@ -60,7 +58,6 @@
"ssa",
"path_crosses_land",
"write_traffic_situations_to_json_file",
"clean_traffic_situation_data",
"generate_encounter",
"check_encounter_evolvement",
"find_start_position_target_ship",
Expand All @@ -75,11 +72,10 @@
"update_position_data_own_ship",
"update_position_data_target_ship",
"decide_target_ship",
"find_value",
"read_situation_files",
"read_own_ship_file",
"read_target_ship_files",
"read_encounter_setting_file",
"read_encounter_settings_file",
"plot_traffic_situations",
"plot_specific_traffic_situation",
]
22 changes: 15 additions & 7 deletions src/trafficgen/check_land_crossing.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
"""Module with helper functions to determine if a generated path is crossing land."""

from typing import List

from global_land_mask import globe

from trafficgen.types import Position

from . import calculate_position_at_certain_time, deg_2_rad, flat2llh, rad_2_deg


def path_crosses_land(position_1, speed, course, lat_lon_0, time_interval=50):
def path_crosses_land(
position_1: Position,
speed: float,
course: float,
lat_lon_0: List[float],
time_interval: float = 50.0,
) -> bool:
"""
Find if path is crossing land.
Expand All @@ -21,19 +31,17 @@ def path_crosses_land(position_1, speed, course, lat_lon_0, time_interval=50):
is_on_land: True if parts of the path crosses land.
"""

north_1 = position_1["north"]
east_1 = position_1["east"]
north_1 = position_1.north
east_1 = position_1.east
lat_0 = lat_lon_0[0]
lon_0 = lat_lon_0[1]

num_checks = 10
for i in range(int(time_interval / num_checks)):
position_2 = calculate_position_at_certain_time(
{"north": north_1, "east": east_1}, speed, course, i * time_interval / num_checks
)
lat, lon, _ = flat2llh(
position_2["north"], position_2["east"], deg_2_rad(lat_0), deg_2_rad(lon_0)
Position(north=north_1, east=east_1), speed, course, i * time_interval / num_checks
)
lat, lon, _ = flat2llh(position_2.north, position_2.east, deg_2_rad(lat_0), deg_2_rad(lon_0))
lat = rad_2_deg(lat)
lon = rad_2_deg(lon)
if globe.is_land(lat, lon):
Expand Down
41 changes: 22 additions & 19 deletions src/trafficgen/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# pyright: reportMissingParameterType=false
"""CLI for trafficgen package."""

import contextlib
import logging
import sys
from os.path import join
from pathlib import Path

import click
Expand All @@ -20,12 +22,12 @@
# if you change the below defaults, then remember to change the description of
# the default values in below @click.option descriptions,
# and docs/usage.rst
default_data_path = Path(__file__).parent.parent.parent / "data"
sit_folder = join(default_data_path, "baseline_situations_input/")
own_ship_file = join(default_data_path, "own_ship/own_ship.json")
target_ship_folder = join(default_data_path, "target_ships/")
settings_file = Path(__file__).parent / "settings" / "encounter_settings.json"
output_folder = join(default_data_path, "test_output/")
default_data_path: Path = Path(__file__).parent.parent.parent / "data"
situation_folder: Path = default_data_path / "baseline_situations_input"
own_ship_file: Path = default_data_path / "own_ship/own_ship.json"
target_ship_folder: Path = default_data_path / "target_ships"
settings_file: Path = Path(__file__).parent / "settings" / "encounter_settings.json"
output_folder: Path = default_data_path / "test_output"


@click.group()
Expand All @@ -43,9 +45,9 @@ def main(args=None):
@click.option(
"-s",
"--situations",
help="Folders with situations (default=./baseline_situations_input/)",
help="Path to folder with situations (default=./baseline_situations_input/)",
type=click.Path(exists=True),
default=sit_folder,
default=situation_folder,
show_default=True,
)
@click.option(
Expand Down Expand Up @@ -123,10 +125,10 @@ def gen_situation(
"""
click.echo("Generating traffic situations")
generated_traffic_situations = generate_traffic_situations(
situation_folder=situations,
own_ship_file=own_ship,
target_ship_folder=targets,
settings_file=settings,
situation_folder=Path(situations),
own_ship_file=Path(own_ship),
target_ship_folder=Path(targets),
settings_file=Path(settings),
)

if visualize:
Expand All @@ -137,20 +139,21 @@ def gen_situation(
# so it can safely be ignored by users without generating an error msg,
# and so that if a user specifies a value of zero or negative number,
# the user will get an error message.
try:

# Ignore TypeError
# TypeError is thrown in case a value for a parameter is not defined.
# In such case, though, we safely ignore that parameter :)
with contextlib.suppress(TypeError):
if visualize_situation > 0:
click.echo("Plotting a specific traffic situation")
plot_specific_traffic_situation(generated_traffic_situations, visualize_situation)
else:
click.echo(
"Invalid traffic situation number specified, not creating map plot. See --help for more info."
) # noqa: E501
except TypeError:
pass # do nothing, value not defined, so we safely ignore this parameter :)

)
if output is not None:
click.echo("Writing traffic situations to files")
write_traffic_situations_to_json_file(generated_traffic_situations, write_folder=output)
write_traffic_situations_to_json_file(generated_traffic_situations, write_folder=Path(output))


main.add_command(gen_situation)
Expand Down
Loading

0 comments on commit 544ddef

Please sign in to comment.