New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@turf/hex-grid

Package Overview
Dependencies
Maintainers
4
Versions
69
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@turf/hex-grid - npm Package Compare versions

Comparing version 4.7.3 to 5.0.0

main.js

15

index.d.ts

@@ -1,2 +0,2 @@

import {Units, BBox, Polygons} from '@turf/helpers'
import { Units, BBox, Polygon, MultiPolygon, Feature, FeatureCollection, Point, Properties } from '@turf/helpers';

@@ -6,4 +6,11 @@ /**

*/
declare function hexGrid(bbox: BBox, cellDiameter: number, units?: Units, triangles?: boolean): Polygons;
declare namespace hexGrid { }
export = hexGrid;
export default function hexGrid(
bbox: BBox,
cellSide: number,
options?: {
units?: Units,
triangles?: boolean,
properties?: Properties,
mask?: Feature<Polygon | MultiPolygon> | Polygon | MultiPolygon;
}
): FeatureCollection<Polygon>;

@@ -1,17 +0,6 @@

var distance = require('@turf/distance');
var helpers = require('@turf/helpers');
var point = helpers.point;
var polygon = helpers.polygon;
var featureCollection = helpers.featureCollection;
import distance from '@turf/distance';
import intersect from '@turf/intersect';
import {getType} from '@turf/invariant';
import {polygon, featureCollection, isObject, isNumber} from '@turf/helpers';
// Precompute cosines and sines of angles used in hexagon creation
// for performance gain
var cosines = [];
var sines = [];
for (var i = 0; i < 6; i++) {
var angle = 2 * Math.PI / 6 * i;
cosines.push(Math.cos(angle));
sines.push(Math.sin(angle));
}
/**

@@ -24,12 +13,16 @@ * Takes a bounding box and the diameter of the cell and returns a {@link FeatureCollection} of flat-topped

* @param {Array<number>} bbox extent in [minX, minY, maxX, maxY] order
* @param {number} cellDiameter diameter of the circumcircle of the hexagons, in specified units
* @param {string} [units=kilometers] used in calculating cell size, can be degrees, radians, miles, or kilometers
* @param {boolean} [triangles=false] whether to return as triangles instead of hexagons
* @param {number} cellSide length of the side of the the hexagons or triangles, in units. It will also coincide with the
* radius of the circumcircle of the hexagons.
* @param {Object} [options={}] Optional parameters
* @param {string} [options.units='kilometers'] used in calculating cell size, can be degrees, radians, miles, or kilometers
* @param {Object} [options.properties={}] passed to each hexagon or triangle of the grid
* @param {Feature<Polygon|MultiPolygon>} [options.mask] if passed a Polygon or MultiPolygon, the grid Points will be created only inside it
* @param {boolean} [options.triangles=false] whether to return as triangles instead of hexagons
* @returns {FeatureCollection<Polygon>} a hexagonal grid
* @example
* var bbox = [-96,31,-84,40];
* var cellDiameter = 50;
* var units = 'miles';
* var cellSide = 50;
* var options = {units: 'miles'};
*
* var hexgrid = turf.hexGrid(bbox, cellDiameter, units);
* var hexgrid = turf.hexGrid(bbox, cellSide, options);
*

@@ -39,3 +32,19 @@ * //addToMap

*/
module.exports = function hexGrid(bbox, cellDiameter, units, triangles) {
function hexGrid(bbox, cellSide, options) {
// Optional parameters
options = options || {};
if (!isObject(options)) throw new Error('options is invalid');
// var units = options.units;
var properties = options.properties || {};
var triangles = options.triangles;
var mask = options.mask;
// validation
if (cellSide === null || cellSide === undefined) throw new Error('cellSide is required');
if (!isNumber(cellSide)) throw new Error('cellSide is invalid');
if (!bbox) throw new Error('bbox is required');
if (!Array.isArray(bbox)) throw new Error('bbox must be array');
if (bbox.length !== 4) throw new Error('bbox must contain 4 numbers');
if (mask && ['Polygon', 'MultiPolygon'].indexOf(getType(mask)) === -1) throw new Error('options.mask must be a (Multi)Polygon');
var west = bbox[0];

@@ -49,5 +58,5 @@ var south = bbox[1];

// https://github.com/Turfjs/turf/issues/758
var xFraction = cellDiameter / (distance(point([west, centerY]), point([east, centerY]), units));
var xFraction = cellSide * 2 / (distance([west, centerY], [east, centerY], options));
var cellWidth = xFraction * (east - west);
var yFraction = cellDiameter / (distance(point([centerX, south]), point([centerX, north]), units));
var yFraction = cellSide * 2 / (distance([centerX, south], [centerX, north], options));
var cellHeight = yFraction * (north - south);

@@ -65,11 +74,10 @@ var radius = cellWidth / 2;

var x_span = box_width / (hex_width - radius / 2);
var x_count = Math.ceil(x_span);
if (Math.round(x_span) === x_count) {
x_count++;
}
// adjust box_width so all hexagons will be inside the bbox
var x_span = (box_width - hex_width) / (hex_width - radius / 2);
var x_count = Math.floor(x_span);
var x_adjust = ((x_count * x_interval - radius / 2) - box_width) / 2 - radius / 2;
var x_adjust = ((x_count * x_interval - radius / 2) - box_width) / 2 - radius / 2 + x_interval / 2;
var y_count = Math.ceil(box_height / hex_height);
// adjust box_height so all hexagons will be inside the bbox
var y_count = Math.floor((box_height - hex_height) / hex_height);

@@ -83,15 +91,19 @@ var y_adjust = (box_height - y_count * hex_height) / 2;

var fc = featureCollection([]);
for (var x = 0; x < x_count; x++) {
// Precompute cosines and sines of angles used in hexagon creation for performance gain
var cosines = [];
var sines = [];
for (var i = 0; i < 6; i++) {
var angle = 2 * Math.PI / 6 * i;
cosines.push(Math.cos(angle));
sines.push(Math.sin(angle));
}
var results = [];
for (var x = 0; x <= x_count; x++) {
for (var y = 0; y <= y_count; y++) {
var isOdd = x % 2 === 1;
if (y === 0 && isOdd) {
continue;
}
if (y === 0 && isOdd) continue;
if (y === 0 && hasOffsetY) continue;
if (y === 0 && hasOffsetY) {
continue;
}
var center_x = x * x_interval + west - x_adjust;

@@ -103,6 +115,32 @@ var center_y = y * y_interval + south + y_adjust;

}
if (triangles) {
fc.features.push.apply(fc.features, hexTriangles([center_x, center_y], cellWidth / 2, cellHeight / 2));
if (triangles === true) {
for (var triangle of hexTriangles(
[center_x, center_y],
cellWidth / 2,
cellHeight / 2,
properties,
cosines,
sines
)) {
if (mask) {
if (intersect(mask, triangle)) results.push(triangle);
} else {
results.push(triangle);
}
}
} else {
fc.features.push(hexagon([center_x, center_y], cellWidth / 2, cellHeight / 2));
var hex = hexagon(
[center_x, center_y],
cellWidth / 2,
cellHeight / 2,
properties,
cosines,
sines
);
if (mask) {
if (intersect(mask, hex)) results.push(hex);
} else {
results.push(hex);
}
}

@@ -112,7 +150,18 @@ }

return fc;
};
return featureCollection(results);
}
//Center should be [x, y]
function hexagon(center, rx, ry) {
/**
* Creates hexagon
*
* @private
* @param {Array<number>} center of the hexagon
* @param {number} rx half hexagon width
* @param {number} ry half hexagon height
* @param {Object} properties passed to each hexagon
* @param {Array<number>} cosines precomputed
* @param {Array<number>} sines precomputed
* @returns {Feature<Polygon>} hexagon
*/
function hexagon(center, rx, ry, properties, cosines, sines) {
var vertices = [];

@@ -126,7 +175,18 @@ for (var i = 0; i < 6; i++) {

vertices.push(vertices[0].slice());
return polygon([vertices]);
return polygon([vertices], properties);
}
//Center should be [x, y]
function hexTriangles(center, rx, ry) {
/**
* Creates triangles composing an hexagon
*
* @private
* @param {Array<number>} center of the hexagon
* @param {number} rx half triangle width
* @param {number} ry half triangle height
* @param {Object} properties passed to each triangle
* @param {Array<number>} cosines precomputed
* @param {Array<number>} sines precomputed
* @returns {Array<Feature<Polygon>>} triangles
*/
function hexTriangles(center, rx, ry, properties, cosines, sines) {
var triangles = [];

@@ -145,5 +205,7 @@ for (var i = 0; i < 6; i++) {

vertices.push(center);
triangles.push(polygon([vertices]));
triangles.push(polygon([vertices], properties));
}
return triangles;
}
export default hexGrid;
{
"name": "@turf/hex-grid",
"version": "4.7.3",
"version": "5.0.0",
"description": "turf hex-grid module",
"main": "index.js",
"main": "main",
"module": "index",
"jsnext:main": "index",
"types": "index.d.ts",
"files": [
"index.js",
"index.d.ts"
"index.d.ts",
"main.js"
],
"scripts": {
"test": "node test.js",
"bench": "node bench.js"
"pretest": "rollup -c ../../rollup.config.js",
"test": "node -r @std/esm test.js",
"bench": "node -r @std/esm bench.js"
},

@@ -42,13 +46,21 @@ "repository": {

"devDependencies": {
"@turf/centroid": "^4.7.3",
"@turf/truncate": "^4.7.3",
"benchmark": "^2.1.4",
"load-json-file": "^2.0.0",
"tape": "^4.6.3",
"write-json-file": "^2.0.0"
"@std/esm": "*",
"@turf/bbox-polygon": "*",
"@turf/truncate": "*",
"benchmark": "*",
"load-json-file": "*",
"rollup": "*",
"tape": "*",
"write-json-file": "*"
},
"dependencies": {
"@turf/distance": "^4.7.3",
"@turf/helpers": "^4.7.3"
"@turf/distance": "5.x",
"@turf/helpers": "5.x",
"@turf/intersect": "5.x",
"@turf/invariant": "5.x"
},
"@std/esm": {
"esm": "js",
"cjs": true
}
}
# @turf/hex-grid
# hexGrid
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
## hexGrid
Takes a bounding box and the diameter of the cell and returns a [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) of flat-topped

@@ -12,5 +14,9 @@ hexagons or triangles ([Polygon](http://geojson.org/geojson-spec.html#polygon) features) aligned in an "odd-q" vertical grid as

- `bbox` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)&lt;[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>** extent in [minX, minY, maxX, maxY] order
- `cellDiameter` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** diameter of the circumcircle of the hexagons, in specified units
- `units` **\[[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** used in calculating cell size, can be degrees, radians, miles, or kilometers (optional, default `kilometers`)
- `triangles` **\[[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** whether to return as triangles instead of hexagons (optional, default `false`)
- `cellSide` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** length of the side of the the hexagons or triangles, in units. It will also coincide with the
radius of the circumcircle of the hexagons.
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** Optional parameters (optional, default `{}`)
- `options.units` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** used in calculating cell size, can be degrees, radians, miles, or kilometers (optional, default `'kilometers'`)
- `options.properties` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** passed to each hexagon or triangle of the grid (optional, default `{}`)
- `options.mask` **[Feature](http://geojson.org/geojson-spec.html#feature-objects)&lt;([Polygon](http://geojson.org/geojson-spec.html#polygon) \| [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon))>?** if passed a Polygon or MultiPolygon, the grid Points will be created only inside it
- `options.triangles` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** whether to return as triangles instead of hexagons (optional, default `false`)

@@ -21,6 +27,6 @@ **Examples**

var bbox = [-96,31,-84,40];
var cellDiameter = 50;
var units = 'miles';
var cellSide = 50;
var options = {units: 'miles'};
var hexgrid = turf.hexGrid(bbox, cellDiameter, units);
var hexgrid = turf.hexGrid(bbox, cellSide, options);

@@ -27,0 +33,0 @@ //addToMap

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc