-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
75 lines (71 loc) · 2.99 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import {isObject, featureCollection} from '@turf/helpers';
import rhumbDestination from '@turf/rhumb-destination';
/**
* Takes a {@link Point} grid and returns a correspondent matrix {Array<Array<number>>}
* of the 'property' values
*
* @name matrixToGrid
* @param {Array<Array<number>>} matrix of numbers
* @param {Point|Array<number>} origin position of the first bottom-left (South-West) point of the grid
* @param {number} cellSize the distance across each cell
* @param {Object} [options={}] optional parameters
* @param {string} [options.zProperty='elevation'] the grid points property name associated with the matrix value
* @param {Object} [options.properties={}] GeoJSON properties passed to all the points
* @param {string} [options.units='kilometers'] used in calculating cellSize, can be miles, or kilometers
* @returns {FeatureCollection<Point>} grid of points
*
* @example
* var matrixToGrid = require('matrix-to-grid');
* var matrix = [
* [ 1, 13, 20, 9, 10, 13, 18],
* [34, 8, 0, 4, 5, 8, 13],
* [10, 5, 2, 1, 2, 5, 24],
* [ 0, 4, 56, 19, 0, 4, 9],
* [10, 5, 2, 12, 2, 5, 10],
* [57, 8, 5, 4, 5, 0, 57],
* [ 3, 13, 0, 9, 5, 13, 35],
* [18, 13, 10, 9, 78, 13, 18]
* ];
* var origin = [-70.823364, -33.553984]
* matrixToGrid(matrix, origin, 10);
* //= pointGrid
*/
export default function matrixToGrid(matrix, origin, cellSize, options) {
// Optional parameters
options = options || {};
if (!isObject(options)) throw new Error('options is invalid');
var zProperty = options.zProperty || 'elevation';
var properties = options.properties;
var units = options.units;
// validation
if (!matrix || !Array.isArray(matrix)) throw new Error('matrix is required');
if (!origin) throw new Error('origin is required');
// all matrix array have to be of the same size
var matrixCols = matrix[0].length;
var matrixRows = matrix.length;
for (var row = 1; row < matrixRows; row++) {
if (matrix[row].length !== matrixCols) throw new Error('matrix requires all rows of equal size');
}
var points = [];
for (var r = 0; r < matrixRows; r++) {
// create first point in the row
var first = rhumbDestination(origin, cellSize * r, 0, {units: units});
first.properties[zProperty] = matrix[matrixRows - 1 - r][0];
for (var prop in properties) {
first.properties[prop] = properties[prop];
}
points.push(first);
for (var c = 1; c < matrixCols; c++) {
// create the other points in the same row
var pt = rhumbDestination(first, cellSize * c, 90, {units: units});
for (var prop2 in properties) {
pt.properties[prop2] = properties[prop2];
}
// add matrix property
var val = matrix[matrixRows - 1 - r][c];
pt.properties[zProperty] = val;
points.push(pt);
}
}
return featureCollection(points);
}