-
Notifications
You must be signed in to change notification settings - Fork 7
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
Modification and testing of stack_converter functionality and resolution of issues in the previous pull request #22
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# ------------------------------------------------------------------------------ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As the location of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I will update the |
||
# ------------------------------------------------------------------------------ | ||
# | ||
# Project: pytdml | ||
# Authors: Boyi Shangguan, Kaixuan Wang, Zhaoyan Wu | ||
# Created: 2022-05-04 | ||
# Modified: 2023-10-27 | ||
# Email: [email protected] | ||
# | ||
# ------------------------------------------------------------------------------ | ||
# | ||
# Copyright (c) 2022 OGC Training Data Markup Language for AI Standard Working Group | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
# | ||
# ------------------------------------------------------------------------------ | ||
|
||
import json | ||
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 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) | ||
|
||
# Reads the necessary attributes from the Collection object and maps them to the EOTrainingDataset object | ||
collection_version = stac_collection_dataset.get("stac_version") | ||
collection_id = stac_collection_dataset.get("id") | ||
collection_description = stac_collection_dataset.get("description") | ||
collection_license = stac_collection_dataset.get("license") | ||
collection_bbox = stac_collection_dataset.get("extent").get("spatial").get("bbox") | ||
collection_interval = stac_collection_dataset.get("extent").get("temporal").get("interval") | ||
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) | ||
|
||
if len(collection_bbox) == 1: | ||
collection_extent = collection_bbox[0] | ||
else: | ||
collection_extent = [item for bbox in collection_bbox for item in bbox] | ||
|
||
# Reads the necessary attributes from the item object and maps them to the data object | ||
collection_links = stac_collection_dataset.get("links") | ||
collection_filtered_links = [link for link in collection_links if link.get("rel") == "item"] | ||
|
||
datalist = [] | ||
for link in collection_filtered_links: | ||
item_path = link.get("href") | ||
with open(item_path, 'r') as item_file: | ||
stac_item = json.load(item_file) | ||
link_id = stac_item.get("id") | ||
link_rel = link.get("rel") | ||
feature = Feature(**stac_item) | ||
link_href = [asset['href'] for asset in stac_item.get("assets").values()] | ||
|
||
label = AI_ObjectLabel( | ||
type = "AI_ObjectLabel", | ||
object = feature, | ||
label_class = link_rel | ||
) | ||
|
||
data = AI_EOTrainingData( | ||
type = "AI_EOTrainingData", | ||
id = link_id, | ||
labels = [label], | ||
data_URL = link_href, | ||
data_time = data_time | ||
) | ||
datalist.append(data) | ||
|
||
# Reads the unnecessary attributes from the Collection object and maps them to the EOTrainingDataset object | ||
collection_name = stac_collection_dataset.get("title") | ||
|
||
tasks = [AI_EOTask(task_type="STAC", | ||
id=str(collection_id) + "_task", | ||
dataset_id= str(collection_id), | ||
type='AI_EOTask')] | ||
|
||
dataset = EOTrainingDataset( | ||
# necessary attributes | ||
id = str(collection_id), | ||
name = collection_name, | ||
description = collection_description, | ||
license = collection_license, | ||
tasks = tasks, | ||
data = datalist, | ||
type="AI_EOTrainingDataset", | ||
# unnecessary attributes | ||
version = collection_version, | ||
extent = collection_extent | ||
) | ||
|
||
return dataset |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -322,7 +322,5 @@ def main(): | |
if training_datasets: | ||
write_to_json(training_datasets, json_path) | ||
|
||
|
||
if __name__ == '__main__': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the project top
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I forgot the use case in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At least for the ones that are exposed through the README or that were designed to be used as command line tools. i.e. the ones that are managing command line options like it is done in this file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added logic to enable this function to be used through the command line. |
||
result = yaml_to_eo_tdml("D:\\Project\\pyTDML3\\pytdml\\pytdml\\type\\UiT_HCD_California_2017.yml") | ||
print(result.to_dict()) | ||
main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have already updated
REAME.md
here.