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

Add code checks workflow #1

Merged
merged 3 commits into from
Aug 16, 2023
Merged
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
47 changes: 47 additions & 0 deletions .github/workflows/code_checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: code checks

on:
pull_request:
push:
branches:
- 'master'
- 'feature/**'

permissions:
contents: read # to fetch code (actions/checkout)

jobs:
code_checks:

strategy:
matrix:
os: [ ubuntu-20.04, ubuntu-22.04, windows-latest ]
python-version: ["3.9", "3.10", "3.11"]
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }} on ${{ matrix.os }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install tools
run: |
python -m pip install --upgrade pip
pip install black pytest
- name: Install dependencies
if: runner.os == 'Linux'
shell: bash
run: |
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Install dependencies
if: runner.os == 'Windows'
shell: cmd
run: |
if exist requirements.txt pip install -r requirements.txt
- name: Lint with black
run: |
python -m black --check gcmath.py
- name: Test with pytest
run: |
python -m pytest
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# HEMS lookout

[![status](https://github.com/flederwiesel/hems-lookout/actions/workflows/code_checks.yml/badge.svg)](https://github.com/flederwiesel/hems-lookout/actions/workflows/code_checks.yml)
26 changes: 14 additions & 12 deletions gcmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import math
from dataclasses import dataclass

EARTH_RADIUS = 6371.000785 # [km] as of GRS-80
EARTH_RADIUS = 6371.000785 # [km] as of GRS-80


def isclose(a: float, b: float) -> bool:
Expand All @@ -20,7 +20,7 @@ def isclose(a: float, b: float) -> bool:
return math.isclose(a, b, abs_tol=1e-9)


def deg_to_km(d: float, lat: float=0.0) -> float:
def deg_to_km(d: float, lat: float = 0.0) -> float:
"""Convert decimal degrees to km, travelling on a WGS-84 surface"""
if not isclose(lat, 0.0):
d *= math.cos(math.radians(lat))
Expand All @@ -41,6 +41,7 @@ def km_to_rad(d: float) -> float:
@dataclass
class LatLon:
"""A simple class holding latitude/longitude"""

def __init__(self, lat, lon):
self.lat = lat
self.lon = lon
Expand All @@ -67,7 +68,7 @@ def bearing(src: LatLon, dst: LatLon) -> float:
b = math.degrees(
math.atan2(
math.sin(delta),
math.cos(slat) * math.tan(dlat) - math.sin(slat) * math.cos(delta)
math.cos(slat) * math.tan(dlat) - math.sin(slat) * math.cos(delta),
)
)

Expand All @@ -86,8 +87,10 @@ def distance(src: LatLon, dst: LatLon) -> float:

return c * EARTH_RADIUS


# pylint: disable=too-many-nested-blocks,too-many-branches,too-many-statements


def travel(origin: LatLon, dist: float, bear: float) -> LatLon:
"""
Calculate the position going from one point in a
Expand All @@ -104,11 +107,9 @@ def travel(origin: LatLon, dist: float, bear: float) -> LatLon:
pos = LatLon(origin.lat, origin.lon)

if not isclose(dist, 0.0):

dist = km_to_rad(dist)

if isclose(bear, 0.0) or isclose(bear, 180.0):

dist = math.degrees(dist)

while dist > 360.0:
Expand Down Expand Up @@ -145,7 +146,6 @@ def travel(origin: LatLon, dist: float, bear: float) -> LatLon:
pos.lon += 180.0

elif isclose(bear, 90.0) or isclose(bear, 270.0):

pos.lat = origin.lat

if isclose(origin.lat, 90.0) or isclose(origin.lat, -90.0):
Expand All @@ -168,12 +168,11 @@ def travel(origin: LatLon, dist: float, bear: float) -> LatLon:
while pos.lon > 180.0:
pos.lon -= 360.0
else:

b = math.radians(90.0 - origin.lat)
a = math.acos(
math.cos(b) * math.cos(dist) +
math.sin(b) * math.sin(dist) * math.cos(math.radians(bear))
)
math.cos(b) * math.cos(dist)
+ math.sin(b) * math.sin(dist) * math.cos(math.radians(bear))
)
q = math.sin(a) * math.sin(b)

if isclose(q, 0.0):
Expand All @@ -182,8 +181,11 @@ def travel(origin: LatLon, dist: float, bear: float) -> LatLon:
C = math.acos((math.cos(dist) - math.cos(a) * math.cos(b)) / q)

pos.lat = 90.0 - math.degrees(a)
pos.lon = (origin.lon - math.degrees(C) if bear > 180.0 else
origin.lon + math.degrees(C))
pos.lon = (
origin.lon - math.degrees(C)
if bear > 180.0
else origin.lon + math.degrees(C)
)

while pos.lon <= -180.0:
pos.lon += 360.0
Expand Down