index.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 the diameter of the cell and returns a {@link FeatureCollection} of flat-topped
  11. * hexagons or triangles ({@link Polygon} features) aligned in an "odd-q" vertical grid as
  12. * described in [Hexagonal Grids](http://www.redblobgames.com/grids/hexagons/).
  13. *
  14. * @name hexGrid
  15. * @param {BBox} bbox extent in [minX, minY, maxX, maxY] order
  16. * @param {number} cellSide length of the side of the the hexagons or triangles, in units. It will also coincide with the
  17. * radius of the circumcircle of the hexagons.
  18. * @param {Object} [options={}] Optional parameters
  19. * @param {string} [options.units='kilometers'] used in calculating cell size, can be degrees, radians, miles, or kilometers
  20. * @param {Object} [options.properties={}] passed to each hexagon or triangle of the grid
  21. * @param {Feature<Polygon>} [options.mask] if passed a Polygon or MultiPolygon, the grid Points will be created only inside it
  22. * @param {boolean} [options.triangles=false] whether to return as triangles instead of hexagons
  23. * @returns {FeatureCollection<Polygon>} a hexagonal grid
  24. * @example
  25. * var bbox = [-96,31,-84,40];
  26. * var cellSide = 50;
  27. * var options = {units: 'miles'};
  28. *
  29. * var hexgrid = turf.hexGrid(bbox, cellSide, options);
  30. *
  31. * //addToMap
  32. * var addToMap = [hexgrid];
  33. */
  34. function hexGrid(bbox, cellSide, options) {
  35. if (options === void 0) { options = {}; }
  36. // Issue => https://github.com/Turfjs/turf/issues/1284
  37. var clonedProperties = JSON.stringify(options.properties || {});
  38. var west = bbox[0], south = bbox[1], east = bbox[2], north = bbox[3];
  39. var centerY = (south + north) / 2;
  40. var centerX = (west + east) / 2;
  41. // https://github.com/Turfjs/turf/issues/758
  42. var xFraction = (cellSide * 2) / distance_1.default([west, centerY], [east, centerY], options);
  43. var cellWidth = xFraction * (east - west);
  44. var yFraction = (cellSide * 2) / distance_1.default([centerX, south], [centerX, north], options);
  45. var cellHeight = yFraction * (north - south);
  46. var radius = cellWidth / 2;
  47. var hex_width = radius * 2;
  48. var hex_height = (Math.sqrt(3) / 2) * cellHeight;
  49. var box_width = east - west;
  50. var box_height = north - south;
  51. var x_interval = (3 / 4) * hex_width;
  52. var y_interval = hex_height;
  53. // adjust box_width so all hexagons will be inside the bbox
  54. var x_span = (box_width - hex_width) / (hex_width - radius / 2);
  55. var x_count = Math.floor(x_span);
  56. var x_adjust = (x_count * x_interval - radius / 2 - box_width) / 2 -
  57. radius / 2 +
  58. x_interval / 2;
  59. // adjust box_height so all hexagons will be inside the bbox
  60. var y_count = Math.floor((box_height - hex_height) / hex_height);
  61. var y_adjust = (box_height - y_count * hex_height) / 2;
  62. var hasOffsetY = y_count * hex_height - box_height > hex_height / 2;
  63. if (hasOffsetY) {
  64. y_adjust -= hex_height / 4;
  65. }
  66. // Precompute cosines and sines of angles used in hexagon creation for performance gain
  67. var cosines = [];
  68. var sines = [];
  69. for (var i = 0; i < 6; i++) {
  70. var angle = ((2 * Math.PI) / 6) * i;
  71. cosines.push(Math.cos(angle));
  72. sines.push(Math.sin(angle));
  73. }
  74. var results = [];
  75. for (var x = 0; x <= x_count; x++) {
  76. for (var y = 0; y <= y_count; y++) {
  77. var isOdd = x % 2 === 1;
  78. if (y === 0 && isOdd)
  79. continue;
  80. if (y === 0 && hasOffsetY)
  81. continue;
  82. var center_x = x * x_interval + west - x_adjust;
  83. var center_y = y * y_interval + south + y_adjust;
  84. if (isOdd) {
  85. center_y -= hex_height / 2;
  86. }
  87. if (options.triangles === true) {
  88. hexTriangles([center_x, center_y], cellWidth / 2, cellHeight / 2, JSON.parse(clonedProperties), cosines, sines).forEach(function (triangle) {
  89. if (options.mask) {
  90. if (intersect_1.default(options.mask, triangle))
  91. results.push(triangle);
  92. }
  93. else {
  94. results.push(triangle);
  95. }
  96. });
  97. }
  98. else {
  99. var hex = hexagon([center_x, center_y], cellWidth / 2, cellHeight / 2, JSON.parse(clonedProperties), cosines, sines);
  100. if (options.mask) {
  101. if (intersect_1.default(options.mask, hex))
  102. results.push(hex);
  103. }
  104. else {
  105. results.push(hex);
  106. }
  107. }
  108. }
  109. }
  110. return helpers_1.featureCollection(results);
  111. }
  112. /**
  113. * Creates hexagon
  114. *
  115. * @private
  116. * @param {Array<number>} center of the hexagon
  117. * @param {number} rx half hexagon width
  118. * @param {number} ry half hexagon height
  119. * @param {Object} properties passed to each hexagon
  120. * @param {Array<number>} cosines precomputed
  121. * @param {Array<number>} sines precomputed
  122. * @returns {Feature<Polygon>} hexagon
  123. */
  124. function hexagon(center, rx, ry, properties, cosines, sines) {
  125. var vertices = [];
  126. for (var i = 0; i < 6; i++) {
  127. var x = center[0] + rx * cosines[i];
  128. var y = center[1] + ry * sines[i];
  129. vertices.push([x, y]);
  130. }
  131. //first and last vertex must be the same
  132. vertices.push(vertices[0].slice());
  133. return helpers_1.polygon([vertices], properties);
  134. }
  135. /**
  136. * Creates triangles composing an hexagon
  137. *
  138. * @private
  139. * @param {Array<number>} center of the hexagon
  140. * @param {number} rx half triangle width
  141. * @param {number} ry half triangle height
  142. * @param {Object} properties passed to each triangle
  143. * @param {Array<number>} cosines precomputed
  144. * @param {Array<number>} sines precomputed
  145. * @returns {Array<Feature<Polygon>>} triangles
  146. */
  147. function hexTriangles(center, rx, ry, properties, cosines, sines) {
  148. var triangles = [];
  149. for (var i = 0; i < 6; i++) {
  150. var vertices = [];
  151. vertices.push(center);
  152. vertices.push([center[0] + rx * cosines[i], center[1] + ry * sines[i]]);
  153. vertices.push([
  154. center[0] + rx * cosines[(i + 1) % 6],
  155. center[1] + ry * sines[(i + 1) % 6],
  156. ]);
  157. vertices.push(center);
  158. triangles.push(helpers_1.polygon([vertices], properties));
  159. }
  160. return triangles;
  161. }
  162. exports.default = hexGrid;