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]:
In [8]:
with open("../output/ok-before.svg", "w") as f:
f.write(ok_before)
In [9]:
tx_before = draw_state_earthquakes("Texas", "1995-1-1", "2005-1-1").tostring()
SVG(tx_before)
Out[9]:
In [10]:
with open("../output/tx-before.svg", "w") as f:
f.write(tx_before)
In [11]:
tx_after = draw_state_earthquakes("Texas", "2005-1-1", "2015-1-1").tostring()
SVG(tx_after)
Out[11]:
In [12]:
with open("../output/tx-after.svg", "w") as f:
f.write(tx_after)
In [13]:
ks_before = draw_state_earthquakes("Kansas", "1995-1-1", "2005-1-1").tostring()
SVG(ks_before)
Out[13]:
In [14]:
with open("../output/ks-before.svg", "w") as f:
f.write(ks_before)
In [15]:
ks_after = draw_state_earthquakes("Kansas", "2005-1-1", "2015-1-1").tostring()
SVG(ks_after)
Out[15]:
In [16]:
with open("../output/ks-after.svg", "w") as f:
f.write(ks_after)
In [17]:
ar_before = draw_state_earthquakes("Arkansas", "1995-1-1", "2005-1-1").tostring()
SVG(ar_before)
Out[17]:
In [18]:
with open("../output/ar-before.svg", "w") as f:
f.write(ar_before)
In [19]:
ar_after = draw_state_earthquakes("Arkansas", "2005-1-1", "2015-1-1").tostring()
SVG(ar_after)
Out[19]:
In [20]:
with open("../output/ar-after.svg", "w") as f:
f.write(ar_after)
In [ ]: