Last active
February 20, 2021 11:05
-
-
Save jdbcode/b26285d795504143b2e3f4d18adbb917 to your computer and use it in GitHub Desktop.
geemap_timestamp_gif.ipynb
This file contains 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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "geemap_timestamp_gif.ipynb", | |
"provenance": [], | |
"authorship_tag": "ABX9TyM5hBFEAsmN79NQyH249V73", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/jdbcode/b26285d795504143b2e3f4d18adbb917/geemap_timestamp_gif.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "n1C-ULtpQW-4" | |
}, | |
"source": [ | |
"This notebook makes an animated GIF image from an Earth Engine time series image collection using the [geemap](https://geemap.org/) library. The frames are annotated with the image date according to text position and style you define.\r\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "TRffy89HQmFn" | |
}, | |
"source": [ | |
"Install geemap" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "R_nDfkVpJoDG" | |
}, | |
"source": [ | |
"!pip install geemap" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "2GdUVyFWQqGQ" | |
}, | |
"source": [ | |
"Set up Earth Engine/geemap" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "6nAjmS0pJyIN" | |
}, | |
"source": [ | |
"import ee\r\n", | |
"import geemap\r\n", | |
"ee.Authenticate()\r\n", | |
"ee.Initialize()" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "-BfmHYfoQu1v" | |
}, | |
"source": [ | |
"Develop the image collection\r\n", | |
"\r\n", | |
"Setting the date in the `visImg` function is important!\r\n", | |
"\r\n", | |
"`.set('date', img.date().format('YYYY-MM-dd', tz)))`" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "D01RppCLJ1Y2" | |
}, | |
"source": [ | |
"region = ee.Geometry.Polygon(\r\n", | |
" [[[-121.2840625, 50.14896674956192],\r\n", | |
" [-121.2840625, 24.727187209465008],\r\n", | |
" [-71.71375, 24.727187209465008],\r\n", | |
" [-71.71375, 50.14896674956192]]], None, False)\r\n", | |
"\r\n", | |
"states = ee.FeatureCollection('TIGER/2018/States')\r\n", | |
"states_outline = (ee.Image().byte()\r\n", | |
" .paint(**{'featureCollection': states, 'color': 1, 'width': 1})\r\n", | |
" .visualize(**{'palette': 'grey'}))\r\n", | |
"\r\n", | |
"vis_params = {\r\n", | |
" 'min': -30,\r\n", | |
" 'max': 30,\r\n", | |
" 'palette': ['b2182b', 'ef8a62', 'fddbc7', 'f7f7f7',\r\n", | |
" 'd1e5f0', '67a9cf', '2166ac'][::-1]\r\n", | |
"}\r\n", | |
"\r\n", | |
"tz = 'America/New_York'\r\n", | |
"dataset = (ee.ImageCollection('NOAA/NWS/RTMA')\r\n", | |
" .filterDate(ee.Date('2021-02-03', tz), ee.Date('2021-02-18', tz))\r\n", | |
" .select('TMP'))\r\n", | |
"\r\n", | |
"def visImg(img):\r\n", | |
" return (img.resample('bicubic')\r\n", | |
" .visualize(**vis_params)\r\n", | |
" .blend(states_outline)\r\n", | |
" .set('date', img.date().format('YYYY-MM-dd', tz)))\r\n", | |
"\r\n", | |
"dataset = (ee.ImageCollection.fromImages(\r\n", | |
" dataset.toList(dataset.size()).slice(0, dataset.size(), 2))\r\n", | |
" .map(visImg))\r\n", | |
"\r\n", | |
"dates = dataset.aggregate_array('date').getInfo()" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "IO-Csi87QLf4" | |
}, | |
"source": [ | |
"Get the image collection animation" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Tpd8PcrNKjP9" | |
}, | |
"source": [ | |
"video_args = {\r\n", | |
" 'dimensions': 475,\r\n", | |
" 'region': region,\r\n", | |
" 'crs': 'EPSG:5070',\r\n", | |
" 'framesPerSecond': 10,\r\n", | |
"}\r\n", | |
"\r\n", | |
"ee_gif = '/content/ts_animation.gif'\r\n", | |
"geemap.download_ee_video(dataset, video_args, ee_gif)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "ZGewf4OZQHox" | |
}, | |
"source": [ | |
"Preview the EE image collection animation" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "EUcnnE5sL-M_" | |
}, | |
"source": [ | |
"geemap.show_image(ee_gif)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "u3TInp50P-UG" | |
}, | |
"source": [ | |
"Get consolas font" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "UW5lIhgdOdpd" | |
}, | |
"source": [ | |
"import urllib.request\r\n", | |
"import os\r\n", | |
"font_url = 'https://github.com/tsenart/sight/raw/master/fonts/Consolas.ttf'\r\n", | |
"font_name = '/usr/share/fonts/TTF/consolas.ttf'\r\n", | |
"if not os.path.exists(os.path.dirname(font_name)):\r\n", | |
" os.makedirs(os.path.dirname(font_name))\r\n", | |
"urllib.request.urlretrieve(font_url, font_name)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "WOhSN5c2QATC" | |
}, | |
"source": [ | |
"Add timestamp to animation frames" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ln6-xesJMfCd" | |
}, | |
"source": [ | |
"texted_gif = '/content/ts_animation_timestamp.gif'\r\n", | |
"geemap.add_text_to_gif(ee_gif, texted_gif, xy=('3%', '78%'), text_sequence=dates,\r\n", | |
" font_type='consolas.ttf', font_size=26, font_color='#000',\r\n", | |
" add_progress_bar=True)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "VQ09hiHiQEQh" | |
}, | |
"source": [ | |
"Preview the animation with timestamps" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "-kAEHsXPM3v8" | |
}, | |
"source": [ | |
"geemap.show_image(texted_gif)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "A_hCGcTMRIX1" | |
}, | |
"source": [ | |
"Download the animation" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "V33kH1ihRKLY" | |
}, | |
"source": [ | |
"from google.colab import files\r\n", | |
"files.download(texted_gif) " | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "yVYlTQRGecwB" | |
}, | |
"source": [ | |
"Compress the GIF" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "pM5NZj88ees0" | |
}, | |
"source": [ | |
"texted_gif_compressed = '/content/ts_animation_timestamp_compress.gif'\r\n", | |
"os.system(f'rm {texted_gif_compressed}')\r\n", | |
"cmd = f'ffmpeg -i {texted_gif} {texted_gif_compressed}'\r\n", | |
"print(cmd)\r\n", | |
"os.system(cmd)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "34HkEo99eyba" | |
}, | |
"source": [ | |
"Download the compressed GIF" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "lFWf3p2EeyCK" | |
}, | |
"source": [ | |
"from google.colab import files\r\n", | |
"files.download(texted_gif_compressed)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment