index.js 5.7 KB

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