index.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. 'use strict';
  2. var meta = require('@turf/meta');
  3. var helpers = require('@turf/helpers');
  4. var invariant = require('@turf/invariant');
  5. var clone = require('@turf/clone');
  6. var rhumbDestination = require('@turf/rhumb-destination');
  7. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  8. var clone__default = /*#__PURE__*/_interopDefaultLegacy(clone);
  9. var rhumbDestination__default = /*#__PURE__*/_interopDefaultLegacy(rhumbDestination);
  10. /**
  11. * Moves any geojson Feature or Geometry of a specified distance along a Rhumb Line
  12. * on the provided direction angle.
  13. *
  14. * @name transformTranslate
  15. * @param {GeoJSON} geojson object to be translated
  16. * @param {number} distance length of the motion; negative values determine motion in opposite direction
  17. * @param {number} direction of the motion; angle from North in decimal degrees, positive clockwise
  18. * @param {Object} [options={}] Optional parameters
  19. * @param {string} [options.units='kilometers'] in which `distance` will be express; miles, kilometers, degrees, or radians
  20. * @param {number} [options.zTranslation=0] length of the vertical motion, same unit of distance
  21. * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
  22. * @returns {GeoJSON} the translated GeoJSON object
  23. * @example
  24. * var poly = turf.polygon([[[0,29],[3.5,29],[2.5,32],[0,29]]]);
  25. * var translatedPoly = turf.transformTranslate(poly, 100, 35);
  26. *
  27. * //addToMap
  28. * var addToMap = [poly, translatedPoly];
  29. * translatedPoly.properties = {stroke: '#F00', 'stroke-width': 4};
  30. */
  31. function transformTranslate(geojson, distance, direction, options) {
  32. // Optional parameters
  33. options = options || {};
  34. if (!helpers.isObject(options)) throw new Error("options is invalid");
  35. var units = options.units;
  36. var zTranslation = options.zTranslation;
  37. var mutate = options.mutate;
  38. // Input validation
  39. if (!geojson) throw new Error("geojson is required");
  40. if (distance === undefined || distance === null || isNaN(distance))
  41. throw new Error("distance is required");
  42. if (zTranslation && typeof zTranslation !== "number" && isNaN(zTranslation))
  43. throw new Error("zTranslation is not a number");
  44. // Shortcut no-motion
  45. zTranslation = zTranslation !== undefined ? zTranslation : 0;
  46. if (distance === 0 && zTranslation === 0) return geojson;
  47. if (direction === undefined || direction === null || isNaN(direction))
  48. throw new Error("direction is required");
  49. // Invert with negative distances
  50. if (distance < 0) {
  51. distance = -distance;
  52. direction = direction + 180;
  53. }
  54. // Clone geojson to avoid side effects
  55. if (mutate === false || mutate === undefined) geojson = clone__default['default'](geojson);
  56. // Translate each coordinate
  57. meta.coordEach(geojson, function (pointCoords) {
  58. var newCoords = invariant.getCoords(
  59. rhumbDestination__default['default'](pointCoords, distance, direction, { units: units })
  60. );
  61. pointCoords[0] = newCoords[0];
  62. pointCoords[1] = newCoords[1];
  63. if (zTranslation && pointCoords.length === 3)
  64. pointCoords[2] += zTranslation;
  65. });
  66. return geojson;
  67. }
  68. module.exports = transformTranslate;
  69. module.exports.default = transformTranslate;