forked from perliedman/elevation-service
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
49 lines (43 loc) · 1.62 KB
/
index.js
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
const {json, send} = require('micro')
const addElevation = require('geojson-elevation').addElevation
const TileSet = require('node-hgt').TileSet
const ImagicoElevationDownloader = require('node-hgt').ImagicoElevationDownloader
const tileDirectory = process.env.TILE_DIRECTORY || './data'
const noData = process.env.NO_DATA ? parseInt(process.env.NO_DATA) : null
const tileDownloader = process.env.TILE_DOWNLOADER === 'none'
? tileDownloader = undefined
: new ImagicoElevationDownloader(tileDirectory)
const maxPostSize = process.env.MAX_POST_SIZE || "500kb";
const tiles = new TileSet(tileDirectory, {downloader:tileDownloader});
module.exports = async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Headers', [
'X-Requested-With',
'Access-Control-Allow-Origin',
'X-HTTP-Method-Override',
'Content-Type',
'Authorization',
'Accept'
].join(','))
res.setHeader('Access-Control-Max-Age', '' + (60 * 60 * 24))
res.setHeader('Access-Control-Allow-Methods', 'POST,OPTIONS')
res.setHeader('Access-Control-Allow-Credentials', 'true')
if (req.method === 'OPTIONS') {
return send(res, 200)
}
if (req.method !== 'POST') {
return send(res, 405, {error: 'Only POST allowed'})
}
const geojson = await json(req, {limit: maxPostSize})
if (!geojson || Object.keys(geojson).length === 0) {
return send(res, 400, {error: 'invalid GeoJSON'})
}
return new Promise((resolve, reject) => {
addElevation(geojson, tiles, function(err, geojson) {
if (err) {
return send(res, 500, err)
}
resolve(geojson)
}, noData)
})
}