forked from mcwhittemore/mapbox-elevation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bc0480b
Showing
5 changed files
with
472 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
}); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
Oops, something went wrong.