index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. var meta = require('@turf/meta');
  3. var helpers = require('@turf/helpers');
  4. var clone = require('@turf/clone');
  5. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  6. var clone__default = /*#__PURE__*/_interopDefaultLegacy(clone);
  7. /**
  8. * Takes input features and flips all of their coordinates from `[x, y]` to `[y, x]`.
  9. *
  10. * @name flip
  11. * @param {GeoJSON} geojson input features
  12. * @param {Object} [options={}] Optional parameters
  13. * @param {boolean} [options.mutate=false] allows GeoJSON input to be mutated (significant performance increase if true)
  14. * @returns {GeoJSON} a feature or set of features of the same type as `input` with flipped coordinates
  15. * @example
  16. * var serbia = turf.point([20.566406, 43.421008]);
  17. *
  18. * var saudiArabia = turf.flip(serbia);
  19. *
  20. * //addToMap
  21. * var addToMap = [serbia, saudiArabia];
  22. */
  23. function flip(geojson, options) {
  24. // Optional parameters
  25. options = options || {};
  26. if (!helpers.isObject(options)) throw new Error("options is invalid");
  27. var mutate = options.mutate;
  28. if (!geojson) throw new Error("geojson is required");
  29. // ensure that we don't modify features in-place and changes to the
  30. // output do not change the previous feature, including changes to nested
  31. // properties.
  32. if (mutate === false || mutate === undefined) geojson = clone__default['default'](geojson);
  33. meta.coordEach(geojson, function (coord) {
  34. var x = coord[0];
  35. var y = coord[1];
  36. coord[0] = y;
  37. coord[1] = x;
  38. });
  39. return geojson;
  40. }
  41. module.exports = flip;
  42. module.exports.default = flip;