index.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var distance_1 = __importDefault(require("@turf/distance"));
  7. var intersect_1 = __importDefault(require("@turf/intersect"));
  8. var helpers_1 = require("@turf/helpers");
  9. /**
  10. * Takes a bounding box and a cell depth and returns a set of triangular {@link Polygon|polygons} in a grid.
  11. *
  12. * @name triangleGrid
  13. * @param {Array<number>} bbox extent in [minX, minY, maxX, maxY] order
  14. * @param {number} cellSide dimension of each cell
  15. * @param {Object} [options={}] Optional parameters
  16. * @param {string} [options.units='kilometers'] used in calculating cellSide, can be degrees, radians, miles, or kilometers
  17. * @param {Feature<Polygon>} [options.mask] if passed a Polygon or MultiPolygon, the grid Points will be created only inside it
  18. * @param {Object} [options.properties={}] passed to each point of the grid
  19. * @returns {FeatureCollection<Polygon>} grid of polygons
  20. * @example
  21. * var bbox = [-95, 30 ,-85, 40];
  22. * var cellSide = 50;
  23. * var options = {units: 'miles'};
  24. *
  25. * var triangleGrid = turf.triangleGrid(bbox, cellSide, options);
  26. *
  27. * //addToMap
  28. * var addToMap = [triangleGrid];
  29. */
  30. function triangleGrid(bbox, cellSide, options) {
  31. if (options === void 0) { options = {}; }
  32. // Containers
  33. var results = [];
  34. // Input Validation is being handled by Typescript
  35. // if (cellSide === null || cellSide === undefined) throw new Error('cellSide is required');
  36. // if (!isNumber(cellSide)) throw new Error('cellSide is invalid');
  37. // if (!bbox) throw new Error('bbox is required');
  38. // if (!Array.isArray(bbox)) throw new Error('bbox must be array');
  39. // if (bbox.length !== 4) throw new Error('bbox must contain 4 numbers');
  40. // if (mask && ['Polygon', 'MultiPolygon'].indexOf(getType(mask)) === -1) throw new Error('options.mask must be a (Multi)Polygon');
  41. // Main
  42. var xFraction = cellSide / distance_1.default([bbox[0], bbox[1]], [bbox[2], bbox[1]], options);
  43. var cellWidth = xFraction * (bbox[2] - bbox[0]);
  44. var yFraction = cellSide / distance_1.default([bbox[0], bbox[1]], [bbox[0], bbox[3]], options);
  45. var cellHeight = yFraction * (bbox[3] - bbox[1]);
  46. var xi = 0;
  47. var currentX = bbox[0];
  48. while (currentX <= bbox[2]) {
  49. var yi = 0;
  50. var currentY = bbox[1];
  51. while (currentY <= bbox[3]) {
  52. var cellTriangle1 = null;
  53. var cellTriangle2 = null;
  54. if (xi % 2 === 0 && yi % 2 === 0) {
  55. cellTriangle1 = helpers_1.polygon([
  56. [
  57. [currentX, currentY],
  58. [currentX, currentY + cellHeight],
  59. [currentX + cellWidth, currentY],
  60. [currentX, currentY],
  61. ],
  62. ], options.properties);
  63. cellTriangle2 = helpers_1.polygon([
  64. [
  65. [currentX, currentY + cellHeight],
  66. [currentX + cellWidth, currentY + cellHeight],
  67. [currentX + cellWidth, currentY],
  68. [currentX, currentY + cellHeight],
  69. ],
  70. ], options.properties);
  71. }
  72. else if (xi % 2 === 0 && yi % 2 === 1) {
  73. cellTriangle1 = helpers_1.polygon([
  74. [
  75. [currentX, currentY],
  76. [currentX + cellWidth, currentY + cellHeight],
  77. [currentX + cellWidth, currentY],
  78. [currentX, currentY],
  79. ],
  80. ], options.properties);
  81. cellTriangle2 = helpers_1.polygon([
  82. [
  83. [currentX, currentY],
  84. [currentX, currentY + cellHeight],
  85. [currentX + cellWidth, currentY + cellHeight],
  86. [currentX, currentY],
  87. ],
  88. ], options.properties);
  89. }
  90. else if (yi % 2 === 0 && xi % 2 === 1) {
  91. cellTriangle1 = helpers_1.polygon([
  92. [
  93. [currentX, currentY],
  94. [currentX, currentY + cellHeight],
  95. [currentX + cellWidth, currentY + cellHeight],
  96. [currentX, currentY],
  97. ],
  98. ], options.properties);
  99. cellTriangle2 = helpers_1.polygon([
  100. [
  101. [currentX, currentY],
  102. [currentX + cellWidth, currentY + cellHeight],
  103. [currentX + cellWidth, currentY],
  104. [currentX, currentY],
  105. ],
  106. ], options.properties);
  107. }
  108. else if (yi % 2 === 1 && xi % 2 === 1) {
  109. cellTriangle1 = helpers_1.polygon([
  110. [
  111. [currentX, currentY],
  112. [currentX, currentY + cellHeight],
  113. [currentX + cellWidth, currentY],
  114. [currentX, currentY],
  115. ],
  116. ], options.properties);
  117. cellTriangle2 = helpers_1.polygon([
  118. [
  119. [currentX, currentY + cellHeight],
  120. [currentX + cellWidth, currentY + cellHeight],
  121. [currentX + cellWidth, currentY],
  122. [currentX, currentY + cellHeight],
  123. ],
  124. ], options.properties);
  125. }
  126. if (options.mask) {
  127. if (intersect_1.default(options.mask, cellTriangle1))
  128. results.push(cellTriangle1);
  129. if (intersect_1.default(options.mask, cellTriangle2))
  130. results.push(cellTriangle2);
  131. }
  132. else {
  133. results.push(cellTriangle1);
  134. results.push(cellTriangle2);
  135. }
  136. currentY += cellHeight;
  137. yi++;
  138. }
  139. xi++;
  140. currentX += cellWidth;
  141. }
  142. return helpers_1.featureCollection(results);
  143. }
  144. exports.default = triangleGrid;