TIN example with Leaflet, turf.js and chroma.js (10,000 points, Leaflet1.0.0b2)
by nathansnider
HTML
<script src="https://api.mapbox.com/mapbox.js/plugins/turf/v2.0.2/turf.min.js"></script>
<script src="//cdn.rawgit.com/gka/chroma.js/master/chroma.min.js"></script>
<script src="//cdn.leafletjs.com/leaflet/v1.0.0-beta.2/leaflet.js"></script>
<link rel="stylesheet" href="//cdn.leafletjs.com/leaflet/v1.0.0-beta.2/leaflet.css">
<div id="map"></div>
CSS
html, body, #map {
height: 100%;
width:100%;
padding:0px;
margin:0px;
}
.info {
padding: 6px 8px;
font: 14px/16px Arial, Helvetica, sans-serif;
background: white;
background: rgba(255, 255, 255, 0.8);
box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
border-radius: 5px;
}
.legend {
text-align: left;
line-height: 18px;
color: #555;
}
.legend i {
width: 18px;
height: 18px;
float: left;
margin-right: 8px;
opacity: 0.7;
}
.legend .colorcircle {
border-radius: 50%;
width: 15px;
height: 15px;
margin-top: 0px;
}
JavaScript
/////////////////////////////////////////////////////////////////////////////////////////////
//setting up the map//
/////////////////////////////////////////////////////////////////////////////////////////////
// set center coordinates
var centerlat = 34.05;
var centerlon = -118.25;
// set default zoom level
var zoomLevel = 11;
// initialize map
var map = L.map('map').setView([centerlat,centerlon], zoomLevel);
// set source for map tiles
ATTR = '© <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a> | ' +
'© <a href="http://cartodb.com/attributions">CartoDB</a>';
CDB_URL = 'http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png';
// add tiles to map
L.tileLayer(CDB_URL, {attribution: ATTR}).addTo(map);
/////////////////////////////////////////////////////////////////////////////////////////////
//generating the GeoJSON objects//
/////////////////////////////////////////////////////////////////////////////////////////////
//create some GeoJSON points to be sampled (function below)
var dotcount = 10000;
var dots = make_dots(dotcount);
var maxAvg = undefined;
var minAvg = undefined;
var tin = turf.tin(dots, 'year')
for (var i = 0; i < tin.features.length; i++) {
var properties = tin.features[i].properties;
properties.average = (properties.a+properties.b+properties.c)/3;
if (minAvg === undefined && maxAvg ===undefined) {
maxAvg = properties.average;
minAvg = properties.average;
} else {
if (properties.average > maxAvg) maxAvg = properties.average;
if (properties.average < minAvg) minAvg = properties.average;
}
}
var avgRange = maxAvg - minAvg;
colorRamp = chroma.scale('YlGnBu');
L.geoJson(tin, {onEachFeature: onEachTri, style: tinStyle}).addTo(map);
/////////////////////////////////////////////////////////////////////////////////////////////
//styling...