Skip to content

Commit e68fd72

Browse files
jsignellgadomski
andauthored
Let user set asset key, roles on stac create-item (#442)
Co-authored-by: Pete Gadomski <pete.gadomski@gmail.com>
1 parent c2c40a1 commit e68fd72

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- For raster footprints users can set destination CRS rather than it being hardcoded to EPSG:4326 ([#440](https://github.com/stac-utils/stactools/pull/440))
1313
- Raster footprint calculation for multi-asset items can elect to use the union or intersection of the asset footprints ([#445](https://github.com/stac-utils/stactools/pull/445))
14+
- On `stac create-item` asset key name and roles are settable ([#442](https://github.com/stac-utils/stactools/pull/442))
1415

1516
### Changed
1617

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import sys
3+
from typing import List
34

45
import click
56
from stactools.core import create
@@ -8,15 +9,24 @@
89
def create_create_item_command(cli: click.Group) -> click.Command:
910
@cli.command("create-item", short_help="Creates an item from an asset")
1011
@click.argument("href")
11-
def create_item_command(href: str) -> None:
12+
@click.argument("asset_key", default="data")
13+
@click.option(
14+
"-r",
15+
"--role",
16+
"roles",
17+
help="Optional, semantic roles of the asset",
18+
multiple=True,
19+
default=["data"],
20+
)
21+
def create_item_command(href: str, asset_key: str, roles: List[str]) -> None:
1222
"""Creates an Item from a href.
1323
1424
The href must be a `rasterio` readable asset. The item's dictionary will
1525
be printed to stdout. This item is intentinonally _extremely_ minimal.
1626
If you need additional capabilities, we recommend using [rio
1727
stac](https://github.com/developmentseed/rio-stac/).
1828
"""
19-
item = create.item(href)
29+
item = create.item(href, asset_key=asset_key, roles=roles)
2030
json.dump(item.to_dict(), sys.stdout)
2131

2232
return create_item_command

src/stactools/core/create.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import datetime
22
import os.path
3-
from typing import Optional
3+
from typing import List, Optional
44

55
import rasterio
66
import shapely.geometry
@@ -11,7 +11,13 @@
1111
from .io import ReadHrefModifier
1212

1313

14-
def item(href: str, read_href_modifier: Optional[ReadHrefModifier] = None) -> Item:
14+
def item(
15+
href: str,
16+
*,
17+
asset_key: str = "data",
18+
roles: List[str] = ["data"],
19+
read_href_modifier: Optional[ReadHrefModifier] = None,
20+
) -> Item:
1521
"""Creates a STAC Item from the asset at the provided href.
1622
1723
The ``read_href_modifer`` argument can be used to modify the href for the
@@ -22,6 +28,8 @@ def item(href: str, read_href_modifier: Optional[ReadHrefModifier] = None) -> It
2228
2329
Args:
2430
href (str): The href of the asset that will be used to create the item.
31+
asset_key (str): The unique key of the asset
32+
roles (List[str]): The semantic roles of the asset
2533
read_href_modifier (Optional[ReadHrefModifier]):
2634
An optional callable that will be used to modify the href before reading.
2735
@@ -60,6 +68,6 @@ def item(href: str, read_href_modifier: Optional[ReadHrefModifier] = None) -> It
6068
projection.transform = proj_transform
6169
projection.shape = proj_shape
6270

63-
item.add_asset("data", Asset(href=href, roles=["data"]))
71+
item.add_asset(asset_key, Asset(href=href, roles=roles))
6472

6573
return item

tests/cli/commands/test_create.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import json
2+
3+
from click.testing import CliRunner
4+
from stactools.cli.cli import cli
5+
6+
from tests import test_data
7+
8+
9+
def test_create_item():
10+
path = test_data.get_path("data-files/core/byte.tif")
11+
runner = CliRunner()
12+
result = runner.invoke(cli, ["create-item", path])
13+
assert result.exit_code == 0
14+
d = json.loads(result.output)
15+
assert list(d["assets"].keys()) == ["data"]
16+
17+
18+
def test_create_item_with_asset_key():
19+
path = test_data.get_path("data-files/core/byte.tif")
20+
runner = CliRunner()
21+
result = runner.invoke(cli, ["create-item", path, "foo"])
22+
assert result.exit_code == 0
23+
d = json.loads(result.output)
24+
assert list(d["assets"].keys()) == ["foo"]
25+
26+
27+
def test_create_item_with_asset_roles():
28+
path = test_data.get_path("data-files/core/byte.tif")
29+
runner = CliRunner()
30+
result = runner.invoke(cli, ["create-item", path, "-r", "reference"])
31+
assert result.exit_code == 0
32+
d = json.loads(result.output)
33+
assert d["assets"]["data"]["roles"] == ["reference"]

0 commit comments

Comments
 (0)