index.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. var centroid = require('@turf/centroid');
  3. var rhumbBearing = require('@turf/rhumb-bearing');
  4. var rhumbDistance = require('@turf/rhumb-distance');
  5. var rhumbDestination = require('@turf/rhumb-destination');
  6. var clone = require('@turf/clone');
  7. var meta = require('@turf/meta');
  8. var invariant = require('@turf/invariant');
  9. var helpers = require('@turf/helpers');
  10. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  11. var centroid__default = /*#__PURE__*/_interopDefaultLegacy(centroid);
  12. var rhumbBearing__default = /*#__PURE__*/_interopDefaultLegacy(rhumbBearing);
  13. var rhumbDistance__default = /*#__PURE__*/_interopDefaultLegacy(rhumbDistance);
  14. var rhumbDestination__default = /*#__PURE__*/_interopDefaultLegacy(rhumbDestination);
  15. var clone__default = /*#__PURE__*/_interopDefaultLegacy(clone);
  16. /**
  17. * Rotates any geojson Feature or Geometry of a specified angle, around its `centroid` or a given `pivot` point.
  18. *
  19. * @name transformRotate
  20. * @param {GeoJSON} geojson object to be rotated
  21. * @param {number} angle of rotation in decimal degrees, positive clockwise
  22. * @param {Object} [options={}] Optional parameters
  23. * @param {Coord} [options.pivot='centroid'] point around which the rotation will be performed
  24. * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
  25. * @returns {GeoJSON} the rotated GeoJSON feature
  26. * @example
  27. * var poly = turf.polygon([[[0,29],[3.5,29],[2.5,32],[0,29]]]);
  28. * var options = {pivot: [0, 25]};
  29. * var rotatedPoly = turf.transformRotate(poly, 10, options);
  30. *
  31. * //addToMap
  32. * var addToMap = [poly, rotatedPoly];
  33. * rotatedPoly.properties = {stroke: '#F00', 'stroke-width': 4};
  34. */
  35. function transformRotate(geojson, angle, options) {
  36. // Optional parameters
  37. options = options || {};
  38. if (!helpers.isObject(options)) throw new Error("options is invalid");
  39. var pivot = options.pivot;
  40. var mutate = options.mutate;
  41. // Input validation
  42. if (!geojson) throw new Error("geojson is required");
  43. if (angle === undefined || angle === null || isNaN(angle))
  44. throw new Error("angle is required");
  45. // Shortcut no-rotation
  46. if (angle === 0) return geojson;
  47. // Use centroid of GeoJSON if pivot is not provided
  48. if (!pivot) pivot = centroid__default['default'](geojson);
  49. // Clone geojson to avoid side effects
  50. if (mutate === false || mutate === undefined) geojson = clone__default['default'](geojson);
  51. // Rotate each coordinate
  52. meta.coordEach(geojson, function (pointCoords) {
  53. var initialAngle = rhumbBearing__default['default'](pivot, pointCoords);
  54. var finalAngle = initialAngle + angle;
  55. var distance = rhumbDistance__default['default'](pivot, pointCoords);
  56. var newCoords = invariant.getCoords(rhumbDestination__default['default'](pivot, distance, finalAngle));
  57. pointCoords[0] = newCoords[0];
  58. pointCoords[1] = newCoords[1];
  59. });
  60. return geojson;
  61. }
  62. module.exports = transformRotate;
  63. module.exports.default = transformRotate;