11#!/usr/bin/env node
22import Fastify from 'fastify'
3+ import { option } from 'yargs' ;
34import yargs from 'yargs/yargs' ;
45
56const OSRM = require ( '../lib/index.js' )
@@ -80,21 +81,30 @@ async function handleNearest(osrm: any, coordinates: [number, number][], query:
8081
8182
8283async function handleTable ( osrm : any , coordinates : [ number , number ] [ ] , query : any ) : Promise < any > {
83- const options = {
84+ const options : any = {
8485 coordinates : coordinates
8586 } ;
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+ }
86103 const res = await table ( osrm , options ) ;
87104 return res ;
88105}
89106
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 ) {
98108 if ( query . overview ) {
99109 options . overview = query . overview ;
100110 }
@@ -111,22 +121,11 @@ async function handleMatch(osrm: any, coordinates: [number, number][], query: an
111121 options . waypoints = query . waypoints . split ( ';' ) . map ( ( t : string ) => parseInt ( t ) ) ;
112122 }
113123
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-
122124 if ( query . steps ) {
123125 options . steps = query . steps === 'true' ? true : false ;
124126 }
125127
126- if ( query . generate_hints ) {
127- options . generate_hints = query . generate_hints == 'true' ? true : false ;
128- }
129-
128+
130129 if ( query . annotations ) {
131130 let annotations ;
132131 if ( query . annotations === 'true' ) {
@@ -136,26 +135,100 @@ async function handleMatch(osrm: any, coordinates: [number, number][], query: an
136135 }
137136 options . annotations = annotations ;
138137 }
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+
139198 //throw new Error(`not implemented ${JSON.stringify(options)}`);
140199 const res = await match ( osrm , options ) ;
141200 return res ;
142201}
143202
144203async 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
148206 } ;
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+ }
149217 const res = await trip ( osrm , options ) ;
150218 return res ;
151219}
152220
153221async 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
158224 } ;
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+ }
159232 const res = await route ( osrm , options ) ;
160233 return res ;
161234}
@@ -196,6 +269,7 @@ async function main() {
196269 // TODO: validation
197270 fastify . get ( '/:service(route|nearest|table|match|trip|tile)/v1/:profile/:coordinates' , async ( request , reply ) => {
198271 const { service, profile, coordinates } = request . params as any ;
272+
199273 const query = request . query as any ;
200274 const parsedCoordinates = coordinates . split ( ';' ) . map ( ( c : string ) => c . split ( ',' ) . map ( ( n : string ) => parseFloat ( n ) ) ) ;
201275 const handlers : Map < string , Function > = new Map ( ) ;
0 commit comments