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

GHRCCLOUD-6109 Leafmap integration #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
256 changes: 256 additions & 0 deletions 6. COG.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A Cloud Optimized GeoTIFF (COG) is a specific type of GeoTIFF file that is optimized for use over the internet. It allows for efficient, partial reading of a large raster image file over the network, without needing to download the entire file first."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pip install leafmap"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Conda install is recommended (pip install had missing dependencies)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"conda install -n geo -c conda-forge localtileserver"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import leafmap\n",
"import os"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Resolves CRS invalid error by downloading the latest version of leafmap"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# leafmap.update_package()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create an interactive map"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Map = leafmap.Map()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Input file's url should point to a cloud url that is not password protected"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# url = \"https://fcx-downloader.s3.amazonaws.com/HLS.L30.T59VLL.2024150T235328.v2.0.B10.tiff\"\n",
"# url = \"https://fcx-downloader.s3.amazonaws.com/TIF/HLS.L30.T59VLL.2024149T235855.v2.0.B10.tiff\"\n",
"# url = \"https://fcx-downloader.s3.amazonaws.com/TIF/HLS_TIF/HLS.L30.T59VLL.2024156T001136.v2.0.B02.tiff\"\n",
"url = \"https://fcx-downloader.s3.amazonaws.com/h1qr42lq/HLS.L30.T16XER.2024156T000315.v2.0.B02.tif\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Validate COG file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"leafmap.cog_validate(url, verbose = True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Titiler is a modern dynamic tile server built on top of FastAPI and Rasterio/GDAL. Leafmap uses Titiler to decode the structure of COG file as a JSON."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"os.environ[\"TITILER_ENDPOINT\"] = \"https://titiler.xyz\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Retrieve the bounding box coordinates, band names, centroid coordinates, and tile layer URL of the COG file."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"leafmap.cog_bounds(url)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"leafmap.cog_bands(url)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"leafmap.cog_center(url)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"leafmap.cog_tile(url)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Add a COG layer to the map. The layer can be given a name or bands as parameters."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Map.add_cog_layer(url)\n",
"Map"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To create timelapse of multiple COG files, images should be a local path consisting of file names ending with \".tif\"\n",
"This expects TIF images in a specific format. For e.g, HLS(Harmonized Landsat and Sentinel-2) L30 TIF images could not be visualized but [Landsat7](https://search.earthdata.nasa.gov/search/granules?p=C1979944011-GHRC_DAAC&pg[0][v]=f&pg[0][qt]=2002-10-02T00%3A00%3A00.000Z%2C2002-10-02T23%3A59%3A59.999Z&pg[0][id]=L71021039*TIF&pg[0][gsk]=-start_date&g=G1979944367-GHRC_DAAC&q=landsat&fdc=Global%2BHydrology%2BResource%2BCenter%2B%2528GHRC%2529&tl=1717122715!3!!&lat=-33.75&long=-75.375&zoom=0) images works fine as seen below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"images = \"/Users/Indhuja/Downloads/L7/\"\n",
"\n",
"import os\n",
"os.listdir(images)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"leafmap.create_timelapse(\n",
" images,\n",
" out_gif=\"l7.gif\",\n",
" # bands = [1,2],\n",
" fps=1,\n",
" mp4=False,\n",
" quiet=False,\n",
" reduce_size=True,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"leafmap.show_image(\"l7.gif\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading