index.cjs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
  2. var _helpers = require('@turf/helpers');
  3. var _invariant = require('@turf/invariant');
  4. function cleanCoords(geojson, options = {}) {
  5. var mutate = typeof options === "object" ? options.mutate : options;
  6. if (!geojson) throw new Error("geojson is required");
  7. var type = _invariant.getType.call(void 0, geojson);
  8. var newCoords = [];
  9. switch (type) {
  10. case "LineString":
  11. newCoords = cleanLine(geojson, type);
  12. break;
  13. case "MultiLineString":
  14. case "Polygon":
  15. _invariant.getCoords.call(void 0, geojson).forEach(function(line) {
  16. newCoords.push(cleanLine(line, type));
  17. });
  18. break;
  19. case "MultiPolygon":
  20. _invariant.getCoords.call(void 0, geojson).forEach(function(polygons) {
  21. var polyPoints = [];
  22. polygons.forEach(function(ring) {
  23. polyPoints.push(cleanLine(ring, type));
  24. });
  25. newCoords.push(polyPoints);
  26. });
  27. break;
  28. case "Point":
  29. return geojson;
  30. case "MultiPoint":
  31. var existing = {};
  32. _invariant.getCoords.call(void 0, geojson).forEach(function(coord) {
  33. var key = coord.join("-");
  34. if (!Object.prototype.hasOwnProperty.call(existing, key)) {
  35. newCoords.push(coord);
  36. existing[key] = true;
  37. }
  38. });
  39. break;
  40. default:
  41. throw new Error(type + " geometry not supported");
  42. }
  43. if (geojson.coordinates) {
  44. if (mutate === true) {
  45. geojson.coordinates = newCoords;
  46. return geojson;
  47. }
  48. return { type, coordinates: newCoords };
  49. } else {
  50. if (mutate === true) {
  51. geojson.geometry.coordinates = newCoords;
  52. return geojson;
  53. }
  54. return _helpers.feature.call(void 0, { type, coordinates: newCoords }, geojson.properties, {
  55. bbox: geojson.bbox,
  56. id: geojson.id
  57. });
  58. }
  59. }
  60. function cleanLine(line, type) {
  61. var points = _invariant.getCoords.call(void 0, line);
  62. if (points.length === 2 && !equals(points[0], points[1])) return points;
  63. var newPoints = [];
  64. var secondToLast = points.length - 1;
  65. var newPointsLength = newPoints.length;
  66. newPoints.push(points[0]);
  67. for (var i = 1; i < secondToLast; i++) {
  68. var prevAddedPoint = newPoints[newPoints.length - 1];
  69. if (points[i][0] === prevAddedPoint[0] && points[i][1] === prevAddedPoint[1])
  70. continue;
  71. else {
  72. newPoints.push(points[i]);
  73. newPointsLength = newPoints.length;
  74. if (newPointsLength > 2) {
  75. if (isPointOnLineSegment(
  76. newPoints[newPointsLength - 3],
  77. newPoints[newPointsLength - 1],
  78. newPoints[newPointsLength - 2]
  79. ))
  80. newPoints.splice(newPoints.length - 2, 1);
  81. }
  82. }
  83. }
  84. newPoints.push(points[points.length - 1]);
  85. newPointsLength = newPoints.length;
  86. if ((type === "Polygon" || type === "MultiPolygon") && equals(points[0], points[points.length - 1]) && newPointsLength < 4) {
  87. throw new Error("invalid polygon");
  88. }
  89. if (type === "LineString" && newPointsLength < 3) {
  90. return newPoints;
  91. }
  92. if (isPointOnLineSegment(
  93. newPoints[newPointsLength - 3],
  94. newPoints[newPointsLength - 1],
  95. newPoints[newPointsLength - 2]
  96. ))
  97. newPoints.splice(newPoints.length - 2, 1);
  98. return newPoints;
  99. }
  100. function equals(pt1, pt2) {
  101. return pt1[0] === pt2[0] && pt1[1] === pt2[1];
  102. }
  103. function isPointOnLineSegment(start, end, point) {
  104. var x = point[0], y = point[1];
  105. var startX = start[0], startY = start[1];
  106. var endX = end[0], endY = end[1];
  107. var dxc = x - startX;
  108. var dyc = y - startY;
  109. var dxl = endX - startX;
  110. var dyl = endY - startY;
  111. var cross = dxc * dyl - dyc * dxl;
  112. if (cross !== 0) return false;
  113. else if (Math.abs(dxl) >= Math.abs(dyl))
  114. return dxl > 0 ? startX <= x && x <= endX : endX <= x && x <= startX;
  115. else return dyl > 0 ? startY <= y && y <= endY : endY <= y && y <= startY;
  116. }
  117. var turf_clean_coords_default = cleanCoords;
  118. exports.cleanCoords = cleanCoords; exports.default = turf_clean_coords_default;
  119. //# sourceMappingURL=index.cjs.map