index.js 1.3 KB

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