1
1
#!/usr/bin/env node
2
2
import Fastify from 'fastify'
3
+ import { option } from 'yargs' ;
3
4
import yargs from 'yargs/yargs' ;
4
5
5
6
const OSRM = require ( '../lib/index.js' )
@@ -80,21 +81,30 @@ async function handleNearest(osrm: any, coordinates: [number, number][], query:
80
81
81
82
82
83
async function handleTable ( osrm : any , coordinates : [ number , number ] [ ] , query : any ) : Promise < any > {
83
- const options = {
84
+ const options : any = {
84
85
coordinates : coordinates
85
86
} ;
87
+ handleCommonParams ( query , options ) ;
88
+ if ( query . scale_factor ) {
89
+ options . scale_factor = parseFloat ( query . scale_factor ) ;
90
+ }
91
+ if ( query . fallback_coordinate ) {
92
+ options . fallback_coordinate = query . fallback_coordinate ;
93
+ }
94
+ if ( query . fallback_speed ) {
95
+ options . fallback_speed = parseFloat ( query . fallback_speed ) ;
96
+ }
97
+ if ( query . sources ) {
98
+ options . sources = query . sources . split ( ';' ) . map ( ( t : string ) => parseInt ( t ) ) ;
99
+ }
100
+ if ( query . destinations ) {
101
+ options . destinations = query . destinations . split ( ';' ) . map ( ( t : string ) => parseInt ( t ) ) ;
102
+ }
86
103
const res = await table ( osrm , options ) ;
87
104
return res ;
88
105
}
89
106
90
- async function handleMatch ( osrm : any , coordinates : [ number , number ] [ ] , query : any ) : Promise < any > {
91
-
92
-
93
- const options : any = {
94
- coordinates : coordinates ,
95
-
96
- } ;
97
-
107
+ function handleCommonParams ( query : any , options : any ) {
98
108
if ( query . overview ) {
99
109
options . overview = query . overview ;
100
110
}
@@ -111,22 +121,11 @@ async function handleMatch(osrm: any, coordinates: [number, number][], query: an
111
121
options . waypoints = query . waypoints . split ( ';' ) . map ( ( t : string ) => parseInt ( t ) ) ;
112
122
}
113
123
114
- if ( query . tidy ) {
115
- options . tidy = query . tidy == 'true' ? true : false ;
116
- }
117
-
118
- if ( query . gaps ) {
119
- options . gaps = query . gaps ;
120
- }
121
-
122
124
if ( query . steps ) {
123
125
options . steps = query . steps === 'true' ? true : false ;
124
126
}
125
127
126
- if ( query . generate_hints ) {
127
- options . generate_hints = query . generate_hints == 'true' ? true : false ;
128
- }
129
-
128
+
130
129
if ( query . annotations ) {
131
130
let annotations ;
132
131
if ( query . annotations === 'true' ) {
@@ -136,26 +135,100 @@ async function handleMatch(osrm: any, coordinates: [number, number][], query: an
136
135
}
137
136
options . annotations = annotations ;
138
137
}
138
+
139
+ if ( query . exclude ) {
140
+ options . exclude = query . exclude . split ( ',' ) ;
141
+ }
142
+
143
+ if ( query . snapping ) {
144
+ options . snapping = query . snapping ;
145
+ }
146
+
147
+ if ( query . radiuses ) {
148
+ options . radiuses = query . radiuses . split ( ';' ) . map ( ( t : string ) => {
149
+ if ( t === 'unlimited' ) {
150
+ return null ;
151
+ }
152
+ return parseFloat ( t ) ;
153
+ } ) ;
154
+ }
155
+
156
+ if ( query . bearings ) {
157
+ options . bearings = query . bearings . split ( ';' ) . map ( ( bearingWithRange : string ) => {
158
+ if ( bearingWithRange === '' ) {
159
+ return null ;
160
+ }
161
+ return bearingWithRange . split ( ',' ) . map ( ( t : string ) => parseFloat ( t ) ) ;
162
+ } ) ;
163
+ }
164
+
165
+ if ( query . hints ) {
166
+ options . hints = query . hints . split ( ';' ) ;
167
+ }
168
+
169
+ if ( query . generate_hints ) {
170
+ options . generate_hints = query . generate_hints == 'true' ? true : false ;
171
+ }
172
+
173
+ if ( query . skip_waypoints ) {
174
+ options . skip_waypoints = query . skip_waypoints === 'true' ? true : false ;
175
+ }
176
+ }
177
+
178
+ async function handleMatch ( osrm : any , coordinates : [ number , number ] [ ] , query : any ) : Promise < any > {
179
+
180
+
181
+ const options : any = {
182
+ coordinates : coordinates ,
183
+
184
+ } ;
185
+
186
+ handleCommonParams ( query , options ) ;
187
+ if ( query . gaps ) {
188
+ options . gaps = query . gaps ;
189
+ }
190
+
191
+ if ( query . tidy ) {
192
+ options . tidy = query . tidy === 'true' ? true : false ;
193
+ }
194
+
195
+
196
+
197
+
139
198
//throw new Error(`not implemented ${JSON.stringify(options)}`);
140
199
const res = await match ( osrm , options ) ;
141
200
return res ;
142
201
}
143
202
144
203
async function handleTrip ( osrm : any , coordinates : [ number , number ] [ ] , query : any ) : Promise < any > {
145
- const options = {
146
- coordinates : coordinates ,
147
- steps : query . steps === 'true' ? true : false ,
204
+ const options : any = {
205
+ coordinates : coordinates
148
206
} ;
207
+ handleCommonParams ( query , options ) ;
208
+ if ( query . roundtrip ) {
209
+ options . roundtrip = query . roundtrip === 'true' ? true : false ;
210
+ }
211
+ if ( query . source ) {
212
+ options . source = query . source ;
213
+ }
214
+ if ( query . destination ) {
215
+ options . destination = query . destination ;
216
+ }
149
217
const res = await trip ( osrm , options ) ;
150
218
return res ;
151
219
}
152
220
153
221
async function handleRoute ( osrm : any , coordinates : [ number , number ] [ ] , query : any ) : Promise < any > {
154
- const options = {
155
- coordinates : coordinates ,
156
- steps : query . steps === 'true' ? true : false ,
157
- alternatives : query . alternatives === 'true' ? true : false ,
222
+ const options : any = {
223
+ coordinates : coordinates
158
224
} ;
225
+ handleCommonParams ( query , options ) ;
226
+ if ( query . alternatives ) {
227
+ options . alternatives = query . alternatives === 'true' ? true : false ;
228
+ }
229
+ if ( query . approaches ) {
230
+ options . approaches = query . approaches . split ( ';' ) ;
231
+ }
159
232
const res = await route ( osrm , options ) ;
160
233
return res ;
161
234
}
@@ -196,6 +269,7 @@ async function main() {
196
269
// TODO: validation
197
270
fastify . get ( '/:service(route|nearest|table|match|trip|tile)/v1/:profile/:coordinates' , async ( request , reply ) => {
198
271
const { service, profile, coordinates } = request . params as any ;
272
+
199
273
const query = request . query as any ;
200
274
const parsedCoordinates = coordinates . split ( ';' ) . map ( ( c : string ) => c . split ( ',' ) . map ( ( n : string ) => parseFloat ( n ) ) ) ;
201
275
const handlers : Map < string , Function > = new Map ( ) ;
0 commit comments