@turf/square-grid
Advanced tools
Comparing version 6.0.0 to 6.0.1
25
index.js
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var distance_1 = require("@turf/distance"); | ||
var boolean_intersects_1 = require("@turf/boolean-intersects"); | ||
var boolean_intersects_1 = __importDefault(require("@turf/boolean-intersects")); | ||
var distance_1 = __importDefault(require("@turf/distance")); | ||
var helpers_1 = require("@turf/helpers"); | ||
@@ -13,4 +16,6 @@ /** | ||
* @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 {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 | ||
@@ -32,9 +37,2 @@ * @returns {FeatureCollection<Polygon>} grid a grid of polygons | ||
var results = []; | ||
// Input Validation is being handled 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 (options.mask && ['Polygon', 'MultiPolygon'].indexOf(getType(options.mask)) === -1) throw new Error('options.mask must be a (Multi)Polygon'); | ||
var west = bbox[0]; | ||
@@ -66,7 +64,8 @@ var south = bbox[1]; | ||
[currentX + cellWidth, currentY], | ||
[currentX, currentY] | ||
[currentX, currentY], | ||
]], options.properties); | ||
if (options.mask) { | ||
if (boolean_intersects_1.default(options.mask, cellPoly)) | ||
if (boolean_intersects_1.default(options.mask, cellPoly)) { | ||
results.push(cellPoly); | ||
} | ||
} | ||
@@ -73,0 +72,0 @@ else { |
73
index.ts
@@ -1,8 +0,8 @@ | ||
import distance from '@turf/distance'; | ||
import intersect from '@turf/boolean-intersects'; | ||
import {getType} from '@turf/invariant'; | ||
import intersect from "@turf/boolean-intersects"; | ||
import distance from "@turf/distance"; | ||
import { | ||
polygon, featureCollection, isNumber, | ||
BBox, Units, Feature, Properties, Polygon, MultiPolygon, FeatureCollection | ||
} from '@turf/helpers'; | ||
BBox, Feature, featureCollection, | ||
FeatureCollection, isNumber, MultiPolygon, polygon, Polygon, Properties, Units, | ||
} from "@turf/helpers"; | ||
import {getType} from "@turf/invariant"; | ||
@@ -16,4 +16,6 @@ /** | ||
* @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 {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 | ||
@@ -34,41 +36,32 @@ * @returns {FeatureCollection<Polygon>} grid a grid of polygons | ||
properties?: P, | ||
mask?: Feature<Polygon | MultiPolygon> | Polygon | MultiPolygon | ||
mask?: Feature<Polygon | MultiPolygon> | Polygon | MultiPolygon, | ||
} = {}): FeatureCollection<Polygon, P> { | ||
// Containers | ||
var results = []; | ||
const results = []; | ||
const west = bbox[0]; | ||
const south = bbox[1]; | ||
const east = bbox[2]; | ||
const north = bbox[3]; | ||
// Input Validation is being handled 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 (options.mask && ['Polygon', 'MultiPolygon'].indexOf(getType(options.mask)) === -1) throw new Error('options.mask must be a (Multi)Polygon'); | ||
const xFraction = cellSide / (distance([west, south], [east, south], options)); | ||
const cellWidth = xFraction * (east - west); | ||
const yFraction = cellSide / (distance([west, south], [west, north], options)); | ||
const cellHeight = yFraction * (north - south); | ||
var west = bbox[0]; | ||
var south = bbox[1]; | ||
var east = bbox[2]; | ||
var north = bbox[3]; | ||
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 bboxWidth = (east - west); | ||
var bboxHeight = (north - south); | ||
var columns = Math.floor(bboxWidth / cellWidth); | ||
var rows = Math.floor(bboxHeight / cellHeight); | ||
const bboxWidth = (east - west); | ||
const bboxHeight = (north - south); | ||
const columns = Math.floor(bboxWidth / cellWidth); | ||
const rows = Math.floor(bboxHeight / cellHeight); | ||
// adjust origin of the grid | ||
var deltaX = (bboxWidth - columns * cellWidth) / 2; | ||
var deltaY = (bboxHeight - rows * cellHeight) / 2; | ||
const deltaX = (bboxWidth - columns * cellWidth) / 2; | ||
const deltaY = (bboxHeight - rows * cellHeight) / 2; | ||
// iterate over columns & rows | ||
var currentX = west + deltaX; | ||
for (var column = 0; column < columns; column++) { | ||
var currentY = south + deltaY; | ||
for (var row = 0; row < rows; row++) { | ||
var cellPoly = polygon([[ | ||
let currentX = west + deltaX; | ||
for (let column = 0; column < columns; column++) { | ||
let currentY = south + deltaY; | ||
for (let row = 0; row < rows; row++) { | ||
const cellPoly = polygon([[ | ||
[currentX, currentY], | ||
@@ -78,6 +71,6 @@ [currentX, currentY + cellHeight], | ||
[currentX + cellWidth, currentY], | ||
[currentX, currentY] | ||
[currentX, currentY], | ||
]], options.properties); | ||
if (options.mask) { | ||
if (intersect(options.mask, cellPoly)) results.push(cellPoly); | ||
if (intersect(options.mask, cellPoly)) { results.push(cellPoly); } | ||
} else { | ||
@@ -84,0 +77,0 @@ results.push(cellPoly); |
{ | ||
"name": "@turf/square-grid", | ||
"version": "6.0.0", | ||
"version": "6.0.1", | ||
"description": "turf square-grid module", | ||
"main": "index", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.js", | ||
"index.ts" | ||
"index.ts", | ||
"index.d.ts" | ||
], | ||
@@ -35,9 +37,10 @@ "scripts": { | ||
"devDependencies": { | ||
"@std/esm": "*", | ||
"@turf/bbox-polygon": "*", | ||
"@turf/truncate": "*", | ||
"benchmark": "*", | ||
"rollup": "*", | ||
"tape": "*", | ||
"write-json-file": "*" | ||
"write-json-file": "*", | ||
"typescript": "*", | ||
"tslint": "*", | ||
"@types/tape": "*" | ||
}, | ||
@@ -44,0 +47,0 @@ "dependencies": { |
@@ -7,12 +7,12 @@ # @turf/square-grid | ||
Creates a square grid from a bounding box, [Feature](https://tools.ietf.org/html/rfc7946#section-3.2) or [FeatureCollection](https://tools.ietf.org/html/rfc7946#section-3.3). | ||
Creates a square grid from a bounding box, [Feature][1] or [FeatureCollection][2]. | ||
**Parameters** | ||
- `bbox` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[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](https://tools.ietf.org/html/rfc7946#section-3.2)<([Polygon](https://tools.ietf.org/html/rfc7946#section-3.1.6) \| [MultiPolygon](https://tools.ietf.org/html/rfc7946#section-3.1.7))>?** 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 `{}`) | ||
- `bbox` **[Array][3]<[number][4]>** extent in [minX, minY, maxX, maxY] order | ||
- `cellSide` **[number][4]** of each cell, in units | ||
- `options` **[Object][5]** Optional parameters (optional, default `{}`) | ||
- `options.units` **[string][6]** used in calculating cellSide, can be degrees, radians, miles, or kilometers (optional, default `'kilometers'`) | ||
- `options.mask` **[Feature][7]<([Polygon][8] \| [MultiPolygon][9])>?** if passed a Polygon or MultiPolygon, the grid Points will be created only inside it | ||
- `options.properties` **[Object][5]** passed to each point of the grid (optional, default `{}`) | ||
@@ -32,4 +32,24 @@ **Examples** | ||
Returns **[FeatureCollection](https://tools.ietf.org/html/rfc7946#section-3.3)<[Polygon](https://tools.ietf.org/html/rfc7946#section-3.1.6)>** grid a grid of polygons | ||
Returns **[FeatureCollection][10]<[Polygon][8]>** grid a grid of polygons | ||
[1]: https://tools.ietf.org/html/rfc7946#section-3.2 | ||
[2]: https://tools.ietf.org/html/rfc7946#section-3.3 | ||
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array | ||
[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number | ||
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object | ||
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String | ||
[7]: https://tools.ietf.org/html/rfc7946#section-3.2 | ||
[8]: https://tools.ietf.org/html/rfc7946#section-3.1.6 | ||
[9]: https://tools.ietf.org/html/rfc7946#section-3.1.7 | ||
[10]: https://tools.ietf.org/html/rfc7946#section-3.3 | ||
<!-- This file is automatically generated. Please don't edit it directly: | ||
@@ -36,0 +56,0 @@ if you find an error, edit the source file (likely index.js), and re-run |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
12187
6
185
77
0
8