Skip to content

Instantly share code, notes, and snippets.

@li01012
Last active March 30, 2017 21:49
Show Gist options
  • Save li01012/9ff52ddd07512f8fd7ab58c584e23a28 to your computer and use it in GitHub Desktop.
Save li01012/9ff52ddd07512f8fd7ab58c584e23a28 to your computer and use it in GitHub Desktop.
assignment 07
license: mit
<!DOCTYPE html>
<meta charset="utf-8">
<title>class7</title><style>
<style>
.line {
fill: none;
stroke: black;
stroke-width: 3px;
}
rect {
fill: none;
stroke: black;
stroke-width: 3px;
}
path.quake, circle.quake {
fill: crimson;
fill-opacity: 0.2;
}
counties, path.county-border {
fill: none;
stroke: #000;
stroke-opacity: .3px;
}
path.state {
fill: none;
stroke: #111111;
}
//streets {
fill: none;
stroke: black;
stroke-width: 2px;
stroke-linejoin: round;
}
</style>
<svg width="980" height="550"></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.v2.min.js"></script>
<script>
var smallest = 2.5; // threshold magnitude
var data = null; // global variable
var pi = Math.PI,
tau = 2 * pi;
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var projection = d3.geoMercator()
.scale((1 << 15) / tau)
.center([-97.0929, 35.0078]);
var path = d3.geoPath()
.pointRadius(3)
.projection(projection);
var Oklahoma = "https://raw.githubusercontent.com/deldersveld/topojson/master/countries/us-states/OK-40-oklahoma-counties.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);
});
//Earthquake map
var base = "https://gist.githubusercontent.com/pbogden/935370a5272acff2618b/raw/f4d102d819066bb21a0021b4cbab6502e86bf587/";
var usgs = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_month.geojson";
// Plot earthquake data
var url = base + "quakes.json";
d3.json(url, plotQuakes);
//d3.json(usgs, plotQuakes);
// Plot State & county boundaries
d3.json(base + "ok.json", plotState);
function plotQuakes(error, json) {
if (error) console.log(error);
// Make json available for console manipulation er
data = json;
// Filter the small earthquakes
var features = data.features.filter(function(d) { return (+ d.properties.mag >= smallest) });
var layer1 = svg.append("g");
// Plot the earthquakes
layer1.selectAll("path.quake")
.data(features)
.enter().append("path")
.attr("class", "quake")
.attr("d", path)
}
function plotState(error, ok) {
if (error) console.log(error);
var objects = ok.objects.ok;
var all = topojson.merge(ok, objects.geometries);
var layer2 = svg.append("g");
layer2.append("path")
.datum(all)
.attr("class", "state")
.attr("d", path);
layer2.append("path")
.datum(topojson.mesh(ok, objects, function(a, b) { return a !== b; }))
.attr("class", "county-border")
.attr("d", path);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment