State Maps of Earthquakes by Decade

  • All of the data for these maps comes from USGS. I caculated what state the earthquakes were in using PostGIS.
  • The state shapes come from the U.S. Census.
  • The code below generates two maps for each state, one "before" (1995-2004) and one "after" (2005-2014).
  • The maps are generated in an SVG format so they're easy to hand over to the BuzzFeed design team.
  • The earthquakes are not sized by magnitude.

In [1]:
import fiona
import fiona.crs
import pyproj
import svgwrite
from IPython.display import SVG
import shapely.geometry
import pandas as pd

In [2]:
def draw_state_earthquakes(state, start, end):
    with fiona.drivers():
        with fiona.open("../data/stateshapes/{0}/".format(state.lower())) as source:
            drawing = svgwrite.Drawing()
            bbox = source.bounds
            crs = source.crs
            projection = pyproj.Proj(fiona.crs.to_string(crs))
            stroke_width = 0.0075
            padding = 0.05
            width = padding + bbox[2] - bbox[0]
            height = padding + bbox[3] - bbox[1]
            drawing.viewbox(bbox[0]-padding, bbox[1]-padding, width, height)
            main_group = drawing.g()
            main_group.translate(0, bbox[1] + bbox[3])
            main_group.scale(1, -1)
            state_group = drawing.g(id="state")
            quake_group = drawing.g(id="quakes")
            drawing.add(main_group)
            main_group.add(state_group)
            main_group.add(quake_group)
            for feat in source:
                shape = shapely.geometry.shape(feat["geometry"])
                alpha = 1
                red = "#ee3322"
                if shape.type == "Polygon":
                    state_group.add(drawing.polygon(points=shape.exterior.coords, 
                                fill="beige", stroke="black", stroke_width=stroke_width, opacity=alpha))
                else:
                    raise ValueError("Don't recognize geometry type")
            all_quakes = pd.DataFrame.from_csv("../data/earthquake_states.csv", index_col=None, parse_dates=["time", "updated"])
            us_quakes = all_quakes.dropna(subset=["state"])
            state_last_decade = us_quakes[(us_quakes["state"] == state) &\
                                   (us_quakes["time"] < end) &\
                                   (us_quakes["time"] >= start)]
            for i, row in state_last_decade.iterrows():
                quake_group.add(drawing.circle(center=(row["longitude"], row["latitude"]), r=width / 200, 
                                         fill=red, opacity=0.5, stroke="#333", stroke_width=stroke_width))
            return drawing

In [3]:
ok_after = draw_state_earthquakes("Oklahoma", "2005-1-1", "2015-1-1").tostring()
SVG(ok_after)


Out[3]:

In [5]:
with open("../output/ok-after.svg", "w") as f:
    f.write(ok_after)

In [6]:
ok_before = draw_state_earthquakes("Oklahoma", "1995-1-1", "2005-1-1").tostring()
SVG(ok_before)


Out[6]: