{ "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": "markdown", "metadata": {}, "source": [ "## `cartoee` quickstart\n", "\n", "`cartoee` is a lightweight module to aid in creatig publication quality maps from Earth Engine processing results without having to download data. The `cartoee` package does this by requesting png images from EE results (which are usually good enough for visualization) and `cartopy` is used to create the plots. Utility functions are available to create plot aethetics such as gridlines or color bars." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%pylab inline\n", "\n", "import ee\n", "import geemap\n", "\n", "# import the cartoee functionality from geemap\n", "from geemap import cartoee" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting an image\n", "\n", "In this first example we will explore the most basic functionality including plotting and image, adding a colorbar, and adding visual aethetic features. Here we will use SRTM data to plot global elevation." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# get an image\n", "srtm = ee.Image(\"CGIAR/SRTM90_V4\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "region = [-180,-60,180,85] # define bounding box to request data\n", "vis = {'min':0,'max':3000} # define visualization parameters for image" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = plt.figure(figsize=(10,7))\n", "\n", "# use cartoee to get a map\n", "ax = cartoee.get_map(srtm,region=region,vis_params=vis)\n", "\n", "# add a colorbar to the map using the visualization params we passed to the map\n", "cartoee.add_colorbar(ax, vis, loc=\"bottom\",label=\"Elevation\",orientation=\"horizontal\")\n", "\n", "# add gridlines to the map at a specified interval\n", "cartoee.add_gridlines(ax,interval=[60,30],linestyle=\":\")\n", "\n", "# add coastlines using the cartopy api\n", "ax.coastlines(color=\"red\")\n", "\n", "show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a decent map for minimal amount of code. But we can also easily use matplotlib colormaps to visualize our EE results to add more color. Here we add a `cmap` keyword to the `.get_map()` and `.add_colorbar()` functions." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = plt.figure(figsize=(10,7))\n", "\n", "cmap = \"gist_earth\" # colormap we want to use\n", "\n", "# use cartoee to get a map\n", "ax = cartoee.get_map(srtm,region=region,vis_params=vis,cmap= cmap)\n", "\n", "# add a colorbar to the map using the visualization params we passed to the map\n", "cartoee.add_colorbar(ax, vis, cmap=cmap, loc=\"bottom\",label=\"Elevation\",orientation=\"horizontal\")\n", "\n", "# add gridlines to the map at a specified interval\n", "cartoee.add_gridlines(ax,interval=[60,30],linestyle=\":\")\n", "\n", "# add coastlines using the cartopy api\n", "ax.coastlines(color=\"red\")\n", "\n", "show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotting an RGB image\n", "\n", "`cartoee` also allows for plotting of RGB image results directly. Here is an example of plotting a Landsat false-color scene." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# get a landsat image to visualize\n", "image = ee.Image('LANDSAT/LC08/C01/T1_SR/LC08_044034_20140318')\n", "\n", "# define the visualization parameters to view\n", "vis ={\"bands\": ['B5', 'B4', 'B3'], \"min\": 0, \"max\":5000,\"gamma\":1.3}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# use cartoee to get a map\n", "ax = cartoee.get_map(image,vis_params=vis)\n", "\n", "# pad the view for some visual appeal\n", "cartoee.pad_view(ax)\n", "\n", "# add the gridlines and specify that the xtick labels be rotated 45 degrees\n", "cartoee.add_gridlines(ax,interval=0.5,xtick_rotation=45,linestyle=\":\")\n", "\n", "# add the coastline\n", "ax.coastlines(color=\"yellow\")\n", "\n", "show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By default, if a region is not provided via the `region` keyword the whole extent of the image will be plotted as seen in the previous Landsat example. We can also zoom to a specific region of an image by defining the region to plot." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# here is the bounding box of the map extent we want to use\n", "# formatted a [W,S,E,N]\n", "zoom_region = [-122.6265,37.3458,-121.8025, 37.9178]\n", "\n", "# plot the map over the region of interest\n", "ax = cartoee.get_map(image,vis_params=vis,region=zoom_region)\n", "\n", "# add the gridlines and specify that the xtick labels be rotated 45 degrees\n", "cartoee.add_gridlines(ax,interval=0.15,xtick_rotation=45,linestyle=\":\")\n", "\n", "# add coastline\n", "ax.coastlines(color=\"yellow\")\n", "\n", "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 }