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 5.1.5 to 6.0.0

index.ts

111

index.js

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

import distance from '@turf/distance';
import intersect from '@turf/intersect';
import {getType} from '@turf/invariant';
import {polygon, featureCollection, isObject, isNumber} from '@turf/helpers';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var distance_1 = require("@turf/distance");
var intersect_1 = require("@turf/intersect");
var helpers_1 = require("@turf/helpers");
/**

@@ -18,3 +18,3 @@ * Takes a bounding box and the diameter of the cell and returns a {@link FeatureCollection} of flat-topped

* @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 {Feature<Polygon>} [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

@@ -33,18 +33,10 @@ * @returns {FeatureCollection<Polygon>} a hexagonal grid

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');
// Validation is done by Typescript
// 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');
if (options === void 0) { options = {}; }
var west = bbox[0];

@@ -56,30 +48,21 @@ var south = bbox[1];

var centerX = (west + east) / 2;
// https://github.com/Turfjs/turf/issues/758
var xFraction = cellSide * 2 / (distance([west, centerY], [east, centerY], options));
var xFraction = cellSide * 2 / (distance_1.default([west, centerY], [east, centerY], options));
var cellWidth = xFraction * (east - west);
var yFraction = cellSide * 2 / (distance([centerX, south], [centerX, north], options));
var yFraction = cellSide * 2 / (distance_1.default([centerX, south], [centerX, north], options));
var cellHeight = yFraction * (north - south);
var radius = cellWidth / 2;
var hex_width = radius * 2;
var hex_height = Math.sqrt(3) / 2 * cellHeight;
var box_width = east - west;
var box_height = north - south;
var x_interval = 3 / 4 * hex_width;
var y_interval = hex_height;
// 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 + x_interval / 2;
// adjust box_height so all hexagons will be inside the bbox
var y_count = Math.floor((box_height - hex_height) / hex_height);
var y_adjust = (box_height - y_count * hex_height) / 2;
var hasOffsetY = y_count * hex_height - box_height > hex_height / 2;

@@ -89,3 +72,2 @@ if (hasOffsetY) {

}
// Precompute cosines and sines of angles used in hexagon creation for performance gain

@@ -99,44 +81,33 @@ var cosines = [];

}
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 && hasOffsetY) continue;
if (y === 0 && isOdd)
continue;
if (y === 0 && hasOffsetY)
continue;
var center_x = x * x_interval + west - x_adjust;
var center_y = y * y_interval + south + y_adjust;
if (isOdd) {
center_y -= hex_height / 2;
}
if (triangles === true) {
hexTriangles(
[center_x, center_y],
cellWidth / 2,
cellHeight / 2,
properties,
cosines,
sines).forEach(function (triangle) {
if (mask) {
if (intersect(mask, triangle)) results.push(triangle);
} else {
if (options.triangles === true) {
hexTriangles([center_x, center_y], cellWidth / 2, cellHeight / 2, options.properties, cosines, sines).forEach(function (triangle) {
if (options.mask) {
if (intersect_1.default(options.mask, triangle))
results.push(triangle);
}
else {
results.push(triangle);
}
});
} else {
var hex = hexagon(
[center_x, center_y],
cellWidth / 2,
cellHeight / 2,
properties,
cosines,
sines
);
if (mask) {
if (intersect(mask, hex)) results.push(hex);
} else {
}
else {
var hex = hexagon([center_x, center_y], cellWidth / 2, cellHeight / 2, options.properties, cosines, sines);
if (options.mask) {
if (intersect_1.default(options.mask, hex))
results.push(hex);
}
else {
results.push(hex);

@@ -147,6 +118,4 @@ }

}
return featureCollection(results);
return helpers_1.featureCollection(results);
}
/**

@@ -173,5 +142,4 @@ * Creates hexagon

vertices.push(vertices[0].slice());
return polygon([vertices], properties);
return helpers_1.polygon([vertices], properties);
}
/**

@@ -203,7 +171,6 @@ * Creates triangles composing an hexagon

vertices.push(center);
triangles.push(polygon([vertices], properties));
triangles.push(helpers_1.polygon([vertices], properties));
}
return triangles;
}
export default hexGrid;
exports.default = hexGrid;
{
"name": "@turf/hex-grid",
"version": "5.1.5",
"version": "6.0.0",
"description": "turf hex-grid module",
"main": "main.js",
"module": "main.es.js",
"types": "index.d.ts",
"main": "index",
"files": [
"index.js",
"index.d.ts",
"main.js",
"main.es.js"
"index.ts"
],
"scripts": {
"pretest": "rollup -c ../../rollup.config.js",
"test": "node -r @std/esm test.js",
"posttest": "node -r @std/esm ../../scripts/validate-es5-dependencies.js",
"bench": "node -r @std/esm bench.js",
"prepare": "tsc",
"pretest": "tsc",
"test": "node test.js",
"bench": "node bench.js",
"docs": "node ../../scripts/generate-readmes"

@@ -49,4 +45,4 @@ },

"@std/esm": "*",
"@turf/bbox-polygon": "^5.1.5",
"@turf/truncate": "^5.1.5",
"@turf/bbox-polygon": "*",
"@turf/truncate": "*",
"benchmark": "*",

@@ -59,11 +55,7 @@ "load-json-file": "*",

"dependencies": {
"@turf/distance": "^5.1.5",
"@turf/helpers": "^5.1.5",
"@turf/intersect": "^5.1.5",
"@turf/invariant": "^5.1.5"
},
"@std/esm": {
"esm": "js",
"cjs": true
"@turf/distance": "6.x",
"@turf/helpers": "6.x",
"@turf/intersect": "6.x",
"@turf/invariant": "6.x"
}
}
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