Last active
April 7, 2017 12:45
-
-
Save BibMartin/2c042c777e4bcd3f3445225f0257e901 to your computer and use it in GitHub Desktop.
TimeDimension Heatmap
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> | |
<head> | |
<!-- FOLIUM (simplified) HEADER --> | |
<meta http-equiv="content-type" content="text/html; charset=UTF-8" /> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/leaflet.markercluster-src.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/leaflet.markercluster.js"></script> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css" /> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" /> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css" /> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" /> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.css" /> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/MarkerCluster.Default.css" /> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/MarkerCluster.css" /> | |
<link rel="stylesheet" href="https://raw.githubusercontent.com/python-visualization/folium/master/folium/templates/leaflet.awesome.rotate.css" /> | |
<style>html, body {width: 100%;height: 100%;margin: 0;padding: 0;}</style> | |
<style>#map {position:absolute;top:0;bottom:0;right:0;left:0;}</style> | |
<!-- SCRIPTS THAT ARE NOT INCLUDED IN FOLIUM TODAY --> | |
<script src="https://raw.githubusercontent.com/socib/Leaflet.TimeDimension/master/dist/leaflet.timedimension.min.js"></script> | |
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/heatmap.js/2.0.2/heatmap.min.js"></script> | |
<script type="text/javascript" src="https://rawgit.com/pa7/heatmap.js/develop/plugins/leaflet-heatmap/leaflet-heatmap.js"></script> | |
<script type="text/javascript"> | |
var TDHeatmap = L.TimeDimension.Layer.extend({ | |
initialize: function(data, options) { | |
var heatmapCfg = { | |
radius: 15, | |
maxOpacity: 1., | |
scaleRadius: false, | |
useLocalExtrema: false, | |
latField: 'lat', | |
lngField: 'lng', | |
valueField: 'count', | |
defaultWeight : 1, | |
}; | |
heatmapCfg = $.extend({}, heatmapCfg, options.heatmapOptions || {}); | |
var layer = new HeatmapOverlay(heatmapCfg); | |
L.TimeDimension.Layer.prototype.initialize.call(this, layer, options); | |
this._currentLoadedTime = 0; | |
this._currentTimeData = { | |
data: [] | |
}; | |
this.data= data; | |
this.defaultWeight = heatmapCfg.defaultWeight || 1; | |
}, | |
onAdd: function(map) { | |
L.TimeDimension.Layer.prototype.onAdd.call(this, map); | |
map.addLayer(this._baseLayer); | |
if (this._timeDimension) { | |
this._getDataForTime(this._timeDimension.getCurrentTime()); | |
} | |
}, | |
_onNewTimeLoading: function(ev) { | |
this._getDataForTime(ev.time); | |
return; | |
}, | |
isReady: function(time) { | |
return (this._currentLoadedTime == time); | |
}, | |
_update: function() { | |
this._baseLayer.setData(this._currentTimeData); | |
return true; | |
}, | |
_getDataForTime: function(time) { | |
delete this._currentTimeData.data; | |
this._currentTimeData.data = []; | |
var data = this.data[time-1]; | |
for (var i = 0; i < data.length; i++) { | |
this._currentTimeData.data.push({ | |
lat: data[i][0], | |
lng: data[i][1], | |
count: data[i].length>2 ? data[i][2] : this.defaultWeight | |
}); | |
} | |
this._currentLoadedTime = time; | |
if (this._timeDimension && time == this._timeDimension.getCurrentTime() && !this._timeDimension.isLoading()) { | |
this._update(); | |
} | |
this.fire('timeload', { | |
time: time | |
}); | |
} | |
}); | |
L.Control.TimeDimensionCustom = L.Control.TimeDimension.extend({ | |
initialize: function(index, options) { | |
var playerOptions = { | |
buffer: 1, | |
minBufferReady: -1 | |
}; | |
options.playerOptions = $.extend({}, playerOptions, options.playerOptions || {}); | |
L.Control.TimeDimension.prototype.initialize.call(this, options); | |
this.index = index; | |
}, | |
_getDisplayDateFormat: function(date){ | |
return this.index[date.getTime()-1]; | |
} | |
}); | |
</script> | |
<!-- CSS THAT ARE NOT INCLUDED IN FOLIUM TODAY --> | |
<link rel="stylesheet" href="http://apps.socib.es/Leaflet.TimeDimension/dist/leaflet.timedimension.control.min.css" /> | |
</head> | |
<body> | |
<div class="folium-map" id="map" ></div> | |
</body> | |
<script type="text/javascript"> | |
// Usual (simplified) folium code | |
var map = L.map('map', { | |
zoom: 6, | |
center: [41.5, 4], | |
}); | |
var layer = L.tileLayer( | |
'https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png', | |
{ | |
maxZoom: 18, | |
minZoom: 1, | |
attribution: '(c) <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors (c) <a href="http://cartodb.com/attributions">CartoDB</a>, CartoDB <a href ="http://cartodb.com/attributions">attributions</a>', | |
} | |
).addTo(map); | |
// Plugin-specific code. | |
var index = ['<b>Index 1</b>', 'Index 2', '<code>Index 3</code>']; | |
var data = [ | |
[[40, 3, 0.3], [41, 3], [42, 3, 0.6], [43, 3, 0.9]], | |
[[40, 4, 0.3], [41, 4, 0.4], [42, 4, 0.6], [43, 4, 0.9]], | |
[[40, 5, 0.3], [41, 5, 0.4], [42, 5, 0.6], [43, 5, 0.9]], | |
]; | |
var times = []; | |
for (var i=0; i<data.length; i++) {times.push(1+i);} | |
map.timeDimension = L.timeDimension({times : times, currentTime: new Date(1)}); | |
var timeDimensionControl = new L.Control.TimeDimensionCustom(index, { | |
autoPlay: false, | |
backwardButton: true, | |
displayDate: true, | |
forwardButton: true, | |
limitMinimumRange: 5, | |
limitSliders: false, | |
loopButton: false, | |
maxSpeed: 10, | |
minSpeed: 0.1, | |
playButton: true, | |
playReverseButton: false, | |
position: "bottomleft", | |
speedSlider: true, | |
speedStep: 0.1, | |
styleNS: "leaflet-control-timecontrol", | |
timeSlider: true, | |
timeSliderDrapUpdate: false, | |
timeSteps: 1, | |
title: "Time Control" | |
}); | |
map.addControl(timeDimensionControl); | |
var td_heatmap = new TDHeatmap(data, { | |
heatmapOptions: { | |
radius: 15, | |
maxOpacity: .6, | |
scaleRadius: false, | |
useLocalExtrema: false, | |
defaultWeight: 1 | |
} | |
}); | |
td_heatmap.addTo(map); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment