Skip to content

Instantly share code, notes, and snippets.

@bmount
Last active December 11, 2015 07:59
Show Gist options
  • Select an option

  • Save bmount/4570368 to your computer and use it in GitHub Desktop.

Select an option

Save bmount/4570368 to your computer and use it in GitHub Desktop.
piece of srtm San Francisco area in grayscale png
checked in raw
<!doctype html>
<meta charset="utf-8">
<body>
<script>
var topo, w, h, context, summary;
function setup () {
topo = new Image;
topo.src = "simple_sf.png";
topo.onload = render;
document.body.appendChild(topo);
}
function render () {
h = topo.height;
w = topo.width;
var canvas = document.createElement("canvas");
// optional
// document.body.appendChild(canvas);
canvas.width = w;
canvas.height = h;
context = canvas.getContext('2d');
context.drawImage(topo, 0, 0);
calculate();
}
function calculate () {
// compare elevation of west half to east half
// counting only px that are on land and >= 1m in
// elevation. no scaling: values are truncated
// to 0-255m to fit in uint8 (affects a few weird
// below sea level measurements and the tippy top of
// the twin peaks)
// I think you could call getImageData on every <i, j> but my try hung
var /* radar */ waves = context.getImageData(0,0,w,h);
var totalEast = 0,
totalWest = 0,
validEast = 0,
validWest = 0,
radar;
for (var j = 0; j < h; j++) {
for (var i = 0; i < w; i++) {
// row column
radar = waves.data[ (j * w * 4) + (i * 4) ];
if (radar) {
if (i < w/2) {
totalWest += radar;
validWest++;
}
else {
totalEast += radar;
validEast++;
}
}
}
}
summary = [totalWest/validWest, totalEast/validEast];
reportResult();
}
function reportResult () {
var result = document.createElement("p")
result.innerHTML = "Average west elevation: " + summary[0].toFixed(1) + " m" +
"<br> Average east elevation: " + summary[1].toFixed(1) + " m";
document.body.appendChild(result);
}
document.addEventListener("DOMContentLoaded", setup);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment