index.cjs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
  2. var _distance = require('@turf/distance');
  3. var _intersect = require('@turf/intersect');
  4. var _helpers = require('@turf/helpers');
  5. function hexGrid(bbox, cellSide, options = {}) {
  6. const clonedProperties = JSON.stringify(options.properties || {});
  7. const [west, south, east, north] = bbox;
  8. const centerY = (south + north) / 2;
  9. const centerX = (west + east) / 2;
  10. const xFraction = cellSide * 2 / _distance.distance.call(void 0, [west, centerY], [east, centerY], options);
  11. const cellWidth = xFraction * (east - west);
  12. const yFraction = cellSide * 2 / _distance.distance.call(void 0, [centerX, south], [centerX, north], options);
  13. const cellHeight = yFraction * (north - south);
  14. const radius = cellWidth / 2;
  15. const hex_width = radius * 2;
  16. const hex_height = Math.sqrt(3) / 2 * cellHeight;
  17. const box_width = east - west;
  18. const box_height = north - south;
  19. const x_interval = 3 / 4 * hex_width;
  20. const y_interval = hex_height;
  21. const x_span = (box_width - hex_width) / (hex_width - radius / 2);
  22. const x_count = Math.floor(x_span);
  23. const x_adjust = (x_count * x_interval - radius / 2 - box_width) / 2 - radius / 2 + x_interval / 2;
  24. const y_count = Math.floor((box_height - hex_height) / hex_height);
  25. let y_adjust = (box_height - y_count * hex_height) / 2;
  26. const hasOffsetY = y_count * hex_height - box_height > hex_height / 2;
  27. if (hasOffsetY) {
  28. y_adjust -= hex_height / 4;
  29. }
  30. const cosines = [];
  31. const sines = [];
  32. for (let i = 0; i < 6; i++) {
  33. const angle = 2 * Math.PI / 6 * i;
  34. cosines.push(Math.cos(angle));
  35. sines.push(Math.sin(angle));
  36. }
  37. const results = [];
  38. for (let x = 0; x <= x_count; x++) {
  39. for (let y = 0; y <= y_count; y++) {
  40. const isOdd = x % 2 === 1;
  41. if (y === 0 && isOdd) continue;
  42. if (y === 0 && hasOffsetY) continue;
  43. const center_x = x * x_interval + west - x_adjust;
  44. let center_y = y * y_interval + south + y_adjust;
  45. if (isOdd) {
  46. center_y -= hex_height / 2;
  47. }
  48. if (options.triangles === true) {
  49. hexTriangles(
  50. [center_x, center_y],
  51. cellWidth / 2,
  52. cellHeight / 2,
  53. JSON.parse(clonedProperties),
  54. cosines,
  55. sines
  56. ).forEach(function(triangle) {
  57. if (options.mask) {
  58. if (_intersect.intersect.call(void 0, _helpers.featureCollection.call(void 0, [options.mask, triangle])))
  59. results.push(triangle);
  60. } else {
  61. results.push(triangle);
  62. }
  63. });
  64. } else {
  65. const hex = hexagon(
  66. [center_x, center_y],
  67. cellWidth / 2,
  68. cellHeight / 2,
  69. JSON.parse(clonedProperties),
  70. cosines,
  71. sines
  72. );
  73. if (options.mask) {
  74. if (_intersect.intersect.call(void 0, _helpers.featureCollection.call(void 0, [options.mask, hex])))
  75. results.push(hex);
  76. } else {
  77. results.push(hex);
  78. }
  79. }
  80. }
  81. }
  82. return _helpers.featureCollection.call(void 0, results);
  83. }
  84. function hexagon(center, rx, ry, properties, cosines, sines) {
  85. const vertices = [];
  86. for (let i = 0; i < 6; i++) {
  87. const x = center[0] + rx * cosines[i];
  88. const y = center[1] + ry * sines[i];
  89. vertices.push([x, y]);
  90. }
  91. vertices.push(vertices[0].slice());
  92. return _helpers.polygon.call(void 0, [vertices], properties);
  93. }
  94. function hexTriangles(center, rx, ry, properties, cosines, sines) {
  95. const triangles = [];
  96. for (let i = 0; i < 6; i++) {
  97. const vertices = [];
  98. vertices.push(center);
  99. vertices.push([center[0] + rx * cosines[i], center[1] + ry * sines[i]]);
  100. vertices.push([
  101. center[0] + rx * cosines[(i + 1) % 6],
  102. center[1] + ry * sines[(i + 1) % 6]
  103. ]);
  104. vertices.push(center);
  105. triangles.push(_helpers.polygon.call(void 0, [vertices], properties));
  106. }
  107. return triangles;
  108. }
  109. var turf_hex_grid_default = hexGrid;
  110. exports.default = turf_hex_grid_default; exports.hexGrid = hexGrid;
  111. //# sourceMappingURL=index.cjs.map