Skip to content

Commit

Permalink
Merge pull request #17 from stactools-packages/pv/add-raster-extension
Browse files Browse the repository at this point in the history
add raster extension
  • Loading branch information
Phil Varner authored Apr 18, 2022
2 parents ad75bf4 + 0db0f57 commit dd703d0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
22 changes: 20 additions & 2 deletions src/stactools/naip/stac.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import itertools
import os
import re
from datetime import timedelta
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
12 changes: 11 additions & 1 deletion tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = (
Expand Down

0 comments on commit dd703d0

Please sign in to comment.