{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "\"Open\n", "\n", "Uncomment the following line to install [geemap](https://geemap.org) if needed." ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "# !pip install geemap" ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "import ee\n", "import geemap\n", "import os" ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "geemap.show_youtube(\"_6JOA-iiEGU\")" ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map" ] }, { "cell_type": "markdown", "id": "5", "metadata": {}, "source": [ "## Download an ee.Image" ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "image = ee.Image(\"LANDSAT/LE7_TOA_5YEAR/1999_2003\")\n", "\n", "landsat_vis = {\"bands\": [\"B4\", \"B3\", \"B2\"], \"gamma\": 1.4}\n", "Map.addLayer(image, landsat_vis, \"LE7_TOA_5YEAR/1999_2003\", True, 0.7)" ] }, { "cell_type": "code", "execution_count": null, "id": "7", "metadata": {}, "outputs": [], "source": [ "# Draw any shapes on the map using the Drawing tools before executing this code block\n", "feature = Map.draw_last_feature\n", "\n", "if feature is None:\n", " geom = ee.Geometry.Polygon(\n", " [\n", " [\n", " [-115.413031, 35.889467],\n", " [-115.413031, 36.543157],\n", " [-114.034328, 36.543157],\n", " [-114.034328, 35.889467],\n", " [-115.413031, 35.889467],\n", " ]\n", " ]\n", " )\n", " feature = ee.Feature(geom, {})\n", "\n", "roi = feature.geometry()" ] }, { "cell_type": "code", "execution_count": null, "id": "8", "metadata": {}, "outputs": [], "source": [ "out_dir = os.path.join(os.path.expanduser(\"~\"), \"Downloads\")\n", "filename = os.path.join(out_dir, \"landsat.tif\")" ] }, { "cell_type": "markdown", "id": "9", "metadata": {}, "source": [ "### Exporting all bands as one single image" ] }, { "cell_type": "code", "execution_count": null, "id": "10", "metadata": {}, "outputs": [], "source": [ "image = image.clip(roi).unmask()\n", "geemap.ee_export_image(\n", " image, filename=filename, scale=90, region=roi, file_per_band=False\n", ")" ] }, { "cell_type": "markdown", "id": "11", "metadata": {}, "source": [ "### Exporting each band as one image" ] }, { "cell_type": "code", "execution_count": null, "id": "12", "metadata": {}, "outputs": [], "source": [ "geemap.ee_export_image(\n", " image, filename=filename, scale=90, region=roi, file_per_band=True\n", ")" ] }, { "cell_type": "markdown", "id": "13", "metadata": {}, "source": [ "### Export an image to Google Drive" ] }, { "cell_type": "code", "execution_count": null, "id": "14", "metadata": {}, "outputs": [], "source": [ "geemap.ee_export_image_to_drive(\n", " image, description=\"landsat\", folder=\"export\", region=roi, scale=30\n", ")" ] }, { "cell_type": "markdown", "id": "15", "metadata": {}, "source": [ "## Download an ee.ImageCollection" ] }, { "cell_type": "code", "execution_count": null, "id": "16", "metadata": {}, "outputs": [], "source": [ "import ee\n", "import geemap\n", "import os" ] }, { "cell_type": "code", "execution_count": null, "id": "17", "metadata": {}, "outputs": [], "source": [ "loc = ee.Geometry.Point(-99.2222, 46.7816)\n", "collection = (\n", " ee.ImageCollection(\"USDA/NAIP/DOQQ\")\n", " .filterBounds(loc)\n", " .filterDate(\"2008-01-01\", \"2020-01-01\")\n", " .filter(ee.Filter.listContains(\"system:band_names\", \"N\"))\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "18", "metadata": {}, "outputs": [], "source": [ "out_dir = os.path.join(os.path.expanduser(\"~\"), \"Downloads\")" ] }, { "cell_type": "code", "execution_count": null, "id": "19", "metadata": {}, "outputs": [], "source": [ "print(collection.aggregate_array(\"system:index\").getInfo())" ] }, { "cell_type": "code", "execution_count": null, "id": "20", "metadata": {}, "outputs": [], "source": [ "geemap.ee_export_image_collection(collection, out_dir=out_dir)" ] }, { "cell_type": "code", "execution_count": null, "id": "21", "metadata": {}, "outputs": [], "source": [ "geemap.ee_export_image_collection_to_drive(collection, folder=\"export\", scale=10)" ] }, { "cell_type": "markdown", "id": "22", "metadata": {}, "source": [ "## Extract pixels as a Numpy array" ] }, { "cell_type": "code", "execution_count": null, "id": "23", "metadata": {}, "outputs": [], "source": [ "import ee\n", "import geemap\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "img = ee.Image(\"LANDSAT/LC08/C01/T1_SR/LC08_038029_20180810\").select([\"B4\", \"B5\", \"B6\"])\n", "\n", "aoi = ee.Geometry.Polygon(\n", " [[[-110.8, 44.7], [-110.8, 44.6], [-110.6, 44.6], [-110.6, 44.7]]], None, False\n", ")\n", "\n", "rgb_img = geemap.ee_to_numpy(img, region=aoi)\n", "print(rgb_img.shape)" ] }, { "cell_type": "code", "execution_count": null, "id": "24", "metadata": {}, "outputs": [], "source": [ "# Scale the data to [0, 255] to show as an RGB image.\n", "# Adapted from https://bit.ly/2XlmQY8. Credits to Justin Braaten\n", "rgb_img_test = (255 * ((rgb_img[:, :, 0:3] - 100) / 3500)).astype(\"uint8\")\n", "plt.imshow(rgb_img_test)\n", "plt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }