index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import circle from '@turf/circle';
  2. import lineArc from '@turf/line-arc';
  3. import { coordEach } from '@turf/meta';
  4. import { isObject, polygon } from '@turf/helpers';
  5. import { getCoords } from '@turf/invariant';
  6. /**
  7. * Creates a circular sector of a circle of given radius and center {@link Point},
  8. * between (clockwise) bearing1 and bearing2; 0 bearing is North of center point, positive clockwise.
  9. *
  10. * @name sector
  11. * @param {Coord} center center point
  12. * @param {number} radius radius of the circle
  13. * @param {number} bearing1 angle, in decimal degrees, of the first radius of the sector
  14. * @param {number} bearing2 angle, in decimal degrees, of the second radius of the sector
  15. * @param {Object} [options={}] Optional parameters
  16. * @param {string} [options.units='kilometers'] miles, kilometers, degrees, or radians
  17. * @param {number} [options.steps=64] number of steps
  18. * @param {Properties} [options.properties={}] Translate properties to Feature Polygon
  19. * @returns {Feature<Polygon>} sector polygon
  20. * @example
  21. * var center = turf.point([-75, 40]);
  22. * var radius = 5;
  23. * var bearing1 = 25;
  24. * var bearing2 = 45;
  25. *
  26. * var sector = turf.sector(center, radius, bearing1, bearing2);
  27. *
  28. * //addToMap
  29. * var addToMap = [center, sector];
  30. */
  31. function sector(center, radius, bearing1, bearing2, options) {
  32. // Optional parameters
  33. options = options || {};
  34. if (!isObject(options)) throw new Error("options is invalid");
  35. var properties = options.properties;
  36. // validation
  37. if (!center) throw new Error("center is required");
  38. if (bearing1 === undefined || bearing1 === null)
  39. throw new Error("bearing1 is required");
  40. if (bearing2 === undefined || bearing2 === null)
  41. throw new Error("bearing2 is required");
  42. if (!radius) throw new Error("radius is required");
  43. if (typeof options !== "object") throw new Error("options must be an object");
  44. if (convertAngleTo360(bearing1) === convertAngleTo360(bearing2)) {
  45. return circle(center, radius, options);
  46. }
  47. var coords = getCoords(center);
  48. var arc = lineArc(center, radius, bearing1, bearing2, options);
  49. var sliceCoords = [[coords]];
  50. coordEach(arc, function (currentCoords) {
  51. sliceCoords[0].push(currentCoords);
  52. });
  53. sliceCoords[0].push(coords);
  54. return polygon(sliceCoords, 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) beta += 360;
  67. return beta;
  68. }
  69. export default sector;