diff --git a/src/stactools/naip/stac.py b/src/stactools/naip/stac.py index aea47e4..0ad7740 100644 --- a/src/stactools/naip/stac.py +++ b/src/stactools/naip/stac.py @@ -1,3 +1,4 @@ +import itertools import os import re from datetime import timedelta @@ -9,6 +10,7 @@ from pystac.extensions.eo import EOExtension from pystac.extensions.item_assets import ItemAssetsExtension from pystac.extensions.projection import ProjectionExtension +from pystac.extensions.raster import DataType, RasterBand, RasterExtension from pystac.utils import str_to_datetime from shapely.geometry import box, mapping, shape from stactools.core.io import read_text @@ -108,7 +110,7 @@ def create_item( """ with rio.open(cog_href) as ds: - gsd = ds.res[0] + gsd = round(ds.res[0], 1) epsg = int(ds.crs.to_authority()[1]) image_shape = list(ds.shape) original_bbox = list(ds.bounds) @@ -207,7 +209,23 @@ def create_item( ), ) - asset_eo = EOExtension.ext(item.assets["image"]) + image_asset = item.assets["image"] + + # EO Extension + asset_eo = EOExtension.ext(image_asset) asset_eo.bands = constants.NAIP_BANDS + # Raster Extension + RasterExtension.ext(image_asset, add_if_missing=True).bands = list( + itertools.repeat( + RasterBand.create( + nodata=0, + spatial_resolution=gsd, + data_type=DataType.UINT8, + unit="none", + ), + 4, + ) + ) + return item diff --git a/tests/test_commands.py b/tests/test_commands.py index 6dc09e3..611e9f5 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -2,6 +2,7 @@ from tempfile import TemporaryDirectory import pystac +from pystac.extensions.raster import DataType, RasterExtension from stactools.testing import CliTestCase from stactools.naip.commands import create_naip_command @@ -36,10 +37,19 @@ def test_create_item(self): item_path = os.path.join(tmp_dir, jsons[0]) - item = pystac.read_file(item_path) + item = pystac.Item.from_file(item_path) item.validate() + image_asset = item.assets["image"] + raster_ext = RasterExtension.ext(image_asset) + self.assertEqual(len(raster_ext.bands), 4) + for raster_band in RasterExtension.ext(image_asset).bands: + self.assertEqual(raster_band.nodata, 0) + self.assertEqual(raster_band.spatial_resolution, item.properties["gsd"]) + self.assertEqual(raster_band.data_type, DataType.UINT8) + self.assertEqual(raster_band.unit, "none") + def test_create_item_no_resource_desc(self): fgdc_href = test_data.get_path("data-files/m_4207201_ne_18_h_20160809.txt") cog_href = (