-
Notifications
You must be signed in to change notification settings - Fork 1
/
leaflet.html
241 lines (205 loc) · 7.98 KB
/
leaflet.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>WMS is dead, long live WASM</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<!-- Load Leaflet from CDN-->
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet-src.js"></script>
<!-- load esri leaflet and its geocoder for address/place search -->
<script src="https://unpkg.com/[email protected]/dist/esri-leaflet.js"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/esri-leaflet-geocoder.css">
<script src="https://unpkg.com/[email protected]/dist/esri-leaflet-geocoder.js"></script>
<!-- slider library-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/8.2.1/nouislider.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/noUiSlider/8.2.1/nouislider.min.js"></script>
<!--load the lerc decoder -->
<script src="https://unpkg.com/[email protected]/LercDecode.js"></script>
<!--This is the wasm lerc1 decoder-->
<script src="lerc1dec.js"></script>
<!--This should be moved to lerc1dec.js-->
<script>
var Lerc = Module;
Lerc.onRuntimeInitialized = () => {
// void *data, size_t sz, --> json image : { width, height, needs_ndv, precision | message}
Lerc.getwh = this.cwrap('getwh', 'number', ['number', 'number']);
// void *data, size_t sz, ndv, outbuffer, outsize, message -> success
Lerc.lercDecode = this.cwrap('decode', 'number', [
'number', 'number', 'number',
'number', 'number', 'number'
]);
// float *src, size_t sz(257*257*4), min, max, int *dst -> success
Lerc.topixel8 = this.cwrap('topixel8', 'number',
['number', 'number', 'number', 'number', 'number']);
// float *src, size_t sz(257*257*4), pixel size, sun angle, int *dst -> success
Lerc.hillshade = this.cwrap('hillshade', 'number',
['number', 'number', 'number', 'number', 'number']);
// apply the RGBA palette to lower byte of pixels, in place
// i32 palette[256], i32 *pixels, i32 numpixels
Lerc.applypalette = this.cwrap('applypalette', 'number'
['number', 'number', 'number']);
// get the Lerc blob info -> object
Lerc.getInfo = function (data) {
if (data.length > 100)
data = data.slice(0, 100)
let workBuffer = this._malloc(data.length);
this.writeArrayToMemory(data, workBuffer);
let cresult = this.getwh(workBuffer, data.length);
if (cresult == 0) // Error, not valid lerc1
return null;
let response = this.UTF8ToString(cresult);
this._free(cresult);
this._free(workBuffer);
return JSON.parse(response)
};
Lerc.wasm_decode = function(data, ndv) {
let image = this.getInfo(data);
if (!image) {
console.log("Invalid lerc1 data");
return null;
}
raw = this._malloc(data.length);
this.writeArrayToMemory(data, raw);
outsize = image.width * image.height * 4; // Always float32
bufptr = this._malloc(outsize);
message = this._malloc(1024);
decoded = this.lercDecode(raw, data.length, ndv, bufptr, outsize, message);
if (decoded) {
// A view, remember to call clean when done with it
image.data = new Float32Array(this.HEAP8.buffer, bufptr, outsize / 4);
image.bufptr = bufptr;
image.clean = function() {
delete this.data;
Lerc._free(bufptr);
delete image.bufptr;
}
} else {
image.error = this.UTF8ToString(message);
this._free(bufptr);
console.log(image.error);
};
this._free(message);
this._free(raw);
if (image.error)
console.log(image.error);
return image;
}
// Wrapper for wasm_decode, returns array copy and cleans up wasm heap
Lerc.decode = function(data, ndv) {
image = this.wasm_decode(data, ndv);
values = new Float32Array(image.data); // copy
image.clean(); // Free heap array
image.data = values; // replace it with copy
delete image.clean;
return image;
}
L.esri.Geocoding.geosearch().addTo(map);
lercElevation.addTo(map);
}
</script>
<!-- load our plugin -->
<script src="LercLayer.js"></script>
<style>
body {
margin:0;
padding:0;
}
#map {
position: absolute;
top:0;
bottom:0;
right:0;left:0;
}
#info-pane {
position: absolute;
top: 10px;
right: 10px;
min-width: 200px;
z-index: 500;
padding: 1em;
background: white;
}
.noUi-connect {
background: #ccc;
}
</style>
</head>
<body>
<div id="map"></div>
<div id="info-pane" class="leaflet-bar">
<div id="sungle">
<input type="range" min="40" max="140" value="45" class="slider" id="sunAngle" label="Sun Elevation">
</div>
<label>Sun Elevation Angle</label>
<hr>
<div id="slider"></div>
<br><label id="min">0 meters</label>
<br><label id="max">4000 meters</label>
<hr>
<div id="pixel-value">Esri Tiled Elevation Service</div>
</div>
<script>
// create a UI slider for the end user to toggle the pixel range to display
var slider = document.getElementById('slider');
noUiSlider.create(slider, {
start: [0, 4000],
step: 100,
connect: true,
range: { 'min': -8000, 'max': 8000 }
});
// When the slider value changes, update the input and span
slider.noUiSlider.on('set', function (values, handle) {
document.getElementById('min').innerHTML = parseInt(values[0], 10) + ' meters';
document.getElementById('max').innerHTML = parseInt(values[1], 10) + ' meters';
// redraw the tiles without fetching the from the server
for (var key in lercElevation._tiles) {
lercElevation.draw(lercElevation._tiles[key].el);
}
});
let sungle = document.getElementById('sunAngle');
sungle.oninput = function() {
lercElevation.sunAngle = +this.value;
for (var key in lercElevation._tiles) {
lercElevation.draw(lercElevation._tiles[key].el);
}
};
let southWest = L.latLng(-90, -179);
let northEast = L.latLng(90, 179);
let worldBounds = L.latLngBounds(southWest, northEast);
// set up the map
var map = L.map('map', {
noWrap: true,
minZoom: 3,
maxBounds: worldBounds
}).setView([30, 45], 3);
let lercElevation = new LercLayer({
noWrap: true,
attribution: 'USGS, <a href="https://github.com/Esri/lerc">LERC</a>',
tileSize: 256,
});
map.on('mousemove', function (e) {
// the gather the x/y and z of the tile url
var layerPoint = map.project(e.latlng).floor();
var tilePoint = layerPoint.divideBy(256).floor();
tilePoint.z = map.getZoom();
// the tile data block
var block = lercElevation._tiles[tilePoint.x + ':' +
tilePoint.y + ':' + tilePoint.z].el.decodedPixels;
// Read the data value from the block if it exists
if (block) {
var pointInTile = layerPoint.subtract(tilePoint.multiplyBy(256));
document.getElementById('pixel-value').innerHTML = "current elevation: "
+ Math.round(block.data[pointInTile.y * block.width + pointInTile.x])
+ " meters";
} else {
document.getElementById('pixel-value').innerHTML = "Elevation: undefined";
}
});
lercElevation.palette = lercElevation.buildPalette({
index: [0, 64, 128, 192, 255],
values: [0xFFFF0000, 0xff404040, 0xff40f040, 0xff4040c0, 0xffffffff],
});
</script>
</body>
</html>