-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
node_osrm_support.hpp
1659 lines (1445 loc) · 56.4 KB
/
node_osrm_support.hpp
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef OSRM_BINDINGS_NODE_SUPPORT_HPP
#define OSRM_BINDINGS_NODE_SUPPORT_HPP
#include "nodejs/json_v8_renderer.hpp"
#include "util/json_renderer.hpp"
#include "osrm/approach.hpp"
#include "osrm/bearing.hpp"
#include "osrm/coordinate.hpp"
#include "osrm/engine_config.hpp"
#include "osrm/json_container.hpp"
#include "osrm/match_parameters.hpp"
#include "osrm/nearest_parameters.hpp"
#include "osrm/osrm.hpp"
#include "osrm/route_parameters.hpp"
#include "osrm/status.hpp"
#include "osrm/storage_config.hpp"
#include "osrm/table_parameters.hpp"
#include "osrm/tile_parameters.hpp"
#include "osrm/trip_parameters.hpp"
#include <boost/assert.hpp>
#include <boost/optional.hpp>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include <exception>
#include <memory>
#include <utility>
namespace node_osrm
{
using engine_config_ptr = std::unique_ptr<osrm::EngineConfig>;
using route_parameters_ptr = std::unique_ptr<osrm::RouteParameters>;
using trip_parameters_ptr = std::unique_ptr<osrm::TripParameters>;
using tile_parameters_ptr = std::unique_ptr<osrm::TileParameters>;
using match_parameters_ptr = std::unique_ptr<osrm::MatchParameters>;
using nearest_parameters_ptr = std::unique_ptr<osrm::NearestParameters>;
using table_parameters_ptr = std::unique_ptr<osrm::TableParameters>;
struct PluginParameters
{
bool renderJSONToBuffer = false;
};
using ObjectOrString = typename mapbox::util::variant<osrm::json::Object, std::string>;
template <typename ResultT> inline v8::Local<v8::Value> render(const ResultT &result);
template <> v8::Local<v8::Value> inline render(const std::string &result)
{
return Nan::CopyBuffer(result.data(), result.size()).ToLocalChecked();
}
template <> v8::Local<v8::Value> inline render(const ObjectOrString &result)
{
if (result.is<osrm::json::Object>())
{
// Convert osrm::json object tree into matching v8 object tree
v8::Local<v8::Value> value;
renderToV8(value, result.get<osrm::json::Object>());
return value;
}
else
{
// Return the string object as a node Buffer
return Nan::CopyBuffer(result.get<std::string>().data(), result.get<std::string>().size())
.ToLocalChecked();
}
}
inline void ParseResult(const osrm::Status &result_status, osrm::json::Object &result)
{
const auto code_iter = result.values.find("code");
const auto end_iter = result.values.end();
BOOST_ASSERT(code_iter != end_iter);
if (result_status == osrm::Status::Error)
{
throw std::logic_error(code_iter->second.get<osrm::json::String>().value.c_str());
}
result.values.erase(code_iter);
const auto message_iter = result.values.find("message");
if (message_iter != end_iter)
{
result.values.erase(message_iter);
}
}
inline void ParseResult(const osrm::Status & /*result_status*/, const std::string & /*unused*/) {}
inline engine_config_ptr argumentsToEngineConfig(const Nan::FunctionCallbackInfo<v8::Value> &args)
{
Nan::HandleScope scope;
auto engine_config = std::make_unique<osrm::EngineConfig>();
if (args.Length() == 0)
{
return engine_config;
}
else if (args.Length() > 1)
{
Nan::ThrowError("Only accepts one parameter");
return engine_config_ptr();
}
BOOST_ASSERT(args.Length() == 1);
if (args[0]->IsString())
{
engine_config->storage_config =
osrm::StorageConfig(*Nan::Utf8String(Nan::To<v8::String>(args[0]).ToLocalChecked()));
engine_config->use_shared_memory = false;
return engine_config;
}
else if (!args[0]->IsObject())
{
Nan::ThrowError("Parameter must be a path or options object");
return engine_config_ptr();
}
BOOST_ASSERT(args[0]->IsObject());
auto params = Nan::To<v8::Object>(args[0]).ToLocalChecked();
auto path = Nan::Get(params, Nan::New("path").ToLocalChecked()).ToLocalChecked();
if (path.IsEmpty())
return engine_config_ptr();
auto memory_file = Nan::Get(params, Nan::New("memory_file").ToLocalChecked()).ToLocalChecked();
if (memory_file.IsEmpty())
return engine_config_ptr();
auto shared_memory =
Nan::Get(params, Nan::New("shared_memory").ToLocalChecked()).ToLocalChecked();
if (shared_memory.IsEmpty())
return engine_config_ptr();
auto mmap_memory = Nan::Get(params, Nan::New("mmap_memory").ToLocalChecked()).ToLocalChecked();
if (mmap_memory.IsEmpty())
return engine_config_ptr();
if (!memory_file->IsUndefined())
{
if (path->IsUndefined())
{
Nan::ThrowError("memory_file option requires a path to a file.");
return engine_config_ptr();
}
engine_config->memory_file =
*Nan::Utf8String(Nan::To<v8::String>(memory_file).ToLocalChecked());
}
auto dataset_name =
Nan::Get(params, Nan::New("dataset_name").ToLocalChecked()).ToLocalChecked();
if (dataset_name.IsEmpty())
return engine_config_ptr();
if (!dataset_name->IsUndefined())
{
if (dataset_name->IsString())
{
engine_config->dataset_name =
*Nan::Utf8String(Nan::To<v8::String>(dataset_name).ToLocalChecked());
}
else
{
Nan::ThrowError("dataset_name needs to be a string");
return engine_config_ptr();
}
}
if (!path->IsUndefined())
{
engine_config->storage_config =
osrm::StorageConfig(*Nan::Utf8String(Nan::To<v8::String>(path).ToLocalChecked()));
engine_config->use_shared_memory = false;
}
if (!shared_memory->IsUndefined())
{
if (shared_memory->IsBoolean())
{
engine_config->use_shared_memory = Nan::To<bool>(shared_memory).FromJust();
}
else
{
Nan::ThrowError("Shared_memory option must be a boolean");
return engine_config_ptr();
}
}
if (!mmap_memory->IsUndefined())
{
if (mmap_memory->IsBoolean())
{
engine_config->use_mmap = Nan::To<bool>(mmap_memory).FromJust();
}
else
{
Nan::ThrowError("mmap_memory option must be a boolean");
return engine_config_ptr();
}
}
if (path->IsUndefined() && !engine_config->use_shared_memory)
{
Nan::ThrowError("Shared_memory must be enabled if no path is "
"specified");
return engine_config_ptr();
}
auto algorithm = Nan::Get(params, Nan::New("algorithm").ToLocalChecked()).ToLocalChecked();
if (algorithm.IsEmpty())
return engine_config_ptr();
if (algorithm->IsString())
{
auto algorithm_str = Nan::To<v8::String>(algorithm).ToLocalChecked();
if (*Nan::Utf8String(algorithm_str) == std::string("CH"))
{
engine_config->algorithm = osrm::EngineConfig::Algorithm::CH;
}
else if (*Nan::Utf8String(algorithm_str) == std::string("CoreCH"))
{
engine_config->algorithm = osrm::EngineConfig::Algorithm::CH;
}
else if (*Nan::Utf8String(algorithm_str) == std::string("MLD"))
{
engine_config->algorithm = osrm::EngineConfig::Algorithm::MLD;
}
else
{
Nan::ThrowError("algorithm option must be one of 'CH', 'CoreCH', or 'MLD'.");
return engine_config_ptr();
}
}
else if (!algorithm->IsUndefined())
{
Nan::ThrowError("algorithm option must be a string and one of 'CH', 'CoreCH', or 'MLD'.");
return engine_config_ptr();
}
// Set EngineConfig system-wide limits on construction, if requested
auto max_locations_trip =
Nan::Get(params, Nan::New("max_locations_trip").ToLocalChecked()).ToLocalChecked();
auto max_locations_viaroute =
Nan::Get(params, Nan::New("max_locations_viaroute").ToLocalChecked()).ToLocalChecked();
auto max_locations_distance_table =
Nan::Get(params, Nan::New("max_locations_distance_table").ToLocalChecked())
.ToLocalChecked();
auto max_locations_map_matching =
Nan::Get(params, Nan::New("max_locations_map_matching").ToLocalChecked()).ToLocalChecked();
auto max_results_nearest =
Nan::Get(params, Nan::New("max_results_nearest").ToLocalChecked()).ToLocalChecked();
auto max_alternatives =
Nan::Get(params, Nan::New("max_alternatives").ToLocalChecked()).ToLocalChecked();
auto max_radius_map_matching =
Nan::Get(params, Nan::New("max_radius_map_matching").ToLocalChecked()).ToLocalChecked();
if (!max_locations_trip->IsUndefined() && !max_locations_trip->IsNumber())
{
Nan::ThrowError("max_locations_trip must be an integral number");
return engine_config_ptr();
}
if (!max_locations_viaroute->IsUndefined() && !max_locations_viaroute->IsNumber())
{
Nan::ThrowError("max_locations_viaroute must be an integral number");
return engine_config_ptr();
}
if (!max_locations_distance_table->IsUndefined() && !max_locations_distance_table->IsNumber())
{
Nan::ThrowError("max_locations_distance_table must be an integral number");
return engine_config_ptr();
}
if (!max_locations_map_matching->IsUndefined() && !max_locations_map_matching->IsNumber())
{
Nan::ThrowError("max_locations_map_matching must be an integral number");
return engine_config_ptr();
}
if (!max_results_nearest->IsUndefined() && !max_results_nearest->IsNumber())
{
Nan::ThrowError("max_results_nearest must be an integral number");
return engine_config_ptr();
}
if (!max_alternatives->IsUndefined() && !max_alternatives->IsNumber())
{
Nan::ThrowError("max_alternatives must be an integral number");
return engine_config_ptr();
}
if (max_locations_trip->IsNumber())
engine_config->max_locations_trip = Nan::To<int>(max_locations_trip).FromJust();
if (max_locations_viaroute->IsNumber())
engine_config->max_locations_viaroute = Nan::To<int>(max_locations_viaroute).FromJust();
if (max_locations_distance_table->IsNumber())
engine_config->max_locations_distance_table =
Nan::To<int>(max_locations_distance_table).FromJust();
if (max_locations_map_matching->IsNumber())
engine_config->max_locations_map_matching =
Nan::To<int>(max_locations_map_matching).FromJust();
if (max_results_nearest->IsNumber())
engine_config->max_results_nearest = Nan::To<int>(max_results_nearest).FromJust();
if (max_alternatives->IsNumber())
engine_config->max_alternatives = Nan::To<int>(max_alternatives).FromJust();
if (max_radius_map_matching->IsNumber())
engine_config->max_radius_map_matching =
Nan::To<double>(max_radius_map_matching).FromJust();
return engine_config;
}
inline boost::optional<std::vector<osrm::Coordinate>>
parseCoordinateArray(const v8::Local<v8::Array> &coordinates_array)
{
Nan::HandleScope scope;
boost::optional<std::vector<osrm::Coordinate>> resulting_coordinates;
std::vector<osrm::Coordinate> temp_coordinates;
for (uint32_t i = 0; i < coordinates_array->Length(); ++i)
{
v8::Local<v8::Value> coordinate = Nan::Get(coordinates_array, i).ToLocalChecked();
if (coordinate.IsEmpty())
return resulting_coordinates;
if (!coordinate->IsArray())
{
Nan::ThrowError("Coordinates must be an array of (lon/lat) pairs");
return resulting_coordinates;
}
v8::Local<v8::Array> coordinate_pair = v8::Local<v8::Array>::Cast(coordinate);
if (coordinate_pair->Length() != 2)
{
Nan::ThrowError("Coordinates must be an array of (lon/lat) pairs");
return resulting_coordinates;
}
if (!Nan::Get(coordinate_pair, 0).ToLocalChecked()->IsNumber() ||
!Nan::Get(coordinate_pair, 1).ToLocalChecked()->IsNumber())
{
Nan::ThrowError("Each member of a coordinate pair must be a number");
return resulting_coordinates;
}
double lon = Nan::To<double>(Nan::Get(coordinate_pair, 0).ToLocalChecked()).FromJust();
double lat = Nan::To<double>(Nan::Get(coordinate_pair, 1).ToLocalChecked()).FromJust();
if (std::isnan(lon) || std::isnan(lat) || std::isinf(lon) || std::isinf(lat))
{
Nan::ThrowError("Lng/Lat coordinates must be valid numbers");
return resulting_coordinates;
}
if (lon > 180 || lon < -180 || lat > 90 || lat < -90)
{
Nan::ThrowError("Lng/Lat coordinates must be within world bounds "
"(-180 < lng < 180, -90 < lat < 90)");
return resulting_coordinates;
}
temp_coordinates.emplace_back(osrm::util::FloatLongitude{std::move(lon)},
osrm::util::FloatLatitude{std::move(lat)});
}
resulting_coordinates = boost::make_optional(std::move(temp_coordinates));
return resulting_coordinates;
}
// Parses all the non-service specific parameters
template <typename ParamType>
inline bool argumentsToParameter(const Nan::FunctionCallbackInfo<v8::Value> &args,
ParamType ¶ms,
bool requires_multiple_coordinates)
{
Nan::HandleScope scope;
if (args.Length() < 2)
{
Nan::ThrowTypeError("Two arguments required");
return false;
}
if (!args[0]->IsObject())
{
Nan::ThrowTypeError("First arg must be an object");
return false;
}
v8::Local<v8::Object> obj = Nan::To<v8::Object>(args[0]).ToLocalChecked();
v8::Local<v8::Value> coordinates =
Nan::Get(obj, Nan::New("coordinates").ToLocalChecked()).ToLocalChecked();
if (coordinates.IsEmpty())
return false;
if (coordinates->IsUndefined())
{
Nan::ThrowError("Must provide a coordinates property");
return false;
}
else if (coordinates->IsArray())
{
auto coordinates_array = v8::Local<v8::Array>::Cast(coordinates);
if (coordinates_array->Length() < 2 && requires_multiple_coordinates)
{
Nan::ThrowError("At least two coordinates must be provided");
return false;
}
else if (!requires_multiple_coordinates && coordinates_array->Length() != 1)
{
Nan::ThrowError("Exactly one coordinate pair must be provided");
return false;
}
auto maybe_coordinates = parseCoordinateArray(coordinates_array);
if (maybe_coordinates)
{
std::copy(maybe_coordinates->begin(),
maybe_coordinates->end(),
std::back_inserter(params->coordinates));
}
else
{
return false;
}
}
else if (!coordinates->IsUndefined())
{
BOOST_ASSERT(!coordinates->IsArray());
Nan::ThrowError("Coordinates must be an array of (lon/lat) pairs");
return false;
}
if (Nan::Has(obj, Nan::New("approaches").ToLocalChecked()).FromJust())
{
v8::Local<v8::Value> approaches =
Nan::Get(obj, Nan::New("approaches").ToLocalChecked()).ToLocalChecked();
if (approaches.IsEmpty())
return false;
if (!approaches->IsArray())
{
Nan::ThrowError("Approaches must be an arrays of strings");
return false;
}
auto approaches_array = v8::Local<v8::Array>::Cast(approaches);
if (approaches_array->Length() != params->coordinates.size())
{
Nan::ThrowError("Approaches array must have the same length as coordinates array");
return false;
}
for (uint32_t i = 0; i < approaches_array->Length(); ++i)
{
v8::Local<v8::Value> approach_raw = Nan::Get(approaches_array, i).ToLocalChecked();
if (approach_raw.IsEmpty())
return false;
if (approach_raw->IsNull())
{
params->approaches.emplace_back();
}
else if (approach_raw->IsString())
{
const Nan::Utf8String approach_utf8str(approach_raw);
std::string approach_str{*approach_utf8str,
*approach_utf8str + approach_utf8str.length()};
if (approach_str == "curb")
{
params->approaches.push_back(osrm::Approach::CURB);
}
else if (approach_str == "unrestricted")
{
params->approaches.push_back(osrm::Approach::UNRESTRICTED);
}
else
{
Nan::ThrowError("'approaches' param must be one of [curb, unrestricted]");
return false;
}
}
else
{
Nan::ThrowError("Approach must be a string: [curb, unrestricted] or null");
return false;
}
}
}
if (Nan::Has(obj, Nan::New("bearings").ToLocalChecked()).FromJust())
{
v8::Local<v8::Value> bearings =
Nan::Get(obj, Nan::New("bearings").ToLocalChecked()).ToLocalChecked();
if (bearings.IsEmpty())
return false;
if (!bearings->IsArray())
{
Nan::ThrowError("Bearings must be an array of arrays of numbers");
return false;
}
auto bearings_array = v8::Local<v8::Array>::Cast(bearings);
if (bearings_array->Length() != params->coordinates.size())
{
Nan::ThrowError("Bearings array must have the same length as coordinates array");
return false;
}
for (uint32_t i = 0; i < bearings_array->Length(); ++i)
{
v8::Local<v8::Value> bearing_raw = Nan::Get(bearings_array, i).ToLocalChecked();
if (bearing_raw.IsEmpty())
return false;
if (bearing_raw->IsNull())
{
params->bearings.emplace_back();
}
else if (bearing_raw->IsArray())
{
auto bearing_pair = v8::Local<v8::Array>::Cast(bearing_raw);
if (bearing_pair->Length() == 2)
{
if (!Nan::Get(bearing_pair, 0).ToLocalChecked()->IsNumber() ||
!Nan::Get(bearing_pair, 1).ToLocalChecked()->IsNumber())
{
Nan::ThrowError("Bearing values need to be numbers in range 0..360");
return false;
}
const auto bearing =
Nan::To<int>(Nan::Get(bearing_pair, 0).ToLocalChecked()).FromJust();
const auto range =
Nan::To<int>(Nan::Get(bearing_pair, 1).ToLocalChecked()).FromJust();
if (bearing < 0 || bearing > 360 || range < 0 || range > 180)
{
Nan::ThrowError("Bearing values need to be in range 0..360, 0..180");
return false;
}
params->bearings.push_back(
osrm::Bearing{static_cast<short>(bearing), static_cast<short>(range)});
}
else
{
Nan::ThrowError("Bearing must be an array of [bearing, range] or null");
return false;
}
}
else
{
Nan::ThrowError("Bearing must be an array of [bearing, range] or null");
return false;
}
}
}
if (Nan::Has(obj, Nan::New("hints").ToLocalChecked()).FromJust())
{
v8::Local<v8::Value> hints =
Nan::Get(obj, Nan::New("hints").ToLocalChecked()).ToLocalChecked();
if (hints.IsEmpty())
return false;
if (!hints->IsArray())
{
Nan::ThrowError("Hints must be an array of strings/null");
return false;
}
v8::Local<v8::Array> hints_array = v8::Local<v8::Array>::Cast(hints);
if (hints_array->Length() != params->coordinates.size())
{
Nan::ThrowError("Hints array must have the same length as coordinates array");
return false;
}
for (uint32_t i = 0; i < hints_array->Length(); ++i)
{
v8::Local<v8::Value> hint = Nan::Get(hints_array, i).ToLocalChecked();
if (hint.IsEmpty())
return false;
if (hint->IsString())
{
if (Nan::To<v8::String>(hint).ToLocalChecked()->Length() == 0)
{
Nan::ThrowError("Hint cannot be an empty string");
return false;
}
params->hints.push_back(osrm::engine::Hint::FromBase64(*Nan::Utf8String(hint)));
}
else if (hint->IsNull())
{
params->hints.emplace_back();
}
else
{
Nan::ThrowError("Hint must be null or string");
return false;
}
}
}
if (Nan::Has(obj, Nan::New("radiuses").ToLocalChecked()).FromJust())
{
v8::Local<v8::Value> radiuses =
Nan::Get(obj, Nan::New("radiuses").ToLocalChecked()).ToLocalChecked();
if (radiuses.IsEmpty())
return false;
if (!radiuses->IsArray())
{
Nan::ThrowError("Radiuses must be an array of non-negative doubles or null");
return false;
}
v8::Local<v8::Array> radiuses_array = v8::Local<v8::Array>::Cast(radiuses);
if (radiuses_array->Length() != params->coordinates.size())
{
Nan::ThrowError("Radiuses array must have the same length as coordinates array");
return false;
}
for (uint32_t i = 0; i < radiuses_array->Length(); ++i)
{
v8::Local<v8::Value> radius = Nan::Get(radiuses_array, i).ToLocalChecked();
if (radius.IsEmpty())
return false;
if (radius->IsNull())
{
params->radiuses.emplace_back();
}
else if (radius->IsNumber() && Nan::To<double>(radius).FromJust() >= 0)
{
params->radiuses.push_back(Nan::To<double>(radius).FromJust());
}
else
{
Nan::ThrowError("Radius must be non-negative double or null");
return false;
}
}
}
if (Nan::Has(obj, Nan::New("generate_hints").ToLocalChecked()).FromJust())
{
v8::Local<v8::Value> generate_hints =
Nan::Get(obj, Nan::New("generate_hints").ToLocalChecked()).ToLocalChecked();
if (generate_hints.IsEmpty())
return false;
if (!generate_hints->IsBoolean())
{
Nan::ThrowError("generate_hints must be of type Boolean");
return false;
}
params->generate_hints = Nan::To<bool>(generate_hints).FromJust();
}
if (Nan::Has(obj, Nan::New("skip_waypoints").ToLocalChecked()).FromJust())
{
v8::Local<v8::Value> skip_waypoints =
Nan::Get(obj, Nan::New("skip_waypoints").ToLocalChecked()).ToLocalChecked();
if (skip_waypoints.IsEmpty())
return false;
if (!skip_waypoints->IsBoolean())
{
Nan::ThrowError("skip_waypoints must be of type Boolean");
return false;
}
params->skip_waypoints = Nan::To<bool>(skip_waypoints).FromJust();
}
if (Nan::Has(obj, Nan::New("exclude").ToLocalChecked()).FromJust())
{
v8::Local<v8::Value> exclude =
Nan::Get(obj, Nan::New("exclude").ToLocalChecked()).ToLocalChecked();
if (exclude.IsEmpty())
return false;
if (!exclude->IsArray())
{
Nan::ThrowError("Exclude must be an array of strings or empty");
return false;
}
v8::Local<v8::Array> exclude_array = v8::Local<v8::Array>::Cast(exclude);
for (uint32_t i = 0; i < exclude_array->Length(); ++i)
{
v8::Local<v8::Value> class_name = Nan::Get(exclude_array, i).ToLocalChecked();
if (class_name.IsEmpty())
return false;
if (class_name->IsString())
{
std::string class_name_str = *Nan::Utf8String(class_name);
params->exclude.emplace_back(class_name_str);
}
else
{
Nan::ThrowError("Exclude must be an array of strings or empty");
return false;
}
}
}
return true;
}
template <typename ParamType>
inline bool parseCommonParameters(const v8::Local<v8::Object> &obj, ParamType ¶ms)
{
if (Nan::Has(obj, Nan::New("steps").ToLocalChecked()).FromJust())
{
auto steps = Nan::Get(obj, Nan::New("steps").ToLocalChecked()).ToLocalChecked();
if (steps.IsEmpty())
return false;
if (steps->IsBoolean())
{
params->steps = Nan::To<bool>(steps).FromJust();
}
else
{
Nan::ThrowError("'steps' param must be a boolean");
return false;
}
}
if (Nan::Has(obj, Nan::New("annotations").ToLocalChecked()).FromJust())
{
auto annotations = Nan::Get(obj, Nan::New("annotations").ToLocalChecked()).ToLocalChecked();
if (annotations.IsEmpty())
return false;
if (annotations->IsBoolean())
{
params->annotations = Nan::To<bool>(annotations).FromJust();
}
else if (annotations->IsArray())
{
v8::Local<v8::Array> annotations_array = v8::Local<v8::Array>::Cast(annotations);
for (std::size_t i = 0; i < annotations_array->Length(); i++)
{
const Nan::Utf8String annotations_utf8str(
Nan::Get(annotations_array, i).ToLocalChecked());
std::string annotations_str{*annotations_utf8str,
*annotations_utf8str + annotations_utf8str.length()};
if (annotations_str == "duration")
{
params->annotations_type =
params->annotations_type | osrm::RouteParameters::AnnotationsType::Duration;
}
else if (annotations_str == "nodes")
{
params->annotations_type =
params->annotations_type | osrm::RouteParameters::AnnotationsType::Nodes;
}
else if (annotations_str == "distance")
{
params->annotations_type =
params->annotations_type | osrm::RouteParameters::AnnotationsType::Distance;
}
else if (annotations_str == "weight")
{
params->annotations_type =
params->annotations_type | osrm::RouteParameters::AnnotationsType::Weight;
}
else if (annotations_str == "datasources")
{
params->annotations_type = params->annotations_type |
osrm::RouteParameters::AnnotationsType::Datasources;
}
else if (annotations_str == "speed")
{
params->annotations_type =
params->annotations_type | osrm::RouteParameters::AnnotationsType::Speed;
}
else
{
Nan::ThrowError("this 'annotations' param is not supported");
return false;
}
}
}
else
{
Nan::ThrowError("this 'annotations' param is not supported");
return false;
}
}
if (Nan::Has(obj, Nan::New("geometries").ToLocalChecked()).FromJust())
{
v8::Local<v8::Value> geometries =
Nan::Get(obj, Nan::New("geometries").ToLocalChecked()).ToLocalChecked();
if (geometries.IsEmpty())
return false;
if (!geometries->IsString())
{
Nan::ThrowError("Geometries must be a string: [polyline, polyline6, geojson]");
return false;
}
const Nan::Utf8String geometries_utf8str(geometries);
std::string geometries_str{*geometries_utf8str,
*geometries_utf8str + geometries_utf8str.length()};
if (geometries_str == "polyline")
{
params->geometries = osrm::RouteParameters::GeometriesType::Polyline;
}
else if (geometries_str == "polyline6")
{
params->geometries = osrm::RouteParameters::GeometriesType::Polyline6;
}
else if (geometries_str == "geojson")
{
params->geometries = osrm::RouteParameters::GeometriesType::GeoJSON;
}
else
{
Nan::ThrowError("'geometries' param must be one of [polyline, polyline6, geojson]");
return false;
}
}
if (Nan::Has(obj, Nan::New("overview").ToLocalChecked()).FromJust())
{
v8::Local<v8::Value> overview =
Nan::Get(obj, Nan::New("overview").ToLocalChecked()).ToLocalChecked();
if (overview.IsEmpty())
return false;
if (!overview->IsString())
{
Nan::ThrowError("Overview must be a string: [simplified, full, false]");
return false;
}
const Nan::Utf8String overview_utf8str(overview);
std::string overview_str{*overview_utf8str, *overview_utf8str + overview_utf8str.length()};
if (overview_str == "simplified")
{
params->overview = osrm::RouteParameters::OverviewType::Simplified;
}
else if (overview_str == "full")
{
params->overview = osrm::RouteParameters::OverviewType::Full;
}
else if (overview_str == "false")
{
params->overview = osrm::RouteParameters::OverviewType::False;
}
else
{
Nan::ThrowError("'overview' param must be one of [simplified, full, false]");
return false;
}
}
return true;
}
inline PluginParameters
argumentsToPluginParameters(const Nan::FunctionCallbackInfo<v8::Value> &args)
{
if (args.Length() < 3 || !args[1]->IsObject())
{
return {};
}
v8::Local<v8::Object> obj = Nan::To<v8::Object>(args[1]).ToLocalChecked();
if (Nan::Has(obj, Nan::New("format").ToLocalChecked()).FromJust())
{
v8::Local<v8::Value> format =
Nan::Get(obj, Nan::New("format").ToLocalChecked()).ToLocalChecked();
if (format.IsEmpty())
{
return {};
}
if (!format->IsString())
{
Nan::ThrowError("format must be a string: \"object\" or \"json_buffer\"");
return {};
}
const Nan::Utf8String format_utf8str(format);
std::string format_str{*format_utf8str, *format_utf8str + format_utf8str.length()};
if (format_str == "object")
{
return {false};
}
else if (format_str == "json_buffer")
{
return {true};
}
else
{
Nan::ThrowError("format must be a string: \"object\" or \"json_buffer\"");
return {};
}
}
return {};
}
inline route_parameters_ptr
argumentsToRouteParameter(const Nan::FunctionCallbackInfo<v8::Value> &args,
bool requires_multiple_coordinates)
{
route_parameters_ptr params = std::make_unique<osrm::RouteParameters>();
bool has_base_params = argumentsToParameter(args, params, requires_multiple_coordinates);
if (!has_base_params)
return route_parameters_ptr();
v8::Local<v8::Object> obj = Nan::To<v8::Object>(args[0]).ToLocalChecked();
if (Nan::Has(obj, Nan::New("continue_straight").ToLocalChecked()).FromJust())
{
auto value = Nan::Get(obj, Nan::New("continue_straight").ToLocalChecked()).ToLocalChecked();
if (value.IsEmpty())
return route_parameters_ptr();
if (!value->IsBoolean() && !value->IsNull())
{
Nan::ThrowError("'continue_straight' param must be boolean or null");
return route_parameters_ptr();
}
if (value->IsBoolean())
{
params->continue_straight = Nan::To<bool>(value).FromJust();
}
}
if (Nan::Has(obj, Nan::New("alternatives").ToLocalChecked()).FromJust())
{
auto value = Nan::Get(obj, Nan::New("alternatives").ToLocalChecked()).ToLocalChecked();
if (value.IsEmpty())
return route_parameters_ptr();
if (value->IsBoolean())
{
params->alternatives = Nan::To<bool>(value).FromJust();
params->number_of_alternatives = Nan::To<bool>(value).FromJust() ? 1u : 0u;
}
else if (value->IsNumber())
{
params->alternatives = Nan::To<bool>(value).FromJust();
params->number_of_alternatives = Nan::To<unsigned>(value).FromJust();
}
else
{
Nan::ThrowError("'alternatives' param must be boolean or number");
return route_parameters_ptr();
}
}
if (Nan::Has(obj, Nan::New("waypoints").ToLocalChecked()).FromJust())
{
v8::Local<v8::Value> waypoints =
Nan::Get(obj, Nan::New("waypoints").ToLocalChecked()).ToLocalChecked();
if (waypoints.IsEmpty())
return route_parameters_ptr();
// must be array
if (!waypoints->IsArray())
{
Nan::ThrowError(
"Waypoints must be an array of integers corresponding to the input coordinates.");
return route_parameters_ptr();
}
auto waypoints_array = v8::Local<v8::Array>::Cast(waypoints);
// must have at least two elements