Download Sentinel-2 Data from Planetary Computer¶
The notebook demonstrates how to download Sentinel-2 data from the Planetary Computer using the geoai
package.
Install package¶
To use the geoai-py
package, ensure it is installed in your environment. Uncomment the command below if needed.
In [ ]:
Copied!
# %pip install geoai-py
# %pip install geoai-py
Import libraries¶
In [ ]:
Copied!
import geoai
import geoai
Search data¶
Go to the Planetary Computer and search for the data you are interested in. Copy the STAC item link and paste it to the code below.
In [ ]:
Copied!
# Example for Sentinel-2 item
item_url = "https://planetarycomputer.microsoft.com/api/stac/v1/collections/sentinel-2-l2a/items/S2B_MSIL2A_20250228T173149_R055_T14SLH_20250228T212633"
# Example for Sentinel-2 item
item_url = "https://planetarycomputer.microsoft.com/api/stac/v1/collections/sentinel-2-l2a/items/S2B_MSIL2A_20250228T173149_R055_T14SLH_20250228T212633"
Download sample data¶
In [ ]:
Copied!
# Specify which bands to download (Sentinel-2 bands)
bands_to_download = [
"B01",
"B02",
"B03",
"B04",
"B05",
"B06",
"B07",
"B08",
"B8A",
"B09",
"B11",
"B12",
"AOT",
"WVP",
"SCL",
]
# Specify which bands to download (Sentinel-2 bands)
bands_to_download = [
"B01",
"B02",
"B03",
"B04",
"B05",
"B06",
"B07",
"B08",
"B8A",
"B09",
"B11",
"B12",
"AOT",
"WVP",
"SCL",
]
In [ ]:
Copied!
# Create a directory for the downloaded bands
output_directory = "sentinel2_bands"
# Create a directory for the downloaded bands
output_directory = "sentinel2_bands"
In [ ]:
Copied!
# Download the bands, save them to the output directory, and create a merged GeoTIFF.
# The download process may take a while. Please be patient.
downloaded_bands = geoai.download_pc_stac_item(
item_url=item_url,
bands=bands_to_download,
output_dir=output_directory,
show_progress=True,
merge_bands=True,
merged_filename="sentinel2_all_bands.tif",
overwrite=False, # Skip files that already exist
cell_size=10, # Resample all bands to 10m resolution
)
# Download the bands, save them to the output directory, and create a merged GeoTIFF.
# The download process may take a while. Please be patient.
downloaded_bands = geoai.download_pc_stac_item(
item_url=item_url,
bands=bands_to_download,
output_dir=output_directory,
show_progress=True,
merge_bands=True,
merged_filename="sentinel2_all_bands.tif",
overwrite=False, # Skip files that already exist
cell_size=10, # Resample all bands to 10m resolution
)
In [ ]:
Copied!
# Print the paths to the downloaded files
for band, path in downloaded_bands.items():
print(f"Downloaded {band}: {path}")
# Print the paths to the downloaded files
for band, path in downloaded_bands.items():
print(f"Downloaded {band}: {path}")
Visualize data¶
In [ ]:
Copied!
raster_url = "https://huggingface.co/datasets/giswqs/geospatial/resolve/main/sentinel2a_kansas.tif"
raster_url = "https://huggingface.co/datasets/giswqs/geospatial/resolve/main/sentinel2a_kansas.tif"
In [ ]:
Copied!
geoai.view_raster(raster_url, bidx=[8, 4, 3], rescale="1000,4000")
geoai.view_raster(raster_url, bidx=[8, 4, 3], rescale="1000,4000")