index.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var circle_1 = __importDefault(require("@turf/circle"));
  7. var destination_1 = __importDefault(require("@turf/destination"));
  8. var helpers_1 = require("@turf/helpers");
  9. /**
  10. * Creates a circular arc, of a circle of the given radius and center point, between bearing1 and bearing2;
  11. * 0 bearing is North of center point, positive clockwise.
  12. *
  13. * @name lineArc
  14. * @param {Coord} center center point
  15. * @param {number} radius radius of the circle
  16. * @param {number} bearing1 angle, in decimal degrees, of the first radius of the arc
  17. * @param {number} bearing2 angle, in decimal degrees, of the second radius of the arc
  18. * @param {Object} [options={}] Optional parameters
  19. * @param {number} [options.steps=64] number of steps
  20. * @param {string} [options.units='kilometers'] miles, kilometers, degrees, or radians
  21. * @returns {Feature<LineString>} line arc
  22. * @example
  23. * var center = turf.point([-75, 40]);
  24. * var radius = 5;
  25. * var bearing1 = 25;
  26. * var bearing2 = 47;
  27. *
  28. * var arc = turf.lineArc(center, radius, bearing1, bearing2);
  29. *
  30. * //addToMap
  31. * var addToMap = [center, arc]
  32. */
  33. function lineArc(center, radius, bearing1, bearing2, options) {
  34. if (options === void 0) { options = {}; }
  35. // default params
  36. var steps = options.steps || 64;
  37. var angle1 = convertAngleTo360(bearing1);
  38. var angle2 = convertAngleTo360(bearing2);
  39. var properties = !Array.isArray(center) && center.type === "Feature"
  40. ? center.properties
  41. : {};
  42. // handle angle parameters
  43. if (angle1 === angle2) {
  44. return helpers_1.lineString(circle_1.default(center, radius, options).geometry.coordinates[0], properties);
  45. }
  46. var arcStartDegree = angle1;
  47. var arcEndDegree = angle1 < angle2 ? angle2 : angle2 + 360;
  48. var alfa = arcStartDegree;
  49. var coordinates = [];
  50. var i = 0;
  51. while (alfa < arcEndDegree) {
  52. coordinates.push(destination_1.default(center, radius, alfa, options).geometry.coordinates);
  53. i++;
  54. alfa = arcStartDegree + (i * 360) / steps;
  55. }
  56. if (alfa > arcEndDegree) {
  57. coordinates.push(destination_1.default(center, radius, arcEndDegree, options).geometry.coordinates);
  58. }
  59. return helpers_1.lineString(coordinates, properties);
  60. }
  61. exports.default = lineArc;
  62. /**
  63. * Takes any angle in degrees
  64. * and returns a valid angle between 0-360 degrees
  65. *
  66. * @private
  67. * @param {number} alfa angle between -180-180 degrees
  68. * @returns {number} angle between 0-360 degrees
  69. */
  70. function convertAngleTo360(alfa) {
  71. var beta = alfa % 360;
  72. if (beta < 0) {
  73. beta += 360;
  74. }
  75. return beta;
  76. }