-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathserver.js
217 lines (176 loc) · 6.58 KB
/
server.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
var express = require('express'),
directory = require('serve-index'),
polyline = require('@mapbox/polyline'),
search = require('../api/search'),
extract = require('../api/extract'),
street = require('../api/street'),
near = require('../api/near'),
pretty = require('../lib/pretty'),
analyze = require('../lib/analyze'),
project = require('../lib/project'),
proximity = require('../lib/proximity');
const morgan = require( 'morgan' );
const logger = require('pelias-logger').get('interpolation');
const through = require( 'through2' );
const _ = require('lodash');
// optionally override port using env var
var PORT = process.env.PORT || 3000;
// help text
if( process.argv.length !== 4 ){
console.error('invalid args.');
console.error('usage: {addressdb} {streetdb}');
console.error('example: node cmd/server address.db street.db');
process.exit(1);
}
var app = express();
app.use(log());
var conn = {
search: search( process.argv[2], process.argv[3] ),
extract: extract( process.argv[2], process.argv[3] ),
street: street( process.argv[3] ),
near: near( process.argv[3] )
};
function log() {
morgan.token('url', (req, res) => {
// if there's a DNT header, just return '/' as the URL
if (['DNT', 'dnt', 'do_not_track'].some(header => _.has(req.headers, header))) {
return _.get(req, 'route.path');
} else {
return req.originalUrl;
}
});
// 'short' format includes response time but leaves out date
return morgan('short', {
stream: through( function write( ln, _, next ){
logger.info( ln.toString().trim() );
next();
})
});
}
// search with geojson view
// eg: http://localhost:3000/search/geojson?lat=-41.288788&lon=174.766843&number=16&street=glasgow%20street
app.get('/search/geojson', function( req, res ){
var point = { lat: req.query.lat, lon: req.query.lon };
var number = req.query.number;
var street = req.query.street;
conn.search.query( point, number, street, function( err, point ){
if( err ){ return res.status(400).json( err ); }
if( !point ){ return res.status(200).json({}); }
res.json( pretty.geojson.point( point, point.lon, point.lat ) );
});
});
// search with table view
// eg: http://localhost:3000/search/table?lat=-41.288788&lon=174.766843&number=16&street=glasgow%20street
app.get('/search/table', function( req, res ){
var point = { lat: req.query.lat, lon: req.query.lon };
var number = req.query.number;
var street = req.query.street;
conn.search.query( point, number, street, function( err, point ){
if( err ){ return res.status(400).json( err ); }
if( !point ){ return res.status(200).send(''); }
res.setHeader('Content-Type', 'text/html');
res.send( pretty.htmltable([ point ]) );
});
});
// extract with geojson view
// eg: http://localhost:3000/extract/geojson?lat=-41.288788&lon=174.766843&names=glasgow%20street
app.get('/extract/geojson', function( req, res ){
var point = { lat: req.query.lat, lon: req.query.lon };
var names = req.query.names ? req.query.names.split(',') : [];
conn.extract.query( point, names, function( err, data ){
if( err ){ return res.status(400).json( err ); }
if( !data ){ return res.status(200).json({}); }
res.json( pretty.geojson( data ) );
});
});
// extract with table view
// eg: http://localhost:3000/extract/table?lat=-41.288788&lon=174.766843&names=glasgow%20street
app.get('/extract/table', function( req, res ){
var point = { lat: req.query.lat, lon: req.query.lon };
var names = req.query.names ? req.query.names.split(',') : [];
conn.extract.query( point, names, function( err, data ){
if( err ){ return res.status(400).send( err ); }
if( !data ){ return res.status(200).send(''); }
if( !data.length ){ return res.status(200).send(''); }
res.setHeader('Content-Type', 'text/html');
res.send( pretty.htmltable( data ) );
});
});
// get streets near point, ordered by proximity to point ASC
// eg: http://localhost:3000/street/near/geojson
app.get('/street/near/geojson', function( req, res ){
var point = { lat: req.query.lat, lon: req.query.lon };
var max_distance = req.query.dist || 0.01;
conn.near.query( point, function( err, ordered ){
if( err ){ return res.status(400).json( err ); }
if( !ordered || !ordered.length ){ return res.status(200).json({}); }
// remove points over a certain distance (in degrees)
ordered = ordered.filter( function( o ){
return o.proj.dist <= max_distance;
});
var geojson = {
'type': 'FeatureCollection',
'features': ordered.map( function( o ){
return {
'type': 'Feature',
'properties': {
'id': o.street.id,
'name': Array.isArray( o.street.name ) ? o.street.name[0] : o.street.name,
'polyline': o.street.line,
'distance': ( Math.floor(( o.proj.dist || 0 ) * 1000000 ) / 1000000 )
},
'geometry': {
'type': 'LineString',
'coordinates': o.street.coordinates
}
};
})
};
res.json( geojson );
});
});
// get street geometry as geojson
// eg: http://localhost:3000/street/1/geojson
app.get('/street/:id/geojson', function( req, res ){
conn.street.query( req.params.id.split(','), function( err, rows ){
if( err ){ return res.status(400).json( err ); }
if( !rows || !rows.length ){ return res.status(200).json({}); }
// dedupe
// @todo: debug and improve this by returning less results
// @copy-pasted
var deduped = [];
rows = rows.filter( function( row ){
if( deduped[ row.id ] ){ return false; }
deduped[ row.id ] = true;
return true;
});
var geojson = {
'type': 'FeatureCollection',
'features': rows.map( function( row ){
return {
'type': 'Feature',
'properties': {
'id': row.id,
'name': Array.isArray( row.name ) ? row.name[0] : row.name,
'polyline': row.line
},
'geometry': polyline.toGeoJSON( row.line, 6 )
};
})
};
res.json( geojson );
});
});
// root page
app.get('/', function( req, res ){ res.redirect('/demo'); });
// serve the demo app
app.use('/demo', express.static('demo'));
// serve the builds dir (for downloads)
// app.get('/data', function( req, res ){ res.redirect('/builds'); });
// app.use('/builds', express.static('/data/builds'));
// app.use('/builds', directory('/data/builds', { hidden: false, icons: false, view: 'details' }));
app.listen( PORT, function() {
// force loading of libpostal
analyze.street( 'test street' );
console.log( 'server listening on port', PORT );
});