@turf/hex-grid 7.1.0-alpha.70 → 7.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -38,10 +38,8 @@ function hexGrid(bbox, cellSide, options = {}) {
38
38
  for (let x = 0; x <= x_count; x++) {
39
39
  for (let y = 0; y <= y_count; y++) {
40
40
  const isOdd = x % 2 === 1;
41
- if (y === 0 && isOdd)
42
- continue;
43
- if (y === 0 && hasOffsetY)
44
- continue;
41
+ if (y === 0 && isOdd) continue;
42
+ if (y === 0 && hasOffsetY) continue;
45
43
  const center_x = x * x_interval + west - x_adjust;
46
44
  let center_y = y * y_interval + south + y_adjust;
47
45
  if (isOdd) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../index.ts"],"names":[],"mappings":";AAAA,SAAS,gBAAgB;AACzB,SAAS,iBAAiB;AAQ1B,SAAS,SAAS,yBAAgC;AA2BlD,SAAS,QACP,MACA,UACA,UAKI,CAAC,GAC0B;AAE/B,QAAM,mBAAmB,KAAK,UAAU,QAAQ,cAAc,CAAC,CAAC;AAEhE,QAAM,CAAC,MAAM,OAAO,MAAM,KAAK,IAAI;AACnC,QAAM,WAAW,QAAQ,SAAS;AAClC,QAAM,WAAW,OAAO,QAAQ;AAGhC,QAAM,YACH,WAAW,IAAK,SAAS,CAAC,MAAM,OAAO,GAAG,CAAC,MAAM,OAAO,GAAG,OAAO;AACrE,QAAM,YAAY,aAAa,OAAO;AACtC,QAAM,YACH,WAAW,IAAK,SAAS,CAAC,SAAS,KAAK,GAAG,CAAC,SAAS,KAAK,GAAG,OAAO;AACvE,QAAM,aAAa,aAAa,QAAQ;AACxC,QAAM,SAAS,YAAY;AAE3B,QAAM,YAAY,SAAS;AAC3B,QAAM,aAAc,KAAK,KAAK,CAAC,IAAI,IAAK;AAExC,QAAM,YAAY,OAAO;AACzB,QAAM,aAAa,QAAQ;AAE3B,QAAM,aAAc,IAAI,IAAK;AAC7B,QAAM,aAAa;AAG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A,IACpC,CAAC;AACD,aAAS,KAAK,MAAM;AACpB,cAAU,KAAK,QAAQ,CAAC,QAAQ,GAAG,UAAU,CAAC;AAAA,EAChD;AACA,SAAO;AACT;AAGA,IAAO,wBAAQ","sourcesContent":["import { distance } from \"@turf/distance\";\nimport { intersect } from \"@turf/intersect\";\nimport {\n Feature,\n FeatureCollection,\n GeoJsonProperties,\n Polygon,\n BBox,\n} from \"geojson\";\nimport { polygon, featureCollection, Units } from \"@turf/helpers\";\n\n/**\n * Takes a bounding box and the diameter of the cell and returns a {@link FeatureCollection} of flat-topped\n * hexagons or triangles ({@link Polygon} features) aligned in an \"odd-q\" vertical grid as\n * described in [Hexagonal Grids](http://www.redblobgames.com/grids/hexagons/).\n *\n * @name hexGrid\n * @param {BBox} bbox extent in [minX, minY, maxX, maxY] order\n * @param {number} cellSide length of the side of the the hexagons or triangles, in units. It will also coincide with the\n * radius of the circumcircle of the hexagons.\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.units='kilometers'] used in calculating cell size, can be degrees, radians, miles, or kilometers\n * @param {Object} [options.properties={}] passed to each hexagon or triangle of the grid\n * @param {Feature<Polygon>} [options.mask] if passed a Polygon or MultiPolygon, the grid Points will be created only inside it\n * @param {boolean} [options.triangles=false] whether to return as triangles instead of hexagons\n * @returns {FeatureCollection<Polygon>} a hexagonal grid\n * @example\n * var bbox = [-96,31,-84,40];\n * var cellSide = 50;\n * var options = {units: 'miles'};\n *\n * var hexgrid = turf.hexGrid(bbox, cellSide, options);\n *\n * //addToMap\n * var addToMap = [hexgrid];\n */\nfunction hexGrid<P extends GeoJsonProperties = GeoJsonProperties>(\n bbox: BBox,\n cellSide: number,\n options: {\n units?: Units;\n triangles?: boolean;\n properties?: P;\n mask?: Feature<Polygon>;\n } = {}\n): FeatureCollection<Polygon, P> {\n // Issue => https://github.com/Turfjs/turf/issues/1284\n const clonedProperties = JSON.stringify(options.properties || {});\n\n const [west, south, east, north] = bbox;\n const centerY = (south + north) / 2;\n const centerX = (west + east) / 2;\n\n // https://github.com/Turfjs/turf/issues/758\n const xFraction =\n (cellSide * 2) / distance([west, centerY], [east, centerY], options);\n const cellWidth = xFraction * (east - west);\n const yFraction =\n (cellSide * 2) / distance([centerX, south], [centerX, north], options);\n const cellHeight = yFraction * (north - south);\n const radius = cellWidth / 2;\n\n const hex_width = radius * 2;\n const hex_height = (Math.sqrt(3) / 2) * cellHeight;\n\n const box_width = east - west;\n const box_height = north - south;\n\n const x_interval = (3 / 4) * hex_width;\n const y_interval = hex_height;\n\n // adjust box_width so all hexagons will be inside the bbox\n const x_span = (box_width - hex_width) / (hex_width - radius / 2);\n const x_count = Math.floor(x_span);\n\n const x_adjust =\n (x_count * x_interval - radius / 2 - box_width) / 2 -\n radius / 2 +\n x_interval / 2;\n\n // adjust box_height so all hexagons will be inside the bbox\n const y_count = Math.floor((box_height - hex_height) / hex_height);\n\n let y_adjust = (box_height - y_count * hex_height) / 2;\n\n const hasOffsetY = y_count * hex_height - box_height > hex_height / 2;\n if (hasOffsetY) {\n y_adjust -= hex_height / 4;\n }\n\n // Precompute cosines and sines of angles used in hexagon creation for performance gain\n const cosines = [];\n const sines = [];\n for (let i = 0; i < 6; i++) {\n const angle = ((2 * Math.PI) / 6) * i;\n cosines.push(Math.cos(angle));\n sines.push(Math.sin(angle));\n }\n\n const results = [];\n for (let x = 0; x <= x_count; x++) {\n for (let y = 0; y <= y_count; y++) {\n const isOdd = x % 2 === 1;\n if (y === 0 && isOdd) continue;\n if (y === 0 && hasOffsetY) continue;\n\n const center_x = x * x_interval + west - x_adjust;\n let center_y = y * y_interval + south + y_adjust;\n\n if (isOdd) {\n center_y -= hex_height / 2;\n }\n\n if (options.triangles === true) {\n hexTriangles(\n [center_x, center_y],\n cellWidth / 2,\n cellHeight / 2,\n JSON.parse(clonedProperties),\n cosines,\n sines\n ).forEach(function (triangle) {\n if (options.mask) {\n if (intersect(featureCollection([options.mask, triangle])))\n results.push(triangle);\n } else {\n results.push(triangle);\n }\n });\n } else {\n const hex = hexagon(\n [center_x, center_y],\n cellWidth / 2,\n cellHeight / 2,\n JSON.parse(clonedProperties),\n cosines,\n sines\n );\n if (options.mask) {\n if (intersect(featureCollection([options.mask, hex])))\n results.push(hex);\n } else {\n results.push(hex);\n }\n }\n }\n }\n\n return featureCollection(results) as FeatureCollection<Polygon, P>;\n}\n\n/**\n * Creates hexagon\n *\n * @private\n * @param {Array<number>} center of the hexagon\n * @param {number} rx half hexagon width\n * @param {number} ry half hexagon height\n * @param {Object} properties passed to each hexagon\n * @param {Array<number>} cosines precomputed\n * @param {Array<number>} sines precomputed\n * @returns {Feature<Polygon>} hexagon\n */\nfunction hexagon(\n center: number[],\n rx: number,\n ry: number,\n properties: GeoJsonProperties,\n cosines: number[],\n sines: number[]\n) {\n const vertices = [];\n for (let i = 0; i < 6; i++) {\n const x = center[0] + rx * cosines[i];\n const y = center[1] + ry * sines[i];\n vertices.push([x, y]);\n }\n //first and last vertex must be the same\n vertices.push(vertices[0].slice());\n return polygon([vertices], properties);\n}\n\n/**\n * Creates triangles composing an hexagon\n *\n * @private\n * @param {Array<number>} center of the hexagon\n * @param {number} rx half triangle width\n * @param {number} ry half triangle height\n * @param {Object} properties passed to each triangle\n * @param {Array<number>} cosines precomputed\n * @param {Array<number>} sines precomputed\n * @returns {Array<Feature<Polygon>>} triangles\n */\nfunction hexTriangles(\n center: number[],\n rx: number,\n ry: number,\n properties: GeoJsonProperties,\n cosines: number[],\n sines: number[]\n) {\n const triangles = [];\n for (let i = 0; i < 6; i++) {\n const vertices = [];\n vertices.push(center);\n vertices.push([center[0] + rx * cosines[i], center[1] + ry * sines[i]]);\n vertices.push([\n center[0] + rx * cosines[(i + 1) % 6],\n center[1] + ry * sines[(i + 1) % 6],\n ]);\n vertices.push(center);\n triangles.push(polygon([vertices], properties));\n }\n return triangles;\n}\n\nexport { hexGrid };\nexport default hexGrid;\n"]}
1
+ {"version":3,"sources":["/home/runner/work/turf/turf/packages/turf-hex-grid/dist/cjs/index.cj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AAC,CAAA;AACjC,EAAA,OAAO,8BAAA,CAAS,QAAQ,CAAA,EAAG,UAAU,CAAA;AACvC;AAcA,SAAS,YAAA,CACP,MAAA,EACA,EAAA,EACA,EAAA,EACA,UAAA,EACA,OAAA,EACA,KAAA,EACA;AACA,EAAA,MAAM,UAAA,EAAY,CAAC,CAAA;AACnB,EAAA,IAAA,CAAA,IAAS,EAAA,EAAI,CAAA,EAAG,EAAA,EAAI,CAAA,EAAG,CAAA,EAAA,EAAK;AAC1B,IAAA,MAAM,SAAA,EAAW,CAAC,CAAA;AAClB,IAAA,QAAA,CAAS,IAAA,CAAK,MAAM,CAAA;AACpB,IAAA,QAAA,CAAS,IAAA,CAAK,CAAC,MAAA,CAAO,CAAC,EAAA,EAAI,GAAA,EAAK,OAAA,CAAQ,CAAC,CAAA,EAAG,MAAA,CAAO,CAAC,EAAA,EAAI,GAAA,EAAK,KAAA,CAAM,CAAC,CAAC,CAAC,CAAA;AACtE,IAAA,QAAA,CAAS,IAAA,CAAK;AAAA,MACZ,MAAA,CAAO,CAAC,EAAA,EAAI,GAAA,EAAK,OAAA,CAAA,CAAS,EAAA,EAAI,CAAA,EAAA,EAAK,CAAC,CAAA;AAAA,MACpC,MAAA,CAAO,CAAC,EAAA,EAAI,GAAA,EAAK,KAAA,CAAA,CAAO,EAAA,EAAI,CAAA,EAAA,EAAK,CAAC;AAAA,IACpC,CAAC,CAAA;AACD,IAAA,QAAA,CAAS,IAAA,CAAK,MAAM,CAAA;AACpB,IAAA,SAAA,CAAU,IAAA,CAAK,8BAAA,CAAS,QAAQ,CAAA,EAAG,UAAU,CAAC,CAAA;AAAA,EAChD;AACA,EAAA,OAAO,SAAA;AACT;AAGA,IAAO,sBAAA,EAAQ,OAAA;AD7Gf;AACE;AACA;AACF,mEAAC","file":"/home/runner/work/turf/turf/packages/turf-hex-grid/dist/cjs/index.cjs","sourcesContent":[null,"import { distance } from \"@turf/distance\";\nimport { intersect } from \"@turf/intersect\";\nimport {\n Feature,\n FeatureCollection,\n GeoJsonProperties,\n Polygon,\n BBox,\n} from \"geojson\";\nimport { polygon, featureCollection, Units } from \"@turf/helpers\";\n\n/**\n * Takes a bounding box and the diameter of the cell and returns a {@link FeatureCollection} of flat-topped\n * hexagons or triangles ({@link Polygon} features) aligned in an \"odd-q\" vertical grid as\n * described in [Hexagonal Grids](http://www.redblobgames.com/grids/hexagons/).\n *\n * @function\n * @param {BBox} bbox extent in [minX, minY, maxX, maxY] order\n * @param {number} cellSide length of the side of the the hexagons or triangles, in units. It will also coincide with the\n * radius of the circumcircle of the hexagons.\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.units='kilometers'] used in calculating cell size, can be degrees, radians, miles, or kilometers\n * @param {Object} [options.properties={}] passed to each hexagon or triangle of the grid\n * @param {Feature<Polygon>} [options.mask] if passed a Polygon or MultiPolygon, the grid Points will be created only inside it\n * @param {boolean} [options.triangles=false] whether to return as triangles instead of hexagons\n * @returns {FeatureCollection<Polygon>} a hexagonal grid\n * @example\n * var bbox = [-96,31,-84,40];\n * var cellSide = 50;\n * var options = {units: 'miles'};\n *\n * var hexgrid = turf.hexGrid(bbox, cellSide, options);\n *\n * //addToMap\n * var addToMap = [hexgrid];\n */\nfunction hexGrid<P extends GeoJsonProperties = GeoJsonProperties>(\n bbox: BBox,\n cellSide: number,\n options: {\n units?: Units;\n triangles?: boolean;\n properties?: P;\n mask?: Feature<Polygon>;\n } = {}\n): FeatureCollection<Polygon, P> {\n // Issue => https://github.com/Turfjs/turf/issues/1284\n const clonedProperties = JSON.stringify(options.properties || {});\n\n const [west, south, east, north] = bbox;\n const centerY = (south + north) / 2;\n const centerX = (west + east) / 2;\n\n // https://github.com/Turfjs/turf/issues/758\n const xFraction =\n (cellSide * 2) / distance([west, centerY], [east, centerY], options);\n const cellWidth = xFraction * (east - west);\n const yFraction =\n (cellSide * 2) / distance([centerX, south], [centerX, north], options);\n const cellHeight = yFraction * (north - south);\n const radius = cellWidth / 2;\n\n const hex_width = radius * 2;\n const hex_height = (Math.sqrt(3) / 2) * cellHeight;\n\n const box_width = east - west;\n const box_height = north - south;\n\n const x_interval = (3 / 4) * hex_width;\n const y_interval = hex_height;\n\n // adjust box_width so all hexagons will be inside the bbox\n const x_span = (box_width - hex_width) / (hex_width - radius / 2);\n const x_count = Math.floor(x_span);\n\n const x_adjust =\n (x_count * x_interval - radius / 2 - box_width) / 2 -\n radius / 2 +\n x_interval / 2;\n\n // adjust box_height so all hexagons will be inside the bbox\n const y_count = Math.floor((box_height - hex_height) / hex_height);\n\n let y_adjust = (box_height - y_count * hex_height) / 2;\n\n const hasOffsetY = y_count * hex_height - box_height > hex_height / 2;\n if (hasOffsetY) {\n y_adjust -= hex_height / 4;\n }\n\n // Precompute cosines and sines of angles used in hexagon creation for performance gain\n const cosines = [];\n const sines = [];\n for (let i = 0; i < 6; i++) {\n const angle = ((2 * Math.PI) / 6) * i;\n cosines.push(Math.cos(angle));\n sines.push(Math.sin(angle));\n }\n\n const results = [];\n for (let x = 0; x <= x_count; x++) {\n for (let y = 0; y <= y_count; y++) {\n const isOdd = x % 2 === 1;\n if (y === 0 && isOdd) continue;\n if (y === 0 && hasOffsetY) continue;\n\n const center_x = x * x_interval + west - x_adjust;\n let center_y = y * y_interval + south + y_adjust;\n\n if (isOdd) {\n center_y -= hex_height / 2;\n }\n\n if (options.triangles === true) {\n hexTriangles(\n [center_x, center_y],\n cellWidth / 2,\n cellHeight / 2,\n JSON.parse(clonedProperties),\n cosines,\n sines\n ).forEach(function (triangle) {\n if (options.mask) {\n if (intersect(featureCollection([options.mask, triangle])))\n results.push(triangle);\n } else {\n results.push(triangle);\n }\n });\n } else {\n const hex = hexagon(\n [center_x, center_y],\n cellWidth / 2,\n cellHeight / 2,\n JSON.parse(clonedProperties),\n cosines,\n sines\n );\n if (options.mask) {\n if (intersect(featureCollection([options.mask, hex])))\n results.push(hex);\n } else {\n results.push(hex);\n }\n }\n }\n }\n\n return featureCollection(results) as FeatureCollection<Polygon, P>;\n}\n\n/**\n * Creates hexagon\n *\n * @private\n * @param {Array<number>} center of the hexagon\n * @param {number} rx half hexagon width\n * @param {number} ry half hexagon height\n * @param {Object} properties passed to each hexagon\n * @param {Array<number>} cosines precomputed\n * @param {Array<number>} sines precomputed\n * @returns {Feature<Polygon>} hexagon\n */\nfunction hexagon(\n center: number[],\n rx: number,\n ry: number,\n properties: GeoJsonProperties,\n cosines: number[],\n sines: number[]\n) {\n const vertices = [];\n for (let i = 0; i < 6; i++) {\n const x = center[0] + rx * cosines[i];\n const y = center[1] + ry * sines[i];\n vertices.push([x, y]);\n }\n //first and last vertex must be the same\n vertices.push(vertices[0].slice());\n return polygon([vertices], properties);\n}\n\n/**\n * Creates triangles composing an hexagon\n *\n * @private\n * @param {Array<number>} center of the hexagon\n * @param {number} rx half triangle width\n * @param {number} ry half triangle height\n * @param {Object} properties passed to each triangle\n * @param {Array<number>} cosines precomputed\n * @param {Array<number>} sines precomputed\n * @returns {Array<Feature<Polygon>>} triangles\n */\nfunction hexTriangles(\n center: number[],\n rx: number,\n ry: number,\n properties: GeoJsonProperties,\n cosines: number[],\n sines: number[]\n) {\n const triangles = [];\n for (let i = 0; i < 6; i++) {\n const vertices = [];\n vertices.push(center);\n vertices.push([center[0] + rx * cosines[i], center[1] + ry * sines[i]]);\n vertices.push([\n center[0] + rx * cosines[(i + 1) % 6],\n center[1] + ry * sines[(i + 1) % 6],\n ]);\n vertices.push(center);\n triangles.push(polygon([vertices], properties));\n }\n return triangles;\n}\n\nexport { hexGrid };\nexport default hexGrid;\n"]}
@@ -6,7 +6,7 @@ import { Units } from '@turf/helpers';
6
6
  * hexagons or triangles ({@link Polygon} features) aligned in an "odd-q" vertical grid as
7
7
  * described in [Hexagonal Grids](http://www.redblobgames.com/grids/hexagons/).
8
8
  *
9
- * @name hexGrid
9
+ * @function
10
10
  * @param {BBox} bbox extent in [minX, minY, maxX, maxY] order
11
11
  * @param {number} cellSide length of the side of the the hexagons or triangles, in units. It will also coincide with the
12
12
  * radius of the circumcircle of the hexagons.
@@ -6,7 +6,7 @@ import { Units } from '@turf/helpers';
6
6
  * hexagons or triangles ({@link Polygon} features) aligned in an "odd-q" vertical grid as
7
7
  * described in [Hexagonal Grids](http://www.redblobgames.com/grids/hexagons/).
8
8
  *
9
- * @name hexGrid
9
+ * @function
10
10
  * @param {BBox} bbox extent in [minX, minY, maxX, maxY] order
11
11
  * @param {number} cellSide length of the side of the the hexagons or triangles, in units. It will also coincide with the
12
12
  * radius of the circumcircle of the hexagons.
package/dist/esm/index.js CHANGED
@@ -38,10 +38,8 @@ function hexGrid(bbox, cellSide, options = {}) {
38
38
  for (let x = 0; x <= x_count; x++) {
39
39
  for (let y = 0; y <= y_count; y++) {
40
40
  const isOdd = x % 2 === 1;
41
- if (y === 0 && isOdd)
42
- continue;
43
- if (y === 0 && hasOffsetY)
44
- continue;
41
+ if (y === 0 && isOdd) continue;
42
+ if (y === 0 && hasOffsetY) continue;
45
43
  const center_x = x * x_interval + west - x_adjust;
46
44
  let center_y = y * y_interval + south + y_adjust;
47
45
  if (isOdd) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../index.ts"],"sourcesContent":["import { distance } from \"@turf/distance\";\nimport { intersect } from \"@turf/intersect\";\nimport {\n Feature,\n FeatureCollection,\n GeoJsonProperties,\n Polygon,\n BBox,\n} from \"geojson\";\nimport { polygon, featureCollection, Units } from \"@turf/helpers\";\n\n/**\n * Takes a bounding box and the diameter of the cell and returns a {@link FeatureCollection} of flat-topped\n * hexagons or triangles ({@link Polygon} features) aligned in an \"odd-q\" vertical grid as\n * described in [Hexagonal Grids](http://www.redblobgames.com/grids/hexagons/).\n *\n * @name hexGrid\n * @param {BBox} bbox extent in [minX, minY, maxX, maxY] order\n * @param {number} cellSide length of the side of the the hexagons or triangles, in units. It will also coincide with the\n * radius of the circumcircle of the hexagons.\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.units='kilometers'] used in calculating cell size, can be degrees, radians, miles, or kilometers\n * @param {Object} [options.properties={}] passed to each hexagon or triangle of the grid\n * @param {Feature<Polygon>} [options.mask] if passed a Polygon or MultiPolygon, the grid Points will be created only inside it\n * @param {boolean} [options.triangles=false] whether to return as triangles instead of hexagons\n * @returns {FeatureCollection<Polygon>} a hexagonal grid\n * @example\n * var bbox = [-96,31,-84,40];\n * var cellSide = 50;\n * var options = {units: 'miles'};\n *\n * var hexgrid = turf.hexGrid(bbox, cellSide, options);\n *\n * //addToMap\n * var addToMap = [hexgrid];\n */\nfunction hexGrid<P extends GeoJsonProperties = GeoJsonProperties>(\n bbox: BBox,\n cellSide: number,\n options: {\n units?: Units;\n triangles?: boolean;\n properties?: P;\n mask?: Feature<Polygon>;\n } = {}\n): FeatureCollection<Polygon, P> {\n // Issue => https://github.com/Turfjs/turf/issues/1284\n const clonedProperties = JSON.stringify(options.properties || {});\n\n const [west, south, east, north] = bbox;\n const centerY = (south + north) / 2;\n const centerX = (west + east) / 2;\n\n // https://github.com/Turfjs/turf/issues/758\n const xFraction =\n (cellSide * 2) / distance([west, centerY], [east, centerY], options);\n const cellWidth = xFraction * (east - west);\n const yFraction =\n (cellSide * 2) / distance([centerX, south], [centerX, north], options);\n const cellHeight = yFraction * (north - south);\n const radius = cellWidth / 2;\n\n const hex_width = radius * 2;\n const hex_height = (Math.sqrt(3) / 2) * cellHeight;\n\n const box_width = east - west;\n const box_height = north - south;\n\n const x_interval = (3 / 4) * hex_width;\n const y_interval = hex_height;\n\n // adjust box_width so all hexagons will be inside the bbox\n const x_span = (box_width - hex_width) / (hex_width - radius / 2);\n const x_count = Math.floor(x_span);\n\n const x_adjust =\n (x_count * x_interval - radius / 2 - box_width) / 2 -\n radius / 2 +\n x_interval / 2;\n\n // adjust box_height so all hexagons will be inside the bbox\n const y_count = Math.floor((box_height - hex_height) / hex_height);\n\n let y_adjust = (box_height - y_count * hex_height) / 2;\n\n const hasOffsetY = y_count * hex_height - box_height > hex_height / 2;\n if (hasOffsetY) {\n y_adjust -= hex_height / 4;\n }\n\n // Precompute cosines and sines of angles used in hexagon creation for performance gain\n const cosines = [];\n const sines = [];\n for (let i = 0; i < 6; i++) {\n const angle = ((2 * Math.PI) / 6) * i;\n cosines.push(Math.cos(angle));\n sines.push(Math.sin(angle));\n }\n\n const results = [];\n for (let x = 0; x <= x_count; x++) {\n for (let y = 0; y <= y_count; y++) {\n const isOdd = x % 2 === 1;\n if (y === 0 && isOdd) continue;\n if (y === 0 && hasOffsetY) continue;\n\n const center_x = x * x_interval + west - x_adjust;\n let center_y = y * y_interval + south + y_adjust;\n\n if (isOdd) {\n center_y -= hex_height / 2;\n }\n\n if (options.triangles === true) {\n hexTriangles(\n [center_x, center_y],\n cellWidth / 2,\n cellHeight / 2,\n JSON.parse(clonedProperties),\n cosines,\n sines\n ).forEach(function (triangle) {\n if (options.mask) {\n if (intersect(featureCollection([options.mask, triangle])))\n results.push(triangle);\n } else {\n results.push(triangle);\n }\n });\n } else {\n const hex = hexagon(\n [center_x, center_y],\n cellWidth / 2,\n cellHeight / 2,\n JSON.parse(clonedProperties),\n cosines,\n sines\n );\n if (options.mask) {\n if (intersect(featureCollection([options.mask, hex])))\n results.push(hex);\n } else {\n results.push(hex);\n }\n }\n }\n }\n\n return featureCollection(results) as FeatureCollection<Polygon, P>;\n}\n\n/**\n * Creates hexagon\n *\n * @private\n * @param {Array<number>} center of the hexagon\n * @param {number} rx half hexagon width\n * @param {number} ry half hexagon height\n * @param {Object} properties passed to each hexagon\n * @param {Array<number>} cosines precomputed\n * @param {Array<number>} sines precomputed\n * @returns {Feature<Polygon>} hexagon\n */\nfunction hexagon(\n center: number[],\n rx: number,\n ry: number,\n properties: GeoJsonProperties,\n cosines: number[],\n sines: number[]\n) {\n const vertices = [];\n for (let i = 0; i < 6; i++) {\n const x = center[0] + rx * cosines[i];\n const y = center[1] + ry * sines[i];\n vertices.push([x, y]);\n }\n //first and last vertex must be the same\n vertices.push(vertices[0].slice());\n return polygon([vertices], properties);\n}\n\n/**\n * Creates triangles composing an hexagon\n *\n * @private\n * @param {Array<number>} center of the hexagon\n * @param {number} rx half triangle width\n * @param {number} ry half triangle height\n * @param {Object} properties passed to each triangle\n * @param {Array<number>} cosines precomputed\n * @param {Array<number>} sines precomputed\n * @returns {Array<Feature<Polygon>>} triangles\n */\nfunction hexTriangles(\n center: number[],\n rx: number,\n ry: number,\n properties: GeoJsonProperties,\n cosines: number[],\n sines: number[]\n) {\n const triangles = [];\n for (let i = 0; i < 6; i++) {\n const vertices = [];\n vertices.push(center);\n vertices.push([center[0] + rx * cosines[i], center[1] + ry * sines[i]]);\n vertices.push([\n center[0] + rx * cosines[(i + 1) % 6],\n center[1] + ry * sines[(i + 1) % 6],\n ]);\n vertices.push(center);\n triangles.push(polygon([vertices], properties));\n }\n return triangles;\n}\n\nexport { hexGrid };\nexport default hexGrid;\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,SAAS,iBAAiB;AAQ1B,SAAS,SAAS,yBAAgC;AA2BlD,SAAS,QACP,MACA,UACA,UAKI,CAAC,GAC0B;AAE/B,QAAM,mBAAmB,KAAK,UAAU,QAAQ,cAAc,CAAC,CAAC;AAEhE,QAAM,CAAC,MAAM,OAAO,MAAM,KAAK,IAAI;AACnC,QAAM,WAAW,QAAQ,SAAS;AAClC,QAAM,WAAW,OAAO,QAAQ;AAGhC,QAAM,YACH,WAAW,IAAK,SAAS,CAAC,MAAM,OAAO,GAAG,CAAC,MAAM,OAAO,GAAG,OAAO;AACrE,QAAM,YAAY,aAAa,OAAO;AACtC,QAAM,YACH,WAAW,IAAK,SAAS,CAAC,SAAS,KAAK,GAAG,CAAC,SAAS,KAAK,GAAG,OAAO;AACvE,QAAM,aAAa,aAAa,QAAQ;AACxC,QAAM,SAAS,YAAY;AAE3B,QAAM,YAAY,SAAS;AAC3B,QAAM,aAAc,KAAK,KAAK,CAAC,IAAI,IAAK;AAExC,QAAM,YAAY,OAAO;AACzB,QAAM,aAAa,QAAQ;AAE3B,QAAM,aAAc,IAAI,IAAK;AAC7B,QAAM,aAAa;AAGn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,IACpC,CAAC;AACD,aAAS,KAAK,MAAM;AACpB,cAAU,KAAK,QAAQ,CAAC,QAAQ,GAAG,UAAU,CAAC;AAAA,EAChD;AACA,SAAO;AACT;AAGA,IAAO,wBAAQ;","names":[]}
1
+ {"version":3,"sources":["../../index.ts"],"sourcesContent":["import { distance } from \"@turf/distance\";\nimport { intersect } from \"@turf/intersect\";\nimport {\n Feature,\n FeatureCollection,\n GeoJsonProperties,\n Polygon,\n BBox,\n} from \"geojson\";\nimport { polygon, featureCollection, Units } from \"@turf/helpers\";\n\n/**\n * Takes a bounding box and the diameter of the cell and returns a {@link FeatureCollection} of flat-topped\n * hexagons or triangles ({@link Polygon} features) aligned in an \"odd-q\" vertical grid as\n * described in [Hexagonal Grids](http://www.redblobgames.com/grids/hexagons/).\n *\n * @function\n * @param {BBox} bbox extent in [minX, minY, maxX, maxY] order\n * @param {number} cellSide length of the side of the the hexagons or triangles, in units. It will also coincide with the\n * radius of the circumcircle of the hexagons.\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.units='kilometers'] used in calculating cell size, can be degrees, radians, miles, or kilometers\n * @param {Object} [options.properties={}] passed to each hexagon or triangle of the grid\n * @param {Feature<Polygon>} [options.mask] if passed a Polygon or MultiPolygon, the grid Points will be created only inside it\n * @param {boolean} [options.triangles=false] whether to return as triangles instead of hexagons\n * @returns {FeatureCollection<Polygon>} a hexagonal grid\n * @example\n * var bbox = [-96,31,-84,40];\n * var cellSide = 50;\n * var options = {units: 'miles'};\n *\n * var hexgrid = turf.hexGrid(bbox, cellSide, options);\n *\n * //addToMap\n * var addToMap = [hexgrid];\n */\nfunction hexGrid<P extends GeoJsonProperties = GeoJsonProperties>(\n bbox: BBox,\n cellSide: number,\n options: {\n units?: Units;\n triangles?: boolean;\n properties?: P;\n mask?: Feature<Polygon>;\n } = {}\n): FeatureCollection<Polygon, P> {\n // Issue => https://github.com/Turfjs/turf/issues/1284\n const clonedProperties = JSON.stringify(options.properties || {});\n\n const [west, south, east, north] = bbox;\n const centerY = (south + north) / 2;\n const centerX = (west + east) / 2;\n\n // https://github.com/Turfjs/turf/issues/758\n const xFraction =\n (cellSide * 2) / distance([west, centerY], [east, centerY], options);\n const cellWidth = xFraction * (east - west);\n const yFraction =\n (cellSide * 2) / distance([centerX, south], [centerX, north], options);\n const cellHeight = yFraction * (north - south);\n const radius = cellWidth / 2;\n\n const hex_width = radius * 2;\n const hex_height = (Math.sqrt(3) / 2) * cellHeight;\n\n const box_width = east - west;\n const box_height = north - south;\n\n const x_interval = (3 / 4) * hex_width;\n const y_interval = hex_height;\n\n // adjust box_width so all hexagons will be inside the bbox\n const x_span = (box_width - hex_width) / (hex_width - radius / 2);\n const x_count = Math.floor(x_span);\n\n const x_adjust =\n (x_count * x_interval - radius / 2 - box_width) / 2 -\n radius / 2 +\n x_interval / 2;\n\n // adjust box_height so all hexagons will be inside the bbox\n const y_count = Math.floor((box_height - hex_height) / hex_height);\n\n let y_adjust = (box_height - y_count * hex_height) / 2;\n\n const hasOffsetY = y_count * hex_height - box_height > hex_height / 2;\n if (hasOffsetY) {\n y_adjust -= hex_height / 4;\n }\n\n // Precompute cosines and sines of angles used in hexagon creation for performance gain\n const cosines = [];\n const sines = [];\n for (let i = 0; i < 6; i++) {\n const angle = ((2 * Math.PI) / 6) * i;\n cosines.push(Math.cos(angle));\n sines.push(Math.sin(angle));\n }\n\n const results = [];\n for (let x = 0; x <= x_count; x++) {\n for (let y = 0; y <= y_count; y++) {\n const isOdd = x % 2 === 1;\n if (y === 0 && isOdd) continue;\n if (y === 0 && hasOffsetY) continue;\n\n const center_x = x * x_interval + west - x_adjust;\n let center_y = y * y_interval + south + y_adjust;\n\n if (isOdd) {\n center_y -= hex_height / 2;\n }\n\n if (options.triangles === true) {\n hexTriangles(\n [center_x, center_y],\n cellWidth / 2,\n cellHeight / 2,\n JSON.parse(clonedProperties),\n cosines,\n sines\n ).forEach(function (triangle) {\n if (options.mask) {\n if (intersect(featureCollection([options.mask, triangle])))\n results.push(triangle);\n } else {\n results.push(triangle);\n }\n });\n } else {\n const hex = hexagon(\n [center_x, center_y],\n cellWidth / 2,\n cellHeight / 2,\n JSON.parse(clonedProperties),\n cosines,\n sines\n );\n if (options.mask) {\n if (intersect(featureCollection([options.mask, hex])))\n results.push(hex);\n } else {\n results.push(hex);\n }\n }\n }\n }\n\n return featureCollection(results) as FeatureCollection<Polygon, P>;\n}\n\n/**\n * Creates hexagon\n *\n * @private\n * @param {Array<number>} center of the hexagon\n * @param {number} rx half hexagon width\n * @param {number} ry half hexagon height\n * @param {Object} properties passed to each hexagon\n * @param {Array<number>} cosines precomputed\n * @param {Array<number>} sines precomputed\n * @returns {Feature<Polygon>} hexagon\n */\nfunction hexagon(\n center: number[],\n rx: number,\n ry: number,\n properties: GeoJsonProperties,\n cosines: number[],\n sines: number[]\n) {\n const vertices = [];\n for (let i = 0; i < 6; i++) {\n const x = center[0] + rx * cosines[i];\n const y = center[1] + ry * sines[i];\n vertices.push([x, y]);\n }\n //first and last vertex must be the same\n vertices.push(vertices[0].slice());\n return polygon([vertices], properties);\n}\n\n/**\n * Creates triangles composing an hexagon\n *\n * @private\n * @param {Array<number>} center of the hexagon\n * @param {number} rx half triangle width\n * @param {number} ry half triangle height\n * @param {Object} properties passed to each triangle\n * @param {Array<number>} cosines precomputed\n * @param {Array<number>} sines precomputed\n * @returns {Array<Feature<Polygon>>} triangles\n */\nfunction hexTriangles(\n center: number[],\n rx: number,\n ry: number,\n properties: GeoJsonProperties,\n cosines: number[],\n sines: number[]\n) {\n const triangles = [];\n for (let i = 0; i < 6; i++) {\n const vertices = [];\n vertices.push(center);\n vertices.push([center[0] + rx * cosines[i], center[1] + ry * sines[i]]);\n vertices.push([\n center[0] + rx * cosines[(i + 1) % 6],\n center[1] + ry * sines[(i + 1) % 6],\n ]);\n vertices.push(center);\n triangles.push(polygon([vertices], properties));\n }\n return triangles;\n}\n\nexport { hexGrid };\nexport default hexGrid;\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,S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AAG,IAAI,GAAG,KAAK;AAC1B,UAAM,IAAI,OAAO,CAAC,IAAI,KAAK,QAAQ,CAAC;AACpC,UAAM,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,CAAC;AAClC,aAAS,KAAK,CAAC,GAAG,CAAC,CAAC;AAAA,EACtB;AAEA,WAAS,KAAK,SAAS,CAAC,EAAE,MAAM,CAAC;AACjC,SAAO,QAAQ,CAAC,QAAQ,GAAG,UAAU;AACvC;AAcA,SAAS,aACP,QACA,IACA,IACA,YACA,SACA,OACA;AACA,QAAM,YAAY,CAAC;AACnB,WAAS,IAAI,GAAG,IAAI,GAAG,KAAK;AAC1B,UAAM,WAAW,CAAC;AAClB,aAAS,KAAK,MAAM;AACpB,aAAS,KAAK,CAAC,OAAO,CAAC,IAAI,KAAK,QAAQ,CAAC,GAAG,OAAO,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC;AACtE,aAAS,KAAK;AAAA,MACZ,OAAO,CAAC,IAAI,KAAK,SAAS,IAAI,KAAK,CAAC;AAAA,MACpC,OAAO,CAAC,IAAI,KAAK,OAAO,IAAI,KAAK,CAAC;AAAA,IACpC,CAAC;AACD,aAAS,KAAK,MAAM;AACpB,cAAU,KAAK,QAAQ,CAAC,QAAQ,GAAG,UAAU,CAAC;AAAA,EAChD;AACA,SAAO;AACT;AAGA,IAAO,wBAAQ;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turf/hex-grid",
3
- "version": "7.1.0-alpha.70+948cdafaf",
3
+ "version": "7.2.0",
4
4
  "description": "turf hex-grid module",
5
5
  "author": "Turf Authors",
6
6
  "contributors": [
@@ -62,26 +62,26 @@
62
62
  "test:types": "tsc --esModuleInterop --module node16 --moduleResolution node16 --noEmit --strict types.ts"
63
63
  },
64
64
  "devDependencies": {
65
- "@turf/bbox-polygon": "^7.1.0-alpha.70+948cdafaf",
66
- "@turf/truncate": "^7.1.0-alpha.70+948cdafaf",
65
+ "@turf/bbox-polygon": "^7.2.0",
66
+ "@turf/truncate": "^7.2.0",
67
67
  "@types/benchmark": "^2.1.5",
68
- "@types/tape": "^4.2.32",
68
+ "@types/tape": "^4.13.4",
69
69
  "benchmark": "^2.1.4",
70
70
  "load-json-file": "^7.0.1",
71
71
  "npm-run-all": "^4.1.5",
72
- "tape": "^5.7.2",
73
- "tsup": "^8.0.1",
74
- "tsx": "^4.6.2",
75
- "typescript": "^5.2.2",
72
+ "tape": "^5.9.0",
73
+ "tsup": "^8.3.5",
74
+ "tsx": "^4.19.2",
75
+ "typescript": "^5.5.4",
76
76
  "write-json-file": "^5.0.0"
77
77
  },
78
78
  "dependencies": {
79
- "@turf/distance": "^7.1.0-alpha.70+948cdafaf",
80
- "@turf/helpers": "^7.1.0-alpha.70+948cdafaf",
81
- "@turf/intersect": "^7.1.0-alpha.70+948cdafaf",
82
- "@turf/invariant": "^7.1.0-alpha.70+948cdafaf",
79
+ "@turf/distance": "^7.2.0",
80
+ "@turf/helpers": "^7.2.0",
81
+ "@turf/intersect": "^7.2.0",
82
+ "@turf/invariant": "^7.2.0",
83
83
  "@types/geojson": "^7946.0.10",
84
- "tslib": "^2.6.2"
84
+ "tslib": "^2.8.1"
85
85
  },
86
- "gitHead": "948cdafaf70606d2e27fcc79973fa48ee1182067"
86
+ "gitHead": "7b0f0374c4668cd569f8904c71e2ae7d941be867"
87
87
  }