OSGeoLive-Notebooks/Cartopy/cartopy-shapely-katrina.ipynb

148 lines
4.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.patches as mpatches\n",
"import matplotlib.pyplot as plt\n",
"import shapely.geometry as sgeom\n",
"\n",
"import cartopy.crs as ccrs\n",
"import cartopy.io.shapereader as shpreader\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def sample_data():\n",
" \"\"\"\n",
" Return a list of latitudes and a list of longitudes (lons, lats)\n",
" for Hurricane Katrina (2005).\n",
"\n",
" The data was originally sourced from the HURDAT2 dataset from AOML/NOAA:\n",
" http://www.aoml.noaa.gov/hrd/hurdat/newhurdat-all.html on 14th Dec 2012.\n",
"\n",
" \"\"\"\n",
" lons = [-75.1, -75.7, -76.2, -76.5, -76.9, -77.7, -78.4, -79.0,\n",
" -79.6, -80.1, -80.3, -81.3, -82.0, -82.6, -83.3, -84.0,\n",
" -84.7, -85.3, -85.9, -86.7, -87.7, -88.6, -89.2, -89.6,\n",
" -89.6, -89.6, -89.6, -89.6, -89.1, -88.6, -88.0, -87.0,\n",
" -85.3, -82.9]\n",
"\n",
" lats = [23.1, 23.4, 23.8, 24.5, 25.4, 26.0, 26.1, 26.2, 26.2, 26.0,\n",
" 25.9, 25.4, 25.1, 24.9, 24.6, 24.4, 24.4, 24.5, 24.8, 25.2,\n",
" 25.7, 26.3, 27.2, 28.2, 29.3, 29.5, 30.2, 31.1, 32.6, 34.1,\n",
" 35.6, 37.0, 38.6, 40.1]\n",
"\n",
" return lons, lats\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"def main():\n",
" fig = plt.figure()\n",
" ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.LambertConformal())\n",
"\n",
" ax.set_extent([-125, -66.5, 20, 50], ccrs.Geodetic())\n",
"\n",
" shapename = 'admin_1_states_provinces_lakes'\n",
" states_shp = shpreader.natural_earth(resolution='110m',\n",
" category='cultural', name=shapename)\n",
"\n",
" lons, lats = sample_data()\n",
"\n",
" # to get the effect of having just the states without a map \"background\"\n",
" # turn off the outline and background patches\n",
" ax.background_patch.set_visible(False)\n",
" ax.outline_patch.set_visible(False)\n",
"\n",
" ax.set_title('US States which intersect the track of '\n",
" 'Hurricane Katrina (2005)')\n",
"\n",
" # turn the lons and lats into a shapely LineString\n",
" track = sgeom.LineString(zip(lons, lats))\n",
"\n",
" # buffer the linestring by two degrees (note: this is a non-physical\n",
" # distance)\n",
" track_buffer = track.buffer(2)\n",
"\n",
" def colorize_state(geometry):\n",
" facecolor = (0.9375, 0.9375, 0.859375)\n",
" if geometry.intersects(track):\n",
" facecolor = 'red'\n",
" elif geometry.intersects(track_buffer):\n",
" facecolor = '#FF7E00'\n",
" return {'facecolor': facecolor, 'edgecolor': 'black'}\n",
"\n",
" ax.add_geometries(\n",
" shpreader.Reader(states_shp).geometries(),\n",
" ccrs.PlateCarree(),\n",
" styler=colorize_state)\n",
"\n",
" ax.add_geometries([track_buffer], ccrs.PlateCarree(),\n",
" facecolor='#C8A2C8', alpha=0.5)\n",
" ax.add_geometries([track], ccrs.PlateCarree(),\n",
" facecolor='none', edgecolor='k')\n",
"\n",
" # make two proxy artists to add to a legend\n",
" direct_hit = mpatches.Rectangle((0, 0), 1, 1, facecolor=\"red\")\n",
" within_2_deg = mpatches.Rectangle((0, 0), 1, 1, facecolor=\"#FF7E00\")\n",
" labels = ['State directly intersects\\nwith track',\n",
" 'State is within \\n2 degrees of track']\n",
" ax.legend([direct_hit, within_2_deg], labels,\n",
" loc='lower left', bbox_to_anchor=(0.025, -0.1), fancybox=True)\n",
"\n",
" plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"main()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"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"
}
},
"nbformat": 4,
"nbformat_minor": 2
}