index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // index.ts
  2. import { circle } from "@turf/circle";
  3. import { lineArc } from "@turf/line-arc";
  4. import { coordEach } from "@turf/meta";
  5. import { isObject, polygon } from "@turf/helpers";
  6. import { getCoords } from "@turf/invariant";
  7. function sector(center, radius, bearing1, bearing2, options = {}) {
  8. options = options || {};
  9. if (!isObject(options)) throw new Error("options is invalid");
  10. const properties = options.properties;
  11. if (!center) throw new Error("center is required");
  12. if (bearing1 === void 0 || bearing1 === null)
  13. throw new Error("bearing1 is required");
  14. if (bearing2 === void 0 || bearing2 === null)
  15. throw new Error("bearing2 is required");
  16. if (!radius) throw new Error("radius is required");
  17. if (typeof options !== "object") throw new Error("options must be an object");
  18. if (convertAngleTo360(bearing1) === convertAngleTo360(bearing2)) {
  19. return circle(center, radius, options);
  20. }
  21. const coords = getCoords(center);
  22. const arc = lineArc(center, radius, bearing1, bearing2, options);
  23. const sliceCoords = [[coords]];
  24. coordEach(arc, function(currentCoords) {
  25. sliceCoords[0].push(currentCoords);
  26. });
  27. sliceCoords[0].push(coords);
  28. return polygon(sliceCoords, properties);
  29. }
  30. function convertAngleTo360(alpha) {
  31. let beta = alpha % 360;
  32. if (beta < 0) {
  33. beta += 360;
  34. }
  35. return beta;
  36. }
  37. var turf_sector_default = sector;
  38. export {
  39. turf_sector_default as default,
  40. sector
  41. };
  42. //# sourceMappingURL=index.js.map