{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://githubtocolab.com/gee-community/geemap/blob/master/examples/notebooks/100_numpy_to_cog.ipynb)\n", "\n", "Create a fresh conda env to run this example if needed.\n", "\n", "```\n", "conda create -n cog python=3.9\n", "conda install mamba -c conda-forge\n", "mamba install geemap rio-cogeo -c conda-forge\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# !pip install geemap rio-cogeo" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import geemap" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "url = 'https://github.com/giswqs/leafmap/raw/master/examples/data/cog.tif'\n", "in_cog = 'cog.tif'\n", "out_cog = \"ndvi.tif\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Download a sample dataset." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.download_from_url(url, in_cog)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Convert image to numpy array." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "arr = geemap.image_to_numpy(in_cog)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "arr.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Computer NDVI." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ndvi = (arr[3] - arr[0]) / (arr[3] + arr[0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ndvi.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Convert numpy array to COG." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.numpy_to_cog(ndvi, out_cog, profile=in_cog)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m = geemap.Map()\n", "m.add_raster(in_cog, band=[4, 1, 2], layer_name=\"Color infrared\")\n", "m.add_raster(out_cog, palette=\"Greens\", layer_name=\"NDVI\")\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](https://i.imgur.com/OVaTyP3.gif)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }