-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathpreview.html
207 lines (188 loc) · 6.41 KB
/
preview.html
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
<!doctype html>
<html>
<head>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.18.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.18.0/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
ul { list-style-type: none; width: 30%; }
li { margin-bottom: 20px; }
li img { width: 49%; }
#map { position:fixed; top:0; bottom:0; right: 0; width: 65%; }
</style>
</head>
<body>
<div id="map"></div>
<script>
var query = {}
window.location.search.substring(1).split('&').forEach(function (p) {
p = p.split('=')
query[p[0]] = p[1]
})
var prefix = query.prefix || 'data'
if (!prefix.endsWith('/')) { prefix = prefix + '/' }
query = Object.assign({
images: prefix + 'images/',
labels: prefix + 'labels/color/',
sample: prefix + 'sample.txt',
labelRatio: 0
}, query)
// map
mapboxgl.accessToken = query.accessToken
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/satellite-v8',
center: [-74.50, 40],
zoom: 9
})
.on('load', function () {
map.addSource('tile', { type: 'geojson', data: { type: 'FeatureCollection', features: [] } })
map.addLayer({
'id': 'tile',
'source': 'tile',
'type': 'line',
'paint': {
'line-color': 'red',
'line-width': 4
}
})
})
// grab tile list and show images + labels side by side
var tiles = []
fetch(query.sample)
.then(function (response) { return response.text() })
.then(function (text) {
var lines = text.trim().split('\n')
.map(function (line) {
if (line.startsWith('#')) { return null }
line = line.split(' ')
var tile = line.slice(1, 4).map(Number)
tiles.push(tile)
var labelCounts = line.slice(4)
if (query.labelRatio >= 0 && labelCounts.length) {
labelCounts = labelCounts.map(Number)
r = 1 - labelCounts[labelCounts.length - 1] / labelCounts.reduce((a, b) => a + b, 0)
if (r <= query.labelRatio) { return null }
}
var lonlat = [tile2long(tile[1], tile[0]), tile2lat(tile[2], tile[0])].map(function (c) { return c.toFixed(4) })
return `
<li data-tile="${tile.join(',')}" data-label-counts="${labelCounts}">
<img src="blank.gif" class="lazy" data-src="${query.images}${tile.join('-')}.jpg">
<img src="blank.gif" class="lazy" data-src="${query.labels}${tile.join('-')}.png">
<div>${tile} (${lonlat.reverse()})</div>
<div>${labelCounts}</div>
</li>
`
})
.filter(Boolean)
document.querySelector('ul').innerHTML += lines.join('\n')
initLazyLoad()
var items = document.querySelectorAll('li')
for (var i = 0; i < items.length; i++) {
var li = items[i]
li.addEventListener('click', onClick)
li.addEventListener('mouseover', onHover);
}
var boxes = {
type: 'FeatureCollection', features: tiles.map(tileToFeature)
}
map.addSource('all-tiles', { type: 'geojson', data: boxes })
map.addLayer({
'id': 'all-tiles',
'source': 'all-tiles',
'type': 'fill',
'paint': {
'fill-color': 'red',
'fill-opacity': 0.25
}
})
})
function onClick (e) {
var tile = e.currentTarget.dataset.tile.split(',').map(Number)
var [w, s, e, n] = tileToBBOX(tile)
var z = +tile[0]
map.jumpTo({center: [ (w + e) / 2, (s + n) / 2 ], zoom: z - 1, speed: 2})
map.getSource('tile').setData(tileToFeature(tile))
}
function onHover (e) {
console.log(e.currentTarget.dataset)
}
// modified from https://github.com/mapbox/tilebelt/blob/master/index.js
function tile2long(x,z) { return (x/Math.pow(2,z)*360-180); }
function tile2lat(y,z) {
var n=Math.PI-2*Math.PI*y/Math.pow(2,z);
return (180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n))));
}
function tileToBBOX(tile) {
var e = tile2long(tile[1] + 1, tile[0]);
var w = tile2long(tile[1], tile[0]);
var s = tile2lat(tile[2] + 1, tile[0]);
var n = tile2lat(tile[2], tile[0]);
return [w, s, e, n];
}
function tileToFeature(tile) {
var [w, s, e, n] = tileToBBOX(tile)
return {
type: 'Feature',
properties: {},
geometry: {
type: 'Polygon',
coordinates: [
[
[w, n],
[w, s],
[e, s],
[e, n],
[w, n]
]
]
}
}
}
/* copied from:
* lazyload.js (c) Lorenzo Giuliani
* MIT License (http://www.opensource.org/licenses/mit-license.html)
*
* expects a list of:
* `<img src="blank.gif" data-src="my_image.png" width="600" height="400" class="lazy">`
*/
function loadImage (el, fn) {
var img = new Image()
, src = el.getAttribute('data-src');
img.onload = function() {
if (!! el.parent)
el.parent.replaceChild(img, el)
else
el.src = src;
fn? fn() : null;
}
img.src = src;
}
function elementInViewport(el) {
var rect = el.getBoundingClientRect()
return (
rect.top >= 0
&& rect.left >= 0
&& rect.top <= (window.innerHeight || document.documentElement.clientHeight)
)
}
function initLazyLoad () {
var query = document.querySelectorAll('img.lazy')
var processScroll = function(){
for (var i = 0; i < images.length; i++) {
if (elementInViewport(images[i])) {
loadImage(images[i], function () {
images.splice(i, i);
});
}
};
};
var images = Array.prototype.slice.call(query)
processScroll();
window.addEventListener('scroll',processScroll);
}
</script>
</body>
<ul></ul>
</html>