Skip to content

Instantly share code, notes, and snippets.

@clhenrick
Last active June 27, 2016 23:13
Show Gist options
  • Save clhenrick/4e9553a7208f2c6817662f6caa2dc845 to your computer and use it in GitHub Desktop.
Save clhenrick/4e9553a7208f2c6817662f6caa2dc845 to your computer and use it in GitHub Desktop.
WWSD #16: turf: measurement
license: gpl-3.0
<!DOCTYPE html>
<meta charset="utf-8">
<style>
path {
fill: #ccc;
stroke: #fff;
/* stroke-width: .5px; */
}
path:hover {
fill: red;
}
</style>
<body>
<canvas></canvas>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Turf.js/2.0.2/turf.min.js"></script>
<script>
var width = 500,
height = 960;
var canvas = d3.select("canvas").node()
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext('2d')
var projection = d3.geo.mercator()
.center([-115.007503,40.8469266])
.scale(2800)
var path = d3.geo.path()
.projection(projection)
.context(ctx)
var url = "http://enjalot.github.io/wwsd/data/USA/california-streams.topojson"
d3.json(url, function(error, topology) {
if (error) throw error;
console.log("topojson", topology)
var geojson = topojson.feature(topology, topology.objects['us-streams-unmerged-18']);
// console.log("geojson", geojson)
var maxLength = d3.max(geojson.features, function(d) {
var length = turf.lineDistance(d, 'miles');
d.properties.length = length;
return length;
});
var color = d3.scale.threshold()
.domain([0, 2, 20, maxLength])
.range(["#853c3c", "#85763c", "#3c8544", "#0068b7"]);
var lineScale = d3.scale.threshold()
.domain([0, 2, 20, maxLength])
.range([0.2, 0.4, 0.6, 1]);
ctx.strokeStyle = "#111";
ctx.fillStyle = "#99ffe8"
ctx.lineWidth = 1;
geojson.features.forEach(function(feature) {
ctx.strokeStyle = color(feature.properties.length)
ctx.lineWidth = lineScale(feature.properties.length)
ctx.beginPath()
path(feature)
ctx.stroke();
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment