index.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // index.ts
  2. import { bbox as turfBBox } from "@turf/bbox";
  3. import { getCoords, getGeom } from "@turf/invariant";
  4. import { polygon, multiPolygon, lineString } from "@turf/helpers";
  5. import { clone } from "@turf/clone";
  6. function lineToPolygon(lines, options = {}) {
  7. var _a, _b, _c;
  8. var properties = options.properties;
  9. var autoComplete = (_a = options.autoComplete) != null ? _a : true;
  10. var orderCoords = (_b = options.orderCoords) != null ? _b : true;
  11. var mutate = (_c = options.mutate) != null ? _c : false;
  12. if (!mutate) {
  13. lines = clone(lines);
  14. }
  15. switch (lines.type) {
  16. case "FeatureCollection":
  17. var coords = [];
  18. lines.features.forEach(function(line) {
  19. coords.push(
  20. getCoords(lineStringToPolygon(line, {}, autoComplete, orderCoords))
  21. );
  22. });
  23. return multiPolygon(coords, properties);
  24. default:
  25. return lineStringToPolygon(lines, properties, autoComplete, orderCoords);
  26. }
  27. }
  28. function lineStringToPolygon(line, properties, autoComplete, orderCoords) {
  29. properties = properties ? properties : line.type === "Feature" ? line.properties : {};
  30. var geom = getGeom(line);
  31. var coords = geom.coordinates;
  32. var type = geom.type;
  33. if (!coords.length) throw new Error("line must contain coordinates");
  34. switch (type) {
  35. case "LineString":
  36. if (autoComplete) coords = autoCompleteCoords(coords);
  37. return polygon([coords], properties);
  38. case "MultiLineString":
  39. var multiCoords = [];
  40. var largestArea = 0;
  41. coords.forEach(function(coord) {
  42. if (autoComplete) coord = autoCompleteCoords(coord);
  43. if (orderCoords) {
  44. var area = calculateArea(turfBBox(lineString(coord)));
  45. if (area > largestArea) {
  46. multiCoords.unshift(coord);
  47. largestArea = area;
  48. } else multiCoords.push(coord);
  49. } else {
  50. multiCoords.push(coord);
  51. }
  52. });
  53. return polygon(multiCoords, properties);
  54. default:
  55. throw new Error("geometry type " + type + " is not supported");
  56. }
  57. }
  58. function autoCompleteCoords(coords) {
  59. var first = coords[0];
  60. var x1 = first[0];
  61. var y1 = first[1];
  62. var last = coords[coords.length - 1];
  63. var x2 = last[0];
  64. var y2 = last[1];
  65. if (x1 !== x2 || y1 !== y2) {
  66. coords.push(first);
  67. }
  68. return coords;
  69. }
  70. function calculateArea(bbox) {
  71. var west = bbox[0];
  72. var south = bbox[1];
  73. var east = bbox[2];
  74. var north = bbox[3];
  75. return Math.abs(west - east) * Math.abs(south - north);
  76. }
  77. var turf_line_to_polygon_default = lineToPolygon;
  78. export {
  79. turf_line_to_polygon_default as default,
  80. lineToPolygon
  81. };
  82. //# sourceMappingURL=index.js.map