index.cjs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 _area = require('@turf/area');
  20. var _booleanpointinpolygon = require('@turf/boolean-point-in-polygon');
  21. var _explode = require('@turf/explode');
  22. var _invariant = require('@turf/invariant');
  23. var _helpers = require('@turf/helpers');
  24. // lib/grid-to-matrix.js
  25. var _meta = require('@turf/meta');
  26. function gridToMatrix(grid, options) {
  27. options = options || {};
  28. if (!_helpers.isObject.call(void 0, options)) throw new Error("options is invalid");
  29. var zProperty = options.zProperty || "elevation";
  30. var flip = options.flip;
  31. var flags = options.flags;
  32. _invariant.collectionOf.call(void 0, grid, "Point", "input must contain Points");
  33. var pointsMatrix = sortPointsByLatLng(grid, flip);
  34. var matrix = [];
  35. for (var r = 0; r < pointsMatrix.length; r++) {
  36. var pointRow = pointsMatrix[r];
  37. var row = [];
  38. for (var c = 0; c < pointRow.length; c++) {
  39. var point = pointRow[c];
  40. if (point.properties[zProperty]) row.push(point.properties[zProperty]);
  41. else row.push(0);
  42. if (flags === true) point.properties.matrixPosition = [r, c];
  43. }
  44. matrix.push(row);
  45. }
  46. return matrix;
  47. }
  48. function sortPointsByLatLng(points, flip) {
  49. var pointsByLatitude = {};
  50. _meta.featureEach.call(void 0, points, function(point) {
  51. var lat = _invariant.getCoords.call(void 0, point)[1];
  52. if (!pointsByLatitude[lat]) pointsByLatitude[lat] = [];
  53. pointsByLatitude[lat].push(point);
  54. });
  55. var orderedRowsByLatitude = Object.keys(pointsByLatitude).map(function(lat) {
  56. var row = pointsByLatitude[lat];
  57. var rowOrderedByLongitude = row.sort(function(a, b) {
  58. return _invariant.getCoords.call(void 0, a)[0] - _invariant.getCoords.call(void 0, b)[0];
  59. });
  60. return rowOrderedByLongitude;
  61. });
  62. var pointMatrix = orderedRowsByLatitude.sort(function(a, b) {
  63. if (flip) return _invariant.getCoords.call(void 0, a[0])[1] - _invariant.getCoords.call(void 0, b[0])[1];
  64. else return _invariant.getCoords.call(void 0, b[0])[1] - _invariant.getCoords.call(void 0, a[0])[1];
  65. });
  66. return pointMatrix;
  67. }
  68. // index.ts
  69. var _marchingsquares = require('marchingsquares');
  70. function isobands(pointGrid, breaks, options) {
  71. options = options || {};
  72. if (!_helpers.isObject.call(void 0, options)) throw new Error("options is invalid");
  73. const zProperty = options.zProperty || "elevation";
  74. const commonProperties = options.commonProperties || {};
  75. const breaksProperties = options.breaksProperties || [];
  76. _invariant.collectionOf.call(void 0, pointGrid, "Point", "Input must contain Points");
  77. if (!breaks) throw new Error("breaks is required");
  78. if (!Array.isArray(breaks)) throw new Error("breaks is not an Array");
  79. if (!_helpers.isObject.call(void 0, commonProperties))
  80. throw new Error("commonProperties is not an Object");
  81. if (!Array.isArray(breaksProperties))
  82. throw new Error("breaksProperties is not an Array");
  83. const matrix = gridToMatrix(pointGrid, { zProperty, flip: true });
  84. let contours = createContourLines(matrix, breaks, zProperty);
  85. contours = rescaleContours(contours, matrix, pointGrid);
  86. const multipolygons = contours.map((contour, index) => {
  87. if (breaksProperties[index] && !_helpers.isObject.call(void 0, breaksProperties[index])) {
  88. throw new Error("Each mappedProperty is required to be an Object");
  89. }
  90. const contourProperties = __spreadValues(__spreadValues({}, commonProperties), breaksProperties[index]);
  91. contourProperties[zProperty] = contour[zProperty];
  92. const multiP = _helpers.multiPolygon.call(void 0,
  93. contour.groupedRings,
  94. contourProperties
  95. );
  96. return multiP;
  97. });
  98. return _helpers.featureCollection.call(void 0, multipolygons);
  99. }
  100. function createContourLines(matrix, breaks, property) {
  101. const contours = [];
  102. for (let i = 1; i < breaks.length; i++) {
  103. const lowerBand = +breaks[i - 1];
  104. const upperBand = +breaks[i];
  105. const isobandsCoords = _marchingsquares.isoBands.call(void 0, matrix, lowerBand, upperBand - lowerBand);
  106. const nestedRings = orderByArea(isobandsCoords);
  107. const groupedRings = groupNestedRings(nestedRings);
  108. contours.push({
  109. groupedRings,
  110. [property]: lowerBand + "-" + upperBand
  111. });
  112. }
  113. return contours;
  114. }
  115. function rescaleContours(contours, matrix, points) {
  116. const gridBbox = _bbox.bbox.call(void 0, points);
  117. const originalWidth = gridBbox[2] - gridBbox[0];
  118. const originalHeigth = gridBbox[3] - gridBbox[1];
  119. const x0 = gridBbox[0];
  120. const y0 = gridBbox[1];
  121. const matrixWidth = matrix[0].length - 1;
  122. const matrixHeight = matrix.length - 1;
  123. const scaleX = originalWidth / matrixWidth;
  124. const scaleY = originalHeigth / matrixHeight;
  125. return contours.map(function(contour) {
  126. contour.groupedRings = contour.groupedRings.map(
  127. function(lineRingSet) {
  128. return lineRingSet.map(function(lineRing) {
  129. return lineRing.map((point) => [
  130. point[0] * scaleX + x0,
  131. point[1] * scaleY + y0
  132. ]);
  133. });
  134. }
  135. );
  136. return contour;
  137. });
  138. }
  139. function orderByArea(ringsCoords) {
  140. const ringsWithArea = ringsCoords.map(function(coords) {
  141. return { ring: coords, area: _area.area.call(void 0, _helpers.polygon.call(void 0, [coords])) };
  142. });
  143. ringsWithArea.sort(function(a, b) {
  144. return b.area - a.area;
  145. });
  146. return ringsWithArea.map(function(x) {
  147. return x.ring;
  148. });
  149. }
  150. function groupNestedRings(orderedLinearRings) {
  151. const lrList = orderedLinearRings.map((lr) => {
  152. return { lrCoordinates: lr, grouped: false };
  153. });
  154. const groupedLinearRingsCoords = [];
  155. while (!allGrouped(lrList)) {
  156. for (let i = 0; i < lrList.length; i++) {
  157. if (!lrList[i].grouped) {
  158. const group = [];
  159. group.push(lrList[i].lrCoordinates);
  160. lrList[i].grouped = true;
  161. const outerMostPoly = _helpers.polygon.call(void 0, [lrList[i].lrCoordinates]);
  162. for (let j = i + 1; j < lrList.length; j++) {
  163. if (!lrList[j].grouped) {
  164. const lrPoly = _helpers.polygon.call(void 0, [lrList[j].lrCoordinates]);
  165. if (isInside(lrPoly, outerMostPoly)) {
  166. group.push(lrList[j].lrCoordinates);
  167. lrList[j].grouped = true;
  168. }
  169. }
  170. }
  171. groupedLinearRingsCoords.push(group);
  172. }
  173. }
  174. }
  175. return groupedLinearRingsCoords;
  176. }
  177. function isInside(testPolygon, targetPolygon) {
  178. const points = _explode.explode.call(void 0, testPolygon);
  179. for (let i = 0; i < points.features.length; i++) {
  180. if (!_booleanpointinpolygon.booleanPointInPolygon.call(void 0, points.features[i], targetPolygon)) {
  181. return false;
  182. }
  183. }
  184. return true;
  185. }
  186. function allGrouped(list) {
  187. for (let i = 0; i < list.length; i++) {
  188. if (list[i].grouped === false) {
  189. return false;
  190. }
  191. }
  192. return true;
  193. }
  194. var turf_isobands_default = isobands;
  195. exports.default = turf_isobands_default; exports.isobands = isobands;
  196. //# sourceMappingURL=index.cjs.map