@turf/point-grid
Advanced tools
Comparing version 4.4.0 to 4.5.1
@@ -6,4 +6,4 @@ import {BBox, Points, Units, Feature, Features} from '@turf/helpers'; | ||
*/ | ||
declare function pointGrid(bbox: BBox | Feature<any> | Features<any>, cellSize: number, units?: Units, centered?: boolean): Points; | ||
declare function pointGrid(bbox: BBox | Feature<any> | Features<any>, cellSide: number, units?: Units, centered?: boolean, bboxIsMask?: boolean): Points; | ||
declare namespace pointGrid { } | ||
export = pointGrid; |
31
index.js
var distance = require('@turf/distance'); | ||
var turfBBox = require('@turf/bbox'); | ||
var helpers = require('@turf/helpers'); | ||
var inside = require('@turf/inside'); | ||
var invariant = require('@turf/invariant'); | ||
var getGeomType = invariant.getGeomType; | ||
var point = helpers.point; | ||
@@ -12,12 +15,14 @@ var featureCollection = helpers.featureCollection; | ||
* @param {Array<number>|FeatureCollection|Feature<any>} bbox extent in [minX, minY, maxX, maxY] order | ||
* @param {number} cellSize the distance across each cell | ||
* @param {string} [units=kilometers] used in calculating cellSize, can be degrees, radians, miles, or kilometers | ||
* @param {number} cellSide the distance between points | ||
* @param {string} [units=kilometers] used in calculating cellSide, can be degrees, radians, miles, or kilometers | ||
* @param {boolean} [centered=false] adjust points position to center the grid into bbox | ||
* @param {boolean} [bboxIsMask=false] if true, and bbox is a Polygon or MultiPolygon, the grid Point will be created | ||
* only if inside the bbox Polygon(s) | ||
* @returns {FeatureCollection<Point>} grid of points | ||
* @example | ||
* var extent = [-70.823364, -33.553984, -70.473175, -33.302986]; | ||
* var cellSize = 3; | ||
* var cellSide = 3; | ||
* var units = 'miles'; | ||
* | ||
* var grid = turf.pointGrid(extent, cellSize, units); | ||
* var grid = turf.pointGrid(extent, cellSide, units); | ||
* | ||
@@ -27,5 +32,6 @@ * //addToMap | ||
*/ | ||
module.exports = function (bbox, cellSize, units, centered) { | ||
module.exports = function (bbox, cellSide, units, centered, bboxIsMask) { | ||
var results = []; | ||
var bboxMask = bbox; | ||
// validation | ||
@@ -41,5 +47,5 @@ if (!bbox) throw new Error('bbox is required'); | ||
var xFraction = cellSize / (distance(point([west, south]), point([east, south]), units)); | ||
var xFraction = cellSide / (distance(point([west, south]), point([east, south]), units)); | ||
var cellWidth = xFraction * (east - west); | ||
var yFraction = cellSize / (distance(point([west, south]), point([west, north]), units)); | ||
var yFraction = cellSide / (distance(point([west, south]), point([west, north]), units)); | ||
var cellHeight = yFraction * (north - south); | ||
@@ -57,2 +63,4 @@ | ||
var isPoly = !Array.isArray(bboxMask) && (getGeomType(bboxMask) === 'Polygon' || getGeomType(bboxMask) === 'MultiPolygon'); | ||
var currentX = west; | ||
@@ -64,3 +72,10 @@ if (centered === true) currentX += deltaX; | ||
while (currentY <= north) { | ||
results.push(point([currentX, currentY])); | ||
var pt = point([currentX, currentY]); | ||
if (bboxIsMask === true && isPoly) { | ||
if (inside(pt, bboxMask)) { | ||
results.push(pt); | ||
} | ||
} else { | ||
results.push(pt); | ||
} | ||
currentY += cellHeight; | ||
@@ -67,0 +82,0 @@ } |
{ | ||
"name": "@turf/point-grid", | ||
"version": "4.4.0", | ||
"version": "4.5.1", | ||
"description": "turf point-grid module", | ||
@@ -26,2 +26,5 @@ "main": "index.js", | ||
"author": "Turf Authors", | ||
"contributors": [ | ||
"Stefano Borghi <@stebogit>" | ||
], | ||
"license": "MIT", | ||
@@ -33,3 +36,3 @@ "bugs": { | ||
"devDependencies": { | ||
"@turf/meta": "^4.4.0", | ||
"@turf/meta": "^4.5.1", | ||
"benchmark": "^2.1.4", | ||
@@ -42,6 +45,8 @@ "load-json-file": "^2.0.0", | ||
"dependencies": { | ||
"@turf/bbox": "^4.4.0", | ||
"@turf/distance": "^4.4.0", | ||
"@turf/helpers": "^4.4.0" | ||
"@turf/bbox": "^4.5.1", | ||
"@turf/distance": "^4.5.1", | ||
"@turf/helpers": "^4.5.1", | ||
"@turf/inside": "^4.5.1", | ||
"@turf/invariant": "^4.5.1" | ||
} | ||
} |
@@ -10,5 +10,7 @@ # @turf/point-grid | ||
- `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)** the distance across 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`) | ||
- `cellSide` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** the distance between points | ||
- `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`) | ||
- `centered` **\[[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** adjust points position to center the grid into bbox (optional, default `false`) | ||
- `bboxIsMask` **\[[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** if true, and bbox is a Polygon or MultiPolygon, the grid Point will be created | ||
only if inside the bbox Polygon(s) (optional, default `false`) | ||
@@ -19,6 +21,6 @@ **Examples** | ||
var extent = [-70.823364, -33.553984, -70.473175, -33.302986]; | ||
var cellSize = 3; | ||
var cellSide = 3; | ||
var units = 'miles'; | ||
var grid = turf.pointGrid(extent, cellSize, units); | ||
var grid = turf.pointGrid(extent, cellSide, units); | ||
@@ -25,0 +27,0 @@ //addToMap |
8170
81
55
5
+ Added@turf/inside@^4.5.1
+ Added@turf/invariant@^4.5.1
+ Added@turf/inside@4.7.3(transitive)
Updated@turf/bbox@^4.5.1
Updated@turf/distance@^4.5.1
Updated@turf/helpers@^4.5.1