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

Sentinel 2 L2A: Add band assets for lower spatial res versions #88

Merged
merged 7 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
54 changes: 54 additions & 0 deletions stactools_sentinel2/stactools/sentinel2/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,57 @@
center_wavelength=2.190,
full_width_half_max=0.242),
}

# A dict describing the resolutions that are
# available for each band as separate assets.
# The first resolution is the sensor gsd; others
# are downscaled versions.
BANDS_TO_RESOLUTIONS = {
'B01': [
60,
],
'B02': [
10,
20,
60,
],
'B03': [
10,
20,
60,
],
'B04': [
10,
20,
60,
],
'B05': [
20,
20,
60,
],
'B06': [
20,
60,
],
'B07': [
20,
60,
],
'B08': [
10,
],
'B8A': [
20,
60,
],
'B09': [60],
'B11': [
20,
60,
],
'B12': [
20,
60,
],
}
36 changes: 21 additions & 15 deletions stactools_sentinel2/stactools/sentinel2/stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
from stactools.sentinel2.product_metadata import ProductMetadata
from stactools.sentinel2.granule_metadata import GranuleMetadata
from stactools.sentinel2.utils import extract_gsd
from stactools.sentinel2.constants import (DATASTRIP_METADATA_ASSET_KEY,
SENTINEL_PROVIDER, SENTINEL_LICENSE,
SENTINEL_BANDS,
SENTINEL_INSTRUMENTS,
SENTINEL_CONSTELLATION,
INSPIRE_METADATA_ASSET_KEY)
from stactools.sentinel2.constants import (
BANDS_TO_RESOLUTIONS, DATASTRIP_METADATA_ASSET_KEY, SENTINEL_PROVIDER,
SENTINEL_LICENSE, SENTINEL_BANDS, SENTINEL_INSTRUMENTS,
SENTINEL_CONSTELLATION, INSPIRE_METADATA_ASSET_KEY)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -172,29 +170,37 @@ def image_asset_from_href(
return ('preview', asset)

# Extract gsd and proj info
gsd = extract_gsd(asset_href)
shape = list(resolution_to_shape[int(gsd)])
filename_gsd = extract_gsd(asset_href)
shape = list(resolution_to_shape[int(filename_gsd)])
transform = transform_from_bbox(proj_bbox, shape)

def set_asset_properties(asset):
item.common_metadata.set_gsd(gsd, asset)
def set_asset_properties(asset: pystac.Asset, band_gsd: Optional[int] = None):
if filename_gsd is None:
item.common_metadata.set_gsd(filename_gsd, asset)
else:
item.common_metadata.set_gsd(band_gsd, asset)
item.ext.projection.set_shape(shape, asset)
item.ext.projection.set_bbox(proj_bbox, asset)
item.ext.projection.set_transform(transform, asset)

# Handle band image

band_id_search = re.search(r'_(B\w{2})_', asset_href)
band_id_search = re.search(r'_(B\w{2})', asset_href)
if band_id_search is not None:
band_id = band_id_search.group(1)
band_id, href_res = os.path.splitext(asset_href)[0].split('_')[-2:]
asset_res = int(href_res.replace('m', ''))
band = SENTINEL_BANDS[band_id]
if asset_res == BANDS_TO_RESOLUTIONS[band_id][0]:
asset_key = band_id
else:
asset_key = f'{band_id}_{asset_res}m'
asset = pystac.Asset(href=asset_href,
media_type=asset_media_type,
title=band.description,
title=f'{band.description} - {href_res}',
roles=['data'])
item.ext.eo.set_bands([SENTINEL_BANDS[band_id]], asset)
set_asset_properties(asset)
return (band_id, asset)
set_asset_properties(asset, band_gsd=BANDS_TO_RESOLUTIONS[band_id][0])
return (asset_key, asset)

# Handle auxiliary images

Expand Down
42 changes: 40 additions & 2 deletions tests/sentinel2/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import defaultdict
import os
from tempfile import TemporaryDirectory

Expand All @@ -7,7 +8,7 @@

from stactools.core.projection import reproject_geom
from stactools.sentinel2.commands import create_sentinel2_command
from stactools.sentinel2.constants import SENTINEL_BANDS
from stactools.sentinel2.constants import BANDS_TO_RESOLUTIONS, SENTINEL_BANDS
from tests.utils import (TestData, CliTestCase)


Expand Down Expand Up @@ -58,13 +59,50 @@ def check_proj_bbox(item):
item.validate()

bands_seen = set()
bands_to_assets = defaultdict(list)

for asset in item.assets.values():
for key, asset in item.assets.items():
self.assertTrue(is_absolute_href(asset.href))
bands = item.ext.eo.get_bands(asset)
if bands is not None:
bands_seen |= set(b.name for b in bands)
if key.split('_')[0] in SENTINEL_BANDS:
for b in bands:
bands_to_assets[b.name].append(
(key, asset))

self.assertEqual(bands_seen, set(SENTINEL_BANDS.keys()))

# Check that multiple resolutions exist for assets that
# have them, and that they are named such that the highest
# resolution asset is the band name, and others are
# appended with the resolution.

resolutions_seen = defaultdict(list)

for band_name, assets in bands_to_assets.items():
for (asset_key, asset) in assets:
resolutions = BANDS_TO_RESOLUTIONS[band_name]

asset_split = asset_key.split('_')
self.assertLessEqual(len(asset_split), 2)

href_band, href_res = os.path.splitext(
asset.href)[0].split('_')[-2:]
asset_res = int(href_res.replace('m', ''))
self.assertEqual(href_band, band_name)
if len(asset_split) == 1:
self.assertEqual(asset_res, resolutions[0])
resolutions_seen[band_name].append(asset_res)
else:
self.assertNotEqual(asset_res, resolutions[0])
self.assertIn(asset_res, resolutions)
resolutions_seen[band_name].append(asset_res)

self.assertEqual(set(resolutions_seen.keys()),
set(BANDS_TO_RESOLUTIONS.keys()))
for band in resolutions_seen:
self.assertEqual(set(resolutions_seen[band]),
set(BANDS_TO_RESOLUTIONS[band]))

check_proj_bbox(item)