Binesh final project -- points plotted on map.
Last active
December 22, 2015 20:25
-
-
Save pbogden/add2b4d8a4bbfc6aaa72 to your computer and use it in GitHub Desktop.
binesh6
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
d3.geo.tile=function(){function t(){var t=Math.max(Math.log(n)/Math.LN2-8,0),h=Math.round(t+e),o=Math.pow(2,t-h+8),u=[(r[0]-n/2)/o,(r[1]-n/2)/o],l=[],c=d3.range(Math.max(0,Math.floor(-u[0])),Math.max(0,Math.ceil(a[0]/o-u[0]))),M=d3.range(Math.max(0,Math.floor(-u[1])),Math.max(0,Math.ceil(a[1]/o-u[1])));return M.forEach(function(t){c.forEach(function(a){l.push([a,t,h])})}),l.translate=u,l.scale=o,l}var a=[960,500],n=256,r=[a[0]/2,a[1]/2],e=0;return t.size=function(n){return arguments.length?(a=n,t):a},t.scale=function(a){return arguments.length?(n=a,t):n},t.translate=function(a){return arguments.length?(r=a,t):r},t.zoomDelta=function(a){return arguments.length?(e=+a,t):e},t}; |
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> | |
<meta charset="utf-8"> | |
<title>DC Gun Crime 2013-2014</title> | |
<style> | |
body { | |
margin: 0; | |
} | |
path { | |
fill: black; | |
opacity: 0.3; | |
stroke: black; | |
} | |
form, #info, #title { | |
font: 17px sans-serif; | |
position: absolute; | |
} | |
</style> | |
<body> | |
<form id='toggle'> | |
DC Crime 2013 | |
<input type="radio" name="select" value="crime13" checked> | |
<br>DC Crime 2014 | |
<input type="radio" name="select" value="crime14"> | |
</form> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.19/topojson.min.js"></script> | |
<script src="d3.geo.tile.min.js"></script> | |
<script> | |
var width = Math.max(960, window.innerWidth), | |
height = Math.max(500, window.innerHeight); | |
var tile = d3.geo.tile() | |
.size([width, height]); | |
var projection = d3.geo.mercator() | |
.translate([width / 2, height / 2]) | |
.scale([100000]); | |
var center = projection([-77.065, 38.8888]); | |
var path = d3.geo.path() | |
.projection(projection); | |
var zoom = d3.behavior.zoom() | |
.scale(projection.scale() * 2 * Math.PI) | |
.scaleExtent([1 << 11, 1 << 22]) | |
.translate([width - center[0], height - center[1]]) | |
.on("zoom", zoomed); | |
// With the center computed, now adjust the projection such that | |
// it uses the zoom behavior’s translate and scale. | |
projection | |
.scale(1 / 2 / Math.PI) | |
.translate([0, 0]); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var raster = svg.append("g"); | |
var vector = svg.append("g"); | |
svg.call(zoom); | |
// Crime Incidents 2013 | |
d3.json("https://gist.githubusercontent.com/geoatlantic/df9c44bcfcdbff0151a8/raw/5f5673ce9d10e76fbd7e8fbca79c72a53406ae3e/crime_incidents_gun_2013.json", function(error, crime_incidents_gun_2013) { | |
if (error) throw error; | |
// Plot GUN crime 2013 | |
vector.append("g").selectAll("path") | |
.data(crime_incidents_gun_2013.features) | |
.enter() | |
.append("path") | |
.attr("d", path) | |
zoomed(); | |
}); | |
function zoomed() { | |
var tiles = tile | |
.scale(zoom.scale()) | |
.translate(zoom.translate()) | |
(); | |
vector | |
.attr("transform", "translate(" + zoom.translate() + ")scale(" + zoom.scale() + ")") | |
.style("stroke-width", 1 / zoom.scale()); | |
// rescale the pointRadius | |
path.pointRadius( 5 / zoom.scale() ); | |
// redraw the circles | |
vector.selectAll("path") | |
.attr("d", path); | |
var image = raster | |
.attr("transform", "scale(" + tiles.scale + ")translate(" + tiles.translate + ")") | |
.selectAll("image") | |
.data(tiles, function(d) { return d; }); | |
image.exit() | |
.remove(); | |
image.enter().append("image") | |
.attr("xlink:href", function(d) { return "http://" + ["a", "b", "c"][Math.random() * 3 | 0] + ".tile.openstreetmap.org/" + d[2] + "/" + d[0] + "/" + d[1] + ".png"; }) | |
.attr("width", 1) | |
.attr("height", 1) | |
.attr("x", function(d) { return d[0]; }) | |
.attr("y", function(d) { return d[1]; }); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment