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

Feat/get catalog online #29

Merged
merged 5 commits into from
May 30, 2024
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
10 changes: 6 additions & 4 deletions catalog/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ def get_catalog() -> dict:
unused in plugin
this method needs too long time to run everytime on launch plugin
run when build this plugin and make dictionary with output of this script
UPDATE 2024-05-30 : catalog can be retrieved through following URL:
https://data.earth.jaxa.jp/app/qgis/catalog.json
This script to be used only when above URL does not work.
"""
catalog = {}

res = requests.get(STAC_CATALOG_URL)
res_json = res.json()

children = list(
filter(lambda d: d["rel"] == "child", res_json.get("links", [])))
children = list(filter(lambda d: d["rel"] == "child", res_json.get("links", [])))
for child in children:
res_child = requests.get(child["href"])
res_child_json = res_child.json()
Expand All @@ -33,12 +35,12 @@ def get_catalog() -> dict:
"bands": dataset_bands,
"keywords": dataset_keywords,
"bbox": dataset_bbox,
"temporal": dataset_temporal
"temporal": dataset_temporal,
}
return catalog


if __name__ == "__main__":
catalog = get_catalog()
with open('./catalog.json', mode='w') as f:
with open("./catalog.json", mode="w") as f:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このコードはローカルファイルにカタログを保存していますが、ファイルの書き込みに失敗した場合のエラーハンドリングがありません。例外処理を追加することをお勧めします。

+ try:
    with open("./catalog.json", mode="w") as f:
        json.dump(catalog, f, ensure_ascii=False)
+ except Exception as e:
+     print(f"Error writing to file: {str(e)}")

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
with open("./catalog.json", mode="w") as f:
try:
with open("./catalog.json", mode="w") as f:
json.dump(catalog, f, ensure_ascii=False)
except Exception as e:
print(f"Error writing to file: {str(e)}")

json.dump(catalog, f, ensure_ascii=False)
38 changes: 34 additions & 4 deletions jaxaEarthApiDialog.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""
/***************************************************************************
Sample
JAXA Earth API Plugin
A QGIS plugin
QGIS Sample Plugin
QGIS Plugin for JAXA Earth API, easily get satellite datasets.
-------------------
begin : 2021-06-30
git sha : $Format:%H$
Expand All @@ -16,6 +16,7 @@
import json
import datetime
import webbrowser
import requests

# QGIS-API
from qgis.PyQt import uic
Expand All @@ -29,8 +30,37 @@
# Load module
from .jaxa.earth import je

with open(os.path.join(os.path.dirname(__file__), "catalog.json")) as f:
CATALOG = json.load(f)
# Load jaxa dataset catalog
CATALOG_URL = "https://data.earth.jaxa.jp/app/qgis/catalog.json"

try:
response = requests.get(CATALOG_URL, timeout=60)
if response.status_code == 200:
CATALOG = response.json()
print(response)
else:
message = "Could not load catalog.json\n"
message += f"Response {response.status_code} \nLoad local catalog"
QMessageBox.information(
None,
"JAXA Earth API Plugin - Warning",
message,
)
# Fallback load local catalog
with open(os.path.join(os.path.dirname(__file__), "catalog.json")) as f:
CATALOG = json.load(f)
except Exception as e:
message = "Could not load catalog.json\n"
message += f"{str(e)} \nLoad local catalog"
QMessageBox.information(
None,
"JAXA Earth API Plugin - Warning",
message,
)
print(e)
# Fallback load local catalog
with open(os.path.join(os.path.dirname(__file__), "catalog.json")) as f:
CATALOG = json.load(f)


def parse_jaxa_dateid(date_id: str, year=None) -> (int, int, int):
Expand Down
11 changes: 5 additions & 6 deletions jaxaEarthApiPlugin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""
/***************************************************************************
Sample
JAXA Earth API Plugin
A QGIS plugin
QGIS Sample Plugin
QGIS Plugin for JAXA Earth API, easily get satellite datasets.
-------------------
begin : 2021-06-30
git sha : $Format:%H$
Expand Down Expand Up @@ -39,11 +39,10 @@ def __init__(self, iface):
self.dialog = None
self.action = None
# initialize locale
locale = QSettings().value('locale/userLocale')[0:2]
locale = QSettings().value("locale/userLocale")[0:2]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'jaxaEarthApi_{}.qm'.format(locale))
self.plugin_dir, "i18n", "jaxaEarthApi_{}.qm".format(locale)
)

if os.path.exists(locale_path):
self.translator = QTranslator()
Expand Down