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

add_stac_layer failing with the latest Planetary Computer release #245

Closed
TomAugspurger opened this issue May 25, 2022 · 4 comments · Fixed by #246
Closed

add_stac_layer failing with the latest Planetary Computer release #245

TomAugspurger opened this issue May 25, 2022 · 4 comments · Fixed by #246
Labels
bug Something isn't working

Comments

@TomAugspurger
Copy link

TomAugspurger commented May 25, 2022

Environment Information

  • leafmap version: 0.9.2
  • Python version: 3.8.13
  • Operating System: linux

Description

Use add_stac_layer with a STAC item from the Planetary Computer.

What I Did

import leafmap

m = leafmap.Map()
m.add_stac_layer(
    collection="landsat-8-c2-l2", item="LC08_L2SP_047027_20201204_02_T1", assets="SR_B2",
)
m
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [13], in <cell line: 4>()
      1 import leafmap
      3 m = leafmap.Map()
----> 4 m.add_stac_layer(
      5     collection="landsat-8-c2-l2", item="LC08_L2SP_047027_20201204_02_T1", assets="SR_B2",
      6 )
      7 m

File /srv/conda/envs/notebook/lib/python3.8/site-packages/leafmap/leafmap.py:846, in Map.add_stac_layer(self, url, collection, item, assets, bands, titiler_endpoint, name, attribution, opacity, shown, **kwargs)
    818 def add_stac_layer(
    819     self,
    820     url=None,
   (...)
    830     **kwargs,
    831 ):
    832     """Adds a STAC TileLayer to the map.
    833 
    834     Args:
   (...)
    844         shown (bool, optional): A flag indicating whether the layer should be on by default. Defaults to True.
    845     """
--> 846     tile_url = stac_tile(
    847         url, collection, item, assets, bands, titiler_endpoint, **kwargs
    848     )
    849     bounds = stac_bounds(url, collection, item, titiler_endpoint)
    850     self.add_tile_layer(tile_url, name, attribution, opacity, shown)

File /srv/conda/envs/notebook/lib/python3.8/site-packages/leafmap/common.py:1401, in stac_tile(url, collection, item, assets, bands, titiler_endpoint, **kwargs)
   1392 stats = stac_stats(
   1393     collection=collection,
   1394     item=item,
   1395     assets=assets,
   1396     titiler_endpoint=titiler_endpoint,
   1397 )
   1398 if "detail" not in stats:
   1400     percentile_2 = min(
-> 1401         [stats[s][list(stats[s].keys())[0]]["percentile_2"] for s in stats]
   1402     )
   1403     percentile_98 = max(
   1404         [stats[s][list(stats[s].keys())[0]]["percentile_98"] for s in stats]
   1405     )
   1406     kwargs["rescale"] = f"{percentile_2},{percentile_98}"

File /srv/conda/envs/notebook/lib/python3.8/site-packages/leafmap/common.py:1401, in <listcomp>(.0)
   1392 stats = stac_stats(
   1393     collection=collection,
   1394     item=item,
   1395     assets=assets,
   1396     titiler_endpoint=titiler_endpoint,
   1397 )
   1398 if "detail" not in stats:
   1400     percentile_2 = min(
-> 1401         [stats[s][list(stats[s].keys())[0]]["percentile_2"] for s in stats]
   1402     )
   1403     percentile_98 = max(
   1404         [stats[s][list(stats[s].keys())[0]]["percentile_98"] for s in stats]
   1405     )
   1406     kwargs["rescale"] = f"{percentile_2},{percentile_98}"

TypeError: 'float' object is not subscriptable

Here's stats[s]:

ipdb>  pp stats[s]
{'count': 673124.0,
 'histogram': [[629793.0,
                32472.0,
                4089.0,
                2010.0,
                1372.0,
                1104.0,
                919.0,
                633.0,
                516.0,
                216.0],
               [2544.0,
                8811.9,
                15079.8,
                21347.699999999997,
                27615.6,
                33883.5,
                40151.399999999994,
                46419.299999999996,
                52687.2,
                58955.1,
                65223.0]],
 'majority': 7550.0,
 'masked_pixels': 361116.0,
 'max': 65223.0,
 'mean': 8302.842997724045,
 'median': 7791.0,
 'min': 2544.0,
 'minority': 2544.0,
 'percentile_2': 7333.0,
 'percentile_98': 13187.540000000037,
 'std': 3190.7601092841633,
 'sum': 5588842890.0,
 'unique': 16445.0,
 'valid_percent': 65.08,
 'valid_pixels': 673124.0}

It seems that the structure of the stats object changed with this release. https://github.com/microsoft/planetary-computer-apis/blob/174db793bfe9f572fff13b04e13bac1005d212fc/pctiler/setup.py#L13-L14 has the TiTiler versions we're using (if that's the relevant library that changed).

The fix seems pretty simple, to just use stats[s]["precentile_2"]. But I suspect that would break clients working with older versions of TiTiler.

End-users can work around this by providing an expression or rescale.

@TomAugspurger TomAugspurger added the bug Something isn't working label May 25, 2022
giswqs added a commit that referenced this issue May 25, 2022
@giswqs giswqs linked a pull request May 25, 2022 that will close this issue
giswqs added a commit that referenced this issue May 25, 2022
@giswqs
Copy link
Member

giswqs commented May 25, 2022

@TomAugspurger Thanks for reporting. This bug has been fixed in the new release v0.9.3

@TomAugspurger
Copy link
Author

Thanks! I'll get the leafmap version used on the Planetary Computer updated once it's available through conda-forge.

@giswqs
Copy link
Member

giswqs commented May 25, 2022

It should be available in an hour or two.
conda-forge/leafmap-feedstock#45

@giswqs
Copy link
Member

giswqs commented May 25, 2022

Available on conda-forge now.

https://anaconda.org/conda-forge/leafmap

sthagen pushed a commit to sthagen/giswqs-leafmap that referenced this issue Feb 18, 2023
sthagen pushed a commit to sthagen/giswqs-leafmap that referenced this issue Feb 18, 2023
sthagen pushed a commit to sthagen/giswqs-leafmap that referenced this issue Jul 10, 2024
sthagen pushed a commit to sthagen/giswqs-leafmap that referenced this issue Jul 10, 2024
kuzja111 pushed a commit to kuzja111/leafmap that referenced this issue Jul 25, 2024
kuzja111 pushed a commit to kuzja111/leafmap that referenced this issue Jul 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants