Skip to content

Instantly share code, notes, and snippets.

@nkabrown
Last active March 4, 2016 15:03
Show Gist options
  • Save nkabrown/0346fa274bd0d3edad85 to your computer and use it in GitHub Desktop.
Save nkabrown/0346fa274bd0d3edad85 to your computer and use it in GitHub Desktop.
d3 geojson map of allegheny county pa polling stations
<!DOCTYPE html>
<meta charset="UTF-8">
<title>D3 GeoJSON</title>
<style>
html,
body {
background-color: #000;
}
</style>
<div class="container">
<div class="map"></div>
</div>
<script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="https://d3js.org/d3-queue.v2.min.js"></script>
<script>
const queue = d3_queue.queue();
queue.defer(d3.json, 'watershed.geojson')
.defer(d3.json, 'polling_locations.geojson')
.await((error, map, points) => createMap(map, points));
function createMap(map, points) {
const width = 805;
const height = 562;
const projection = d3.geo.mercator()
.scale(1)
.translate([0, 0])
.precision(0);
const path = d3.geo.path().projection(projection);
const bounds = path.bounds(map);
// from Mike Bostock's Project to Bounding Box example http://bl.ocks.org/mbostock/4707858
const scale = .95 / Math.max((bounds[1][0] - bounds[0][0]) / width, (bounds[1][1] - bounds[0][1]) / height);
const translate = [(width - scale * (bounds[1][0] + bounds[0][0])) / 2, (height - scale * (bounds[1][1] + bounds[0][1])) / 2];
projection.scale(scale).translate(translate);
const svg = d3.select('.map')
.append('svg')
.attr('width', width)
.attr('height', height);
svg.selectAll('path')
.data(map.features)
.enter().append('path')
.attr('d', path)
.style('fill', 'none')
.style('stroke', '#dedede');
svg.selectAll('path')
.data(points.features)
.enter().append('path')
.attr('d', path)
.style('fill', 'none')
.style('stroke', '#98ff98');
}
</script>
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment