91 planetary computer
Uncomment the following line to install geemap if needed.
In [1]:
Copied!
# !pip install geemap
# !pip install geemap
In [2]:
Copied!
import geemap
import geemap
If you are using a recently implemented geemap feature that has not yet been released to PyPI or conda-forge, you can uncomment the following line to install the development version from GitHub.
In [3]:
Copied!
# geemap.update_package()
# geemap.update_package()
Add a STAC item via an HTTP URL
In [4]:
Copied!
url = 'https://canada-spot-ortho.s3.amazonaws.com/canada_spot_orthoimages/canada_spot5_orthoimages/S5_2007/S5_11055_6057_20070622/S5_11055_6057_20070622.json'
url = 'https://canada-spot-ortho.s3.amazonaws.com/canada_spot_orthoimages/canada_spot5_orthoimages/S5_2007/S5_11055_6057_20070622/S5_11055_6057_20070622.json'
In [5]:
Copied!
geemap.stac_assets(url)
geemap.stac_assets(url)
Out[5]:
['pan', 'B1', 'B2', 'B3', 'B4']
In [6]:
Copied!
geemap.stac_bounds(url)
geemap.stac_bounds(url)
Out[6]:
[-111.6453245, 60.59892389999882, -110.1583693, 61.30928879999903]
In [7]:
Copied!
geemap.stac_center(url)
geemap.stac_center(url)
Out[7]:
(-110.90184690000001, 60.95410634999892)
In [8]:
Copied!
# geemap.stac_info(url)
# geemap.stac_info(url)
In [9]:
Copied!
# geemap.stac_stats(url)
# geemap.stac_stats(url)
In [10]:
Copied!
m = geemap.Map()
m.add_stac_layer(url, bands=["B3", "B2", "B1"])
m
m = geemap.Map()
m.add_stac_layer(url, bands=["B3", "B2", "B1"])
m
Out[10]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Add a Microsoft Planetry Computer STAC item. The titiler endpoint can set in one of the ways below:
os.environ["TITILER_ENDPOINT"] = "planetary-computer"
titiler_endpoint="pc"
titiler_endpoint="planetary-computer"
In [11]:
Copied!
# import os
# os.environ["TITILER_ENDPOINT"] = "planetary-computer"
# import os
# os.environ["TITILER_ENDPOINT"] = "planetary-computer"
In [12]:
Copied!
collection = "landsat-8-c2-l2"
collection = "landsat-8-c2-l2"
In [13]:
Copied!
items = "LC08_L2SP_047027_20201204_02_T1"
items = "LC08_L2SP_047027_20201204_02_T1"
In [14]:
Copied!
geemap.stac_assets(collection=collection, items=items, titiler_endpoint="pc")
geemap.stac_assets(collection=collection, items=items, titiler_endpoint="pc")
Out[14]:
{'detail': [{'loc': ['query', 'item'], 'msg': 'field required', 'type': 'value_error.missing'}]}
In [15]:
Copied!
geemap.stac_bounds(collection=collection, items=items)
geemap.stac_bounds(collection=collection, items=items)
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Input In [15], in <cell line: 1>() ----> 1 geemap.stac_bounds(collection=collection, items=items) File ~/.local/lib/python3.9/site-packages/geemap/common.py:4620, in stac_bounds(url, collection, item, titiler_endpoint, **kwargs) 4617 else: 4618 r = requests.get(titiler_endpoint.url_for_stac_bounds(), params=kwargs).json() -> 4620 bounds = r["bounds"] 4621 return bounds KeyError: 'bounds'
In [16]:
Copied!
geemap.stac_info(collection=collection, items=items, assets="SR_B7")
geemap.stac_info(collection=collection, items=items, assets="SR_B7")
Out[16]:
{'detail': [{'loc': ['query', 'item'], 'msg': 'field required', 'type': 'value_error.missing'}]}
In [17]:
Copied!
geemap.stac_stats(collection=collection, items=items, assets="SR_B7")
geemap.stac_stats(collection=collection, items=items, assets="SR_B7")
Out[17]:
{'detail': [{'loc': ['query', 'item'], 'msg': 'field required', 'type': 'value_error.missing'}]}
Color infrared composite.
In [18]:
Copied!
m = geemap.Map()
m.add_stac_layer(
collection=collection,
items=items,
assets="SR_B5,SR_B4,SR_B3",
name="Color infrared",
)
m
m = geemap.Map()
m.add_stac_layer(
collection=collection,
items=items,
assets="SR_B5,SR_B4,SR_B3",
name="Color infrared",
)
m
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [18], in <cell line: 2>() 1 m = geemap.Map() ----> 2 m.add_stac_layer( 3 collection=collection, 4 items=items, 5 assets="SR_B5,SR_B4,SR_B3", 6 name="Color infrared", 7 ) 8 m File ~/.local/lib/python3.9/site-packages/geemap/foliumap.py:547, in Map.add_stac_layer(self, url, collection, item, assets, bands, titiler_endpoint, name, attribution, opacity, shown, **kwargs) 519 def add_stac_layer( 520 self, 521 url=None, (...) 531 **kwargs, 532 ): 533 """Adds a STAC TileLayer to the map. 534 535 Args: (...) 545 shown (bool, optional): A flag indicating whether the layer should be on by default. Defaults to True. 546 """ --> 547 tile_url = stac_tile( 548 url, collection, item, assets, bands, titiler_endpoint, **kwargs 549 ) 550 bounds = stac_bounds(url, collection, item, titiler_endpoint) 551 self.add_tile_layer( 552 url=tile_url, 553 name=name, (...) 556 shown=shown, 557 ) File ~/.local/lib/python3.9/site-packages/geemap/common.py:4553, in stac_tile(url, collection, item, assets, bands, titiler_endpoint, **kwargs) 4540 if ( 4541 (assets is not None) 4542 and ("asset_expression" not in kwargs) 4543 and ("expression" not in kwargs) 4544 and ("rescale" not in kwargs) 4545 ): 4546 stats = stac_stats( 4547 collection=collection, 4548 item=item, 4549 assets=assets, 4550 titiler_endpoint=titiler_endpoint, 4551 ) 4552 percentile_2 = min( -> 4553 [stats[s][list(stats[s].keys())[0]]["percentile_2"] for s in stats] 4554 ) 4555 percentile_98 = max( 4556 [stats[s][list(stats[s].keys())[0]]["percentile_98"] for s in stats] 4557 ) 4558 kwargs["rescale"] = f"{percentile_2},{percentile_98}" File ~/.local/lib/python3.9/site-packages/geemap/common.py:4553, in <listcomp>(.0) 4540 if ( 4541 (assets is not None) 4542 and ("asset_expression" not in kwargs) 4543 and ("expression" not in kwargs) 4544 and ("rescale" not in kwargs) 4545 ): 4546 stats = stac_stats( 4547 collection=collection, 4548 item=item, 4549 assets=assets, 4550 titiler_endpoint=titiler_endpoint, 4551 ) 4552 percentile_2 = min( -> 4553 [stats[s][list(stats[s].keys())[0]]["percentile_2"] for s in stats] 4554 ) 4555 percentile_98 = max( 4556 [stats[s][list(stats[s].keys())[0]]["percentile_98"] for s in stats] 4557 ) 4558 kwargs["rescale"] = f"{percentile_2},{percentile_98}" AttributeError: 'list' object has no attribute 'keys'
False color composite.
In [19]:
Copied!
m = geemap.Map()
m.add_stac_layer(
collection=collection, items=items, assets="SR_B7,SR_B5,SR_B4", name="False color"
)
m
m = geemap.Map()
m.add_stac_layer(
collection=collection, items=items, assets="SR_B7,SR_B5,SR_B4", name="False color"
)
m
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [19], in <cell line: 2>() 1 m = geemap.Map() ----> 2 m.add_stac_layer( 3 collection=collection, items=items, assets="SR_B7,SR_B5,SR_B4", name="False color" 4 ) 5 m File ~/.local/lib/python3.9/site-packages/geemap/foliumap.py:547, in Map.add_stac_layer(self, url, collection, item, assets, bands, titiler_endpoint, name, attribution, opacity, shown, **kwargs) 519 def add_stac_layer( 520 self, 521 url=None, (...) 531 **kwargs, 532 ): 533 """Adds a STAC TileLayer to the map. 534 535 Args: (...) 545 shown (bool, optional): A flag indicating whether the layer should be on by default. Defaults to True. 546 """ --> 547 tile_url = stac_tile( 548 url, collection, item, assets, bands, titiler_endpoint, **kwargs 549 ) 550 bounds = stac_bounds(url, collection, item, titiler_endpoint) 551 self.add_tile_layer( 552 url=tile_url, 553 name=name, (...) 556 shown=shown, 557 ) File ~/.local/lib/python3.9/site-packages/geemap/common.py:4553, in stac_tile(url, collection, item, assets, bands, titiler_endpoint, **kwargs) 4540 if ( 4541 (assets is not None) 4542 and ("asset_expression" not in kwargs) 4543 and ("expression" not in kwargs) 4544 and ("rescale" not in kwargs) 4545 ): 4546 stats = stac_stats( 4547 collection=collection, 4548 item=item, 4549 assets=assets, 4550 titiler_endpoint=titiler_endpoint, 4551 ) 4552 percentile_2 = min( -> 4553 [stats[s][list(stats[s].keys())[0]]["percentile_2"] for s in stats] 4554 ) 4555 percentile_98 = max( 4556 [stats[s][list(stats[s].keys())[0]]["percentile_98"] for s in stats] 4557 ) 4558 kwargs["rescale"] = f"{percentile_2},{percentile_98}" File ~/.local/lib/python3.9/site-packages/geemap/common.py:4553, in <listcomp>(.0) 4540 if ( 4541 (assets is not None) 4542 and ("asset_expression" not in kwargs) 4543 and ("expression" not in kwargs) 4544 and ("rescale" not in kwargs) 4545 ): 4546 stats = stac_stats( 4547 collection=collection, 4548 item=item, 4549 assets=assets, 4550 titiler_endpoint=titiler_endpoint, 4551 ) 4552 percentile_2 = min( -> 4553 [stats[s][list(stats[s].keys())[0]]["percentile_2"] for s in stats] 4554 ) 4555 percentile_98 = max( 4556 [stats[s][list(stats[s].keys())[0]]["percentile_98"] for s in stats] 4557 ) 4558 kwargs["rescale"] = f"{percentile_2},{percentile_98}" AttributeError: 'list' object has no attribute 'keys'
Calculate NDVI.
In [20]:
Copied!
geemap.stac_stats(
collection=collection,
items=items,
expression="(SR_B5-SR_B4)/(SR_B5+SR_B4)",
)
geemap.stac_stats(
collection=collection,
items=items,
expression="(SR_B5-SR_B4)/(SR_B5+SR_B4)",
)
Out[20]:
{'detail': [{'loc': ['query', 'item'], 'msg': 'field required', 'type': 'value_error.missing'}]}
In [21]:
Copied!
m = geemap.Map()
m.add_stac_layer(
collection=collection,
items=items,
expression="(SR_B5-SR_B4)/(SR_B5+SR_B4)",
name="NDVI",
)
m
m = geemap.Map()
m.add_stac_layer(
collection=collection,
items=items,
expression="(SR_B5-SR_B4)/(SR_B5+SR_B4)",
name="NDVI",
)
m
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Input In [21], in <cell line: 2>() 1 m = geemap.Map() ----> 2 m.add_stac_layer( 3 collection=collection, 4 items=items, 5 expression="(SR_B5-SR_B4)/(SR_B5+SR_B4)", 6 name="NDVI", 7 ) 8 m File ~/.local/lib/python3.9/site-packages/geemap/foliumap.py:547, in Map.add_stac_layer(self, url, collection, item, assets, bands, titiler_endpoint, name, attribution, opacity, shown, **kwargs) 519 def add_stac_layer( 520 self, 521 url=None, (...) 531 **kwargs, 532 ): 533 """Adds a STAC TileLayer to the map. 534 535 Args: (...) 545 shown (bool, optional): A flag indicating whether the layer should be on by default. Defaults to True. 546 """ --> 547 tile_url = stac_tile( 548 url, collection, item, assets, bands, titiler_endpoint, **kwargs 549 ) 550 bounds = stac_bounds(url, collection, item, titiler_endpoint) 551 self.add_tile_layer( 552 url=tile_url, 553 name=name, (...) 556 shown=shown, 557 ) File ~/.local/lib/python3.9/site-packages/geemap/common.py:4585, in stac_tile(url, collection, item, assets, bands, titiler_endpoint, **kwargs) 4582 else: 4583 r = requests.get(titiler_endpoint.url_for_stac_item(), params=kwargs).json() -> 4585 return r["tiles"][0] KeyError: 'tiles'
Calculate NDVI and add a colormap. See available colormaps at https://planetarycomputer.microsoft.com/docs/reference/data/
In [22]:
Copied!
m = geemap.Map()
m.add_stac_layer(
collection=collection,
items=items,
assets="SR_B5,SR_B4,SR_B3",
name="Color infrared",
)
m.add_stac_layer(
collection=collection,
items=items,
expression="(SR_B5-SR_B4)/(SR_B5+SR_B4)",
colormap_name="greens",
name="NDVI Green",
)
m
m = geemap.Map()
m.add_stac_layer(
collection=collection,
items=items,
assets="SR_B5,SR_B4,SR_B3",
name="Color infrared",
)
m.add_stac_layer(
collection=collection,
items=items,
expression="(SR_B5-SR_B4)/(SR_B5+SR_B4)",
colormap_name="greens",
name="NDVI Green",
)
m
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [22], in <cell line: 2>() 1 m = geemap.Map() ----> 2 m.add_stac_layer( 3 collection=collection, 4 items=items, 5 assets="SR_B5,SR_B4,SR_B3", 6 name="Color infrared", 7 ) 8 m.add_stac_layer( 9 collection=collection, 10 items=items, (...) 13 name="NDVI Green", 14 ) 15 m File ~/.local/lib/python3.9/site-packages/geemap/foliumap.py:547, in Map.add_stac_layer(self, url, collection, item, assets, bands, titiler_endpoint, name, attribution, opacity, shown, **kwargs) 519 def add_stac_layer( 520 self, 521 url=None, (...) 531 **kwargs, 532 ): 533 """Adds a STAC TileLayer to the map. 534 535 Args: (...) 545 shown (bool, optional): A flag indicating whether the layer should be on by default. Defaults to True. 546 """ --> 547 tile_url = stac_tile( 548 url, collection, item, assets, bands, titiler_endpoint, **kwargs 549 ) 550 bounds = stac_bounds(url, collection, item, titiler_endpoint) 551 self.add_tile_layer( 552 url=tile_url, 553 name=name, (...) 556 shown=shown, 557 ) File ~/.local/lib/python3.9/site-packages/geemap/common.py:4553, in stac_tile(url, collection, item, assets, bands, titiler_endpoint, **kwargs) 4540 if ( 4541 (assets is not None) 4542 and ("asset_expression" not in kwargs) 4543 and ("expression" not in kwargs) 4544 and ("rescale" not in kwargs) 4545 ): 4546 stats = stac_stats( 4547 collection=collection, 4548 item=item, 4549 assets=assets, 4550 titiler_endpoint=titiler_endpoint, 4551 ) 4552 percentile_2 = min( -> 4553 [stats[s][list(stats[s].keys())[0]]["percentile_2"] for s in stats] 4554 ) 4555 percentile_98 = max( 4556 [stats[s][list(stats[s].keys())[0]]["percentile_98"] for s in stats] 4557 ) 4558 kwargs["rescale"] = f"{percentile_2},{percentile_98}" File ~/.local/lib/python3.9/site-packages/geemap/common.py:4553, in <listcomp>(.0) 4540 if ( 4541 (assets is not None) 4542 and ("asset_expression" not in kwargs) 4543 and ("expression" not in kwargs) 4544 and ("rescale" not in kwargs) 4545 ): 4546 stats = stac_stats( 4547 collection=collection, 4548 item=item, 4549 assets=assets, 4550 titiler_endpoint=titiler_endpoint, 4551 ) 4552 percentile_2 = min( -> 4553 [stats[s][list(stats[s].keys())[0]]["percentile_2"] for s in stats] 4554 ) 4555 percentile_98 = max( 4556 [stats[s][list(stats[s].keys())[0]]["percentile_98"] for s in stats] 4557 ) 4558 kwargs["rescale"] = f"{percentile_2},{percentile_98}" AttributeError: 'list' object has no attribute 'keys'
Last update:
2022-03-14