{ "cells": [ { "cell_type": "markdown", "id": "0", "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 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", "pip install OWSLib\n", "jupyter notebook\n", "```" ] }, { "cell_type": "markdown", "id": "1", "metadata": {}, "source": [ "## References\n", "\n", "- https://github.com/gee-community/geemap/discussions/238\n", "- https://www.net-analysis.com/blog/cartopylayout.html" ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "# all imports should go here\n", "import pandas as pd\n", "import sys\n", "import os\n", "import subprocess\n", "import datetime\n", "import platform\n", "import datetime\n", "import math\n", "\n", "import matplotlib.pyplot as plt\n", "\n", "# import seaborn as sb\n", "\n", "from geemap import cartoee\n", "\n", "import cartopy\n", "import cartopy.crs as ccrs\n", "from cartopy.io.img_tiles import OSM\n", "import cartopy.feature as cfeature\n", "from cartopy.io import shapereader\n", "from cartopy.io.img_tiles import StamenTerrain\n", "from cartopy.io.img_tiles import GoogleTiles\n", "from owslib.wmts import WebMapTileService\n", "\n", "from matplotlib.path import Path\n", "import matplotlib.patheffects as PathEffects\n", "from matplotlib import patheffects\n", "import matplotlib.patches as mpatches\n", "import matplotlib.lines as mlines\n", "\n", "import numpy as np\n", "\n", "import ee\n", "import geemap\n", "\n", "geemap.ee_initialize()\n", "\n", "# get an image\n", "srtm = ee.Image(\"CGIAR/SRTM90_V4\")\n", "# region = [-180, -60, 180, 85] # define bounding box to request data\n", "region = [110, -45, 160, -10]\n", "vis = {\"min\": 0, \"max\": 3000} # define visualization parameters for image\n", "\n", "\n", "fig = plt.figure(figsize=(10, 7))\n", "\n", "# ------------------------------- Surrounding frame ------------------------------\n", "# set up frame full height, full width of figure, this must be called first\n", "\n", "left = -0.05\n", "bottom = -0.05\n", "width = 1.1\n", "height = 1.05\n", "rect = [left, bottom, width, height]\n", "ax3 = plt.axes(rect)\n", "\n", "\n", "# turn on the spines we want, ie just the surrounding frame\n", "plt.axis(\"off\")\n", "\n", "\n", "ax3.text(\n", " 0.01,\n", " 0.01,\n", " \"© Don Cameron, 2017: net-analysis.com. \"\n", " + \"Map generated at \"\n", " + datetime.datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\"),\n", " fontsize=8,\n", ")\n", "\n", "\n", "# --------------------------------- Main Map -------------------------------------\n", "#\n", "# set up main map almost full height (allow room for title), right 80% of figure\n", "BORDERS2_10m = cartopy.feature.NaturalEarthFeature(\n", " \"cultural\", \"admin_1_states_provinces\", \"10m\", edgecolor=\"black\", facecolor=\"none\"\n", ")\n", "\n", "LAND_10m = cartopy.feature.NaturalEarthFeature(\n", " \"physical\",\n", " \"land\",\n", " \"10m\",\n", " edgecolor=\"face\",\n", " facecolor=cartopy.feature.COLORS[\"land\"],\n", ")\n", "\n", "RIVERS_10m = cartopy.feature.NaturalEarthFeature(\n", " \"physical\",\n", " \"rivers_lake_centerlines\",\n", " \"10m\",\n", " edgecolor=cartopy.feature.COLORS[\"water\"],\n", " facecolor=\"none\",\n", ")\n", "\n", "left = 0.2\n", "bottom = 0\n", "width = 0.8\n", "height = 0.90\n", "rect = [left, bottom, width, height]\n", "\n", "ax = plt.axes(rect, projection=ccrs.PlateCarree())\n", "# ax.set_extent((150, 155, -30, -23))\n", "# region = [110, -45, 160, -10]\n", "ax.set_extent((110, 160, -45, -10))\n", "\n", "cartoee.add_layer(ax, srtm, region=region, vis_params=vis, cmap=\"terrain\")\n", "cartoee.add_colorbar(\n", " ax, vis, cmap=\"terrain\", loc=\"bottom\", label=\"Elevation\", orientation=\"horizontal\"\n", ")\n", "\n", "ax.coastlines(resolution=\"10m\", zorder=2)\n", "\n", "# land polygons, including major islands, use cartopy default color\n", "# ax.add_feature(LAND_10m)\n", "# ax.add_feature(RIVERS_10m)\n", "# ax.add_feature(BORDERS2_10m, edgecolor='grey')\n", "\n", "# ax.stock_img()\n", "# stock image is good enough for example, but OCEAN_10m could be used, but very slow\n", "# ax.add_feature(OCEAN_10m)\n", "\n", "# ax.gridlines(draw_labels=True, xlocs=[150, 152, 154, 155])\n", "ax.gridlines(draw_labels=True, xlocs=[120, 130, 140, 150])\n", "\n", "\n", "cartoee.add_scale_bar(ax, length=500, xy=(0.5, 0.08), fontsize=16)\n", "cartoee.add_north_arrow(ax, xy=(0.9, 0.95))\n", "\n", "\n", "# ---------------------------------Locating Map ------------------------\n", "#\n", "# set up index map 20% height, left 16% of figure\n", "left = 0\n", "bottom = 0\n", "width = 0.16\n", "height = 0.2\n", "rect = [left, bottom, width, height]\n", "\n", "\n", "ax2 = plt.axes(rect, projection=ccrs.PlateCarree())\n", "ax2.set_extent((-179, 180, -80, 80))\n", "# ax2.set_global() will show the whole world as context\n", "\n", "ax2.coastlines(resolution=\"110m\", zorder=2)\n", "ax2.add_feature(cfeature.LAND)\n", "ax2.add_feature(cfeature.OCEAN)\n", "\n", "ax2.gridlines()\n", "\n", "\n", "lon0, lon1, lat0, lat1 = ax.get_extent()\n", "box_x = [lon0, lon1, lon1, lon0, lon0]\n", "box_y = [lat0, lat0, lat1, lat1, lat0]\n", "\n", "plt.plot(box_x, box_y, color=\"red\", transform=ccrs.Geodetic())\n", "\n", "\n", "# -------------------------------- Title -----------------------------\n", "# set up map title top 4% of figure, right 80% of figure\n", "\n", "left = 0.2\n", "bottom = 0.95\n", "width = 0.8\n", "height = 0.04\n", "rect = [left, bottom, width, height]\n", "ax6 = plt.axes(rect)\n", "ax6.text(0.5, 0.0, \"Multi-Axes Map Example\", ha=\"center\", fontsize=20)\n", "plt.axis(\"off\")\n", "\n", "\n", "# ------------------------------------ Legend -------------------------------------\n", "\n", "# legends can be quite long, so set near top of map (0.4 - bottom + 0.5 height = 0.9 - near top)\n", "left = 0\n", "bottom = 0.4\n", "width = 0.16\n", "height = 0.5\n", "rect = [left, bottom, width, height]\n", "rect = [left, bottom, width, height]\n", "ax5 = plt.axes(rect)\n", "\n", "# create an array of color patches and associated names for drawing in a legend\n", "# colors are the predefined colors for cartopy features (only for example, Cartopy names are unusual)\n", "colors = sorted(cartopy.feature.COLORS.keys())\n", "\n", "# handles is a list of patch handles\n", "handles = []\n", "# names is the list of corresponding labels to appear in the legend\n", "names = []\n", "\n", "# for each cartopy defined color, draw a patch, append handle to list, and append color name to names list\n", "for c in colors:\n", " patch = mpatches.Patch(color=cfeature.COLORS[c], label=c)\n", " handles.append(patch)\n", " names.append(c)\n", "# end for\n", "\n", "# do some example lines with colors\n", "river = mlines.Line2D(\n", " [], [], color=cfeature.COLORS[\"water\"], marker=\"\", markersize=15, label=\"river\"\n", ")\n", "coast = mlines.Line2D([], [], color=\"black\", marker=\"\", markersize=15, label=\"coast\")\n", "bdy = mlines.Line2D(\n", " [], [], color=\"grey\", marker=\"\", markersize=15, label=\"state boundary\"\n", ")\n", "handles.append(river)\n", "handles.append(coast)\n", "handles.append(bdy)\n", "names.append(\"river\")\n", "names.append(\"coast\")\n", "names.append(\"state boundary\")\n", "\n", "\n", "# create legend\n", "ax5.legend(handles, names)\n", "ax5.set_title(\"Legend\", loc=\"left\")\n", "plt.axis(\"off\")\n", "\n", "plt.savefig(\"test.png\", bbox_inches=\"tight\")\n", "\n", "plt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }