@turf/square-grid
Advanced tools
Comparing version 3.13.1 to 3.14.0
@@ -1,2 +0,2 @@ | ||
import {Units, BBox, Polygons} from '@turf/helpers' | ||
import {Units, BBox, Polygons, Feature, Features} from '@turf/helpers' | ||
@@ -6,4 +6,4 @@ /** | ||
*/ | ||
declare function squareGrid(bbox: BBox, cellSize: number, units?: Units): Polygons; | ||
declare function squareGrid(bbox: BBox | Feature | Features, cellSize: number, units?: Units, completelyWithin?: boolean): Polygons; | ||
declare namespace squareGrid { } | ||
export = squareGrid; |
57
index.js
@@ -5,31 +5,57 @@ var featureCollection = require('@turf/helpers').featureCollection; | ||
var distance = require('@turf/distance'); | ||
var turfBBox = require('@turf/bbox'); | ||
/** | ||
* Takes a bounding box and a cell depth and returns a set of square {@link Polygon|polygons} in a grid. | ||
* Creates a square grid from a bounding box, {@link Feature} or {@link FeatureCollection}. | ||
* | ||
* @name squareGrid | ||
* @param {Array<number>} bbox extent in [minX, minY, maxX, maxY] order | ||
* @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 | ||
* @return {FeatureCollection<Polygon>} grid a grid of polygons | ||
* @param {boolean} [completelyWithin=false] adjust width & height cellSize to fit exactly within bbox | ||
* @returns {FeatureCollection<Polygon>} grid a grid of polygons | ||
* @example | ||
* var bbox = [-96,31,-84,40]; | ||
* var cellSize = 10; | ||
* var bbox = [-95, 30 ,-85, 40]; | ||
* var cellSize = 50; | ||
* var units = 'miles'; | ||
* | ||
* var squareGrid = turf.squareGrid(bbox, cellSize, units); | ||
* | ||
* //=squareGrid | ||
*/ | ||
module.exports = function squareGrid(bbox, cellSize, units) { | ||
module.exports = function squareGrid(bbox, cellSize, units, completelyWithin) { | ||
var results = []; | ||
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 currentX = bbox[0]; | ||
while (currentX <= bbox[2]) { | ||
var currentY = bbox[1]; | ||
while (currentY <= bbox[3]) { | ||
// validation | ||
if (!bbox) throw new Error('bbox is required'); | ||
if (!Array.isArray(bbox)) bbox = turfBBox(bbox); // Convert GeoJSON to bbox | ||
if (bbox.length !== 4) throw new Error('bbox must contain 4 numbers'); | ||
var west = bbox[0]; | ||
var south = bbox[1]; | ||
var east = bbox[2]; | ||
var north = bbox[3]; | ||
// distance | ||
var xDistance = distance(point([west, south]), point([east, south]), units); | ||
var yDistance = distance(point([west, south]), point([west, north]), units); | ||
// rows & columns | ||
var columns = Math.ceil(xDistance / cellSize); | ||
var rows = Math.ceil(yDistance / cellSize); | ||
// columns | width | x | ||
var xFraction = cellSize / xDistance; | ||
var cellWidth = xFraction * (east - west); | ||
if (completelyWithin === true) cellWidth = cellWidth * ((xDistance / cellSize) / columns); | ||
// 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; | ||
for (var column = 0; column < columns; column++) { | ||
var currentY = south; | ||
for (var row = 0; row < rows; row++) { | ||
var cellPoly = polygon([[ | ||
@@ -48,4 +74,3 @@ [currentX, currentY], | ||
} | ||
return featureCollection(results); | ||
}; |
{ | ||
"name": "@turf/square-grid", | ||
"version": "3.13.1", | ||
"version": "3.14.0", | ||
"description": "turf square-grid module", | ||
@@ -33,5 +33,6 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@turf/bbox": "^3.13.0", | ||
"@turf/meta": "^3.13.0", | ||
"@turf/bbox": "^3.14.0", | ||
"@turf/meta": "^3.14.0", | ||
"benchmark": "^2.1.3", | ||
"mkdirp": "^0.5.1", | ||
"tape": "^4.6.3", | ||
@@ -41,5 +42,5 @@ "write-json-file": "^2.0.0" | ||
"dependencies": { | ||
"@turf/distance": "^3.13.0", | ||
"@turf/distance": "^3.14.0", | ||
"@turf/helpers": "^3.13.0" | ||
} | ||
} |
@@ -5,9 +5,10 @@ # @turf/square-grid | ||
Takes a bounding box and a cell depth and returns a set of square [polygons](http://geojson.org/geojson-spec.html#polygon) in a grid. | ||
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). | ||
**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 | ||
- `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`) | ||
@@ -17,8 +18,7 @@ **Examples** | ||
```javascript | ||
var bbox = [-96,31,-84,40]; | ||
var cellSize = 10; | ||
var bbox = [-95, 30 ,-85, 40]; | ||
var cellSize = 50; | ||
var units = 'miles'; | ||
var squareGrid = turf.squareGrid(bbox, cellSize, units); | ||
//=squareGrid | ||
@@ -25,0 +25,0 @@ ``` |
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
7296
72
6
Updated@turf/distance@^3.14.0