@turf/square-grid
Advanced tools
Comparing version 4.7.3 to 5.0.0
@@ -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>; |
84
index.js
@@ -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)<[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)<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)<[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)<([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 |
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
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 2 instances 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
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
12582
6
178
57
6
7
2
1
+ Added@turf/boolean-contains@*
+ Added@turf/boolean-overlap@*
+ Added@turf/intersect@5.x
+ Added@turf/invariant@5.x
+ Added@turf/bbox@7.1.0(transitive)
+ Added@turf/bearing@7.1.0(transitive)
+ Added@turf/boolean-contains@7.1.0(transitive)
+ Added@turf/boolean-overlap@7.1.0(transitive)
+ Added@turf/boolean-point-in-polygon@7.1.0(transitive)
+ Added@turf/boolean-point-on-line@7.1.0(transitive)
+ Added@turf/clean-coords@5.1.5(transitive)
+ Added@turf/destination@7.1.0(transitive)
+ Added@turf/distance@5.1.57.1.0(transitive)
+ Added@turf/geojson-rbush@7.1.0(transitive)
+ Added@turf/helpers@5.1.57.1.0(transitive)
+ Added@turf/intersect@5.1.6(transitive)
+ Added@turf/invariant@5.2.07.1.0(transitive)
+ Added@turf/line-intersect@7.1.0(transitive)
+ Added@turf/line-overlap@7.1.0(transitive)
+ Added@turf/line-segment@7.1.0(transitive)
+ Added@turf/meta@5.2.07.1.0(transitive)
+ Added@turf/nearest-point-on-line@7.1.0(transitive)
+ Added@turf/truncate@5.1.5(transitive)
+ Added@types/geojson@7946.0.14(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedgeojson-equality-ts@1.0.2(transitive)
+ Addedpoint-in-polygon-hao@1.1.0(transitive)
+ Addedquickselect@2.0.0(transitive)
+ Addedrbush@3.0.1(transitive)
+ Addedsweepline-intersections@1.5.0(transitive)
+ Addedtinyqueue@2.0.3(transitive)
+ Addedtslib@2.8.1(transitive)
+ Addedturf-jsts@1.2.3(transitive)
- Removed@turf/bbox@^4.7.3
- Removed@turf/bbox@4.7.3(transitive)
- Removed@turf/distance@4.7.3(transitive)
- Removed@turf/helpers@4.7.3(transitive)
- Removed@turf/invariant@4.7.3(transitive)
- Removed@turf/meta@4.7.4(transitive)
Updated@turf/distance@5.x
Updated@turf/helpers@5.x