Created
January 4, 2021 02:36
-
-
Save giswqs/a56dec6e48d3f8b1de0f96aa141db042 to your computer and use it in GitHub Desktop.
cartoee gif
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "<a href=\"https://githubtocolab.com/giswqs/geemap/blob/master/examples/notebooks/52_cartoee_gif.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n\nUncomment 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\nSee below the commands to install cartopy and geemap using conda/mamba:\n\n```\nconda create -n carto python=3.8\nconda activate carto\nconda install mamba -c conda-forge\nmamba install cartopy scipy -c conda-forge\nmamba install geemap -c conda-forge\njupyter notebook\n```" | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "# How to create timelapse animations using cartoee" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "import os\nimport ee\nimport geemap\nfrom geemap import cartoee\n%pylab inline", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# geemap.update_package()", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## Create an interactive map" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "Map = geemap.Map()\nMap", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## Create an ImageCollection" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "lon = -74.51\nlat = -8.37\nstart_year = 1984\nend_year = 2011\n\npoint = ee.Geometry.Point(lon, lat)\nyears = ee.List.sequence(start_year, end_year)\n\ndef 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\ncollection = ee.ImageCollection(years.map(get_best_image))", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## Display a sample image" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "vis_params = {\n \"bands\": ['B5', 'B4', 'B3'],\n \"min\": 0,\n \"max\": 5000\n}\n\nimage = ee.Image(collection.first())\nMap.addLayer(image, vis_params, 'First image')\nMap.setCenter(lon, lat, 8)\nMap", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## Get a sample output image" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "w = 0.2\nh = 0.15\n\nregion = [lon-w, lat-h, lon+w, lat+h]\n\nfig = plt.figure(figsize=(10, 8))\n\n# use cartoee to get a map\nax = cartoee.get_map(image, region=region, vis_params=vis_params)\n\n# add gridlines to the map at a specified interval\ncartoee.add_gridlines(ax, interval=[0.1, 0.1], linestyle=\":\")\n\n# add north arrow\nnorth_arrow_dict = {\n \"text\": \"N\",\n \"xy\": (0.1, 0.3),\n \"arrow_length\": 0.15,\n \"text_color\": \"red\",\n \"arrow_color\": \"white\",\n \"fontsize\": 20,\n \"width\": 5,\n \"headwidth\": 15,\n \"ha\": \"center\",\n \"va\": \"center\"\n}\ncartoee.add_north_arrow(ax, **north_arrow_dict)\n\n# add scale bar\nscale_bar_dict = {\n \"length\": 5, \n \"xy\": (0.1, 0.05), \n \"linewidth\": 3,\n \"fontsize\": 20,\n \"color\": \"red\",\n \"unit\": \"km\",\n \"ha\": \"center\",\n \"va\": \"bottom\" \n}\ncartoee.add_scale_bar(ax, **scale_bar_dict)\n\nax.set_title(label = 'Ucayali River, Peru', fontsize=15)\n\nshow()", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "## Create timelapse animations" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"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.1, 0.1),\n plot_title = \"Ucayali River, Peru\",\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)", | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"hide_input": false, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3", | |
"language": "python" | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.8.5", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"toc": { | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"base_numbering": 1, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": false | |
}, | |
"gist": { | |
"id": "", | |
"data": { | |
"description": "cartoee gif", | |
"public": true | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment