Skip to content

Instantly share code, notes, and snippets.

@nohevog1
Last active April 7, 2017 23:04
Show Gist options
  • Save nohevog1/ab3887648264c31bc21d494e15ec550d to your computer and use it in GitHub Desktop.
Save nohevog1/ab3887648264c31bc21d494e15ec550d to your computer and use it in GitHub Desktop.
Raster&VectorII_HW8
license: mit
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
position: absolute;
margin: 0px;
font: 16px sans-serif;
}
body {
margin: 0;
}
path {
fill: none;
stroke: blue;
stroke-linejoin: round;
stroke-width: 1.5px;
}
/* NEW: style the earthquakes */
path.quake {
fill: yellow;
}
</style>
<svg>
</svg>
<script src="//d3js.org/d3.v4.min.js"></script>
<script src="//d3js.org/d3-tile.v0.0.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script>
var pi = Math.PI,
tau = 2 * pi;
var width = Math.max(960, window.innerWidth),
height = Math.max(500, window.innerHeight);
// Initialize the projection to fit the world in a 1×1 square centered at the origin.
var projection = d3.geoMercator()
.scale(1 / tau)
.translate([0, 0]);
var path = d3.geoPath()
.projection(projection);
var tile = d3.tile()
.size([width, height]);
var zoom = d3.zoom()
.scaleExtent([1 << 11, 1 << 14])
.on("zoom", zoomed);
var svg = d3.select("svg")
.attr("width", width)
.attr("height", height)
.on("mousemove", mousemoved);
var raster = svg.append("g");
var vector = svg.append("path");
var us = "https://umbcvis.github.io/classes/class-03/us.json";
d3.json(us, function(error, us) {
if (error) throw error;
var states = us.objects.us.geometries.map(function(d) { return topojson.feature(us, d); })
vector
.datum(topojson.mesh(us, us.objects.us));
// Compute the projected initial center.
var center = projection([-98.5, 39.5]);
// Apply a zoom transform equivalent to projection.{scale,translate,center}.
svg
.call(zoom)
.call(zoom.transform, d3.zoomIdentity
.translate(width / 2, height / 2)
.scale(1 << 12)
.translate(-center[0], -center[1]));
// NEW: Title
svg.append('text')
.attr('x', width / 2)
.attr('y', "1.5em")
.style('font-size', '1.5em')
.style('text-anchor', 'middle')
.style('fill', 'Magenta')
.text('Weekly display of Earthquakes in the United States')
});
function zoomed() {
var transform = d3.event.transform;
var tiles = tile
.scale(transform.k)
.translate([transform.x, transform.y])
();
projection
.scale(transform.k / tau)
.translate([transform.x, transform.y]);
vector
.attr("d", path);
var image = raster
.attr("transform", stringify(tiles.scale, tiles.translate))
.selectAll("image")
.data(tiles, function(d) { return d; });
image.exit().remove();
image.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] * 256; })
.attr("y", function(d) { return d[1] * 256; })
.attr("width", 256)
.attr("height", 256);
}
function stringify(scale, translate) {
var k = scale / 256, r = scale % 1 ? Number : Math.round;
return "translate(" + r(translate[0] * scale) + "," + r(translate[1] * scale) + ") scale(" + k + ")";
}
var path = d3.geoPath()
.projection(projection);
// NEW: Read the earthquake data and send it to function "plotQuakes"
var usgs = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson";
// Topojson for US states
var url = "https://umbcvis.github.io/classes/class-03/us.json"
d3.json(url, plotStates);
d3.json(usgs, plotQuakes);
function plotStates(error, json) {
// Convert topojson to an Array of GeoJSON features
// This defines "data", a global variable that can be manipulated in the developer console
data = json.objects.us.geometries.map(function(d) { return topojson.feature(json, d); })
}
// NEW: Function that plots the earthquake data
function plotQuakes(error, json) {
if (error) return console.log(error);
// Global variable, data, will be visible in the console
data = json;
// Extract an array of feature objects, one for each earthquake
var features = json.features;
// Plot the earthquakes
var layer2 = svg.append("g");
layer2.selectAll("path.quake")
.data(features)
.enter().append("path")
.attr("class", "quake")
.attr("d", path)
}
// Add a <div> element to label the mouse position
var info = d3.select("body").append("div")
.attr("class", "info");
// Label the geographic coordinates of the mouse position on the map
function mousemoved() {
info.text(formatLocation(projection.invert(d3.mouse(this)), projection.scale()));
}
// Format the text that will be displayed in the the <div> element on the map
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment