{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Contextily 1: Intro" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Overview:\n", "1. Read in the NYS Mesonet sites table with GeoPandas\n", "1. Plot the NYS Mesonet sites on a map\n", "1. Use the [contextily](https://contextily.readthedocs.io/en/latest/) package to overlay an image as a basemap, served via a remote tile server" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prerequisites\n", "\n", "| Concepts | Importance | Notes |\n", "| --- | --- | --- |\n", "| (Geo)Pandas| Necessary | |\n", "| Cartopy| Necessary | |\n", "\n", "* **Time to learn**: 10 minutes\n", "***" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import cartopy.crs as ccrs\n", "import cartopy.feature as cfeature\n", "import geopandas as gp\n", "import pandas as pd\n", "from metpy.plots import USCOUNTIES\n", "import contextily as ctx\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Read in the NYS Mesonet sites table with Geopandas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, open the NYSM sites table using Pandas, as we've done before." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nysm_sites = pd.read_csv('/spare11/atm533/data/nysm_sites.csv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nysm_sites.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the Shapefiles Intro notebook, we used Geopandas to read in a shapefile as a Dataframe. Geopandas is not limited to shapefiles; it can also georeference existing Pandas dataframes that contain relevant columns or rows. The NYSM dataframe has latitude and longitude columns; we will pass these in to the `GeoDataFrame` method which creates the geo-referenced dataframe." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
GeoDataFrame
requires a column named geometry. Besides polygons and multipolygons, another valid geometry is Points. We thus create a Geometry series using Geopandas' points_from_xy
method. This method accepts two series... in this case, the longitude and latitude columns from the NYSM sites dataframe.\n",
"