-
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathmap.view.js
More file actions
425 lines (396 loc) Β· 12.3 KB
/
map.view.js
File metadata and controls
425 lines (396 loc) Β· 12.3 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// create redux reducers
const reducers = (function createReducers(redux, keplerGl) {
// mount keplergl reducer
return redux.combineReducers({
keplerGl: keplerGl.keplerGlReducer.initialState({
uiState: {
readOnly: false,
currentModal: null
}
})
});
})(Redux, KeplerGl);
// create redux middlewares
const middleWares = (function createMiddlewares(keplerGl) {
return keplerGl.enhanceReduxMiddleware([
// add middlewares here
]);
})(KeplerGl);
// create redux enhancers
const enhancers = (function createEnhancers(redux, middles) {
return redux.applyMiddleware(...middles);
})(Redux, middleWares);
// create redux store
const store = (function createStore(redux, enhancers) {
const initialState = {};
return redux.createStore(reducers, initialState, redux.compose(enhancers));
})(Redux, enhancers);
// create keplergl app components
let KeplerElement = (function makeKeplerElement(react, keplerGl, mapboxToken) {
// create keplergl app
return function App() {
const rootElm = react.useRef(null);
// set window dimensions state
const windowState = react.useState({
width: window.innerWidth,
height: window.innerHeight
});
const windowDimension = windowState[0];
const setDimension = windowState[1];
// add window resize event handler
react.useEffect(function sideEffect() {
function handleResize() {
setDimension({ width: window.innerWidth, height: window.innerHeight });
}
window.addEventListener('resize', handleResize);
return function() {
window.removeEventListener('resize', handleResize);
};
}, []);
// create default map styles
const mapStyles = createMapStyles(mapboxToken);
// create keplergl app element
return react.createElement('div', {
style: {
position: 'absolute',
left: 0,
width: '100vw',
height: '100vh'
}
},
react.createElement(keplerGl.KeplerGl, {
mapboxApiAccessToken: mapboxToken,
id: 'map',
width: windowDimension.width,
height: windowDimension.height,
theme: THEME,
mapStyles: mapStyles
})
);
};
})(React, KeplerGl, MAPBOX_TOKEN);
// create keplergl map app
const app = (function createReactReduxProvider(react, reactRedux, KeplerElement) {
return react.createElement(reactRedux.Provider, {store},
react.createElement(KeplerElement, null)
);
})(React, ReactRedux, KeplerElement);
// render keplergl map app
(function render(react, reactDOM, app) {
reactDOM.render(app, document.getElementById('app'));
console.log('rendering map...');
})(React, ReactDOM, app);
// load map data and config from webview
let vscode, title, message,
dataUrlInput, saveFileTypeSelector,
map, mapStyle, mapConfig = {}, mapData = [],
dataUrl, dataFileName, dataType;
// initialize vs code api for messaging
vscode = acquireVsCodeApi();
// wire document load state changes
document.addEventListener('readystatechange', event => {
switch (document.readyState) {
case 'loading':
console.log(`loading: data url: ${dataUrl}`);
break;
case 'interactive':
// document loading finished, images and stylesheets are still loading ...
console.log('interactive!');
// get initial data info: document url, views, etc.
//vscode.postMessage({command: 'getDataInfo'});
break;
case "complete":
// web page is fully loaded
console.log('document.readystatechange complete: map.view:complete!');
break;
}
});
document.addEventListener('DOMContentLoaded', event => {
// initialize page elements
title = document.getElementById('title');
message = document.getElementById('message');
dataUrlInput = document.getElementById('data-url-input');
saveFileTypeSelector = document.getElementById('save-file-type-selector');
map = document.getElementById('map');
try {
// notify webview
vscode.postMessage({command: 'refresh'});
console.log('loading data ...');
} catch (error) {
// ignore: must be loaded outside of vscode webview
}
});
// map config/data update handler
window.addEventListener('message', event => {
switch (event.data.command) {
case 'showMessage':
showMessage(event.data.message);
break;
case 'refresh':
// clear loading map view message
showMessage('');
console.log('refreshing map view ...');
vscode.setState({ uri: event.data.uri });
// title.innerText = event.data.fileName;
dataFileName = event.data.fileName;
dataUrl = event.data.uri;
mapConfig = event.data.mapConfig;
mapStyle = event.data.mapStyle;
mapData = event.data.mapData;
dataType = event.data.dataType;
view(mapConfig, mapData, dataType);
break;
}
});
// map view update
function view(mapConfig, mapData, dataType) {
try {
// load data into keplergl map
initializeMap(KeplerGl, store, mapConfig, mapData, dataType);
} catch (error) {
console.error(`map.view:error: ${error}`);
showMessage(error.message);
}
}
function initializeMap(keplerGl, store, config, data, dataType) {
console.log(`initializeMap: loading ${dataType} data ...`);
let dataSets = {};
let dataConfig = config;
let tagData = false;
let geoDataFeatures;
const geoJsonFormat = new ol.format.GeoJSON();
switch (dataType) {
case '.csv':
data = KeplerGl.processCsvData(data);
tagData = true;
break;
case '.igc':
// read IGC data
const igcFormat = new ol.format.IGC();
const igcFeatures = igcFormat.readFeatures(data, {
// dataProjection: 'EPSG:4326',
// featureProjection: 'EPSG:3857'
});
geoDataFeatures = geoJsonFormat.writeFeatures(igcFeatures);
data = JSON.parse(geoDataFeatures);
data = KeplerGl.processGeojson(data);
tagData = true;
break;
case '.gml':
// read GML data
const gmlFormat = new ol.format.GML32();
const gmlFeatures = gmlFormat.readFeatures(data, {});
geoDataFeatures = geoJsonFormat.writeFeatures(gmlFeatures);
data = JSON.parse(geoDataFeatures);
data = KeplerGl.processGeojson(data);
tagData = true;
break;
case '.wkt':
// read WKT data
const wktFormat = new ol.format.WKT();
const wktFeatures = wktFormat.readFeatures(data, {});
geoDataFeatures = geoJsonFormat.writeFeatures(wktFeatures);
data = JSON.parse(geoDataFeatures);
data = KeplerGl.processGeojson(data);
tagData = true;
break;
case '.wkb':
// read WKB data
const wkbFormat = new ol.format.WKB();
const wkbFeatures = wkbFormat.readFeatures(data, {hex: true});
geoDataFeatures = geoJsonFormat.writeFeatures(wkbFeatures);
data = JSON.parse(geoDataFeatures);
data = KeplerGl.processGeojson(data);
tagData = true;
break;
case 'geo.json':
case '.geojson':
case '.gpx':
case '.kml':
case '.shp':
case '.fgb':
case '.topo.json':
case '.topojson':
// convert geojson data to keplergl geo data
data = KeplerGl.processGeojson(data);
tagData = true;
break;
case '.json':
const loadedData = keplerGl.KeplerGlSchema.load(data, config);
dataSets = loadedData.datasets;
dataConfig = loadedData.config;
break;
}
if (tagData) {
// create dataset with processed data and info tag
dataSets = {
data,
info: {
id: dataFileName
}
}
// console.log(JSON.stringify(dataSets));
}
// load map data
store.dispatch(keplerGl.addDataToMap({
datasets: dataSets,
config: dataConfig,
options: {
centerMap: true
}
}));
}
// save map data
function saveData() {
// get requested file type
const dataFileType = saveFileTypeSelector.value;
// get keplergl map info with visState, mapState, mapStyle and uiState
const mapInfo = store.getState().keplerGl.map;
// get corresponding map data
let mapData = {};
switch (dataFileType) {
case '.kgl.json':
// get keplergl map config
mapData = KeplerGl.KeplerGlSchema.getConfigToSave(mapInfo);
postMapData('saveData', mapData, dataFileType);
break;
case '.csv':
// TODO
break;
case '.json':
// get keplergl map data
mapData = KeplerGl.KeplerGlSchema.getDatasetToSave(mapInfo);
postMapData('saveData', mapData, dataFileType);
break;
case '.map.json':
mapData = {
"config": KeplerGl.KeplerGlSchema.getConfigToSave(mapInfo),
"datasets": KeplerGl.KeplerGlSchema.getDatasetToSave(mapInfo),
"info": {
"app": "kepler.gl",
"source": "GeoDataViewer",
"created_at": new Date(Date.now()).toUTCString()
}
}
postMapData('saveData', mapData, dataFileType);
break;
case '.geojson':
// TODO
break;
case '.kgl.html': // keplergl html
// create html map data
mapData = {
...KeplerGl.KeplerGlSchema.save(mapInfo),
mapboxApiAccessToken: MAPBOX_TOKEN,
mode: 'EDIT'
};
postMapData('saveData', mapData, dataFileType);
break;
case '.png':
const mapNode = document.getElementById('kepler-gl__map');
domtoimage.toPng(mapNode).then(dataUrl => {
postMapData('saveData', dataUrl, dataFileType);
})
//.catch(error => showMessage(error.message));
break;
}
} // end of saveData()
// posts map data for saving
function postMapData(command, mapData, dataFileType) {
vscode.postMessage({
command: command,
data: mapData,
fileType: dataFileType
});
}
// view raw map source code
function viewMapSource() {
vscode.postMessage({
command: 'loadView',
viewName: 'vscode.open',
uri: dataUrl
});
}
// launch map view for url
function loadMapViewFromUrl(e) {
if (!e) e = window.event;
const keyCode = e.keyCode || e.which;
if (keyCode == '13') { // enter key
const url = dataUrlInput.value;
vscode.postMessage({
command: 'loadView',
viewName: 'map.view',
uri: url
});
}
}
// open geo data file
function openGeoDataFile() {
vscode.postMessage({command: 'openGeoDataFile'});
}
// show map gallery
function showMapGallery() {
vscode.postMessage({
command: 'showMapGallery'
});
}
// show help page
function showHelp() {
vscode.postMessage({
command: 'loadView',
viewName: 'vscode.open',
uri: 'https://github.com/RandomFractals/geo-data-viewer#usage'
});
}
// show buy coffee page :)
function buyCoffee() {
vscode.postMessage({
command: 'loadView',
viewName: 'vscode.open',
uri: 'https://ko-fi.com/datapixy'
});
}
function showMessage(text) {
message.innerText = text;
}
// creates deafult map styles
function createMapStyles(mapboxToken) {
const defaultLayerGroups = [];
return [
{
id: 'dark_streets',
label: 'Dark Streets',
url: 'mapbox://styles/mapbox/dark-v10',
icon: `https://api.mapbox.com/styles/v1/mapbox/dark-v10/static/-87.623177,41.881832,9.19,0,0/400x300?access_token=${mapboxToken}&logo=false&attribution=false`,
layerGroups: defaultLayerGroups
},
{
id: 'light_streets',
label: 'Light Streets',
url: 'mapbox://styles/mapbox/light-v10',
icon: `https://api.mapbox.com/styles/v1/mapbox/light-v10/static/-87.623177,41.881832,9.19,0,0/400x300?access_token=${mapboxToken}&logo=false&attribution=false`,
layerGroups: defaultLayerGroups
},
/* { // note: looks same as outdoors
id: 'streets',
label: 'Streets',
url: 'mapbox://styles/mapbox/streets-v10',
icon: `https://api.mapbox.com/styles/v1/mapbox/streets-v10/static/-87.623177,41.881832,9.19,0,0/400x300?access_token=${mapboxToken}&logo=false&attribution=false`,
layerGroups: defaultLayerGroups
}, */
{
id: 'outdoors',
label: 'Outdoors',
url: 'mapbox://styles/mapbox/outdoors-v10',
icon: `https://api.mapbox.com/styles/v1/mapbox/outdoors-v10/static/-87.623177,41.881832,9.19,0,0/400x300?access_token=${mapboxToken}&logo=false&attribution=false`,
layerGroups: defaultLayerGroups
},
{
id: 'satellite',
label: 'Satellite',
url: 'mapbox://styles/mapbox/satellite-v9',
icon: `https://api.mapbox.com/styles/v1/mapbox/satellite-v9/static/-87.623177,41.881832,9.19,0,0/400x300?access_token=${mapboxToken}&logo=false&attribution=false`,
layerGroups: defaultLayerGroups
}
];
}