-
Notifications
You must be signed in to change notification settings - Fork 1
/
read-hgt7.html
277 lines (207 loc) · 8.82 KB
/
read-hgt7.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
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
<!doctype html>
<html lang=en >
<head>
<title>HGT ZIP USGS 1-Sec - read-hgt8.html</title>
<meta charset=utf-8 />
<meta name=viewport content='width=device-width,user-scalable=no,minimum-scale=1.0,maximum-scale=1.0' />
</head>
<body>
<script src=http://mrdoob.github.io/three.js/build/three.min.js ></script>
<script src=http://mrdoob.github.io/three.js/examples/js/controls/OrbitControls.js ></script>
<script src=http://mrdoob.github.io/three.js/examples/js/libs/stats.min.js ></script>
<script src=https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js ></script>
<script>
var scale = 10;
var viewSize = 3601;
var viewLeft = 0;
var viewTop = 0;
var fileName = 'N37W123.SRTMGL1.hgt.zip';
var sourceDir = 'http://jaanga.github.io/terrain-usgs-data-1second/hgt-zip/';
var stats, renderer, scene, camera, controls;
var geometry, vertices, material, mesh, axisHelper;
var canvasHeightMap, contextHeightMap, imageHeightMap;
var canvasTerrain, contextTerrain, png;
var terrain;
var m_max_elev = -99999;
var m_min_elev = 99999;
init();
animate();
function init() {
var css = document.head.appendChild( document.createElement('style') );
css.innerHTML ='body { font: 600 12pt monospace; margin: 0; overflow: hidden; }' ;
var info = document.body.appendChild( document.createElement( 'div' ) );
info.style.cssText = 'margin: 0 20px; max-width: 200px; position: absolute; ';
info.innerHTML = '<a href="" ><h3>' + document.title + '</h3></a>' +
'<p>Move the East/West & North/South sliders a bit. Then have fun with the Size and Scale sliders.</p>' +
'<div id=msg1 ></div>' +
'<p>East / West <input type=range id=inpLeft min=0 max=3345 value=' + viewLeft + ' step=1 onmousemove=viewLeft=parseFloat(inpLeft.value);updateTerrain(); /></p>' +
'<p>North / South <input type=range id=inpTop min=0 max=3345 value=' + viewTop + ' step=1 onmousemove=viewTop=parseFloat(inpTop.value);updateTerrain(); /></p>' +
'<p>View Size <input type=range id=inpSize min=100 max=3601 value=' + viewSize + ' step=1 onmousemove=viewSize=parseFloat(inpSize.value);updateTerrain(); /></p>' +
'<p>Vertical Scale <input type=range id=inpScale min=1 max=20 value=' + scale + ' step=1 onmousemove=scale=parseFloat(inpScale.value);updateTerrain(); /></p>' +
'';
stats = new Stats();
stats.domElement.style.cssText = 'position: absolute; right: 0; top: 0; z-index: 100; ';
document.body.appendChild( stats.domElement );
renderer = new THREE.WebGLRenderer( { alpha: 1, antialias: true, clearColor: 0xffffff } );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.set( 200, 200, 200 );
controls = new THREE.OrbitControls( camera, renderer.domElement );
scene = new THREE.Scene();
// axes
var axisHelper = new THREE.AxisHelper( 50 );
scene.add( axisHelper );
material = new THREE.MeshNormalMaterial( { shading: THREE.SmoothShading, side: 2 } );
geometry = new THREE.PlaneBufferGeometry( 256, 256, 255, 255 );
geometry.applyMatrix( new THREE.Matrix4().makeRotationX( 0.5 * Math.PI ) );
vertices = geometry.attributes.position.array;
canvasHeightMap = document.createElement( 'canvas' );
// document.body.appendChild( canvasHeightMap );
// canvasHeightMap.style.cssText = ' border: 5px solid red; margin: 20px; position: absolute; top: 0; z-index: -10; ';
canvasHeightMap.width = canvasHeightMap.height = 3601;
contextHeightMap = canvasHeightMap.getContext( '2d' );
imageHeightMap = contextHeightMap.createImageData( 3601, 3601 );
png = imageHeightMap.data;
canvasTerrain = document.createElement( 'canvas' );
canvasTerrain.width = canvasTerrain.height = 256;
contextTerrain = canvasTerrain.getContext( '2d' );
requestFile( sourceDir + fileName );
}
function requestFile( fileName ) {
console.log("Loading " + fileName)
startTime = Date.now();
xmlHttp = new XMLHttpRequest();
xmlHttp.open( 'GET', fileName, true );
xmlHttp.responseType = "arraybuffer";
xmlHttp.onreadystatechange = callbackRequestFile;
xmlHttp.send( null );
}
function callbackRequestFile() {
if ( xmlHttp.readyState !== 4 ) { return; }
var zip = new JSZip( xmlHttp.response );
var keys = Object.keys( zip.files );
var fileName = keys[ 0 ];
byteArray = zip.file( fileName ).asUint8Array();
parseHGT();
parent.msg1.innerHTML = msg1.innerHTML = 'file:' + fileName + ' bytes:' + byteArray.length;
}
function parseHGT() {
var elevation, prev_elev;
var zeros = 0;
var voids = 0;
prev_elev = 0;
for ( var i = 0, j = 0; i < byteArray.length; ) {
elevation = ( byteArray[ i++ ] << 8 ) + byteArray[ i++ ] ;
elevation = elevation < 32768 ? elevation: elevation - 65535;
if (elevation < -11000) {
// should be NONE if from http://e4ftl01.cr.usgs.gov/SRTM/SRTMGL1.003/2000.02.11/
voids++;
elevation = prev_elev;
}
png[ j++ ] = elevation & 0x0000ff;
png[ j++ ] = ( elevation & 0x00ff00 ) >> 8;
png[ j++ ] = 0; //( elevation & 0xff0000 ) >> 16;
png[ j++ ] = 255;
if (elevation > m_max_elev) {
m_max_elev = elevation;
}
if (elevation < m_min_elev) {
m_min_elev = elevation;
}
if (elevation == 0) {
zeros++;
}
prev_elev = elevation;
}
console.log("Loop " + byteArray.length + ", max " + m_max_elev + ", min " + m_min_elev +
", zeros " + zeros + ", voids " + voids);
contextHeightMap.putImageData( imageHeightMap, 0, 0 );
updateTerrain();
}
function updateTerrain() {
var max_elev = -99999;
var min_elev = 99999;
contextTerrain.clearRect( 0, 0, 256, 256 );
contextTerrain.drawImage( canvasHeightMap, viewLeft, viewTop, viewSize, viewSize, 0, 0, 256, 256 );
var rgb = contextTerrain.getImageData( 0, 0, 256, 256 ).data;
var elevation;
var scl = 0.01 * scale;
var last_elev = 0;
var below = 0;
var above = 0;
var loopcnt = 0;
var r,g,b,a;
for ( var i = 0, j = 1; i < rgb.length; i += 4, j += 3 ) {
r = rgb[ i ];
g = rgb[ i + 1 ];
//b = rgb[ i + 2 ]; // should be zero
//a = rgb[ i + 3 ]; // should be 255
elevation = r + (256 * g);
//if ( rgb[ i + 2 ] > 0 ) {
// elevation = 0;
//}
elevation = elevation < 32768 ? elevation : elevation - 65536; // 16777216;
// the data should come in already without error,
// BUT IT IS MODIFIED!!!!!!!!!!!!!
if (elevation < m_min_elev) {
elevation = last_elev;
below++;
} else if (elevation > m_max_elev) {
elevation = last_elev;
above++;
}
//elevation = elevation < -11000 || elevation > 3000 ? 0 : elevation ;
vertices[ j ] = scl * elevation ;
if (elevation > max_elev) {
max_elev = elevation;
}
if (elevation < min_elev) {
min_elev = elevation;
}
last_elev = elevation;
loopcnt++;
}
var step = rgb.length / loopcnt;
console.log("Loop " + loopcnt + ", len " + rgb.length + ", max " + max_elev +
", min " + min_elev + ", above " + above + ", below " + below +
", step " + step);
geometry.computeFaceNormals();
geometry.computeVertexNormals();
scene.remove( terrain )
terrain = new THREE.Mesh( geometry.clone(), material );
terrain.scale.z = -1;
scene.add( terrain );
// setLocationMapTarget();
}
function updateTerrain_OLD() {
contextTerrain.drawImage( canvasHeightMap, viewLeft, viewTop, viewSize, viewSize, 0, 0, 256, 256 );
var rgb = contextTerrain.getImageData( 0, 0, 256, 256 ).data;
var elevation;
var scl = 0.01 * scale;
for ( var i = 0, j = 1; i < rgb.length; i += 4, j += 3 ) {
elevation = rgb[ i ] + 256 * rgb[ i + 1 ]; // + 65536 * rgb[ i + 2 ] ;
elevation = elevation < 32768 ? elevation : elevation - 65536; // 16777216;
// uncomment the next line of code to see the errors corrected, but we should not need this correction
// the data should come in already without error
// elevation = elevation < -11000 ? 0 : elevation ;
// elevation = elevation < -11000 ? previousElevation : elevation ;
vertices[ j ] = scl * elevation ;
previousElevation = elevation
}
geometry.computeFaceNormals();
geometry.computeVertexNormals();
scene.remove( terrain )
terrain = new THREE.Mesh( geometry.clone(), material );
terrain.scale.z = -1;
scene.add( terrain );
}
function animate() {
requestAnimationFrame( animate );
controls.update();
stats.update();
renderer.render( scene, camera );
}
</script>
</body>
</html>