index.cjs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
  2. var _distance = require('@turf/distance');
  3. var _helpers = require('@turf/helpers');
  4. var _meta = require('@turf/meta');
  5. var _tin = require('@turf/tin');
  6. // lib/turf-dissolve.ts
  7. var _clone = require('@turf/clone');
  8. var _invariant = require('@turf/invariant');
  9. // lib/turf-line-dissolve.ts
  10. function lineDissolve(geojson, options = {}) {
  11. options = options || {};
  12. if (!_helpers.isObject.call(void 0, options)) {
  13. throw new Error("options is invalid");
  14. }
  15. const mutate = options.mutate;
  16. if (_invariant.getType.call(void 0, geojson) !== "FeatureCollection") {
  17. throw new Error("geojson must be a FeatureCollection");
  18. }
  19. if (!geojson.features.length) {
  20. throw new Error("geojson is empty");
  21. }
  22. if (mutate === false || mutate === void 0) {
  23. geojson = _clone.clone.call(void 0, geojson);
  24. }
  25. const result = [];
  26. const lastLine = _meta.lineReduce.call(void 0,
  27. geojson,
  28. (previousLine, currentLine) => {
  29. const merged = mergeLineStrings(previousLine, currentLine);
  30. if (merged) {
  31. return merged;
  32. } else {
  33. result.push(previousLine);
  34. return currentLine;
  35. }
  36. }
  37. );
  38. if (lastLine) {
  39. result.push(lastLine);
  40. }
  41. if (!result.length) {
  42. return null;
  43. } else if (result.length === 1) {
  44. return result[0];
  45. } else {
  46. return _helpers.multiLineString.call(void 0,
  47. result.map((line) => {
  48. return line.coordinates;
  49. })
  50. );
  51. }
  52. }
  53. function coordId(coord) {
  54. return coord[0].toString() + "," + coord[1].toString();
  55. }
  56. function mergeLineStrings(a, b) {
  57. const coords1 = a.geometry.coordinates;
  58. const coords2 = b.geometry.coordinates;
  59. const s1 = coordId(coords1[0]);
  60. const e1 = coordId(coords1[coords1.length - 1]);
  61. const s2 = coordId(coords2[0]);
  62. const e2 = coordId(coords2[coords2.length - 1]);
  63. let coords;
  64. if (s1 === e2) {
  65. coords = coords2.concat(coords1.slice(1));
  66. } else if (s2 === e1) {
  67. coords = coords1.concat(coords2.slice(1));
  68. } else if (s1 === s2) {
  69. coords = coords1.slice(1).reverse().concat(coords2);
  70. } else if (e1 === e2) {
  71. coords = coords1.concat(coords2.reverse().slice(1));
  72. } else {
  73. return null;
  74. }
  75. return _helpers.lineString.call(void 0, coords);
  76. }
  77. // lib/turf-polygon-dissolve.ts
  78. var _topojsonclient = require('topojson-client');
  79. var _topojsonserver = require('topojson-server');
  80. function polygonDissolve(geojson, options = {}) {
  81. if (_invariant.getType.call(void 0, geojson) !== "FeatureCollection") {
  82. throw new Error("geojson must be a FeatureCollection");
  83. }
  84. if (!geojson.features.length) {
  85. throw new Error("geojson is empty");
  86. }
  87. if (options.mutate === false || options.mutate === void 0) {
  88. geojson = _clone.clone.call(void 0, geojson);
  89. }
  90. const geoms = [];
  91. _meta.flattenEach.call(void 0, geojson, (feature2) => {
  92. geoms.push(feature2.geometry);
  93. });
  94. const topo = _topojsonserver.topology.call(void 0, { geoms: _helpers.geometryCollection.call(void 0, geoms).geometry });
  95. const merged = _topojsonclient.merge.call(void 0, topo, topo.objects.geoms.geometries);
  96. return merged;
  97. }
  98. // lib/turf-dissolve.ts
  99. function dissolve(geojson, options = {}) {
  100. options = options || {};
  101. if (!_helpers.isObject.call(void 0, options)) {
  102. throw new Error("options is invalid");
  103. }
  104. const mutate = options.mutate;
  105. if (_invariant.getType.call(void 0, geojson) !== "FeatureCollection") {
  106. throw new Error("geojson must be a FeatureCollection");
  107. }
  108. if (!geojson.features.length) {
  109. throw new Error("geojson is empty");
  110. }
  111. if (mutate === false || mutate === void 0) {
  112. geojson = _clone.clone.call(void 0, geojson);
  113. }
  114. const type = getHomogenousType(geojson);
  115. if (!type) {
  116. throw new Error("geojson must be homogenous");
  117. }
  118. const data = geojson;
  119. switch (type) {
  120. case "LineString":
  121. return lineDissolve(data, options);
  122. case "Polygon":
  123. return polygonDissolve(data, options);
  124. default:
  125. throw new Error(type + " is not supported");
  126. }
  127. }
  128. function getHomogenousType(geojson) {
  129. const types = {};
  130. _meta.flattenEach.call(void 0, geojson, (feature2) => {
  131. types[feature2.geometry.type] = true;
  132. });
  133. const keys = Object.keys(types);
  134. if (keys.length === 1) {
  135. return keys[0];
  136. }
  137. return null;
  138. }
  139. // index.ts
  140. function concave(points, options = {}) {
  141. const maxEdge = options.maxEdge || Infinity;
  142. const cleaned = removeDuplicates(points);
  143. const tinPolys = _tin.tin.call(void 0, cleaned);
  144. tinPolys.features = tinPolys.features.filter((triangle) => {
  145. const pt1 = triangle.geometry.coordinates[0][0];
  146. const pt2 = triangle.geometry.coordinates[0][1];
  147. const pt3 = triangle.geometry.coordinates[0][2];
  148. const dist1 = _distance.distance.call(void 0, pt1, pt2, options);
  149. const dist2 = _distance.distance.call(void 0, pt2, pt3, options);
  150. const dist3 = _distance.distance.call(void 0, pt1, pt3, options);
  151. return dist1 <= maxEdge && dist2 <= maxEdge && dist3 <= maxEdge;
  152. });
  153. if (tinPolys.features.length < 1) {
  154. return null;
  155. }
  156. const dissolved = dissolve(tinPolys);
  157. if (dissolved.coordinates.length === 1) {
  158. dissolved.coordinates = dissolved.coordinates[0];
  159. dissolved.type = "Polygon";
  160. }
  161. return _helpers.feature.call(void 0, dissolved);
  162. }
  163. function removeDuplicates(points) {
  164. const cleaned = [];
  165. const existing = {};
  166. _meta.featureEach.call(void 0, points, (pt) => {
  167. if (!pt.geometry) {
  168. return;
  169. }
  170. const key = pt.geometry.coordinates.join("-");
  171. if (!Object.prototype.hasOwnProperty.call(existing, key)) {
  172. cleaned.push(pt);
  173. existing[key] = true;
  174. }
  175. });
  176. return _helpers.featureCollection.call(void 0, cleaned);
  177. }
  178. var turf_concave_default = concave;
  179. exports.concave = concave; exports.default = turf_concave_default;
  180. //# sourceMappingURL=index.cjs.map