index.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // index.ts
  2. import { clone } from "@turf/clone";
  3. import { booleanClockwise } from "@turf/boolean-clockwise";
  4. import { geomEach, featureEach } from "@turf/meta";
  5. import { getCoords } from "@turf/invariant";
  6. import { featureCollection, isObject } from "@turf/helpers";
  7. function rewind(geojson, options = {}) {
  8. var _a, _b;
  9. options = options || {};
  10. if (!isObject(options)) throw new Error("options is invalid");
  11. const mutate = (_a = options.mutate) != null ? _a : false;
  12. const reverse = (_b = options.reverse) != null ? _b : false;
  13. if (!geojson) throw new Error("<geojson> is required");
  14. if (typeof reverse !== "boolean")
  15. throw new Error("<reverse> must be a boolean");
  16. if (typeof mutate !== "boolean")
  17. throw new Error("<mutate> must be a boolean");
  18. if (!mutate && geojson.type !== "Point" && geojson.type !== "MultiPoint") {
  19. geojson = clone(geojson);
  20. }
  21. const results = [];
  22. switch (geojson.type) {
  23. case "GeometryCollection":
  24. geomEach(geojson, function(geometry) {
  25. rewindFeature(geometry, reverse);
  26. });
  27. return geojson;
  28. case "FeatureCollection":
  29. featureEach(geojson, function(feature) {
  30. const rewoundFeature = rewindFeature(feature, reverse);
  31. featureEach(rewoundFeature, function(result) {
  32. results.push(result);
  33. });
  34. });
  35. return featureCollection(results);
  36. }
  37. return rewindFeature(geojson, reverse);
  38. }
  39. function rewindFeature(geojson, reverse) {
  40. const type = geojson.type === "Feature" ? geojson.geometry.type : geojson.type;
  41. switch (type) {
  42. case "GeometryCollection":
  43. geomEach(geojson, function(geometry) {
  44. rewindFeature(geometry, reverse);
  45. });
  46. return geojson;
  47. case "LineString":
  48. rewindLineString(getCoords(geojson), reverse);
  49. return geojson;
  50. case "Polygon":
  51. rewindPolygon(getCoords(geojson), reverse);
  52. return geojson;
  53. case "MultiLineString":
  54. getCoords(geojson).forEach(function(lineCoords) {
  55. rewindLineString(lineCoords, reverse);
  56. });
  57. return geojson;
  58. case "MultiPolygon":
  59. getCoords(geojson).forEach(function(lineCoords) {
  60. rewindPolygon(lineCoords, reverse);
  61. });
  62. return geojson;
  63. case "Point":
  64. case "MultiPoint":
  65. return geojson;
  66. }
  67. }
  68. function rewindLineString(coords, reverse) {
  69. if (booleanClockwise(coords) === reverse) coords.reverse();
  70. }
  71. function rewindPolygon(coords, reverse) {
  72. if (booleanClockwise(coords[0]) !== reverse) {
  73. coords[0].reverse();
  74. }
  75. for (let i = 1; i < coords.length; i++) {
  76. if (booleanClockwise(coords[i]) === reverse) {
  77. coords[i].reverse();
  78. }
  79. }
  80. }
  81. var turf_rewind_default = rewind;
  82. export {
  83. turf_rewind_default as default,
  84. rewind
  85. };
  86. //# sourceMappingURL=index.js.map