index.cjs 5.1 KB

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