index.js 6.3 KB

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