{ "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/gee-community/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 gee python=3.9\n", "conda activate gee\n", "conda install mamba -c conda-forge\n", "mamba install cartopy scipy -c conda-forge\n", "mamba install geemap -c conda-forge\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import libraries" ] }, { "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\n", "import cartopy.io.img_tiles as cimgt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Initialize Earth Engine" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.ee_initialize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Add Earth Engine dataset" ] }, { "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": "markdown", "metadata": {}, "source": [ "## Use Google basemap" ] }, { "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, basemap='ROADMAP', zoom_level=8)\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=\"yellow\")\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Use Stamen Terrain basemap" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "basemap = cimgt.Stamen('terrain-background')" ] }, { "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, basemap=basemap, zoom_level=8)\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=\"yellow\")\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Use OpenStreetMap basemap" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "basemap = cimgt.OSM()" ] }, { "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, basemap=basemap, zoom_level=8)\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=\"yellow\")\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Use other basemaps\n", "\n", "For more basemaps, see https://scitools.org.uk/cartopy/docs/v0.19/cartopy/io/img_tiles.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Add text" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from matplotlib.transforms import offset_copy\n", "import cartopy.crs as ccrs" ] }, { "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, basemap='SATELLITE', zoom_level=8)\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=\"yellow\")\n", "\n", "plt.plot(\n", " -122.4457,\n", " 37.7574,\n", " marker='o',\n", " color='blue',\n", " markersize=10,\n", " alpha=0.7,\n", " transform=ccrs.Geodetic(),\n", ")\n", "\n", "# Use the cartopy interface to create a matplotlib transform object\n", "# for the Geodetic coordinate system. We will use this along with\n", "# matplotlib's offset_copy function to define a coordinate system which\n", "# translates the text by 25 pixels to the left.\n", "geodetic_transform = ccrs.Geodetic()._as_mpl_transform(ax)\n", "text_transform = offset_copy(geodetic_transform, units='dots', x=-25)\n", "\n", "plt.text(\n", " -122.4457,\n", " 37.7574,\n", " u'San Francisco',\n", " verticalalignment='center',\n", " horizontalalignment='right',\n", " transform=text_transform,\n", " fontsize='large',\n", " fontweight='bold',\n", " color='white',\n", " bbox=dict(facecolor='sandybrown', alpha=0.5, boxstyle='round'),\n", ")\n", "\n", "# Save the plot by calling plt.savefig() BEFORE plt.show()\n", "plt.savefig('sfo.pdf')\n", "plt.savefig('sfo.jpg', dpi=300)\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](https://i.imgur.com/Yw2N42R.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Global-scale maps" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# get an earth engine image of ocean data for Jan-Mar 2018\n", "ocean = (\n", " ee.ImageCollection('NASA/OCEANDATA/MODIS-Terra/L3SMI')\n", " .filter(ee.Filter.date('2018-01-01', '2018-03-01'))\n", " .median()\n", " .select([\"sst\"], [\"SST\"])\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# set parameters for plotting\n", "# will plot the Sea Surface Temp with specific range and colormap\n", "visualization = {'bands': \"SST\", 'min': -2, 'max': 30}\n", "# specify region to focus on\n", "bbox = [180, -88, -180, 88]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = plt.figure(figsize=(15, 10))\n", "\n", "# plot the result with cartoee using a PlateCarre projection (default)\n", "ax = cartoee.get_map(\n", " ocean,\n", " cmap='plasma',\n", " vis_params=visualization,\n", " region=bbox,\n", " basemap=cimgt.Stamen('terrain'),\n", " zoom_level=2,\n", ")\n", "cb = cartoee.add_colorbar(ax, vis_params=visualization, loc='right', cmap='plasma')\n", "\n", "ax.set_title(label='Sea Surface Temperature', fontsize=15)\n", "\n", "ax.coastlines()\n", "\n", "# Save the plot by calling plt.savefig() BEFORE plt.show()\n", "plt.savefig('sst.pdf')\n", "plt.savefig('sst.jpg', dpi=300)\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](https://i.imgur.com/CYNUy6R.png)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }