index.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 'use strict';
  2. var circle = require('@turf/circle');
  3. var lineArc = require('@turf/line-arc');
  4. var meta = require('@turf/meta');
  5. var helpers = require('@turf/helpers');
  6. var invariant = require('@turf/invariant');
  7. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  8. var circle__default = /*#__PURE__*/_interopDefaultLegacy(circle);
  9. var lineArc__default = /*#__PURE__*/_interopDefaultLegacy(lineArc);
  10. /**
  11. * Creates a circular sector of a circle of given radius and center {@link Point},
  12. * between (clockwise) bearing1 and bearing2; 0 bearing is North of center point, positive clockwise.
  13. *
  14. * @name sector
  15. * @param {Coord} center center point
  16. * @param {number} radius radius of the circle
  17. * @param {number} bearing1 angle, in decimal degrees, of the first radius of the sector
  18. * @param {number} bearing2 angle, in decimal degrees, of the second radius of the sector
  19. * @param {Object} [options={}] Optional parameters
  20. * @param {string} [options.units='kilometers'] miles, kilometers, degrees, or radians
  21. * @param {number} [options.steps=64] number of steps
  22. * @param {Properties} [options.properties={}] Translate properties to Feature Polygon
  23. * @returns {Feature<Polygon>} sector polygon
  24. * @example
  25. * var center = turf.point([-75, 40]);
  26. * var radius = 5;
  27. * var bearing1 = 25;
  28. * var bearing2 = 45;
  29. *
  30. * var sector = turf.sector(center, radius, bearing1, bearing2);
  31. *
  32. * //addToMap
  33. * var addToMap = [center, sector];
  34. */
  35. function sector(center, radius, bearing1, bearing2, options) {
  36. // Optional parameters
  37. options = options || {};
  38. if (!helpers.isObject(options)) throw new Error("options is invalid");
  39. var properties = options.properties;
  40. // validation
  41. if (!center) throw new Error("center is required");
  42. if (bearing1 === undefined || bearing1 === null)
  43. throw new Error("bearing1 is required");
  44. if (bearing2 === undefined || bearing2 === null)
  45. throw new Error("bearing2 is required");
  46. if (!radius) throw new Error("radius is required");
  47. if (typeof options !== "object") throw new Error("options must be an object");
  48. if (convertAngleTo360(bearing1) === convertAngleTo360(bearing2)) {
  49. return circle__default['default'](center, radius, options);
  50. }
  51. var coords = invariant.getCoords(center);
  52. var arc = lineArc__default['default'](center, radius, bearing1, bearing2, options);
  53. var sliceCoords = [[coords]];
  54. meta.coordEach(arc, function (currentCoords) {
  55. sliceCoords[0].push(currentCoords);
  56. });
  57. sliceCoords[0].push(coords);
  58. return helpers.polygon(sliceCoords, properties);
  59. }
  60. /**
  61. * Takes any angle in degrees
  62. * and returns a valid angle between 0-360 degrees
  63. *
  64. * @private
  65. * @param {number} alfa angle between -180-180 degrees
  66. * @returns {number} angle between 0-360 degrees
  67. */
  68. function convertAngleTo360(alfa) {
  69. var beta = alfa % 360;
  70. if (beta < 0) beta += 360;
  71. return beta;
  72. }
  73. module.exports = sector;
  74. module.exports.default = sector;