Number of responses by zip code from the USGS' site from the 2011-11-05 3.2 quake near Piedmont, CA, USA, via Yahoo! Pipes.
-
-
Save johan/1343564 to your computer and use it in GitHub Desktop.
California earthquake responses by zip code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> | |
<title>USGS responses by zip to the 2011-11-05 3.2 earthquake near Piedmont, CA, USA</title> | |
<script src="http://mbostock.github.com/d3/d3.js?2.5.0"></script> | |
<script src="http://mbostock.github.com/d3/d3.geo.js?2.5.0"></script> | |
<script src="http://mbostock.github.com/d3/d3.csv.js?2.5.0"></script> | |
<link href="http://mbostock.github.com/d3/talk/20111018/style.css" rel="stylesheet"/> | |
<link href="http://mbostock.github.com/d3/talk/20111018/colorbrewer/colorbrewer.css" rel="stylesheet"/> | |
<style type="text/css"> | |
#states path { | |
fill: none; | |
stroke: #fff; | |
stroke-width: 1.5px; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="body"> | |
<div id="footer"> | |
USGS Earthquake comments, 2011 | |
<div class="hint">use the menu to change the color scale</div> | |
<div><select> | |
<optgroup label="Colors by Cynthia Brewer."> | |
<option value="YlGn">YlGn</option> | |
<option value="YlGnBu">YlGnBu</option> | |
<option value="GnBu">GnBu</option> | |
<option value="BuGn">BuGn</option> | |
<option value="PuBuGn">PuBuGn</option> | |
<option value="PuBu">PuBu</option> | |
<option value="BuPu">BuPu</option> | |
<option value="RdPu">RdPu</option> | |
<option value="PuRd">PuRd</option> | |
<option value="OrRd">OrRd</option> | |
<option value="YlOrRd" selected>YlOrRd</option> | |
<option value="YlOrBr">YlOrBr</option> | |
<option value="Purples">Purples</option> | |
<option value="Blues">Blues</option> | |
<option value="Greens">Greens</option> | |
<option value="Oranges">Oranges</option> | |
<option value="Reds">Reds</option> | |
<option value="Greys">Greys</option> | |
</optgroup> | |
</select></div> | |
</div> | |
</div> | |
<script src="y_pipes.js"></script> | |
<script src="index.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var usgs_dir = 'http://earthquake.usgs.gov/earthquakes/dyfi/events/nc/' | |
, usgs_evt = '71676251' // 2011-11-05 3.2 quake near Piedmont, CA, USA | |
, usgs_csv = '/us/cdi_zip.txt' | |
, usgs_url = usgs_dir + usgs_evt + usgs_csv | |
; | |
var path = d3.geo.path() | |
.projection(d3.geo.albersUsa() | |
.scale(1400) | |
.translate([680, 360])); | |
var svg = d3.select("#body").append("svg:svg") | |
.attr("class", "YlOrRd") | |
.attr("width", 960) | |
.attr("height", 500) | |
.append('svg:g') | |
.attr("transform", "scale(1.9) translate(-100 -198)") | |
; | |
var zipcodes = svg.append("svg:g") | |
.attr("id", "ca-zipcodes"); | |
var states = svg.append("svg:g") | |
.attr("id", "states"); | |
load_csv(usgs_url, show, { header_cb: parse_header, line_cb: parse_line }); | |
function parse_header(name, n) { | |
return ({ '# Columns: ZIP/Location': 'zip' | |
, 'CDI': 'cdi' | |
, 'No. of responses': 'responses' | |
, 'Epicentral distance': 'dist' | |
, 'Latitude': 'lat' | |
, 'Longitude': 'lng' | |
, 'Suspect?': 'suspect' | |
, 'City': 'city' | |
, 'State': 'state' | |
})[name]; | |
} | |
function parse_line(val, n) { | |
return ([ String // zip | |
, String // cdi | |
, Number // responses | |
, Number // dist | |
, Number // lat | |
, Number // lng | |
, Number // suspect | |
, String // city | |
, String // state | |
])[n](val); | |
} | |
function show(csv) { | |
function sum(arr) { return d3.sum(arr, function(d){ return d.responses; }); } | |
function padded_zip(d) { return pad(d.zip); } | |
var pad = d3.format("05d") | |
, data = d3.nest().key(padded_zip).rollup(sum).map(csv) | |
, max = d3.max(d3.values(data)) | |
, quantize = d3.scale.linear().domain([0, max]).rangeRound([0, 8]); | |
window.data = data; // for ease of inspection | |
d3.json("zips-ca.json", function(json) { | |
zipcodes.selectAll("path") | |
.data(json.features) | |
.enter().append("svg:path") | |
.attr("class", function(d) { | |
return "q" + quantize(data[pad(d.id)] || 0) + "-9"; | |
}) | |
.attr("d", path) | |
.append("svg:title") | |
.text(function(d) { | |
var no = data[pad(d.id)] || 0; | |
return d.id + ": " + no + " responses"; | |
}); | |
}); | |
} | |
d3.json("us-ca.json", function(json) { | |
states.selectAll("path") | |
.data(json.features) | |
.enter().append("svg:path") | |
.attr("d", path); | |
}); | |
d3.select("select").on("change", function() { | |
d3.selectAll("svg").attr("class", this.value); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"type":"FeatureCollection","features": | |
[{"type":"Feature","id":"06","properties":{"name":"California"},"geometry":{"type":"Polygon","coordinates":[[[-123.233256,42.006186],[-122.378853,42.011663],[-121.037003,41.995232],[-120.001861,41.995232],[-119.996384,40.264519],[-120.001861,38.999346],[-118.71478,38.101128],[-117.498899,37.21934],[-116.540435,36.501861],[-115.85034,35.970598],[-114.634459,35.00118],[-114.634459,34.87521],[-114.470151,34.710902],[-114.333228,34.448009],[-114.136058,34.305608],[-114.256551,34.174162],[-114.415382,34.108438],[-114.535874,33.933176],[-114.497536,33.697668],[-114.524921,33.54979],[-114.727567,33.40739],[-114.661844,33.034958],[-114.524921,33.029481],[-114.470151,32.843265],[-114.524921,32.755634],[-114.72209,32.717295],[-116.04751,32.624187],[-117.126467,32.536556],[-117.24696,32.668003],[-117.252437,32.876127],[-117.329114,33.122589],[-117.471515,33.297851],[-117.7837,33.538836],[-118.183517,33.763391],[-118.260194,33.703145],[-118.413548,33.741483],[-118.391641,33.840068],[-118.566903,34.042715],[-118.802411,33.998899],[-119.218659,34.146777],[-119.278905,34.26727],[-119.558229,34.415147],[-119.875891,34.40967],[-120.138784,34.475393],[-120.472878,34.448009],[-120.64814,34.579455],[-120.609801,34.858779],[-120.670048,34.902595],[-120.631709,35.099764],[-120.894602,35.247642],[-120.905556,35.450289],[-121.004141,35.461243],[-121.168449,35.636505],[-121.283465,35.674843],[-121.332757,35.784382],[-121.716143,36.195153],[-121.896882,36.315645],[-121.935221,36.638785],[-121.858544,36.6114],[-121.787344,36.803093],[-121.929744,36.978355],[-122.105006,36.956447],[-122.335038,37.115279],[-122.417192,37.241248],[-122.400761,37.361741],[-122.515777,37.520572],[-122.515777,37.783465],[-122.329561,37.783465],[-122.406238,38.15042],[-122.488392,38.112082],[-122.504823,37.931343],[-122.701993,37.893004],[-122.937501,38.029928],[-122.97584,38.265436],[-123.129194,38.451652],[-123.331841,38.566668],[-123.44138,38.698114],[-123.737134,38.95553],[-123.687842,39.032208],[-123.824765,39.366301],[-123.764519,39.552517],[-123.85215,39.831841],[-124.109566,40.105688],[-124.361506,40.259042],[-124.410798,40.439781],[-124.158859,40.877937],[-124.109566,41.025814],[-124.158859,41.14083],[-124.065751,41.442061],[-124.147905,41.715908],[-124.257444,41.781632],[-124.213628,42.000709],[-123.233256,42.006186]]]}} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Loads a csv file cross-domain from anywhere via Yahoo! Pipes | |
function load_csv(url, cb, opts) { | |
function format(raw) { | |
// Y| rows are listed as objects with col_1, ... col_n properties; arrayify | |
function rollup(row) { | |
for (var c = 1, col, arr = []; row.hasOwnProperty(col = 'col_'+ c); c++) | |
arr.push(row[col]); | |
return col_cb ? arr.map(col_cb) : arr; | |
} | |
function object(arr) { | |
function stow(val, idx) { data[headers[idx]] = val; } | |
var data = {}; | |
arr.forEach(stow); | |
return data; | |
} | |
var col_cb = opts.header_cb | |
, headers = rollup(raw.value.items[0]); | |
col_cb = opts.line_cb; | |
return raw.value.items.slice(1).map(rollup).map(object); | |
} | |
var pipe = 'a1aa6137151bc86851b7b65373f95bce' | |
, nth = load_csv.nth = (load_csv.nth || 0) + 1 | |
, name = 'cb' + nth.toString(36) | |
, skip = encodeURIComponent(opts.skip_lines || 0) | |
, head = encodeURIComponent(opts.hdr_line_no || 0) | |
, purl = 'http://pipes.yahoo.com/pipes/pipe.run?_id='+ pipe | |
+ '&_render=json&_callback=load_csv.' + name | |
+ '&u=' + encodeURIComponent(url) +'&x='+ skip +'&c='+ head | |
, load = document.createElement('script'); | |
load_csv[name] = function(json) { | |
delete load_csv[name]; | |
document.head.removeChild(load); | |
cb(format(json)); | |
}; | |
load.src = purl; | |
document.head.appendChild(load); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment