Last active
August 29, 2015 14:22
-
-
Save pgiraud/9f400d54ce2ea7b4f820 to your computer and use it in GitHub Desktop.
D3 GeoJSON labels
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> | |
<html> | |
<head> | |
<script data-require="d3@3.5.3" data-semver="3.5.3" src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="d3.geo.tile.min.js"></script> | |
<link rel="stylesheet" href="style.css" /> | |
<style> | |
svg { | |
font-family: sans-serif; | |
} | |
path.feature { | |
fill: rgba(255, 0, 0, 0.5); | |
} | |
path.feature:hover { | |
fill: rgba(255, 140, 0, 0.5); | |
cursor: zoom-in; | |
} | |
text.halo { | |
opacity: 0.7; | |
stroke: #fff; | |
stroke-width: 5px; | |
} | |
stroke-miterlimit: 1; | |
</style> | |
</head> | |
<body> | |
<h1>Hello Plunker!</h1> | |
<div id="vis">fdfdf</div> | |
<script src="script.js"></script> | |
</body> | |
</html> |
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
// Code goes here | |
var width = 400, | |
height = 300; | |
var tile = d3.geo.tile() | |
.size([width, height]); | |
var projection = d3.geo.mercator(); | |
var path = d3.geo.path() | |
.projection(projection); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var raster = svg.append("g"); | |
var features = svg.append('g').attr('class', 'features'); | |
var labels = svg.append('g').attr('class', 'labels'); | |
d3.json('169_4326.json', function(err, json) { | |
if (err) { | |
throw err; | |
} | |
projection | |
.scale(1) | |
.translate([0, 0]); | |
// compute scale and translate given the data extent | |
var b = path.bounds(json), | |
s = .95 / Math.max((b[1][0] - b[0][0]) / width, (b[1][1] - b[0][1]) / height), | |
t = [(width - s * (b[1][0] + b[0][0])) / 2, (height - s * (b[1][1] + b[0][1])) / 2]; | |
projection | |
.scale(s) | |
.translate(t); | |
features.selectAll("path").data(json.features).enter().append("path") | |
.attr("class", "feature") | |
.attr("d", path) | |
.style("stroke-width", "1") | |
.style("stroke", "black") | |
.append | |
labels.selectAll('.label').data(json.features).enter().append('text') | |
.attr("class", "halo") | |
.attr('transform', function(d) { | |
return "translate(" + path.centroid(d) + ")"; | |
}) | |
.style('text-anchor', 'middle') | |
.text(function(d) { | |
return d.properties.name | |
}); | |
labels.selectAll('.label').data(json.features).enter().append('text') | |
.attr("class", "label") | |
.attr('transform', function(d) { | |
return "translate(" + path.centroid(d) + ")"; | |
}) | |
.style('text-anchor', 'middle') | |
.text(function(d) { | |
return d.properties.name | |
}); | |
// raster tiles | |
var zoom = d3.behavior.zoom() | |
.scale(projection.scale() * 2 * Math.PI) | |
.scaleExtent([1 << 11, 1 << 14]) | |
.translate(t) | |
var tiles = tile | |
.scale(zoom.scale()) | |
.translate(zoom.translate()) | |
(); | |
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", "d"][Math.random() * 4 | 0] + ".tiles.mapbox.com/v3/examples.map-i86nkdio/" + 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]; }); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment