37 planetary computer
Uncomment the following line to install leafmap if needed.
In [1]:
Copied!
# !pip install leafmap
# !pip install leafmap
In [2]:
Copied!
import leafmap
import leafmap
Add a STAC item via an HTTP URL
In [3]:
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 [4]:
Copied!
leafmap.stac_assets(url)
leafmap.stac_assets(url)
Out[4]:
['pan', 'B1', 'B2', 'B3', 'B4']
In [5]:
Copied!
leafmap.stac_bounds(url)
leafmap.stac_bounds(url)
Out[5]:
[-111.6453245, 60.59892389999882, -110.1583693, 61.30928879999903]
In [6]:
Copied!
leafmap.stac_center(url)
leafmap.stac_center(url)
Out[6]:
(-110.90184690000001, 60.95410634999892)
In [7]:
Copied!
# leafmap.stac_info(url)
# leafmap.stac_info(url)
In [8]:
Copied!
# leafmap.stac_stats(url)
# leafmap.stac_stats(url)
In [9]:
Copied!
m = leafmap.Map()
m.add_stac_layer(url, bands=["B3", "B2", "B1"])
m
m = leafmap.Map()
m.add_stac_layer(url, bands=["B3", "B2", "B1"])
m
Out[9]:
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 [10]:
Copied!
# import os
# os.environ["TITILER_ENDPOINT"] = "planetary-computer"
# import os
# os.environ["TITILER_ENDPOINT"] = "planetary-computer"
In [11]:
Copied!
collection = "landsat-8-c2-l2"
collection = "landsat-8-c2-l2"
In [12]:
Copied!
item = "LC08_L2SP_047027_20201204_02_T1"
item = "LC08_L2SP_047027_20201204_02_T1"
In [13]:
Copied!
leafmap.stac_assets(collection=collection, item=item, titiler_endpoint="pc")
leafmap.stac_assets(collection=collection, item=item, titiler_endpoint="pc")
Out[13]:
['SR_B1', 'SR_B2', 'SR_B3', 'SR_B4', 'SR_B5', 'SR_B6', 'SR_B7', 'ST_QA', 'ST_B10', 'ST_DRAD', 'ST_EMIS', 'ST_EMSD', 'ST_TRAD', 'ST_URAD', 'QA_PIXEL', 'ST_ATRAN', 'ST_CDIST', 'QA_RADSAT', 'SR_QA_AEROSOL']
In [14]:
Copied!
leafmap.stac_bounds(collection=collection, item=item)
leafmap.stac_bounds(collection=collection, item=item)
Out[14]:
[-124.98085491310867, 46.35352512466258, -121.78788697796408, 48.51466487533742]
In [15]:
Copied!
leafmap.stac_info(collection=collection, item=item, assets="SR_B7")
leafmap.stac_info(collection=collection, item=item, assets="SR_B7")
Out[15]:
{'SR_B7': {'bounds': [-124.9808633132731, 46.35352340750138, -121.78787875260963, 48.52103509752985], 'minzoom': 7, 'maxzoom': 12, 'band_metadata': [['b1', {}]], 'band_descriptions': [['b1', '']], 'dtype': 'uint16', 'nodata_type': 'Nodata', 'colorinterp': ['gray'], 'height': 7971, 'count': 1, 'width': 7861, 'overviews': [2, 4, 8, 16, 32, 64], 'nodata_value': 0.0, 'driver': 'GTiff'}}
In [16]:
Copied!
leafmap.stac_stats(collection=collection, item=item, assets="SR_B7")
leafmap.stac_stats(collection=collection, item=item, assets="SR_B7")
Out[16]:
{'SR_B7_b1': {'min': 6912.0, 'max': 32069.0, 'mean': 8275.555009181073, 'count': 673124.0, 'sum': 5570474690.0, 'std': 1156.664685686523, 'median': 8046.0, 'majority': 7295.0, 'minority': 6912.0, 'unique': 8681.0, 'histogram': [[587083.0, 79039.0, 4498.0, 1328.0, 914.0, 222.0, 37.0, 2.0, 0.0, 1.0], [6912.0, 9427.7, 11943.4, 14459.099999999999, 16974.8, 19490.5, 22006.199999999997, 24521.899999999998, 27037.6, 29553.3, 32069.0]], 'valid_percent': 65.08, 'masked_pixels': 361116.0, 'valid_pixels': 673124.0, 'percentile_98': 11233.0, 'percentile_2': 7278.0}}
Color infrared composite.
In [17]:
Copied!
m = leafmap.Map()
m.add_stac_layer(
collection=collection,
item=item,
assets=["SR_B5", "SR_B4", "SR_B3"],
name="Color infrared",
)
m
m = leafmap.Map()
m.add_stac_layer(
collection=collection,
item=item,
assets=["SR_B5", "SR_B4", "SR_B3"],
name="Color infrared",
)
m
Out[17]:
False color composite.
In [18]:
Copied!
m = leafmap.Map()
m.add_stac_layer(
collection=collection, item=item, assets="SR_B7,SR_B5,SR_B4", name="False color"
)
m
m = leafmap.Map()
m.add_stac_layer(
collection=collection, item=item, assets="SR_B7,SR_B5,SR_B4", name="False color"
)
m
Out[18]:
Calculate NDVI.
In [19]:
Copied!
m = leafmap.Map()
m.add_stac_layer(
collection=collection,
item=item,
expression="(SR_B5-SR_B4)/(SR_B5+SR_B4)",
rescale="-1,1",
name="NDVI",
)
m
m = leafmap.Map()
m.add_stac_layer(
collection=collection,
item=item,
expression="(SR_B5-SR_B4)/(SR_B5+SR_B4)",
rescale="-1,1",
name="NDVI",
)
m
Out[19]:
Calculate NDVI and add a colormap. See available colormaps at https://planetarycomputer.microsoft.com/docs/reference/data/
In [20]:
Copied!
m = leafmap.Map()
m.add_stac_layer(
collection=collection, item=item, assets="SR_B5,SR_B4,SR_B3", name="Color infrared"
)
m.add_stac_layer(
collection=collection,
item=item,
expression="(SR_B5-SR_B4)/(SR_B5+SR_B4)",
rescale="-1,1",
colormap_name="greens",
name="NDVI Green",
)
m
m = leafmap.Map()
m.add_stac_layer(
collection=collection, item=item, assets="SR_B5,SR_B4,SR_B3", name="Color infrared"
)
m.add_stac_layer(
collection=collection,
item=item,
expression="(SR_B5-SR_B4)/(SR_B5+SR_B4)",
rescale="-1,1",
colormap_name="greens",
name="NDVI Green",
)
m
Out[20]: