-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
41 lines (38 loc) · 1.37 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var point = require('turf-point');
var featurecollection = require('turf-featurecollection');
var distance = require('turf-distance');
/**
* Takes a bounding box and a cell depth and returns a set of {@link Point|points} in a grid.
*
* @module turf/point-grid
* @category interpolation
* @param {Array<number>} extent extent in [minX, minY, maxX, maxY] order
* @param {Number} cellWidth the distance across each cell
* @param {String} units used in calculating cellWidth ('miles' or 'kilometers')
* @return {FeatureCollection<Point>} grid of points
* @example
* var extent = [-70.823364, -33.553984, -70.473175, -33.302986];
* var cellWidth = 3;
* var units = 'miles';
*
* var grid = turf.pointGrid(extent, cellWidth, units);
*
* //=grid
*/
module.exports = function (bbox, cell, units) {
var fc = featurecollection([]);
var xFraction = cell / (distance(point([bbox[0], bbox[1]]), point([bbox[2], bbox[1]]), units));
var cellWidth = xFraction * (bbox[2] - bbox[0]);
var yFraction = cell / (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]) {
fc.features.push(point([currentX, currentY]));
currentY += cellHeight;
}
currentX += cellWidth;
}
return fc;
};