-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.js
131 lines (109 loc) · 3.03 KB
/
main.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
var work = require('webworkify');
var mapboxgl = require('mapbox-gl');
var concave = require('@turf/concave');
var geojsonhint = require('geojsonhint');
var geobuf = require('geobuf');
var Pbf = require('pbf');
var w = work(require('./worker.js'));
w.addEventListener('message', function(ev) {
var gj = geobuf.decode(new Pbf(ev.data[1]));
map.getSource('results').setData(gj);
});
w.addEventListener('error', function(err) {
console.log(err);
});
mapboxgl.accessToken = 'pk.eyJ1IjoibnBlaWhsIiwiYSI6InVmU21qeVUifQ.jwa9V6XsmccKsEHKh5QfmQ';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v8',
center: [-122.963104,48.569792],
zoom: 10
});
var emptyGeoJson = {
type: 'FeatureCollection',
features: []
};
var dataUrl = 'assets/hydrants.geojson';
map.on('style.load', function() {
console.log('style loaded');
map.addSource('points', {
type: 'geojson',
data: dataUrl
});
map.addSource('results', {
type: 'geojson',
data: emptyGeoJson
});
map.addLayer({
'id': 'points',
'type': 'circle',
'source': 'points'
});
map.addLayer({
'id': 'results',
'type': 'fill',
'source': 'results',
'paint': {
'fill-opacity': 0.6,
'fill-color': '#00FF00'
}
});
});
map.on('source.load', function() {
console.log('source loaded');
});
addButton('Turf Sync', 'turf-sync');
addButton('Turf Async','turf-async');
function addButton(name, id) {
var link = document.createElement('a');
link.href = '#';
link.id = id;
link.className = 'active';
link.textContent = name;
link.onclick = function(e) {
e.preventDefault();
e.stopPropagation();
switch (id) {
case 'turf-sync':
turfSync();
break;
case 'turf-async':
turfAsync();
break;
}
};
var menu = document.getElementById('menu');
menu.appendChild(link);
}
function turfSync() {
map.getSource('results').setData(emptyGeoJson);
var absUrl = window.location + dataUrl;
makeRequest(dataUrl);
}
function makeRequest(url) {
var req = new XMLHttpRequest();
req.addEventListener('load', validateData);
req.addEventListener('error', transferFailed);
req.open('GET', url);
req.send();
}
function validateData() {
var errors = geojsonhint.hint(this.responseText);
if (errors.len > 0) {
console.log('Errors', errors.join(', '));
} else {
turfIt(this.responseText);
}
}
function transferFailed() {
console.log('Error', this.responseText);
}
function turfIt(data) {
var results = concave(JSON.parse(data), 0.75, 'miles');
map.getSource('results').setData(results);
}
function turfAsync() {
map.getSource('results').setData(emptyGeoJson);
var absUrl = window.location + dataUrl;
w.postMessage([absUrl]);
}