index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropSymbols = Object.getOwnPropertySymbols;
  3. var __hasOwnProp = Object.prototype.hasOwnProperty;
  4. var __propIsEnum = Object.prototype.propertyIsEnumerable;
  5. var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
  6. var __spreadValues = (a, b) => {
  7. for (var prop in b || (b = {}))
  8. if (__hasOwnProp.call(b, prop))
  9. __defNormalProp(a, prop, b[prop]);
  10. if (__getOwnPropSymbols)
  11. for (var prop of __getOwnPropSymbols(b)) {
  12. if (__propIsEnum.call(b, prop))
  13. __defNormalProp(a, prop, b[prop]);
  14. }
  15. return a;
  16. };
  17. // index.ts
  18. import { bbox } from "@turf/bbox";
  19. import { coordEach } from "@turf/meta";
  20. import { collectionOf as collectionOf2 } from "@turf/invariant";
  21. import { multiLineString, featureCollection, isObject as isObject2 } from "@turf/helpers";
  22. import { isoContours } from "marchingsquares";
  23. // lib/grid-to-matrix.js
  24. import { getCoords, collectionOf } from "@turf/invariant";
  25. import { featureEach } from "@turf/meta";
  26. import { isObject } from "@turf/helpers";
  27. function gridToMatrix(grid, options) {
  28. options = options || {};
  29. if (!isObject(options)) throw new Error("options is invalid");
  30. var zProperty = options.zProperty || "elevation";
  31. var flip = options.flip;
  32. var flags = options.flags;
  33. collectionOf(grid, "Point", "input must contain Points");
  34. var pointsMatrix = sortPointsByLatLng(grid, flip);
  35. var matrix = [];
  36. for (var r = 0; r < pointsMatrix.length; r++) {
  37. var pointRow = pointsMatrix[r];
  38. var row = [];
  39. for (var c = 0; c < pointRow.length; c++) {
  40. var point = pointRow[c];
  41. if (point.properties[zProperty]) row.push(point.properties[zProperty]);
  42. else row.push(0);
  43. if (flags === true) point.properties.matrixPosition = [r, c];
  44. }
  45. matrix.push(row);
  46. }
  47. return matrix;
  48. }
  49. function sortPointsByLatLng(points, flip) {
  50. var pointsByLatitude = {};
  51. featureEach(points, function(point) {
  52. var lat = getCoords(point)[1];
  53. if (!pointsByLatitude[lat]) pointsByLatitude[lat] = [];
  54. pointsByLatitude[lat].push(point);
  55. });
  56. var orderedRowsByLatitude = Object.keys(pointsByLatitude).map(function(lat) {
  57. var row = pointsByLatitude[lat];
  58. var rowOrderedByLongitude = row.sort(function(a, b) {
  59. return getCoords(a)[0] - getCoords(b)[0];
  60. });
  61. return rowOrderedByLongitude;
  62. });
  63. var pointMatrix = orderedRowsByLatitude.sort(function(a, b) {
  64. if (flip) return getCoords(a[0])[1] - getCoords(b[0])[1];
  65. else return getCoords(b[0])[1] - getCoords(a[0])[1];
  66. });
  67. return pointMatrix;
  68. }
  69. // index.ts
  70. function isolines(pointGrid, breaks, options) {
  71. options = options || {};
  72. if (!isObject2(options)) throw new Error("options is invalid");
  73. const zProperty = options.zProperty || "elevation";
  74. const commonProperties = options.commonProperties || {};
  75. const breaksProperties = options.breaksProperties || [];
  76. collectionOf2(pointGrid, "Point", "Input must contain Points");
  77. if (!breaks) throw new Error("breaks is required");
  78. if (!Array.isArray(breaks)) throw new Error("breaks must be an Array");
  79. if (!isObject2(commonProperties))
  80. throw new Error("commonProperties must be an Object");
  81. if (!Array.isArray(breaksProperties))
  82. throw new Error("breaksProperties must be an Array");
  83. const matrix = gridToMatrix(pointGrid, { zProperty, flip: true });
  84. const createdIsoLines = createIsoLines(
  85. matrix,
  86. breaks,
  87. zProperty,
  88. commonProperties,
  89. breaksProperties
  90. );
  91. const scaledIsolines = rescaleIsolines(createdIsoLines, matrix, pointGrid);
  92. return featureCollection(scaledIsolines);
  93. }
  94. function createIsoLines(matrix, breaks, zProperty, commonProperties, breaksProperties) {
  95. const results = [];
  96. for (let i = 0; i < breaks.length; i++) {
  97. const threshold = +breaks[i];
  98. const properties = __spreadValues(__spreadValues({}, commonProperties), breaksProperties[i]);
  99. properties[zProperty] = threshold;
  100. const isoline = multiLineString(
  101. isoContours(matrix, threshold, { linearRing: false, noFrame: true }),
  102. properties
  103. );
  104. results.push(isoline);
  105. }
  106. return results;
  107. }
  108. function rescaleIsolines(createdIsoLines, matrix, points) {
  109. const gridBbox = bbox(points);
  110. const originalWidth = gridBbox[2] - gridBbox[0];
  111. const originalHeigth = gridBbox[3] - gridBbox[1];
  112. const x0 = gridBbox[0];
  113. const y0 = gridBbox[1];
  114. const matrixWidth = matrix[0].length - 1;
  115. const matrixHeight = matrix.length - 1;
  116. const scaleX = originalWidth / matrixWidth;
  117. const scaleY = originalHeigth / matrixHeight;
  118. const resize = (point) => {
  119. point[0] = point[0] * scaleX + x0;
  120. point[1] = point[1] * scaleY + y0;
  121. };
  122. createdIsoLines.forEach((isoline) => {
  123. coordEach(isoline, resize);
  124. });
  125. return createdIsoLines;
  126. }
  127. var turf_isolines_default = isolines;
  128. export {
  129. turf_isolines_default as default,
  130. isolines
  131. };
  132. //# sourceMappingURL=index.js.map