{ "metadata": { "name": "", "signature": "sha256:63a2e4841c4c2ad2a646eff3756c6efe130bfee7938b42f93a2658e7af14e083" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## State Maps of Earthquakes by Decade\n", "\n", "- All of the data for these maps comes from USGS. I caculated what state the earthquakes were in using PostGIS.\n", "- The state shapes come from the U.S. Census.\n", "- The code below generates two maps for each state, one \"before\" (1995-2004) and one \"after\" (2005-2014).\n", "- The maps are generated in an SVG format so they're easy to hand over to the BuzzFeed design team.\n", "- The earthquakes are not sized by magnitude." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import fiona\n", "import fiona.crs\n", "import pyproj\n", "import svgwrite\n", "from IPython.display import SVG\n", "import shapely.geometry\n", "import pandas as pd" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "def draw_state_earthquakes(state, start, end):\n", " with fiona.drivers():\n", " with fiona.open(\"../data/stateshapes/{0}/\".format(state.lower())) as source:\n", " drawing = svgwrite.Drawing()\n", " bbox = source.bounds\n", " crs = source.crs\n", " projection = pyproj.Proj(fiona.crs.to_string(crs))\n", " stroke_width = 0.0075\n", " padding = 0.05\n", " width = padding + bbox[2] - bbox[0]\n", " height = padding + bbox[3] - bbox[1]\n", " drawing.viewbox(bbox[0]-padding, bbox[1]-padding, width, height)\n", " main_group = drawing.g()\n", " main_group.translate(0, bbox[1] + bbox[3])\n", " main_group.scale(1, -1)\n", " state_group = drawing.g(id=\"state\")\n", " quake_group = drawing.g(id=\"quakes\")\n", " drawing.add(main_group)\n", " main_group.add(state_group)\n", " main_group.add(quake_group)\n", " for feat in source:\n", " shape = shapely.geometry.shape(feat[\"geometry\"])\n", " alpha = 1\n", " red = \"#ee3322\"\n", " if shape.type == \"Polygon\":\n", " state_group.add(drawing.polygon(points=shape.exterior.coords, \n", " fill=\"beige\", stroke=\"black\", stroke_width=stroke_width, opacity=alpha))\n", " else:\n", " raise ValueError(\"Don't recognize geometry type\")\n", " all_quakes = pd.DataFrame.from_csv(\"../data/earthquake_states.csv\", index_col=None, parse_dates=[\"time\", \"updated\"])\n", " us_quakes = all_quakes.dropna(subset=[\"state\"])\n", " state_last_decade = us_quakes[(us_quakes[\"state\"] == state) &\\\n", " (us_quakes[\"time\"] < end) &\\\n", " (us_quakes[\"time\"] >= start)]\n", " for i, row in state_last_decade.iterrows():\n", " quake_group.add(drawing.circle(center=(row[\"longitude\"], row[\"latitude\"]), r=width / 200, \n", " fill=red, opacity=0.5, stroke=\"#333\", stroke_width=stroke_width))\n", " return drawing" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "ok_after = draw_state_earthquakes(\"Oklahoma\", \"2005-1-1\", \"2015-1-1\").tostring()\n", "SVG(ok_after)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "svg": [ "" ], "text": [ "" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "with open(\"../output/ok-after.svg\", \"w\") as f:\n", " f.write(ok_after)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "ok_before = draw_state_earthquakes(\"Oklahoma\", \"1995-1-1\", \"2005-1-1\").tostring()\n", "SVG(ok_before)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "svg": [ "" ], "text": [ "" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "with open(\"../output/ok-before.svg\", \"w\") as f:\n", " f.write(ok_before)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "tx_before = draw_state_earthquakes(\"Texas\", \"1995-1-1\", \"2005-1-1\").tostring()\n", "SVG(tx_before)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "svg": [ "" ], "text": [ "" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "with open(\"../output/tx-before.svg\", \"w\") as f:\n", " f.write(tx_before)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "tx_after = draw_state_earthquakes(\"Texas\", \"2005-1-1\", \"2015-1-1\").tostring()\n", "SVG(tx_after)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "svg": [ "" ], "text": [ "" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "with open(\"../output/tx-after.svg\", \"w\") as f:\n", " f.write(tx_after)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "ks_before = draw_state_earthquakes(\"Kansas\", \"1995-1-1\", \"2005-1-1\").tostring()\n", "SVG(ks_before)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 13, "svg": [ "" ], "text": [ "" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "with open(\"../output/ks-before.svg\", \"w\") as f:\n", " f.write(ks_before)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "ks_after = draw_state_earthquakes(\"Kansas\", \"2005-1-1\", \"2015-1-1\").tostring()\n", "SVG(ks_after)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 15, "svg": [ "" ], "text": [ "" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "with open(\"../output/ks-after.svg\", \"w\") as f:\n", " f.write(ks_after)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "ar_before = draw_state_earthquakes(\"Arkansas\", \"1995-1-1\", \"2005-1-1\").tostring()\n", "SVG(ar_before)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 17, "svg": [ "" ], "text": [ "" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "with open(\"../output/ar-before.svg\", \"w\") as f:\n", " f.write(ar_before)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "ar_after = draw_state_earthquakes(\"Arkansas\", \"2005-1-1\", \"2015-1-1\").tostring()\n", "SVG(ar_after)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 19, "svg": [ "" ], "text": [ "" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "with open(\"../output/ar-after.svg\", \"w\") as f:\n", " f.write(ar_after)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 20 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }