{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\"Open\n", "\n", "Uncomment the following line to install [geemap](https://geemap.org) if needed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# !pip install geemap" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import ee\n", "import geemap\n", "import os" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.show_youtube('_6JOA-iiEGU')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Download an ee.Image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = ee.Image('LE7_TOA_5YEAR/1999_2003')\n", "\n", "landsat_vis = {\n", " 'bands': ['B4', 'B3', 'B2'], \n", " 'gamma': 1.4\n", "}\n", "Map.addLayer(image, landsat_vis, \"LE7_TOA_5YEAR/1999_2003\", True, 0.7)" ] }, { "cell_type": "code", "execution_count": null, "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([[[-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", " feature = ee.Feature(geom, {})\n", "\n", "roi = feature.geometry()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')\n", "filename = os.path.join(out_dir, 'landsat.tif')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exporting all bands as one single image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = image.clip(roi).unmask()\n", "geemap.ee_export_image(image, filename=filename, scale=90, region=roi, file_per_band=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exporting each band as one image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.ee_export_image(image, filename=filename, scale=90, region=roi, file_per_band=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Export an image to Google Drive" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.ee_export_image_to_drive(image, description='landsat', folder='export', region=roi, scale=30)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Download an ee.ImageCollection" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import ee\n", "import geemap\n", "import os" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "loc = ee.Geometry.Point(-99.2222, 46.7816)\n", "collection = 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\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(collection.aggregate_array('system:index').getInfo())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.ee_export_image_collection(collection, out_dir=out_dir)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.ee_export_image_collection_to_drive(collection, folder='export', scale=10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Extract pixels as a Numpy array" ] }, { "cell_type": "code", "execution_count": null, "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') \\\n", " .select(['B4', 'B5', 'B6'])\n", "\n", "aoi = ee.Geometry.Polygon(\n", " [[[-110.8, 44.7],\n", " [-110.8, 44.6],\n", " [-110.6, 44.6],\n", " [-110.6, 44.7]]], None, False)\n", "\n", "rgb_img = geemap.ee_to_numpy(img, region=aoi)\n", "print(rgb_img.shape)" ] }, { "cell_type": "code", "execution_count": null, "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": { "hide_input": false, "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.8.5" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Table of Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": true }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }