{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
Creating maps with Cartopy
\n", "\n", "\n", "> *DS Python for GIS and Geoscience* \n", "> *October, 2020*\n", ">\n", "> *© 2020, Joris Van den Bossche and Stijn Van Hoey. Licensed under [CC BY-SA 4.0 Creative Commons](https://creativecommons.org/licenses/by-sa/4.0/)* Adapted from material from Phil Elson and Ryan Abernathey (see below).\n", "\n", "---\n", "\n", "# Maps in Scientific Python\n", "\n", "Making maps is a fundamental part of GIS and geoscience research.\n", "Maps differ from regular figures in the following principle ways:\n", "\n", "- Maps require a *projection* of geographic coordinates on the 3D Earth to the 2D space of your figure.\n", "- Maps often include extra decorations besides just our data (e.g. continents, country borders, etc.)\n", "\n", "The maps we have made up to now, for example using the GeoPandas `plot()` method, assume the data can be plotted as is on the figure. This works fine if your data is already in projected coordinates, and has a limited extent (small study area). When mapping data of a larger area of the full globe, properly taking into account the projection becomes much more important!\n", "\n", "In this notebook, we will learn about [Cartopy](https://scitools.org.uk/cartopy/docs/latest/), one of the most common packages for making maps within python.\n", "\n", "### Credit: Phil Elson and Ryan Abernathey\n", "\n", "Lots of the material in this notebook was adopted from https://earth-env-data-science.github.io/intro.html by Ryan Abernathey, which itself was adopted from [Phil Elson](https://pelson.github.io/)'s excellent [Cartopy Tutorial](https://github.com/SciTools/cartopy-tutorial). Phil is the creator of Cartopy and published his tutorial under an [open license](http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/), meaning that we can copy, adapt, and redistribute it as long as we give proper attribution. **THANKS PHIL AND RYAN!** 👏👏👏" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Background: Projections" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Most of our media for visualization *are* flat\n", "\n", "Our two most common media are flat:\n", "\n", " * Paper\n", " * Screen\n", "\n", "![](https://raw.githubusercontent.com/SciTools/cartopy-tutorial/master/static/flat_medium.jpg)\n", "\n", "### [Map] Projections: Taking us from spherical to flat\n", "\n", "A map projection (or more commonly refered to as just \"projection\") is:\n", "\n", "> a systematic transformation of the latitudes and longitudes of locations from the surface of a sphere or an ellipsoid into locations on a plane. [[Wikipedia: Map projection](https://en.wikipedia.org/wiki/Map_projection)].\n", "\n", "### The major problem with map projections\n", "\n", "![orange peel](https://raw.githubusercontent.com/SciTools/cartopy-tutorial/master/static/orange_peel.jpg)\n", "\n", " * The surface of a sphere is topologically different to a 2D surface, therefore we *have* to cut the sphere *somewhere*\n", " * A sphere's surface cannot be represented on a plane without distortion.\n", " \n", "There are many different ways to make a projection, and we will not attempt to explain all of the choices and tradeoffs here. Instead, you can read Phil's [original tutorial](https://github.com/SciTools/cartopy-tutorial/blob/master/tutorial/projections_crs_and_terms.ipynb) for a great overview of this topic.\n", "Instead, we will dive into the more practical sides of Caropy usage." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introducing Cartopy\n", "\n", "https://scitools.org.uk/cartopy/docs/latest/\n", "\n", "Cartopy makes use of the powerful [PROJ.4](https://proj4.org/), numpy and shapely libraries and includes a programatic interface built on top of Matplotlib for the creation of publication quality maps.\n", "\n", "Key features of cartopy are its object oriented projection definitions, and its ability to transform points, lines, vectors, polygons and images between those projections.\n", "\n", "### Cartopy Projections and other reference systems\n", "\n", "In Cartopy, each projection is a class.\n", "Most classes of projection can be configured in projection-specific ways, although Cartopy takes an opinionated stance on sensible defaults.\n", "\n", "Let's create a Plate Carree projection instance.\n", "\n", "To do so, we need cartopy's crs module. This is typically imported as ``ccrs`` (Cartopy Coordinate Reference Systems)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import cartopy.crs as ccrs\n", "import cartopy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Cartopy's projection list tells us that the Plate Carree projection is available with the ``ccrs.PlateCarree`` class: https://scitools.org.uk/cartopy/docs/latest/crs/projections.html\n", "\n", "**Note:** we need to *instantiate* the class in order to do anything projection-y with it!" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "<cartopy.crs.PlateCarree object at 0x7fb34d267860>" ], "text/plain": [ "