index.js 2.4 KB

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