MCPcopy Create free account
hub / github.com/Project-OSRM/osrm-backend / parseCoordinateArray

Function parseCoordinateArray

include/nodejs/node_osrm_support.hpp:397–454  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

395}
396
397inline std::optional<std::vector<osrm::Coordinate>>
398parseCoordinateArray(const Napi::Array &coordinates_array)
399{
400 Napi::HandleScope scope(coordinates_array.Env());
401 std::optional<std::vector<osrm::Coordinate>> resulting_coordinates;
402 std::vector<osrm::Coordinate> temp_coordinates;
403
404 for (uint32_t i = 0; i < coordinates_array.Length(); ++i)
405 {
406 Napi::Value coordinate = coordinates_array.Get(i);
407 if (coordinate.IsEmpty())
408 return resulting_coordinates;
409
410 if (!coordinate.IsArray())
411 {
412 ThrowError(coordinates_array.Env(), "Coordinates must be an array of (lon/lat) pairs");
413 return resulting_coordinates;
414 }
415
416 Napi::Array coordinate_pair = coordinate.As<Napi::Array>();
417 if (coordinate_pair.Length() != 2)
418 {
419 ThrowError(coordinates_array.Env(), "Coordinates must be an array of (lon/lat) pairs");
420 return resulting_coordinates;
421 }
422
423 if (!coordinate_pair.Get(static_cast<uint32_t>(0)).IsNumber() ||
424 !coordinate_pair.Get(static_cast<uint32_t>(1)).IsNumber())
425 {
426 ThrowError(coordinates_array.Env(),
427 "Each member of a coordinate pair must be a number");
428 return resulting_coordinates;
429 }
430
431 double lon = coordinate_pair.Get(static_cast<uint32_t>(0)).As<Napi::Number>().DoubleValue();
432 double lat = coordinate_pair.Get(static_cast<uint32_t>(1)).As<Napi::Number>().DoubleValue();
433
434 if (std::isnan(lon) || std::isnan(lat) || std::isinf(lon) || std::isinf(lat))
435 {
436 ThrowError(coordinates_array.Env(), "Lng/Lat coordinates must be valid numbers");
437 return resulting_coordinates;
438 }
439
440 if (lon > 180 || lon < -180 || lat > 90 || lat < -90)
441 {
442 ThrowError(coordinates_array.Env(),
443 "Lng/Lat coordinates must be within world bounds "
444 "(-180 < lng < 180, -90 < lat < 90)");
445 return resulting_coordinates;
446 }
447
448 temp_coordinates.emplace_back(osrm::util::FloatLongitude{std::move(lon)},
449 osrm::util::FloatLatitude{std::move(lat)});
450 }
451
452 resulting_coordinates = std::make_optional(std::move(temp_coordinates));
453 return resulting_coordinates;
454}

Callers 1

argumentsToParameterFunction · 0.85

Calls 4

ThrowErrorFunction · 0.85
IsEmptyMethod · 0.80
GetMethod · 0.45
emplace_backMethod · 0.45

Tested by

no test coverage detected