{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\"Open\n", "\n", "Uncomment the following line to install [geemap](https://geemap.org) and [cartopy](https://scitools.org.uk/cartopy/docs/latest/installing.html#installing) if needed. Keep in mind that cartopy can be challenging to install. If you are unable to install cartopy on your computer, you can try Google Colab with this the [notebook example](https://colab.research.google.com/github/giswqs/geemap/blob/master/examples/notebooks/cartoee_colab.ipynb). \n", "\n", "See below the commands to install cartopy and geemap using conda/mamba:\n", "\n", "```\n", "conda create -n carto python=3.8\n", "conda activate carto\n", "conda install mamba -c conda-forge\n", "mamba install cartopy scipy -c conda-forge\n", "mamba install geemap -c conda-forge\n", "jupyter notebook\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# How to create timelapse animations using cartoee" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import ee\n", "import geemap\n", "from geemap import cartoee\n", "%pylab inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# geemap.update_package()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create an interactive map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create an ImageCollection" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lon = -115.1585\n", "lat = 36.1500\n", "start_year = 1984\n", "end_year = 2011\n", "\n", "point = ee.Geometry.Point(lon, lat)\n", "years = ee.List.sequence(start_year, end_year)\n", "\n", "def get_best_image(year): \n", "\n", " start_date = ee.Date.fromYMD(year, 1, 1)\n", " end_date = ee.Date.fromYMD(year, 12, 31)\n", " image = ee.ImageCollection(\"LANDSAT/LT05/C01/T1_SR\") \\\n", " .filterBounds(point) \\\n", " .filterDate(start_date, end_date) \\\n", " .sort(\"CLOUD_COVER\") \\\n", " .first() \n", " return ee.Image(image)\n", "\n", "collection = ee.ImageCollection(years.map(get_best_image))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Display a sample image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vis_params = {\n", " \"bands\": ['B4', 'B3', 'B2'],\n", " \"min\": 0,\n", " \"max\": 5000\n", "}\n", "\n", "image = ee.Image(collection.first())\n", "Map.addLayer(image, vis_params, 'First image')\n", "Map.setCenter(lon, lat, 8)\n", "Map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get a sample output image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "w = 0.4\n", "h = 0.3\n", "\n", "region = [lon-w, lat-h, lon+w, lat+h]\n", "\n", "fig = plt.figure(figsize=(10, 8))\n", "\n", "# use cartoee to get a map\n", "ax = cartoee.get_map(image, region=region, vis_params=vis_params)\n", "\n", "# add gridlines to the map at a specified interval\n", "cartoee.add_gridlines(ax, interval=[0.2, 0.2], linestyle=\":\")\n", "\n", "# add north arrow\n", "north_arrow_dict = {\n", " \"text\": \"N\",\n", " \"xy\": (0.1, 0.3),\n", " \"arrow_length\": 0.15,\n", " \"text_color\": \"white\",\n", " \"arrow_color\": \"white\",\n", " \"fontsize\": 20,\n", " \"width\": 5,\n", " \"headwidth\": 15,\n", " \"ha\": \"center\",\n", " \"va\": \"center\"\n", "}\n", "cartoee.add_north_arrow(ax, **north_arrow_dict)\n", "\n", "# add scale bar\n", "scale_bar_dict = {\n", " \"length\": 10, \n", " \"xy\": (0.1, 0.05), \n", " \"linewidth\": 3,\n", " \"fontsize\": 20,\n", " \"color\": \"white\",\n", " \"unit\": \"km\",\n", " \"ha\": \"center\",\n", " \"va\": \"bottom\" \n", "}\n", "cartoee.add_scale_bar_lite(ax, **scale_bar_dict)\n", "\n", "ax.set_title(label = 'Las Vegas, NV', fontsize=15)\n", "\n", "show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create timelapse animations" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cartoee.get_image_collection_gif(\n", " ee_ic = collection,\n", " out_dir = os.path.expanduser(\"~/Downloads/timelapse\"),\n", " out_gif = \"animation.gif\",\n", " vis_params = vis_params,\n", " region = region,\n", " fps = 5,\n", " mp4 = True,\n", " grid_interval = (0.2, 0.2),\n", " plot_title = \"Las Vegas, NV\",\n", " date_format = 'YYYY-MM-dd',\n", " fig_size = (10, 8),\n", " dpi_plot = 100,\n", " file_format = \"png\",\n", " north_arrow_dict = north_arrow_dict,\n", " scale_bar_dict = scale_bar_dict,\n", " verbose = True\n", ")" ] } ], "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": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "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 }