Clay-foundation / stacchip

Dynamically create image chips from STAC items
https://clay-foundation.github.io/stacchip/
Apache License 2.0
25 stars 3 forks source link

Support for other collection providers #7

Open thhomas opened 3 months ago

thhomas commented 3 months ago

This Sentinel 2 L2A collection does not seem to be supported. Indeed, the model is slightly different: https://earth-search.aws.element84.com/v1/collections/sentinel-2-l2a https://planetarycomputer.microsoft.com/api/stac/v1/collections/sentinel-2-l2a

especially when it comes to item_assets description, the key referring to bands are different. Leading to the error message when trying to use stacchip Sentinel2Indexer with the second collection:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[12], line 11
      9 for item in items_list[:10]:
     10     print(f"Working on {item}")
---> 11     indexer = Sentinel2Indexer(item).create_index()
     12     chipper = Chipper(indexer, assets=["image"])
     14     # Get first chip for the "image" asset key

File [/srv/conda/envs/notebook/lib/python3.10/site-packages/stacchip/indexer.py:200](https://hub.staging.digitalearthpacific.org/srv/conda/envs/notebook/lib/python3.10/site-packages/stacchip/indexer.py#line=199), in ChipIndexer.create_index(self)
    197 for y in range(0, self.y_size):
    198     for x in range(0, self.x_size):
--> 200         cloud_cover_percentage, nodata_percentage = self.get_stats(x, y)
    202         index["chipid"][counter] = f"{self.item.id}-{x}-{y}"
    203         index["date"][counter] = self.item.datetime.date()

File [/srv/conda/envs/notebook/lib/python3.10/site-packages/stacchip/indexer.py:342](https://hub.staging.digitalearthpacific.org/srv/conda/envs/notebook/lib/python3.10/site-packages/stacchip/indexer.py#line=341), in Sentinel2Indexer.get_stats(self, x, y)
    336 def get_stats(self, x: int, y: int) -> Tuple[float, float]:
    337     """
    338     Cloud and nodata percentage for a chip
    339 
    340     Uses the SCL band to compute these values.
    341     """
--> 342     scl = self.scl[
    343         y * self.chip_size : (y + 1) * self.chip_size,
    344         x * self.chip_size : (x + 1) * self.chip_size,
    345     ]
    347     cloud_percentage = int(np.isin(scl, self.scl_filter).sum()) [/](https://hub.staging.digitalearthpacific.org/) scl.size
    349     nodata_percentage = np.sum(scl == self.nodata_value) [/](https://hub.staging.digitalearthpacific.org/) scl.size

File [/srv/conda/envs/notebook/lib/python3.10/functools.py:981](https://hub.staging.digitalearthpacific.org/srv/conda/envs/notebook/lib/python3.10/functools.py#line=980), in cached_property.__get__(self, instance, owner)
    979 val = cache.get(self.attrname, _NOT_FOUND)
    980 if val is _NOT_FOUND:
--> 981     val = self.func(instance)
    982     try:
    983         cache[self.attrname] = val

File [/srv/conda/envs/notebook/lib/python3.10/site-packages/stacchip/indexer.py:331](https://hub.staging.digitalearthpacific.org/srv/conda/envs/notebook/lib/python3.10/site-packages/stacchip/indexer.py#line=330), in Sentinel2Indexer.scl(self)
    327 """
    328 The Scene Classification (SCL) band data for the STAC item
    329 """
    330 print("Loading scl band")
--> 331 with rasterio.open(self.item.assets["scl"].href) as src:
    332     return src.read(out_shape=(1, *self.shape), resampling=Resampling.nearest)[
    333         0
    334     ]

KeyError: 'scl'
thhomas commented 3 months ago

I could help to fix this hardcoding the test of different Scene Classification band keys "scl" and "SCL" in this case but perhaps there is a cleaner way to do it.