@turf/hex-grid 7.2.0 → 7.3.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.
- package/README.md +7 -5
- package/dist/cjs/index.cjs +2 -2
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +4 -4
- package/dist/esm/index.d.ts +4 -4
- package/dist/esm/index.js +2 -2
- package/dist/esm/index.js.map +1 -1
- package/package.json +14 -14
package/README.md
CHANGED
|
@@ -15,10 +15,10 @@ described in [Hexagonal Grids][3].
|
|
|
15
15
|
radius of the circumcircle of the hexagons.
|
|
16
16
|
* `options` **[Object][6]** Optional parameters (optional, default `{}`)
|
|
17
17
|
|
|
18
|
-
* `options.units` **
|
|
18
|
+
* `options.units` **Units** used in calculating cell size. Supports all valid Turf [Units][7]. (optional, default `'kilometers'`)
|
|
19
19
|
* `options.properties` **[Object][6]** passed to each hexagon or triangle of the grid (optional, default `{}`)
|
|
20
|
-
* `options.mask` **[Feature][8]<[Polygon][2]>?** if passed a Polygon or MultiPolygon, the grid Points will be created only inside it
|
|
21
|
-
* `options.triangles` **[boolean][
|
|
20
|
+
* `options.mask` **[Feature][8]<([Polygon][2] | [MultiPolygon][9])>?** if passed a Polygon or MultiPolygon, the grid Points will be created only inside it
|
|
21
|
+
* `options.triangles` **[boolean][10]** whether to return as triangles instead of hexagons (optional, default `false`)
|
|
22
22
|
|
|
23
23
|
### Examples
|
|
24
24
|
|
|
@@ -47,11 +47,13 @@ Returns **[FeatureCollection][1]<[Polygon][2]>** a hexagonal grid
|
|
|
47
47
|
|
|
48
48
|
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
|
|
49
49
|
|
|
50
|
-
[7]: https://
|
|
50
|
+
[7]: https://turfjs.org/docs/api/types/Units
|
|
51
51
|
|
|
52
52
|
[8]: https://tools.ietf.org/html/rfc7946#section-3.2
|
|
53
53
|
|
|
54
|
-
[9]: https://
|
|
54
|
+
[9]: https://tools.ietf.org/html/rfc7946#section-3.1.7
|
|
55
|
+
|
|
56
|
+
[10]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
|
55
57
|
|
|
56
58
|
<!-- This file is automatically generated. Please don't edit it directly. If you find an error, edit the source file of the module in question (likely index.js or index.ts), and re-run "yarn docs" from the root of the turf project. -->
|
|
57
59
|
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -106,9 +106,9 @@ function hexTriangles(center, rx, ry, properties, cosines, sines) {
|
|
|
106
106
|
}
|
|
107
107
|
return triangles;
|
|
108
108
|
}
|
|
109
|
-
var
|
|
109
|
+
var index_default = hexGrid;
|
|
110
110
|
|
|
111
111
|
|
|
112
112
|
|
|
113
|
-
exports.default =
|
|
113
|
+
exports.default = index_default; exports.hexGrid = hexGrid;
|
|
114
114
|
//# sourceMappingURL=index.cjs.map
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/home/runner/work/turf/turf/packages/turf-hex-grid/dist/cjs/index.cjs","../../index.ts"],"names":[],"mappings":"AAAA;ACAA,0CAAyB;AACzB,4CAA0B;AAQ1B,wCAAkD;AA2BlD,SAAS,OAAA,CACP,IAAA,EACA,QAAA,EACA,QAAA,EAKI,CAAC,CAAA,EAC0B;AAE/B,EAAA,MAAM,iBAAA,EAAmB,IAAA,CAAK,SAAA,CAAU,OAAA,CAAQ,WAAA,GAAc,CAAC,CAAC,CAAA;AAEhE,EAAA,MAAM,CAAC,IAAA,EAAM,KAAA,EAAO,IAAA,EAAM,KAAK,EAAA,EAAI,IAAA;AACnC,EAAA,MAAM,QAAA,EAAA,CAAW,MAAA,EAAQ,KAAA,EAAA,EAAS,CAAA;AAClC,EAAA,MAAM,QAAA,EAAA,CAAW,KAAA,EAAO,IAAA,EAAA,EAAQ,CAAA;AAGhC,EAAA,MAAM,UAAA,EACH,SAAA,EAAW,EAAA,EAAK,gCAAA,CAAU,IAAA,EAAM,OAAO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AAC,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"]}
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/turf/turf/packages/turf-hex-grid/dist/cjs/index.cjs","../../index.ts"],"names":[],"mappings":"AAAA;ACAA,0CAAyB;AACzB,4CAA0B;AAS1B,wCAAkD;AA2BlD,SAAS,OAAA,CACP,IAAA,EACA,QAAA,EACA,QAAA,EAKI,CAAC,CAAA,EAC0B;AAE/B,EAAA,MAAM,iBAAA,EAAmB,IAAA,CAAK,SAAA,CAAU,OAAA,CAAQ,WAAA,GAAc,CAAC,CAAC,CAAA;AAEhE,EAAA,MAAM,CAAC,IAAA,EAAM,KAAA,EAAO,IAAA,EAAM,KAAK,EAAA,EAAI,IAAA;AACnC,EAAA,MAAM,QAAA,EAAA,CAAW,MAAA,EAAQ,KAAA,EAAA,EAAS,CAAA;AAClC,EAAA,MAAM,QAAA,EAAA,CAAW,KAAA,EAAO,IAAA,EAAA,EAAQ,CAAA;AAGhC,EAAA,MAAM,UAAA,EACH,SAAA,EAAW,EAAA,EAAK,gCAAA,CAAU,IAAA,EAAM,OAAO,CAAA,EAAG,CAAC,IAAA,EAAM,OAAO,CAAA,EAAG,OAAO,CAAA;AACrE,EAAA,MAAM,UAAA,EAAY,UAAA,EAAA,CAAa,KAAA,EAAO,IAAA,CAAA;AACtC,EAAA,MAAM,UAAA,EACH,SAAA,EAAW,EAAA,EAAK,gCAAA,CAAU,OAAA,EAAS,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AA,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,cAAA,EAAQ,OAAA;AD9Gf;AACE;AACA;AACF,2DAAC","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 MultiPolygon,\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 {Units} [options.units='kilometers'] used in calculating cell size. Supports all valid Turf {@link https://turfjs.org/docs/api/types/Units Units}.\n * @param {Object} [options.properties={}] passed to each hexagon or triangle of the grid\n * @param {Feature<Polygon | MultiPolygon>} [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 | MultiPolygon>;\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"]}
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GeoJsonProperties, BBox, Feature, Polygon, FeatureCollection } from 'geojson';
|
|
1
|
+
import { GeoJsonProperties, BBox, Feature, Polygon, MultiPolygon, FeatureCollection } from 'geojson';
|
|
2
2
|
import { Units } from '@turf/helpers';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -11,9 +11,9 @@ import { Units } from '@turf/helpers';
|
|
|
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.
|
|
13
13
|
* @param {Object} [options={}] Optional parameters
|
|
14
|
-
* @param {
|
|
14
|
+
* @param {Units} [options.units='kilometers'] used in calculating cell size. Supports all valid Turf {@link https://turfjs.org/docs/api/types/Units Units}.
|
|
15
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
|
|
16
|
+
* @param {Feature<Polygon | MultiPolygon>} [options.mask] if passed a Polygon or MultiPolygon, the grid Points will be created only inside it
|
|
17
17
|
* @param {boolean} [options.triangles=false] whether to return as triangles instead of hexagons
|
|
18
18
|
* @returns {FeatureCollection<Polygon>} a hexagonal grid
|
|
19
19
|
* @example
|
|
@@ -30,7 +30,7 @@ declare function hexGrid<P extends GeoJsonProperties = GeoJsonProperties>(bbox:
|
|
|
30
30
|
units?: Units;
|
|
31
31
|
triangles?: boolean;
|
|
32
32
|
properties?: P;
|
|
33
|
-
mask?: Feature<Polygon>;
|
|
33
|
+
mask?: Feature<Polygon | MultiPolygon>;
|
|
34
34
|
}): FeatureCollection<Polygon, P>;
|
|
35
35
|
|
|
36
36
|
export { hexGrid as default, hexGrid };
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GeoJsonProperties, BBox, Feature, Polygon, FeatureCollection } from 'geojson';
|
|
1
|
+
import { GeoJsonProperties, BBox, Feature, Polygon, MultiPolygon, FeatureCollection } from 'geojson';
|
|
2
2
|
import { Units } from '@turf/helpers';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -11,9 +11,9 @@ import { Units } from '@turf/helpers';
|
|
|
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.
|
|
13
13
|
* @param {Object} [options={}] Optional parameters
|
|
14
|
-
* @param {
|
|
14
|
+
* @param {Units} [options.units='kilometers'] used in calculating cell size. Supports all valid Turf {@link https://turfjs.org/docs/api/types/Units Units}.
|
|
15
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
|
|
16
|
+
* @param {Feature<Polygon | MultiPolygon>} [options.mask] if passed a Polygon or MultiPolygon, the grid Points will be created only inside it
|
|
17
17
|
* @param {boolean} [options.triangles=false] whether to return as triangles instead of hexagons
|
|
18
18
|
* @returns {FeatureCollection<Polygon>} a hexagonal grid
|
|
19
19
|
* @example
|
|
@@ -30,7 +30,7 @@ declare function hexGrid<P extends GeoJsonProperties = GeoJsonProperties>(bbox:
|
|
|
30
30
|
units?: Units;
|
|
31
31
|
triangles?: boolean;
|
|
32
32
|
properties?: P;
|
|
33
|
-
mask?: Feature<Polygon>;
|
|
33
|
+
mask?: Feature<Polygon | MultiPolygon>;
|
|
34
34
|
}): FeatureCollection<Polygon, P>;
|
|
35
35
|
|
|
36
36
|
export { hexGrid as default, hexGrid };
|
package/dist/esm/index.js
CHANGED
|
@@ -106,9 +106,9 @@ function hexTriangles(center, rx, ry, properties, cosines, sines) {
|
|
|
106
106
|
}
|
|
107
107
|
return triangles;
|
|
108
108
|
}
|
|
109
|
-
var
|
|
109
|
+
var index_default = hexGrid;
|
|
110
110
|
export {
|
|
111
|
-
|
|
111
|
+
index_default as default,
|
|
112
112
|
hexGrid
|
|
113
113
|
};
|
|
114
114
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -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 * @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 {
|
|
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 MultiPolygon,\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 {Units} [options.units='kilometers'] used in calculating cell size. Supports all valid Turf {@link https://turfjs.org/docs/api/types/Units Units}.\n * @param {Object} [options.properties={}] passed to each hexagon or triangle of the grid\n * @param {Feature<Polygon | MultiPolygon>} [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 | MultiPolygon>;\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;AAS1B,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,a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AAK,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,gBAAQ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/hex-grid",
|
|
3
|
-
"version": "7.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "7.3.0",
|
|
4
|
+
"description": "Creates a honeycomb-like grid of hexagons within a bounding box.",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"contributors": [
|
|
7
7
|
"James Seppi <@jseppi>",
|
|
@@ -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": "
|
|
66
|
-
"@turf/truncate": "
|
|
65
|
+
"@turf/bbox-polygon": "7.3.0",
|
|
66
|
+
"@turf/truncate": "7.3.0",
|
|
67
67
|
"@types/benchmark": "^2.1.5",
|
|
68
|
-
"@types/tape": "^
|
|
68
|
+
"@types/tape": "^5.8.1",
|
|
69
69
|
"benchmark": "^2.1.4",
|
|
70
70
|
"load-json-file": "^7.0.1",
|
|
71
71
|
"npm-run-all": "^4.1.5",
|
|
72
72
|
"tape": "^5.9.0",
|
|
73
|
-
"tsup": "^8.
|
|
74
|
-
"tsx": "^4.19.
|
|
75
|
-
"typescript": "^5.
|
|
76
|
-
"write-json-file": "^
|
|
73
|
+
"tsup": "^8.4.0",
|
|
74
|
+
"tsx": "^4.19.4",
|
|
75
|
+
"typescript": "^5.8.3",
|
|
76
|
+
"write-json-file": "^6.0.0"
|
|
77
77
|
},
|
|
78
78
|
"dependencies": {
|
|
79
|
-
"@turf/distance": "
|
|
80
|
-
"@turf/helpers": "
|
|
81
|
-
"@turf/intersect": "
|
|
82
|
-
"@turf/invariant": "
|
|
79
|
+
"@turf/distance": "7.3.0",
|
|
80
|
+
"@turf/helpers": "7.3.0",
|
|
81
|
+
"@turf/intersect": "7.3.0",
|
|
82
|
+
"@turf/invariant": "7.3.0",
|
|
83
83
|
"@types/geojson": "^7946.0.10",
|
|
84
84
|
"tslib": "^2.8.1"
|
|
85
85
|
},
|
|
86
|
-
"gitHead": "
|
|
86
|
+
"gitHead": "9f58a103e8f9a587ab640307ed03ba5233913ddd"
|
|
87
87
|
}
|