{ "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": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# !pip install cartopy scipy\n", "# !pip install geemap" ] }, { "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": [ "geemap.ee_initialize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot an RGB image" ] }, { "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": [ "fig = plt.figure(figsize=(15, 10))\n", "\n", "# 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=0,linestyle=\":\")\n", "\n", "# add the coastline\n", "ax.coastlines(color=\"cyan\")\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": [ "fig = plt.figure(figsize=(15, 10))\n", "\n", "# 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=0, linestyle=\":\")\n", "\n", "# add coastline\n", "ax.coastlines(color=\"cyan\")\n", "\n", "show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Adding north arrow, scale bar, and legend" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "fig = plt.figure(figsize=(15, 10))\n", "\n", "# 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=0, linestyle=\":\")\n", "\n", "# add coastline\n", "ax.coastlines(color=\"cyan\")\n", "\n", "# add north arrow\n", "cartoee.add_north_arrow(ax, text=\"N\", xy=(0.05, 0.25), text_color=\"white\", arrow_color=\"white\", fontsize=20)\n", "\n", "# add scale bar\n", "cartoee.add_scale_bar_lite(ax, length=10, xy=(0.1, 0.05), fontsize=20, color=\"white\", unit=\"km\")\n", "\n", "ax.set_title(label = 'Landsat False Color Composite (Band 5/4/3)', fontsize=15)\n", "\n", "# add legend\n", "legend_elements = [Line2D([], [], color='#00ffff', lw=2, label='Coastline'),\n", " Line2D([], [], marker='o', color='#A8321D', label='City', markerfacecolor='#A8321D', markersize=10, ls ='')]\n", "\n", "cartoee.add_legend(ax, legend_elements, loc='lower right')\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 }