index.js 7.1 KB

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