Built with blockbuilder.org
forked from nohevog1's block: US_Earthquakes
license: mit |
Built with blockbuilder.org
forked from nohevog1's block: US_Earthquakes
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<title>Class 5</title> | |
<style> | |
body { | |
position: absolute; | |
margin: 0px; | |
font: 16px sans-serif; | |
} | |
svg { | |
background-color: #4682b4; | |
} | |
.info { | |
color: #000; | |
position: absolute; | |
top: 450px; | |
left: 800px; | |
} | |
path { | |
fill: #555555; | |
stroke: #aaaaaa; | |
} | |
path.quake { | |
fill: crimson; | |
} | |
</style> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="https://d3js.org/topojson.v2.min.js"></script> | |
<script src="//d3js.org/topojson.v1.min.js"></script> | |
<body> | |
<script> | |
var width = 960, height = 500; | |
var svg = d3.select("svg"), | |
width = +svg.attr("width"), | |
height = +svg.attr("height"); | |
var projection = d3.geoMercator() | |
.scale((1 << 18) / tau) | |
.translate([width / 2, height / 2]) | |
.center([-97.0929, 35.0078]); | |
var path = d3.geoPath() | |
.projection(projection); | |
var Oklahoma = "https://raw.githubusercontent.com/nohevog1/Classes/master/OK.json" | |
d3.json(Oklahoma, function(error, Oklahoma) { | |
if (error) throw error; | |
var tiles = d3.tile() | |
.size([width, height]) | |
.scale(projection.scale() * tau) | |
.translate(projection([0, 0])) | |
(); | |
svg.selectAll("image") | |
.data(tiles) | |
.enter().append("image") | |
.attr("xlink:href", function(d) { return "http://" + "abc"[d[1] % 3] + ".tile.openstreetmap.org/" + d[2] + "/" + d[0] + "/" + d[1] + ".png"; }) | |
.attr("x", function(d) { return (d[0] + tiles.translate[0]) * tiles.scale; }) | |
.attr("y", function(d) { return (d[1] + tiles.translate[1]) * tiles.scale; }) | |
.attr("width", tiles.scale) | |
.attr("height", tiles.scale); | |
}); | |
//QUAKES | |
var data; // declare a global variable | |
var svg1 = d3.select("body").append("svg1") | |
.attr("width", width) | |
.attr("height", height) | |
.on("mousemove", mousemoved); | |
var layer1 = svg1.append("g"); | |
var layer2 = svg1.append("g"); | |
var info = d3.select("body").append("div") | |
.attr("class", "info"); | |
// NEW: Title | |
svg1.append('text') | |
.attr('x', width / 2) | |
.attr('y', "1.5em") | |
.style('font-size', '1.5em') | |
.style('text-anchor', 'middle') | |
.text('Earthquakes in the last week') | |
// NEW: Create a color scale for the earthquakes | |
var color = d3.scaleOrdinal(d3.schemeCategory10); | |
// Add a legend | |
var legend = layer2.selectAll('.legend') | |
.data(d3.range(10)) | |
.enter().append('g') | |
.attr('class', 'legend') | |
.attr('transform', 'translate(' + [width - 150, height / 2] +')'); | |
legend.append('circle') | |
.attr('cy', function(d) { return 4 * d * path.pointRadius() ; }) | |
.attr('r', path.pointRadius()) | |
.attr('fill', color) | |
.attr('stroke', '#aaaaaa') | |
legend.append('text') | |
.attr('x', '1em') | |
.attr('y', function(d) { return 4 * d * path.pointRadius(); }) | |
.attr('dy', '.3em') | |
.text(function(d) { return d + ' - ' + (d + 1); }) | |
var usgs = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson"; | |
var url = "https://umbcvis.github.io/classes/class-03/us.json" | |
d3.json(url, plotStates); | |
d3.json(usgs, plotQuakes); | |
function plotStates(error, json) { | |
if (error) throw error; | |
json = json.objects.us.geometries.map(function(d) { return topojson.feature(json, d); }) | |
layer1.selectAll("path") | |
.data(json) | |
.enter() | |
.append("path") | |
.attr("d", path); | |
} | |
// NEW: Various updates for magnitude-dependent labels and colors | |
function plotQuakes(error, json) { | |
if (error) throw error; | |
var features = json.features.filter(inRange); // Only those in the viewport | |
var mags = features.map(function(d) { return d.properties.mag }); | |
var largest = d3.max(mags); | |
layer2.selectAll("path.quake") | |
.data(features) | |
.enter().append("path") | |
.attr("class", "quake") | |
.style("fill", magColor) | |
.attr("d", path) | |
// Remove unnecessary legend elements | |
legend.filter(function(d) { return d > largest; }).remove(); | |
// Update the legend labels to indicate # of earthquakes in range | |
legend.select('text') | |
.text(function(value) { return value + ' - ' + (value + 1) + " (" + | |
mags.filter(function(d) { return d >= value && d < value + 1; }).length + ")"; | |
}); | |
} | |
// projection returns null for [lon, lat] that won't appear in the viewport | |
function inRange(d) { | |
return projection(d.geometry.coordinates) !== null; | |
} | |
// NEW: Return magnitude-dependent color | |
function magColor(d) { | |
return color(Math.floor(d.properties.mag)); | |
} | |
function mousemoved() { | |
info.text(formatLocation(projection.invert(d3.mouse(this)), projection.scale())); | |
} | |
function formatLocation(p, k) { | |
var format = d3.format("." + Math.floor(Math.log(k) / 2 - 2) + "f"); | |
return (p[1] < 0 ? format(-p[1]) + "°S" : format(p[1]) + "°N") + " " | |
+ (p[0] < 0 ? format(-p[0]) + "°W" : format(p[0]) + "°E"); | |
} | |
</script> |