index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var bbox_1 = __importDefault(require("@turf/bbox"));
  7. var invariant_1 = require("@turf/invariant");
  8. var helpers_1 = require("@turf/helpers");
  9. var clone_1 = __importDefault(require("@turf/clone"));
  10. /**
  11. * Converts (Multi)LineString(s) to Polygon(s).
  12. *
  13. * @name lineToPolygon
  14. * @param {FeatureCollection|Feature<LineString|MultiLineString>} lines Features to convert
  15. * @param {Object} [options={}] Optional parameters
  16. * @param {Object} [options.properties={}] translates GeoJSON properties to Feature
  17. * @param {boolean} [options.autoComplete=true] auto complete linestrings (matches first & last coordinates)
  18. * @param {boolean} [options.orderCoords=true] sorts linestrings to place outer ring at the first position of the coordinates
  19. * @param {boolean} [options.mutate=false] mutate the original linestring using autoComplete (matches first & last coordinates)
  20. * @returns {Feature<Polygon|MultiPolygon>} converted to Polygons
  21. * @example
  22. * var line = turf.lineString([[125, -30], [145, -30], [145, -20], [125, -20], [125, -30]]);
  23. *
  24. * var polygon = turf.lineToPolygon(line);
  25. *
  26. * //addToMap
  27. * var addToMap = [polygon];
  28. */
  29. function lineToPolygon(lines, options) {
  30. if (options === void 0) { options = {}; }
  31. var _a, _b, _c;
  32. // Optional parameters
  33. var properties = options.properties;
  34. var autoComplete = (_a = options.autoComplete) !== null && _a !== void 0 ? _a : true;
  35. var orderCoords = (_b = options.orderCoords) !== null && _b !== void 0 ? _b : true;
  36. var mutate = (_c = options.mutate) !== null && _c !== void 0 ? _c : false;
  37. if (!mutate) {
  38. lines = clone_1.default(lines);
  39. }
  40. switch (lines.type) {
  41. case "FeatureCollection":
  42. var coords = [];
  43. lines.features.forEach(function (line) {
  44. coords.push(invariant_1.getCoords(lineStringToPolygon(line, {}, autoComplete, orderCoords)));
  45. });
  46. return helpers_1.multiPolygon(coords, properties);
  47. default:
  48. return lineStringToPolygon(lines, properties, autoComplete, orderCoords);
  49. }
  50. }
  51. /**
  52. * LineString to Polygon
  53. *
  54. * @private
  55. * @param {Feature<LineString|MultiLineString>} line line
  56. * @param {Object} [properties] translates GeoJSON properties to Feature
  57. * @param {boolean} [autoComplete=true] auto complete linestrings
  58. * @param {boolean} [orderCoords=true] sorts linestrings to place outer ring at the first position of the coordinates
  59. * @returns {Feature<Polygon>} line converted to Polygon
  60. */
  61. function lineStringToPolygon(line, properties, autoComplete, orderCoords) {
  62. properties = properties
  63. ? properties
  64. : line.type === "Feature"
  65. ? line.properties
  66. : {};
  67. var geom = invariant_1.getGeom(line);
  68. var coords = geom.coordinates;
  69. var type = geom.type;
  70. if (!coords.length)
  71. throw new Error("line must contain coordinates");
  72. switch (type) {
  73. case "LineString":
  74. if (autoComplete)
  75. coords = autoCompleteCoords(coords);
  76. return helpers_1.polygon([coords], properties);
  77. case "MultiLineString":
  78. var multiCoords = [];
  79. var largestArea = 0;
  80. coords.forEach(function (coord) {
  81. if (autoComplete)
  82. coord = autoCompleteCoords(coord);
  83. // Largest LineString to be placed in the first position of the coordinates array
  84. if (orderCoords) {
  85. var area = calculateArea(bbox_1.default(helpers_1.lineString(coord)));
  86. if (area > largestArea) {
  87. multiCoords.unshift(coord);
  88. largestArea = area;
  89. }
  90. else
  91. multiCoords.push(coord);
  92. }
  93. else {
  94. multiCoords.push(coord);
  95. }
  96. });
  97. return helpers_1.polygon(multiCoords, properties);
  98. default:
  99. throw new Error("geometry type " + type + " is not supported");
  100. }
  101. }
  102. /**
  103. * Auto Complete Coords - matches first & last coordinates
  104. *
  105. * @private
  106. * @param {Array<Array<number>>} coords Coordinates
  107. * @returns {Array<Array<number>>} auto completed coordinates
  108. */
  109. function autoCompleteCoords(coords) {
  110. var first = coords[0];
  111. var x1 = first[0];
  112. var y1 = first[1];
  113. var last = coords[coords.length - 1];
  114. var x2 = last[0];
  115. var y2 = last[1];
  116. if (x1 !== x2 || y1 !== y2) {
  117. coords.push(first);
  118. }
  119. return coords;
  120. }
  121. /**
  122. * area - quick approximate area calculation (used to sort)
  123. *
  124. * @private
  125. * @param {Array<number>} bbox BBox [west, south, east, north]
  126. * @returns {number} very quick area calculation
  127. */
  128. function calculateArea(bbox) {
  129. var west = bbox[0];
  130. var south = bbox[1];
  131. var east = bbox[2];
  132. var north = bbox[3];
  133. return Math.abs(west - east) * Math.abs(south - north);
  134. }
  135. exports.default = lineToPolygon;