1 | var qs = require('query-string')
|
2 | var toOsm = require('obj2osm')
|
3 | var accepts = require('accepts')
|
4 |
|
5 | var OsmJSONStream = require('../lib/util').OsmJSONStream
|
6 | var errors = require('../errors')
|
7 |
|
8 | module.exports = function (req, res, api, params, next) {
|
9 | var accept = accepts(req)
|
10 | var query = qs.parse(qs.extract(req.url))
|
11 | if (!query.bbox) {
|
12 | return next(new errors.MissingBbox())
|
13 | }
|
14 | var bbox = query.bbox.split(',').map(Number)
|
15 | if (!isValidBbox(bbox)) {
|
16 | return next(new errors.InvalidBbox())
|
17 | }
|
18 | var toOsmOptions = {
|
19 | bounds: {minlon: bbox[0], minlat: bbox[1], maxlon: bbox[2], maxlat: bbox[3]}
|
20 | }
|
21 |
|
22 | var source = api.getMap(bbox, {order: 'type', forks: query.forks})
|
23 |
|
24 | var formatTransform
|
25 | switch (accept.types(['xml', 'json'])) {
|
26 | case 'json':
|
27 | formatTransform = OsmJSONStream(toOsmOptions)
|
28 | res.setHeader('content-type', 'application/json; charset=utf-8')
|
29 | break
|
30 | case 'xml':
|
31 | default:
|
32 | formatTransform = toOsm(toOsmOptions)
|
33 | res.setHeader('content-disposition', 'attachment; filename="map.osm"')
|
34 | res.setHeader('content-type', 'text/xml; charset=utf-8')
|
35 | break
|
36 | }
|
37 | formatTransform.on('error', next)
|
38 | source.pipe(formatTransform).pipe(res)
|
39 | }
|
40 |
|
41 | function isValidBbox (bbox) {
|
42 | return bbox.length === 4 &&
|
43 | bbox[0] >= -180 && bbox[0] <= 180 &&
|
44 | bbox[2] >= -180 && bbox[2] <= 180 &&
|
45 | bbox[1] >= -90 && bbox[1] <= 90 &&
|
46 | bbox[3] >= -90 && bbox[3] <= 90
|
47 | }
|