-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added task import from file functions
- Loading branch information
Antonio Golfari
committed
Jun 3, 2024
1 parent
a38d9b6
commit acad134
Showing
8 changed files
with
263 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
""" | ||
Task Planner Library | ||
contains methods to import Task from Tonino Tarsi's TasK Creator .tsk file | ||
https://www.vololiberomontecucco.it/taskcreator/ | ||
Use: from sources.taskplanner import TaskPlanner | ||
Antonio Golfari - 2024 | ||
""" | ||
|
||
from . import utils | ||
from lxml import etree | ||
from pathlib import Path | ||
|
||
|
||
def read_tsk_file(file: Path) -> "etree | None": | ||
|
||
return utils.read_xml_file(file) | ||
|
||
|
||
def read_task(root: etree) -> dict: | ||
"""creates a dict file with all task information to be imported from Airscore""" | ||
|
||
task_info = {} | ||
|
||
if len(root): | ||
rte = root.find('rte') | ||
# task info | ||
task_info['task_type'] = rte.find('type').text # 'race', 'elapsed_time' | ||
# gates info | ||
gates = int(rte.find('ngates').text) | ||
if gates > 1: | ||
task_info['start_iteration'] = gates - 1 | ||
task_info['SS_interval'] = int(rte.find('gateint').text) * 60 # in seconds | ||
# comment | ||
if rte.find('info').text: | ||
task_info['comment'] = rte.find('info').text | ||
# wpt info | ||
task_info['route'] = [] | ||
for node in rte.iter('rtept'): | ||
wpt = dict( | ||
num=int(node.find('index').text), | ||
name=node.find('id').text, | ||
description=node.find('name').text, | ||
lat=float(node.get('lat')), lon=float(node.get('lon')), | ||
altitude=None if node.find('z') is None else int(node.find('z').text), | ||
radius=int(node.find('radius').text), | ||
shape='circle', | ||
how='entry' | ||
) | ||
t = node.find('type').text.lower() | ||
if t == 'takeoff': | ||
wpt['type'] = 'launch' | ||
wpt['how'] = 'exit' | ||
task_info['window_open_time'] = utils.get_time(node.find('open').text) | ||
task_info['window_close_time'] = utils.get_time(node.find('close').text) | ||
elif t == 'start': | ||
wpt['type'] = 'speed' | ||
task_info['start_time'] = utils.get_time(node.find('open').text) | ||
task_info['start_close_time'] = ( | ||
task_info['start_time'] + 3600 if not node.find('close') | ||
else utils.get_time(node.find('close').text) | ||
) | ||
elif t == 'end-of-speed-section': | ||
wpt['type'] = 'endspeed' | ||
elif t == 'goal': | ||
wpt['type'] = 'goal' | ||
if node.find('goalType').text == 'line': | ||
wpt['shape'] = 'line' | ||
task_info['task_deadline'] = utils.get_time(node.find('close').text) | ||
else: | ||
wpt['type'] = 'waypoint' | ||
|
||
task_info['route'].append(wpt) | ||
|
||
return task_info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
Source utilities functions | ||
Antonio Golfari - 2024 | ||
""" | ||
|
||
from lxml import etree | ||
from pathlib import Path | ||
|
||
def read_xml_file(file: Path, clean_namespace: bool = True) -> "etree | None": | ||
"""read the xml file""" | ||
try: | ||
tree = etree.parse(file) | ||
except TypeError: | ||
tree = etree.parse(file.as_posix()) | ||
except etree.Error as e: | ||
print(f"XML Read Error: {e}") | ||
return None | ||
finally: | ||
root = tree.getroot() | ||
if clean_namespace: | ||
clean_xml_namespaces(root) | ||
return root | ||
|
||
def clean_xml_namespaces(root): | ||
for element in root.getiterator(): | ||
if isinstance(element, etree._Comment): | ||
continue | ||
element.tag = etree.QName(element).localname | ||
etree.cleanup_namespaces(root) | ||
|
||
|
||
def get_time(string: str) -> int: | ||
print(string) | ||
if len(string) < 3: | ||
# sometimes this incredibly happens | ||
string += ':00' | ||
h, m = string.replace(';', ':').split(':')[:2] | ||
return int(h) * 3600 + int(m[:2]) * 60 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
""" | ||
XC Track Library | ||
contains methods to import Task from XcTrack .xctsk file | ||
Use: import sources.xctrack | ||
Antonio Golfari - 2024 | ||
""" | ||
|
||
import json | ||
|
||
from . import utils | ||
from pathlib import Path | ||
|
||
|
||
def read_xctsk_file(file) -> "dict | None": | ||
|
||
try: | ||
return json.load(file) | ||
except: | ||
print("xctsk file is not a valid JSON object") | ||
return None | ||
|
||
|
||
def read_task(data: dict) -> dict: | ||
|
||
task_info = {} | ||
if data: | ||
task_info['task_type'] = 'elapsed time' if data.get('taskType') == 'elapsed_time' else 'race' | ||
print(f"len gates: {len(data['sss']['timeGates'])}") | ||
if len(data['sss']['timeGates']): | ||
task_info['start_time'] = utils.get_time(data['sss']['timeGates'][0]) | ||
print(f"start: {task_info['start_time']}") | ||
# xctrack file does not have launch window info | ||
task_info['start_close_time'] = task_info['start_time'] + 3600 | ||
task_info['window_open_time'] = task_info['start_time'] - 3600 | ||
task_info['window_close_time'] = task_info['start_time'] | ||
print(f"s close: {task_info['start_close_time']}") | ||
print(f"w open: {task_info['window_open_time']}") | ||
print(f"w close: {task_info['window_close_time']}") | ||
|
||
task_info['task_deadline'] = utils.get_time(data['goal']['deadline']) | ||
task_info['route'] = [] | ||
for idx, el in enumerate(data['turnpoints']): | ||
w = el['waypoint'] | ||
wpt = dict( | ||
num=idx, | ||
name=w['description'], | ||
description=w['name'], | ||
lat=w['lat'], lon=w['lon'], | ||
altitude=int(w['altSmoothed']), | ||
radius=int(el['radius']), | ||
shape='circle', | ||
how='entry' | ||
) | ||
t = None if el.get('type') is None else el['type'].lower() | ||
if t == 'takeoff': | ||
wpt['type'] = 'launch' | ||
wpt['how'] = 'exit' | ||
elif t == 'sss': | ||
wpt['type'] = 'speed' | ||
elif t == 'ess': | ||
wpt['type'] = 'endspeed' | ||
elif idx == len(data['turnpoints']) - 1: | ||
wpt['type'] = 'goal' | ||
if data['goal']['type'].lower() == 'line': | ||
wpt['shape'] = 'line' | ||
else: | ||
wpt['type'] = 'waypoint' | ||
|
||
task_info['route'].append(wpt) | ||
|
||
return task_info | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.