Skip to content

Instantly share code, notes, and snippets.

@ptvans
Created March 10, 2013 23:09
Show Gist options
  • Save ptvans/5130927 to your computer and use it in GitHub Desktop.
Save ptvans/5130927 to your computer and use it in GitHub Desktop.
Weather stations pseudo heatmap (using topoJSON; colored by Voronoi)
{"description":"Weather stations pseudo heatmap (using topoJSON; colored by Voronoi)","endpoint":"","display":"canvas","public":true,"require":[{"name":"TopoJSON","url":"http://d3js.org/topojson.v0.min.js"},{"name":"TopoJSON","url":"http://d3js.org/topojson.v0.min.js"}],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"stations.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"countries.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"stations_1910.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
var r = 5,
month = Math.floor(10); // 1 = January, 12 = December
var projection = d3.geo.equirectangular()
.scale(143)
var c = tb.ctx;
c.fillStyle = '#5C5C5C';
var path = d3.geo.path()
.projection(projection)
.context(c);
var world = tb['countries'];
var countries = topojson.object(world, world.objects.land);
// clipping path
//c.beginPath();
//path(countries);
//c.closePath();
//c.clip();
// background
path(countries);
c.fill();
//var values = _.map(stations.features
var colorScale = d3.scale.linear()
.domain([-500, 1500, 4000])
.range(['#21C4F7', '#F0F8B3', '#D52323'])
.interpolate(d3.interpolateLab)
var stations = tb['stations'];
var delta = 9500;
var lowerLimit = -4000;
var upperLimit = lowerLimit + delta;
var data = _.filter(stations.features, function(d, i) {
return d.properties["val" + month] > lowerLimit && d.properties["val" + month] < upperLimit;
}); // filter out unknown results
var drawGradient = function(p, i) {
var coords = _.map(p, function(d, i) { return projection(d) });
var station = data[i];
var value = station.properties["val" + month];
path.pointRadius(r);
c.fillStyle = colorScale(value);
c.strokeStyle = colorScale(value);
c.beginPath();
c.moveTo(coords[0][0], coords[0][1]);
_.each(coords.splice(1, coords.length-1), function(d) {
c.lineTo(d[0], d[1]);
}),
c.fill();
c.stroke();
c.closePath();
};
var drawPoint = function(p) {
path.pointRadius(0.5);
var color = colorScale(p.properties["val" + month]);
c.fillStyle = "#B9B9B9";
c.beginPath();
path(p)
c.closePath();
c.fill();
};
c.save();
var dataVoronoi = _.map(data, function(d) { return d.geometry.coordinates; });
var points = d3.geom.voronoi(dataVoronoi);
_.each(points, drawGradient);
//_.each(data, drawPoint);
c.restore();
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View raw

(Sorry about that, but we can’t show files that are this big right now.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment