index.cjs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});var __defProp = Object.defineProperty;
  2. var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
  3. // index.ts
  4. var _GeojsonEquality = class _GeojsonEquality {
  5. constructor(opts) {
  6. this.direction = false;
  7. this.compareProperties = true;
  8. var _a, _b, _c;
  9. this.precision = 10 ** -((_a = opts == null ? void 0 : opts.precision) != null ? _a : 17);
  10. this.direction = (_b = opts == null ? void 0 : opts.direction) != null ? _b : false;
  11. this.compareProperties = (_c = opts == null ? void 0 : opts.compareProperties) != null ? _c : true;
  12. }
  13. compare(g1, g2) {
  14. if (g1.type !== g2.type) {
  15. return false;
  16. }
  17. if (!sameLength(g1, g2)) {
  18. return false;
  19. }
  20. switch (g1.type) {
  21. case "Point":
  22. return this.compareCoord(g1.coordinates, g2.coordinates);
  23. case "LineString":
  24. return this.compareLine(g1.coordinates, g2.coordinates);
  25. case "Polygon":
  26. return this.comparePolygon(g1, g2);
  27. case "GeometryCollection":
  28. return this.compareGeometryCollection(g1, g2);
  29. case "Feature":
  30. return this.compareFeature(g1, g2);
  31. case "FeatureCollection":
  32. return this.compareFeatureCollection(g1, g2);
  33. default:
  34. if (g1.type.startsWith("Multi")) {
  35. const g1s = explode(g1);
  36. const g2s = explode(
  37. g2
  38. );
  39. return g1s.every(
  40. (g1part) => g2s.some((g2part) => this.compare(g1part, g2part))
  41. );
  42. }
  43. }
  44. return false;
  45. }
  46. compareCoord(c1, c2) {
  47. return c1.length === c2.length && c1.every((c, i) => Math.abs(c - c2[i]) < this.precision);
  48. }
  49. compareLine(path1, path2, ind = 0, isPoly = false) {
  50. if (!sameLength(path1, path2)) {
  51. return false;
  52. }
  53. const p1 = path1;
  54. let p2 = path2;
  55. if (isPoly && !this.compareCoord(p1[0], p2[0])) {
  56. const startIndex = this.fixStartIndex(p2, p1);
  57. if (!startIndex) {
  58. return false;
  59. } else {
  60. p2 = startIndex;
  61. }
  62. }
  63. const sameDirection = this.compareCoord(p1[ind], p2[ind]);
  64. if (this.direction || sameDirection) {
  65. return this.comparePath(p1, p2);
  66. } else {
  67. if (this.compareCoord(p1[ind], p2[p2.length - (1 + ind)])) {
  68. return this.comparePath(p1.slice().reverse(), p2);
  69. }
  70. return false;
  71. }
  72. }
  73. fixStartIndex(sourcePath, targetPath) {
  74. let correctPath, ind = -1;
  75. for (let i = 0; i < sourcePath.length; i++) {
  76. if (this.compareCoord(sourcePath[i], targetPath[0])) {
  77. ind = i;
  78. break;
  79. }
  80. }
  81. if (ind >= 0) {
  82. correctPath = [].concat(
  83. sourcePath.slice(ind, sourcePath.length),
  84. sourcePath.slice(1, ind + 1)
  85. );
  86. }
  87. return correctPath;
  88. }
  89. comparePath(p1, p2) {
  90. return p1.every((c, i) => this.compareCoord(c, p2[i]));
  91. }
  92. comparePolygon(g1, g2) {
  93. if (this.compareLine(g1.coordinates[0], g2.coordinates[0], 1, true)) {
  94. const holes1 = g1.coordinates.slice(1, g1.coordinates.length);
  95. const holes2 = g2.coordinates.slice(1, g2.coordinates.length);
  96. return holes1.every(
  97. (h1) => holes2.some((h2) => this.compareLine(h1, h2, 1, true))
  98. );
  99. }
  100. return false;
  101. }
  102. compareGeometryCollection(g1, g2) {
  103. return sameLength(g1.geometries, g2.geometries) && this.compareBBox(g1, g2) && g1.geometries.every((g, i) => this.compare(g, g2.geometries[i]));
  104. }
  105. compareFeature(g1, g2) {
  106. return g1.id === g2.id && (this.compareProperties ? equal(g1.properties, g2.properties) : true) && this.compareBBox(g1, g2) && this.compare(g1.geometry, g2.geometry);
  107. }
  108. compareFeatureCollection(g1, g2) {
  109. return sameLength(g1.features, g2.features) && this.compareBBox(g1, g2) && g1.features.every((f, i) => this.compare(f, g2.features[i]));
  110. }
  111. compareBBox(g1, g2) {
  112. return Boolean(!g1.bbox && !g2.bbox) || (g1.bbox && g2.bbox ? this.compareCoord(g1.bbox, g2.bbox) : false);
  113. }
  114. };
  115. __name(_GeojsonEquality, "GeojsonEquality");
  116. var GeojsonEquality = _GeojsonEquality;
  117. function sameLength(g1, g2) {
  118. return g1.coordinates ? g1.coordinates.length === g2.coordinates.length : g1.length === g2.length;
  119. }
  120. __name(sameLength, "sameLength");
  121. function explode(g) {
  122. return g.coordinates.map((part) => ({
  123. type: g.type.replace("Multi", ""),
  124. coordinates: part
  125. }));
  126. }
  127. __name(explode, "explode");
  128. function geojsonEquality(g1, g2, opts) {
  129. const eq = new GeojsonEquality(opts);
  130. return eq.compare(g1, g2);
  131. }
  132. __name(geojsonEquality, "geojsonEquality");
  133. function equal(object1, object2) {
  134. if (object1 === null && object2 === null) {
  135. return true;
  136. }
  137. if (object1 === null || object2 === null) {
  138. return false;
  139. }
  140. const objKeys1 = Object.keys(object1);
  141. const objKeys2 = Object.keys(object2);
  142. if (objKeys1.length !== objKeys2.length) return false;
  143. for (var key of objKeys1) {
  144. const value1 = object1[key];
  145. const value2 = object2[key];
  146. const isObjects = isObject(value1) && isObject(value2);
  147. if (isObjects && !equal(value1, value2) || !isObjects && value1 !== value2) {
  148. return false;
  149. }
  150. }
  151. return true;
  152. }
  153. __name(equal, "equal");
  154. var isObject = /* @__PURE__ */ __name((object) => {
  155. return object != null && typeof object === "object";
  156. }, "isObject");
  157. var geojson_equality_ts_default = GeojsonEquality;
  158. exports.GeojsonEquality = GeojsonEquality; exports.default = geojson_equality_ts_default; exports.geojsonEquality = geojsonEquality;
  159. //# sourceMappingURL=index.cjs.map