opengeos / leafmap

A Python package for interactive mapping and geospatial analysis with minimal coding in a Jupyter environment
https://leafmap.org
MIT License
3.22k stars 386 forks source link

`add_stac_layer` failing with the latest Planetary Computer release #245

Closed TomAugspurger closed 2 years ago

TomAugspurger commented 2 years ago

Environment Information

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.

giswqs commented 2 years ago

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

TomAugspurger commented 2 years ago

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

giswqs commented 2 years ago

It should be available in an hour or two. https://github.com/conda-forge/leafmap-feedstock/pull/45

giswqs commented 2 years ago

Available on conda-forge now.

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