Last active
August 4, 2018 05:09
-
-
Save jaskiratr/71d0954c5022e4e44cb3f2edc2d4a3b0 to your computer and use it in GitHub Desktop.
Triangle grid stream based on Turf.js triangle grid function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict" | |
exports.__esModule = true | |
var distance_1 = require("@turf/distance") | |
var intersect_1 = require("@turf/intersect") | |
var helpers_1 = require("@turf/helpers") | |
/** | |
* Takes a bounding box and a cell depth and returns a set of triangular {@link Polygon|polygons} in a grid. | |
* | |
* @name triangleGrid | |
* @param {Array<number>} bbox extent in [minX, minY, maxX, maxY] order | |
* @param {number} cellSide dimension of each cell | |
* @param {Object} [options={}] Optional parameters | |
* @param {string} [options.units='kilometers'] used in calculating cellSide, can be degrees, radians, miles, or kilometers | |
* @param {Feature<Polygon>} [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 of polygons | |
* @example | |
* var bbox = [-95, 30 ,-85, 40] | |
* var cellSide = 50 | |
* var options = {units: 'miles'} | |
* | |
* var triangleGrid = turf.triangleGrid(bbox, cellSide, options) | |
* | |
* //addToMap | |
* var addToMap = [triangleGrid] | |
*/ | |
var triangleStream = null | |
function triangleGridStream(bbox, cellSide, options) { | |
return new Promise((resolve, reject) => { | |
triangleStream = triangleGridGenerator(bbox, cellSide, options) | |
if (triangleStream) | |
resolve(triangleStream) | |
else | |
reject("Failed to generate stream") | |
}) | |
} | |
function* triangleGridGenerator(bbox, cellSide, options) { | |
if (options === void 0) { options = {} } | |
// Containers | |
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 (mask && ['Polygon', 'MultiPolygon'].indexOf(getType(mask)) === -1) throw new Error('options.mask must be a (Multi)Polygon') | |
// Main | |
var xFraction = cellSide / (distance_1["default"]([bbox[0], bbox[1]], [bbox[2], bbox[1]], options)) | |
var cellWidth = xFraction * (bbox[2] - bbox[0]) | |
var yFraction = cellSide / (distance_1["default"]([bbox[0], bbox[1]], [bbox[0], bbox[3]], options)) | |
var cellHeight = yFraction * (bbox[3] - bbox[1]) | |
var xi = 0 | |
var currentX = bbox[0] | |
while (currentX <= bbox[2]) { | |
var yi = 0 | |
var currentY = bbox[1] | |
while (currentY <= bbox[3]) { | |
var cellTriangle1 = null | |
var cellTriangle2 = null | |
if (xi % 2 === 0 && yi % 2 === 0) { | |
cellTriangle1 = helpers_1.polygon([ | |
[ | |
[currentX, currentY], | |
[currentX, currentY + cellHeight], | |
[currentX + cellWidth, currentY], | |
[currentX, currentY] | |
] | |
], options.properties) | |
cellTriangle2 = helpers_1.polygon([ | |
[ | |
[currentX, currentY + cellHeight], | |
[currentX + cellWidth, currentY + cellHeight], | |
[currentX + cellWidth, currentY], | |
[currentX, currentY + cellHeight] | |
] | |
], options.properties) | |
} else if (xi % 2 === 0 && yi % 2 === 1) { | |
cellTriangle1 = helpers_1.polygon([ | |
[ | |
[currentX, currentY], | |
[currentX + cellWidth, currentY + cellHeight], | |
[currentX + cellWidth, currentY], | |
[currentX, currentY] | |
] | |
], options.properties) | |
cellTriangle2 = helpers_1.polygon([ | |
[ | |
[currentX, currentY], | |
[currentX, currentY + cellHeight], | |
[currentX + cellWidth, currentY + cellHeight], | |
[currentX, currentY] | |
] | |
], options.properties) | |
} else if (yi % 2 === 0 && xi % 2 === 1) { | |
cellTriangle1 = helpers_1.polygon([ | |
[ | |
[currentX, currentY], | |
[currentX, currentY + cellHeight], | |
[currentX + cellWidth, currentY + cellHeight], | |
[currentX, currentY] | |
] | |
], options.properties) | |
cellTriangle2 = helpers_1.polygon([ | |
[ | |
[currentX, currentY], | |
[currentX + cellWidth, currentY + cellHeight], | |
[currentX + cellWidth, currentY], | |
[currentX, currentY] | |
] | |
], options.properties) | |
} else if (yi % 2 === 1 && xi % 2 === 1) { | |
cellTriangle1 = helpers_1.polygon([ | |
[ | |
[currentX, currentY], | |
[currentX, currentY + cellHeight], | |
[currentX + cellWidth, currentY], | |
[currentX, currentY] | |
] | |
], options.properties) | |
cellTriangle2 = helpers_1.polygon([ | |
[ | |
[currentX, currentY + cellHeight], | |
[currentX + cellWidth, currentY + cellHeight], | |
[currentX + cellWidth, currentY], | |
[currentX, currentY + cellHeight] | |
] | |
], options.properties) | |
} | |
if (options.mask) { | |
// if (intersect_1["default"](options.mask, cellTriangle1)) | |
// // results.push(cellTriangle1) | |
// if (intersect_1["default"](options.mask, cellTriangle2)) | |
// results.push(cellTriangle2) | |
cellTriangle1 = intersect_1["default"](options.mask, cellTriangle1) | |
cellTriangle2 = intersect_1["default"](options.mask, cellTriangle2) | |
if (typeof(cellTriangle1) !== 'undefined' && typeof(cellTriangle2) !== 'undefined') { | |
yield [cellTriangle1, cellTriangle2] | |
} else if (typeof(cellTriangle1) !== 'undefined' && typeof(cellTriangle2) === 'undefined') { | |
yield [cellTriangle1] | |
} else if (typeof(cellTriangle1) === 'undefined' && typeof(cellTriangle2) !== 'undefined') { | |
yield [cellTriangle2] | |
} else if (typeof(cellTriangle1) === 'undefined' && typeof(cellTriangle2) === 'undefined') { | |
yield null | |
} | |
} else { | |
yield [cellTriangle1, cellTriangle2] | |
// results.push(cellTriangle1) | |
// results.push(cellTriangle2) | |
} | |
currentY += cellHeight | |
yi++ | |
} | |
xi++ | |
currentX += cellWidth | |
} | |
// yield helpers_1.featureCollection([cellTriangle1,cellTriangle2]) | |
// return helpers_1.featureCollection(results) | |
} | |
exports["default"] = triangleGridStream |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment