@turf/hex-grid
Advanced tools
Comparing version
35
index.js
@@ -1,7 +0,8 @@ | ||
var point = require('@turf/helpers').point; | ||
var polygon = require('@turf/helpers').polygon; | ||
var distance = require('@turf/distance'); | ||
var featurecollection = require('@turf/helpers').featureCollection; | ||
var helpers = require('@turf/helpers'); | ||
var point = helpers.point; | ||
var polygon = helpers.polygon; | ||
var featureCollection = helpers.featureCollection; | ||
//Precompute cosines and sines of angles used in hexagon creation | ||
// Precompute cosines and sines of angles used in hexagon creation | ||
// for performance gain | ||
@@ -38,6 +39,14 @@ var cosines = []; | ||
module.exports = function hexGrid(bbox, cellSize, units, triangles) { | ||
var xFraction = cellSize / (distance(point([bbox[0], bbox[1]]), point([bbox[2], bbox[1]]), units)); | ||
var cellWidth = xFraction * (bbox[2] - bbox[0]); | ||
var yFraction = cellSize / (distance(point([bbox[0], bbox[1]]), point([bbox[0], bbox[3]]), units)); | ||
var cellHeight = yFraction * (bbox[3] - bbox[1]); | ||
var west = bbox[0]; | ||
var south = bbox[1]; | ||
var east = bbox[2]; | ||
var north = bbox[3]; | ||
var centerY = (south + north) / 2; | ||
var centerX = (west + east) / 2; | ||
// https://github.com/Turfjs/turf/issues/758 | ||
var xFraction = cellSize / (distance(point([west, centerY]), point([east, centerY]), units)); | ||
var cellWidth = xFraction * (east - west); | ||
var yFraction = cellSize / (distance(point([centerX, south]), point([centerX, north]), units)); | ||
var cellHeight = yFraction * (north - south); | ||
var radius = cellWidth / 2; | ||
@@ -48,4 +57,4 @@ | ||
var box_width = bbox[2] - bbox[0]; | ||
var box_height = bbox[3] - bbox[1]; | ||
var box_width = east - west; | ||
var box_height = north - south; | ||
@@ -72,3 +81,3 @@ var x_interval = 3 / 4 * hex_width; | ||
var fc = featurecollection([]); | ||
var fc = featureCollection([]); | ||
for (var x = 0; x < x_count; x++) { | ||
@@ -86,4 +95,4 @@ for (var y = 0; y <= y_count; y++) { | ||
var center_x = x * x_interval + bbox[0] - x_adjust; | ||
var center_y = y * y_interval + bbox[1] + y_adjust; | ||
var center_x = x * x_interval + west - x_adjust; | ||
var center_y = y * y_interval + south + y_adjust; | ||
@@ -90,0 +99,0 @@ if (isOdd) { |
{ | ||
"name": "@turf/hex-grid", | ||
"version": "4.3.0", | ||
"version": "4.3.1", | ||
"description": "turf hex-grid module", | ||
@@ -29,6 +29,8 @@ "main": "index.js", | ||
"contributors": [ | ||
"jseppi", | ||
"jvail", | ||
"lyzidiamond", | ||
"tmcw" | ||
"James Seppi <@jseppi>", | ||
"Morgan Herlocker <@morganherlocker>", | ||
"Tom MacWright <@tmcw>", | ||
"Jan Vaillant <@jvail>", | ||
"Lyzi Diamond <@lyzidiamond>", | ||
"Denis Carriere <@DenisCarriere>" | ||
], | ||
@@ -41,9 +43,4 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@turf/bbox-polygon": "^4.3.0", | ||
"@turf/explode": "^4.3.0", | ||
"@turf/inside": "^4.3.0", | ||
"@turf/truncate": "^4.3.0", | ||
"benchmark": "^2.1.4", | ||
"eslint": "^3.15.0", | ||
"eslint-config-mourner": "^2.0.1", | ||
"load-json-file": "^2.0.0", | ||
@@ -50,0 +47,0 @@ "tape": "^4.6.3", |
9102
1.83%5
-50%128
6.67%