Skip to content

Commit

Permalink
first working version
Browse files Browse the repository at this point in the history
  • Loading branch information
mcwhittemore committed Apr 29, 2017
0 parents commit bc0480b
Show file tree
Hide file tree
Showing 5 changed files with 472 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Mapbox Elevation

Quickly get the elevation of any point in the world from Mapbox's terrian-rgb datasource.

This module requires a Mapbox account to use. [Get a free one here](https://www.mapbox.com/signup/).

The result is in meters.

For details on how this works, please see [this blog post](https://www.mapbox.com/blog/terrain-rgb/).

## Usage

`npm install mapbox-elevation`

```js
var MapboxElevation = require('mapbox-elevation');
var getElevation('YOUR-MAPBOX-TOKEN');

getElevation([86.925313, 27.988730], function(err, elevation) {
console.log('elevation at the summit of mt everest', elevation);
});
```
30 changes: 30 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var tilebelt = require('@mapbox/tilebelt');
var pkg = require('./package.json');
var getPixels = require('get-pixels');

module.exports = function(tk) {
return function(p, cb) {
var tf = tilebelt.pointToTileFraction(p[0], p[1], 20);
var tile = tf.map(Math.floor);
var domain = 'https://api.mapbox.com/v4/';
var source = `mapbox.terrain-rgb/${tile[2]}/${tile[0]}/${tile[1]}.pngraw`;
var url = `${domain}${source}?access_token=${tk}`;
getPixels(url, function(err, pixels) {
if (err) return cb(err);
var xp = tf[0] - tile[0];
var yp = tf[1] - tile[1];
var x = Math.floor(xp*pixels.shape[0]);
var y = Math.floor(yp*pixels.shape[1]);

var R = pixels.get(x, y, 0);
var G = pixels.get(x, y, 1);
var B = pixels.get(x, y, 2);

var height = -10000 + ((R * 256 * 256 + G * 256 + B) * 0.1);

return cb(null, height);

});
}
}

26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"dependencies": {
"@mapbox/tilebelt": "^1.0.1",
"get-pixels": "^3.3.0"
},
"name": "mapbox-elevation",
"version": "1.0.0",
"description": "qucikly get the elevation of any lng,lat in the world",
"main": "index.js",
"directories": {
"test": "test"
},
"devDependencies": {},
"scripts": {
"test": "node test/test.js"
},
"keywords": [
"mapbox",
"elevation",
"terrain",
"height",
"depth"
],
"author": "Matthew Chase Whittemore",
"license": "ISC"
}
19 changes: 19 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var MapboxElevation = require('..');

var getElevation = MapboxElevation(process.env.MAPBOX_PUBLIC_TOKEN);

getElevation([0,0], function(err, elevation) {
if (err) throw err;
console.log('elevation at null island', elevation);
});

getElevation([-71.3030925, 44.270171], function(err, elevation) {
if (err) throw err;
console.log('elevation in mt washington nh', elevation);
});

getElevation([86.925313, 27.988730], function(err, elevation) {
if (err) throw err;
console.log('elevation at the peak of everest', elevation);
});

Loading

0 comments on commit bc0480b

Please sign in to comment.