forked from theopolisme/location-history-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
256 lines (216 loc) · 7.23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
( function ( $, L, prettySize ) {
var map, heat,
heatOptions = {
tileOpacity: 1,
heatOpacity: 1,
radius: 25,
blur: 15
};
function status( message ) {
$( '#currentStatus' ).text( message );
}
// Start at the beginning
stageOne();
function stageOne () {
var dropzone;
// Initialize the map
map = L.map( 'map' ).setView( [0,0], 2 );
L.tileLayer( 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'location-history-visualizer is open source and available <a href="https://github.com/theopolisme/location-history-visualizer">on GitHub</a>. Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors.',
maxZoom: 18,
minZoom: 2
} ).addTo( map );
// Initialize the dropzone
dropzone = new Dropzone( document.body, {
url: '/',
previewsContainer: document.createElement( 'div' ), // >> /dev/null
clickable: false,
accept: function ( file, done ) {
stageTwo( file );
dropzone.disable(); // Your job is done, buddy
}
} );
// For mobile browsers, allow direct file selection as well
$( '#file' ).change( function () {
stageTwo( this.files[0] );
dropzone.disable();
} );
}
function stageTwo ( file ) {
// Google Analytics event - heatmap upload file
ga('send', 'event', 'Heatmap', 'upload', undefined, file.size);
heat = L.heatLayer( [], heatOptions ).addTo( map );
var type;
try {
if ( /\.kml$/i.test( file.name ) ) {
type = 'kml';
} else {
type = 'json';
}
} catch ( ex ) {
status( 'Something went wrong generating your map. Ensure you\'re uploading a Google Takeout JSON file that contains location data and try again, or create an issue on GitHub if the problem persists. ( error: ' + ex.message + ' )' );
return;
}
// First, change tabs
$( 'body' ).addClass( 'working' );
$( '#intro' ).addClass( 'hidden' );
$( '#working' ).removeClass( 'hidden' );
var latlngs = [];
var os = new oboe();
os.node( 'locations.*', function ( location ) {
var SCALAR_E7 = 0.0000001; // Since Google Takeout stores latlngs as integers
if ( type === 'json' ) latlngs.push( [ location.latitudeE7 * SCALAR_E7, location.longitudeE7 * SCALAR_E7 ] );
return oboe.drop;
} ).done( function () {
status( 'Generating map...' );
heat._latlngs = latlngs;
heat.redraw();
stageThree( /* numberProcessed */ latlngs.length );
} );
var fileSize = prettySize( file.size );
status( 'Preparing to import file ( ' + fileSize + ' )...' );
// Now start working!
if ( type === 'json' ) parseJSONFile( file, os );
if ( type === 'kml' ) parseKMLFile( file );
}
function stageThree ( numberProcessed ) {
// Google Analytics event - heatmap render
ga('send', 'event', 'Heatmap', 'render', undefined, numberProcessed);
var $done = $( '#done' );
// Change tabs :D
$( 'body' ).removeClass( 'working' );
$( '#working' ).addClass( 'hidden' );
$done.removeClass( 'hidden' );
// Update count
$( '#numberProcessed' ).text( numberProcessed.toLocaleString() );
$( '#launch' ).click( function () {
var $email = $( '#email' );
if ( $email.is( ':valid' ) ) {
$( this ).text( 'Launching... ' );
$.post( '/heatmap/submit-email.php', {
email: $email.val()
} )
.always( function () {
$( 'body' ).addClass( 'map-active' );
$done.fadeOut();
activateControls();
} );
} else {
alert( 'Please enter a valid email address to proceed.' );
}
} );
function activateControls () {
var $tileLayer = $( '.leaflet-tile-pane' ),
$heatmapLayer = $( '.leaflet-heatmap-layer' ),
originalHeatOptions = $.extend( {}, heatOptions ); // for reset
// Update values of the dom elements
function updateInputs () {
var option;
for ( option in heatOptions ) {
if ( heatOptions.hasOwnProperty( option ) ) {
document.getElementById( option ).value = heatOptions[option];
}
}
}
updateInputs();
$( '.control' ).change( function () {
switch ( this.id ) {
case 'tileOpacity':
$tileLayer.css( 'opacity', this.value );
break;
case 'heatOpacity':
$heatmapLayer.css( 'opacity', this.value );
break;
default:
heatOptions[ this.id ] = Number( this.value );
heat.setOptions( heatOptions );
break;
}
} );
$( '#reset' ).click( function () {
$.extend( heatOptions, originalHeatOptions );
updateInputs();
heat.setOptions( heatOptions );
// Reset opacity too
$heatmapLayer.css( 'opacity', originalHeatOptions.heatOpacity );
$tileLayer.css( 'opacity', originalHeatOptions.tileOpacity );
} );
}
}
/*
Break file into chunks and emit 'data' to oboe instance
*/
function parseJSONFile( file, oboeInstance ) {
var fileSize = file.size;
var prettyFileSize = prettySize(fileSize);
var chunkSize = 512 * 1024; // bytes
var offset = 0;
var self = this; // we need a reference to the current object
var chunkReaderBlock = null;
var startTime = Date.now();
var endTime = Date.now();
var readEventHandler = function ( evt ) {
if ( evt.target.error == null ) {
offset += evt.target.result.length;
var chunk = evt.target.result;
var percentLoaded = ( 100 * offset / fileSize ).toFixed( 0 );
status( percentLoaded + '% of ' + prettyFileSize + ' loaded...' );
oboeInstance.emit( 'data', chunk ); // callback for handling read chunk
} else {
return;
}
if ( offset >= fileSize ) {
oboeInstance.emit( 'done' );
return;
}
// of to the next chunk
chunkReaderBlock( offset, chunkSize, file );
}
chunkReaderBlock = function ( _offset, length, _file ) {
var r = new FileReader();
var blob = _file.slice( _offset, length + _offset );
r.onload = readEventHandler;
r.readAsText( blob );
}
// now let's start the read with the first block
chunkReaderBlock( offset, chunkSize, file );
}
/*
Default behavior for file upload (no chunking)
*/
function parseKMLFile( file ) {
var fileSize = prettySize( file.size );
var reader = new FileReader();
reader.onprogress = function ( e ) {
var percentLoaded = Math.round( ( e.loaded / e.total ) * 100 );
status( percentLoaded + '% of ' + fileSize + ' loaded...' );
};
reader.onload = function ( e ) {
var latlngs;
status( 'Generating map...' );
latlngs = getLocationDataFromKml( e.target.result );
heat._latlngs = latlngs;
heat.redraw();
stageThree( latlngs.length );
}
reader.onerror = function () {
status( 'Something went wrong reading your JSON file. Ensure you\'re uploading a "direct-from-Google" JSON file and try again, or create an issue on GitHub if the problem persists. ( error: ' + reader.error + ' )' );
}
reader.readAsText( file );
}
function getLocationDataFromKml( data ) {
var KML_DATA_REGEXP = /<when>( .*? )<\/when>\s*<gx:coord>( \S* )\s( \S* )\s( \S* )<\/gx:coord>/g,
locations = [],
match = KML_DATA_REGEXP.exec( data );
// match
// [ 1 ] ISO 8601 timestamp
// [ 2 ] longitude
// [ 3 ] latitude
// [ 4 ] altitude ( not currently provided by Location History )
while ( match !== null ) {
locations.push( [ Number( match[ 3 ] ), Number( match[ 2 ] ) ] );
match = KML_DATA_REGEXP.exec( data );
}
return locations;
}
}( jQuery, L, prettySize ) );