forked from theopolisme/location-history-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
210 lines (175 loc) · 6.07 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
( function ( $, L, prettySize ) {
var map, heat,
heatOptions = {
tileOpacity: 1,
heatOpacity: 1,
radius: 25,
blur: 15
};
// 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], document.getElementById("fromDate").value, document.getElementById("toDate").value );
dropzone.disable();
} );
}
function stageTwo ( file, fromDate, toDate ) {
heat = L.heatLayer( [], heatOptions ).addTo( map );
// First, change tabs
$( 'body' ).addClass( 'working' );
$( '#intro' ).addClass( 'hidden' );
$( '#working' ).removeClass( 'hidden' );
if(fromDate && toDate){
fromDate=new Date(document.getElementById("fromDate").value).getTime();
toDate=new Date(document.getElementById("toDate").value).getTime();
}
// Now start working!
processFile( file );
function status ( message ) {
$( '#currentStatus' ).text( message );
}
function processFile ( file ) {
var fileSize = prettySize( file.size ),
reader = new FileReader();
status( 'Preparing to import file (' + fileSize + ')...' );
function getLocationDataFromJson ( data ) {
var SCALAR_E7 = 0.0000001, // Since Google Takeout stores latlngs as integers
locations = JSON.parse( data ).locations;
if(fromDate && toDate) {
i = locations.length;
while (i--) {
if (locations[i].timestampMs < fromDate || locations[i].timestampMs > toDate)
locations.splice(i, 1);
}
}
if ( !locations || locations.length === 0 ) {
throw new ReferenceError( 'No location data found.' );
}
return locations.map( function ( location ) {
return [ location.latitudeE7 * SCALAR_E7, location.longitudeE7 * SCALAR_E7 ];
} );
}
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)
if(fromDate && toDate) {
while ( match !== null ) {
if(new Date(match[1]).getTime() >= fromDate && new Date(match[1]).getTime() <= toDate)
locations.push( [ Number( match[3] ), Number( match[2] ) ] );
match = KML_DATA_REGEXP.exec( data );
}
}
else{
while ( match !== null ) {
locations.push( [ Number( match[3] ), Number( match[2] ) ] );
match = KML_DATA_REGEXP.exec( data );
}
}
return locations;
}
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...' );
try {
if ( /\.kml$/i.test( file.name ) ) {
latlngs = getLocationDataFromKml( e.target.result );
} else {
latlngs = getLocationDataFromJson( e.target.result );
}
} 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;
}
heat._latlngs = latlngs;
heat.redraw();
stageThree( /* numberProcessed */ 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 stageThree ( numberProcessed ) {
var $done = $( '#done' );
// Change tabs :D
$( 'body' ).removeClass( 'working' );
$( '#working' ).addClass( 'hidden' );
$done.removeClass( 'hidden' );
// Update count
$( '#numberProcessed' ).text( numberProcessed.toLocaleString() );
// Fade away when clicked
$done.one( 'click', function () {
$( 'body' ).addClass( 'map-active' );
$done.fadeOut();
activateControls();
} );
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 );
} );
}
}
}( jQuery, L, prettySize ) );