Skip to content
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
4 changes: 2 additions & 2 deletions pytdml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
__version__ = "1.2.0"

from . import type
from . import io
from . import ml

__all__ = ["type"]
1 change: 1 addition & 0 deletions pytdml/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@
from pytdml.io.yaml_converter import yaml_to_eo_tdml, yaml_to_tdml
from pytdml.io.coco_converter import convert_coco_to_tdml
from pytdml.io.stac_converter import convert_stac_to_tdml
from pytdml.io.xml_converter import convert_xml_to_tdml
35 changes: 23 additions & 12 deletions pytdml/io/stac_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,32 @@
# ------------------------------------------------------------------------------

import json
import os
import re
from datetime import datetime
from geojson import Feature
from pystac import Collection
from pytdml.type import EOTrainingDataset, AI_EOTrainingData, AI_ObjectLabel, AI_EOTask


def _parse_stac_datetime(value):
if value is None:
return None

cleaned_date_time_str = re.sub(r"(\\+00:00|Z)$", "", value)
for fmt in ("%Y-%m-%dT%H:%M:%S.%f", "%Y-%m-%dT%H:%M:%S"):
try:
date_time_obj = datetime.strptime(cleaned_date_time_str, fmt)
return date_time_obj.strftime("%Y-%m-%dT%H:%M:%S")
except ValueError:
continue

raise ValueError(f"Unsupported STAC datetime format: {value}")


def convert_stac_to_tdml(stac_dataset_path):
# Reads JSON data in stac format from a given path.
with open(stac_dataset_path, "r") as file:
collection_data = json.load(file)
collection_object = Collection.from_dict(collection_data)
stac_collection_dataset = collection_object.to_dict(
include_self_link=False, transform_hrefs=True
)
stac_collection_dataset = json.load(file)

# Reads the necessary attributes from the Collection object and maps them to the EOTrainingDataset object
collection_version = stac_collection_dataset.get("stac_version")
Expand All @@ -60,12 +71,9 @@ def convert_stac_to_tdml(stac_dataset_path):
data_time = []
for item in collection_interval:
for time in item:
cleaned_date_time_str = re.sub(r"(\\+00:00|Z)$", "", time)
date_time_obj = datetime.strptime(
cleaned_date_time_str, "%Y-%m-%dT%H:%M:%S.%f"
)
formatted_date_time_str = date_time_obj.strftime("%Y-%m-%dT%H:%M:%S")
data_time.append(formatted_date_time_str)
formatted_date_time_str = _parse_stac_datetime(time)
if formatted_date_time_str is not None:
data_time.append(formatted_date_time_str)

if len(collection_bbox) == 1:
collection_extent = collection_bbox[0]
Expand All @@ -79,8 +87,11 @@ def convert_stac_to_tdml(stac_dataset_path):
]

datalist = []
collection_dir = os.path.dirname(os.path.abspath(stac_dataset_path))
for link in collection_filtered_links:
item_path = link.get("href")
if not os.path.isabs(item_path) and not os.path.exists(item_path):
item_path = os.path.normpath(os.path.join(collection_dir, item_path))
with open(item_path, "r") as item_file:
stac_item = json.load(item_file)
link_id = stac_item.get("id")
Expand Down
29 changes: 1 addition & 28 deletions pytdml/io/tdml_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,6 @@
from pytdml.type import TrainingDataset, EOTrainingDataset


def _is_empty(obj):
if isinstance(obj, (str, list, dict)):
return len(obj) == 0
elif obj is None:
return True
else:
return False


def remove_empty_values(d):
if isinstance(d, dict):
return {
k: v
for k, v in ((k, remove_empty_values(v)) for k, v in d.items())
if not _is_empty(v)
}
elif isinstance(d, list):
return [v for v in (remove_empty_values(v) for v in d) if not _is_empty(v)]
elif isinstance(d, tuple):
return tuple(v for v in (remove_empty_values(v) for v in d) if not _is_empty(v))
else:
return d


def write_to_json(
td: TrainingDataset or EOTrainingDataset,
file_path: str,
Expand All @@ -68,7 +44,4 @@ def write_to_json(
Writes a TrainingDataset to a JSON file.
"""
with open(file_path, "w", encoding="utf-8") as f:
json.dump(
remove_empty_values(td.to_dict()), f, indent=indent, ensure_ascii=False
)
# json.dump(remove_empty(td.dict()), f, indent=indent, ensure_ascii=False)
json.dump(td.to_dict(), f, indent=indent, ensure_ascii=False)
Loading