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

Fix support for gridless stac conversions #220

Merged
merged 1 commit into from
Nov 11, 2021
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
37 changes: 14 additions & 23 deletions eodatasets3/stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,21 @@ def _asset_title_fields(asset_name: str) -> Optional[str]:
return None


def _proj_fields(
grid: Dict[str, GridDoc], grid_name: str = "default"
) -> Optional[Dict]:
def _proj_fields(grid: Dict[str, GridDoc], grid_name: str = "default") -> Dict:
"""
Add fields of the STAC Projection (proj) Extension to a STAC Item
Get any proj (Stac projection extension) fields if we have them for the grid.
"""
if not grid:
return {}

grid_doc = grid.get(grid_name or "default")
if grid_doc:
return {
"shape": grid_doc.shape,
"transform": grid_doc.transform,
}
else:
return None
if not grid_doc:
return {}

return {
"shape": grid_doc.shape,
"transform": grid_doc.transform,
}


def _lineage_fields(lineage: Dict) -> Dict:
Expand Down Expand Up @@ -276,21 +277,11 @@ def to_stac_item(
eo = EOExtension.ext(item, add_if_missing=True)
proj = ProjectionExtension.ext(item, add_if_missing=True)

proj_fields = _proj_fields(dataset.grids)

epsg, wkt = _get_projection(dataset)
if epsg is not None:
proj.apply(
shape=proj_fields["shape"],
transform=proj_fields["transform"],
epsg=epsg,
)
proj.apply(epsg=epsg, **_proj_fields(dataset.grids))
elif wkt is not None:
proj.apply(
shape=proj_fields["shape"],
transform=proj_fields["transform"],
wkt2=wkt,
)
proj.apply(wkt2=wkt, **_proj_fields(dataset.grids))
else:
raise STACError("Projection extension requires either epsg or wkt for crs.")

Expand Down
79 changes: 66 additions & 13 deletions tests/integration/test_tostac.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import shutil
from pathlib import Path
from typing import Dict

import pytest

Expand All @@ -11,28 +12,80 @@

TO_STAC_DATA: Path = Path(__file__).parent.joinpath("data/tostac")
ODC_METADATA_FILE: str = "ga_ls8c_ard_3-1-0_088080_2020-05-25_final.odc-metadata.yaml"
STAC_TEMPLATE_FILE: str = "ga_ls_ard_3_stac_item.json"
STAC_EXPECTED_FILE: str = (
"ga_ls8c_ard_3-1-0_088080_2020-05-25_final.stac-item_expected.json"
)


def test_tostac(input_doc_folder: Path):
input_metadata_path = input_doc_folder.joinpath(ODC_METADATA_FILE)
assert input_metadata_path.exists()
@pytest.fixture
def odc_dataset_path(input_doc_folder: Path):
d = input_doc_folder.joinpath(ODC_METADATA_FILE)
assert d.exists()
return d

run_tostac(input_metadata_path)

name = input_metadata_path.stem.replace(".odc-metadata", "")
actual_stac_path = input_metadata_path.with_name(f"{name}.stac-item.json")
assert actual_stac_path.exists()
@pytest.fixture
def expected_stac_doc(input_doc_folder: Path) -> Dict:
d = input_doc_folder.joinpath(STAC_EXPECTED_FILE)
assert d.exists()
return json.load(d.open())

expected_stac_path = input_doc_folder.joinpath(STAC_EXPECTED_FILE)
assert expected_stac_path.exists()

actual_doc = json.load(actual_stac_path.open())
expected_doc = json.load(expected_stac_path.open())
assert_same(expected_doc, actual_doc)
def test_tostac(odc_dataset_path: Path, expected_stac_doc: Dict):

run_tostac(odc_dataset_path)

expected_output_path = odc_dataset_path.with_name(
odc_dataset_path.name.replace(".odc-metadata.yaml", ".stac-item.json")
)

assert expected_output_path.exists()

output_doc = json.load(expected_output_path.open())

assert_same(expected_stac_doc, output_doc)


def test_tostac_no_grids(odc_dataset_path: Path, expected_stac_doc: Dict):
"""
Converted EO1 datasets don't have grid information. Make sure it still outputs
without falling over.
"""

# Remove grids from the input....
dataset = serialise.from_path(odc_dataset_path)
dataset.grids = None
serialise.to_path(odc_dataset_path, dataset)

run_tostac(odc_dataset_path)
expected_output_path = odc_dataset_path.with_name(
odc_dataset_path.name.replace(".odc-metadata.yaml", ".stac-item.json")
)

# No longer expect proj fields (they come from grids).
remove_stac_properties(
expected_stac_doc, ("proj:shape", "proj:transform", "proj:epsg")
)
# But we do still expect a global CRS.
expected_stac_doc["properties"]["proj:epsg"] = 32656

output_doc = json.load(expected_output_path.open())
assert_same(expected_stac_doc, output_doc)


def remove_stac_properties(doc: Dict, remove_properties=()):
"""
Remove the given fields from properties and assets.
"""

def remove_proj(dict: Dict):
for key in list(dict.keys()):
if key in remove_properties:
del dict[key]

remove_proj(doc["properties"])
for name, asset in doc["assets"].items():
remove_proj(asset)


def test_add_property(input_doc_folder: Path):
Expand Down