Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@turf/square-grid

Package Overview
Dependencies
Maintainers
4
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@turf/square-grid - npm Package Compare versions

Comparing version 4.7.3 to 5.0.0

main.js

14

index.d.ts

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

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

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

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

@@ -1,7 +0,5 @@

var distance = require('@turf/distance');
var turfBBox = require('@turf/bbox');
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';

@@ -12,13 +10,15 @@ /**

* @name squareGrid
* @param {Array<number>|FeatureCollection|Feature<any>} bbox extent in [minX, minY, maxX, maxY] order
* @param {number} cellSize width of each cell
* @param {string} [units=kilometers] used in calculating cellSize, can be degrees, radians, miles, or kilometers
* @param {boolean} [completelyWithin=false] adjust width & height cellSize to fit exactly within bbox
* @param {Array<number>} bbox extent in [minX, minY, maxX, maxY] order
* @param {number} cellSide of each cell, in units
* @param {Object} [options={}] Optional parameters
* @param {string} [options.units='kilometers'] used in calculating cellSide, can be degrees, radians, miles, or kilometers
* @param {Feature<Polygon|MultiPolygon>} [options.mask] if passed a Polygon or MultiPolygon, the grid Points will be created only inside it
* @param {Object} [options.properties={}] passed to each point of the grid
* @returns {FeatureCollection<Polygon>} grid a grid of polygons
* @example
* var bbox = [-95, 30 ,-85, 40];
* var cellSize = 50;
* var units = 'miles';
* var cellSide = 50;
* var options = {units: 'miles'};
*
* var squareGrid = turf.squareGrid(bbox, cellSize, units);
* var squareGrid = turf.squareGrid(bbox, cellSide, options);
*

@@ -28,9 +28,20 @@ * //addToMap

*/
module.exports = function squareGrid(bbox, cellSize, units, completelyWithin) {
function squareGrid(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 mask = options.mask;
// Containers
var results = [];
// validation
// Input 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)) bbox = turfBBox(bbox); // Convert GeoJSON to bbox
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');

@@ -42,24 +53,21 @@ var west = bbox[0];

// distance
var xDistance = distance(point([west, south]), point([east, south]), units);
var yDistance = distance(point([west, south]), point([west, north]), units);
var xFraction = cellSide / (distance([west, south], [east, south], options));
var cellWidth = xFraction * (east - west);
var yFraction = cellSide / (distance([west, south], [west, north], options));
var cellHeight = yFraction * (north - south);
// rows & columns
var columns = Math.ceil(xDistance / cellSize);
var rows = Math.ceil(yDistance / cellSize);
var bboxWidth = (east - west);
var bboxHeight = (north - south);
var columns = Math.floor(bboxWidth / cellWidth);
var rows = Math.floor(bboxHeight / cellHeight);
// columns | width | x
var xFraction = cellSize / xDistance;
var cellWidth = xFraction * (east - west);
if (completelyWithin === true) cellWidth = cellWidth * ((xDistance / cellSize) / columns);
// adjust origin of the grid
var deltaX = (bboxWidth - columns * cellWidth) / 2;
var deltaY = (bboxHeight - rows * cellHeight) / 2;
// rows | height | y
var yFraction = cellSize / yDistance;
var cellHeight = yFraction * (north - south);
if (completelyWithin === true) cellHeight = cellHeight * ((yDistance / cellSize) / rows);
// iterate over columns & rows
var currentX = west;
var currentX = west + deltaX;
for (var column = 0; column < columns; column++) {
var currentY = south;
var currentY = south + deltaY;
for (var row = 0; row < rows; row++) {

@@ -72,4 +80,8 @@ var cellPoly = polygon([[

[currentX, currentY]
]]);
results.push(cellPoly);
]], properties);
if (mask) {
if (intersect(mask, cellPoly)) results.push(cellPoly);
} else {
results.push(cellPoly);
}

@@ -81,2 +93,4 @@ currentY += cellHeight;

return featureCollection(results);
};
}
export default squareGrid;
{
"name": "@turf/square-grid",
"version": "4.7.3",
"version": "5.0.0",
"description": "turf square-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"
},

@@ -33,13 +37,22 @@ "repository": {

"devDependencies": {
"@turf/meta": "^4.7.3",
"benchmark": "^2.1.4",
"mkdirp": "^0.5.1",
"tape": "^4.6.3",
"write-json-file": "^2.0.0"
"@std/esm": "*",
"@turf/bbox-polygon": "*",
"@turf/truncate": "*",
"benchmark": "*",
"rollup": "*",
"tape": "*",
"write-json-file": "*"
},
"dependencies": {
"@turf/bbox": "^4.7.3",
"@turf/distance": "^4.7.3",
"@turf/helpers": "^4.7.3"
"@turf/boolean-contains": "*",
"@turf/boolean-overlap": "*",
"@turf/distance": "5.x",
"@turf/helpers": "5.x",
"@turf/intersect": "5.x",
"@turf/invariant": "5.x"
},
"@std/esm": {
"esm": "js",
"cjs": true
}
}
# @turf/square-grid
# squareGrid
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
## squareGrid
Creates a square grid from a bounding box, [Feature](http://geojson.org/geojson-spec.html#feature-objects) or [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects).

@@ -9,6 +11,8 @@

- `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)> | [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)&lt;any>)** extent in [minX, minY, maxX, maxY] order
- `cellSize` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** width of each cell
- `units` **\[[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** used in calculating cellSize, can be degrees, radians, miles, or kilometers (optional, default `kilometers`)
- `completelyWithin` **\[[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** adjust width & height cellSize to fit exactly within bbox (optional, default `false`)
- `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
- `cellSide` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** of each cell, in units
- `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 cellSide, can be degrees, radians, miles, or kilometers (optional, default `'kilometers'`)
- `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.properties` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** passed to each point of the grid (optional, default `{}`)

@@ -19,6 +23,6 @@ **Examples**

var bbox = [-95, 30 ,-85, 40];
var cellSize = 50;
var units = 'miles';
var cellSide = 50;
var options = {units: 'miles'};
var squareGrid = turf.squareGrid(bbox, cellSize, units);
var squareGrid = turf.squareGrid(bbox, cellSide, options);

@@ -25,0 +29,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