{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Earth Engine" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[![colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/davemlz/spyndex/blob/main/docs/tutorials/ee.ipynb)\n", "![level7](https://raw.githubusercontent.com/davemlz/spyndex/main/docs/_static/level7.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "New level: Level 7 - `spyndex + Google Earth Engine`!\n", "\n", "Remember to install `spyndex`!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install -U spyndex" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's start!\n", "\n", "First, import `spyndex` and `Earth Engine`:\n", "\n", "> We also import `eemont` and `geemap` to make things easier." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "import spyndex\n", "import ee, eemont, geemap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And remember to initialize `Earth Engine`:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `ee.Number`\n", "\n", "Just as Python Built-In types, the `ee.Number` can be used with `spyndex`!" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "NIR = ee.Number(0.678)\n", "RED = ee.Number(0.123)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's check our data types:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "NIR type: , value: 0.678\n", "RED type: , value: 0.123\n" ] } ], "source": [ "print(f\"NIR type: {type(NIR)}, value: {NIR.getInfo()}\")\n", "print(f\"RED type: {type(RED)}, value: {RED.getInfo()}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And let's compute the `NDVI`." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "idx = spyndex.computeIndex(\"NDVI\",{\"N\": NIR, \"R\": RED})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And our result is also an `ee.Number`!" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "idx type: , value: 0.6928838951310862\n" ] } ], "source": [ "print(f\"idx type: {type(idx)}, value: {idx.getInfo()}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `ee.Image`\n", "\n", "All spectral indices from `Awesome Spectral Indices` can be computed automatically in Python for `Earth Engine` by using `eemont` for most datasets (Sentinel-2, MODIS, Landsat, etc.). However, some datasets are not included. NAIP is a good example, let's use a NAIP image!" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "img = ee.Image(\"USDA/NAIP/DOQQ/m_3009057_ne_15_1_20170910\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "NAIP images values go from 0 to 255, so, let's correct that!" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "img = img / 255.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's check the image metadata:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'type': 'Image',\n", " 'bands': [{'id': 'R',\n", " 'data_type': {'type': 'PixelType',\n", " 'precision': 'float',\n", " 'min': 0,\n", " 'max': 1},\n", " 'dimensions': [6753, 7640],\n", " 'crs': 'EPSG:26915',\n", " 'crs_transform': [1, 0, 698395, 0, -1, 3334843]},\n", " {'id': 'G',\n", " 'data_type': {'type': 'PixelType',\n", " 'precision': 'float',\n", " 'min': 0,\n", " 'max': 1},\n", " 'dimensions': [6753, 7640],\n", " 'crs': 'EPSG:26915',\n", " 'crs_transform': [1, 0, 698395, 0, -1, 3334843]},\n", " {'id': 'B',\n", " 'data_type': {'type': 'PixelType',\n", " 'precision': 'float',\n", " 'min': 0,\n", " 'max': 1},\n", " 'dimensions': [6753, 7640],\n", " 'crs': 'EPSG:26915',\n", " 'crs_transform': [1, 0, 698395, 0, -1, 3334843]},\n", " {'id': 'N',\n", " 'data_type': {'type': 'PixelType',\n", " 'precision': 'float',\n", " 'min': 0,\n", " 'max': 1},\n", " 'dimensions': [6753, 7640],\n", " 'crs': 'EPSG:26915',\n", " 'crs_transform': [1, 0, 698395, 0, -1, 3334843]}]}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "img.getInfo()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Great! We have 4 bands (R, G, B, N) and all of them go now from 0 to 1.\n", "\n", "Let's compute some indices!" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "idx = spyndex.computeIndex(\n", " index = [\"VARI\",\"ExG\",\"NDVI\"],\n", " params = {\n", " \"R\": img[\"R\"],\n", " \"G\": img[\"G\"],\n", " \"B\": img[\"B\"],\n", " \"N\": img[\"N\"]\n", " }\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we check the indices, we'll see that the result is another `ee.Image`!" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "idx type: \n" ] } ], "source": [ "print(f\"idx type: {type(idx)}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's add those indices as new bands in the original image." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "img = img.addBands(idx)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we check our bands, we'll see that the new bands (indices) were added:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['R', 'G', 'B', 'N', 'VARI', 'ExG', 'NDVI']" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "img.bandNames().getInfo()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's visualize evrything interactively with `geemap`!\n", "\n", "First, import the colormaps:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "import geemap.colormaps as cm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And then, plot everything!" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "878c3125b2794febbb3e9fab862c0f5c", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Map(center=[30.09375197359924, -90.90623221580759], controls=(WidgetControl(options=['position', 'transparent_…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "Map = geemap.Map()\n", "Map.addLayer(img[[\"R\",\"G\",\"B\"]],{\"min\":0,\"max\":0.3},\"RGB\")\n", "Map.addLayer(img[\"VARI\"],{\"palette\":cm.palettes.ndvi},\"VARI\")\n", "Map.addLayer(img[\"ExG\"],{\"palette\":cm.palettes.ndvi},\"ExG\")\n", "Map.addLayer(img[\"NDVI\"],{\"palette\":cm.palettes.ndvi},\"NDVI\")\n", "Map.centerObject(img,13)\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Amazing!!!! :)" ] } ], "metadata": { "interpreter": { "hash": "ec6a25acaecf7f06cb08206f3f56e96ccaf6fbab432a979bcf67c9e0ca577c87" }, "kernelspec": { "display_name": "Python 3.9.7 64-bit ('spyndex': conda)", "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.9.7" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }