@turf/isobands 6.5.0 → 7.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -8
- package/dist/es/index.js +145 -3709
- package/dist/es/lib/grid-to-matrix.js +101 -0
- package/dist/es/lib/marchingsquares-isobands.js +3453 -0
- package/dist/js/index.d.ts +20 -0
- package/dist/js/index.js +149 -3721
- package/dist/js/lib/grid-to-matrix.d.ts +37 -0
- package/dist/js/lib/grid-to-matrix.js +104 -0
- package/dist/js/lib/marchingsquares-isobands.d.ts +1 -0
- package/dist/js/lib/marchingsquares-isobands.js +3456 -0
- package/package.json +25 -21
- package/index.d.ts +0 -19
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { getCoords, collectionOf } from "@turf/invariant";
|
|
2
|
+
import { featureEach } from "@turf/meta";
|
|
3
|
+
import { isObject } from "@turf/helpers";
|
|
4
|
+
/**
|
|
5
|
+
* Takes a {@link Point} grid and returns a correspondent matrix {Array<Array<number>>}
|
|
6
|
+
* of the 'property' values
|
|
7
|
+
*
|
|
8
|
+
* @name gridToMatrix
|
|
9
|
+
* @param {FeatureCollection<Point>} grid of points
|
|
10
|
+
* @param {Object} [options={}] Optional parameters
|
|
11
|
+
* @param {string} [options.zProperty='elevation'] the property name in `points` from which z-values will be pulled
|
|
12
|
+
* @param {boolean} [options.flip=false] returns the matrix upside-down
|
|
13
|
+
* @param {boolean} [options.flags=false] flags, adding a `matrixPosition` array field ([row, column]) to its properties,
|
|
14
|
+
* the grid points with coordinates on the matrix
|
|
15
|
+
* @returns {Array<Array<number>>} matrix of property values
|
|
16
|
+
* @example
|
|
17
|
+
* var extent = [-70.823364, -33.553984, -70.473175, -33.302986];
|
|
18
|
+
* var cellSize = 3;
|
|
19
|
+
* var grid = turf.pointGrid(extent, cellSize);
|
|
20
|
+
* // add a random property to each point between 0 and 60
|
|
21
|
+
* for (var i = 0; i < grid.features.length; i++) {
|
|
22
|
+
* grid.features[i].properties.elevation = (Math.random() * 60);
|
|
23
|
+
* }
|
|
24
|
+
* gridToMatrix(grid);
|
|
25
|
+
* //= [
|
|
26
|
+
* [ 1, 13, 10, 9, 10, 13, 18],
|
|
27
|
+
* [34, 8, 5, 4, 5, 8, 13],
|
|
28
|
+
* [10, 5, 2, 1, 2, 5, 4],
|
|
29
|
+
* [ 0, 4, 56, 19, 1, 4, 9],
|
|
30
|
+
* [10, 5, 2, 1, 2, 5, 10],
|
|
31
|
+
* [57, 8, 5, 4, 5, 0, 57],
|
|
32
|
+
* [ 3, 13, 10, 9, 5, 13, 18],
|
|
33
|
+
* [18, 13, 10, 9, 78, 13, 18]
|
|
34
|
+
* ]
|
|
35
|
+
*/
|
|
36
|
+
export default function gridToMatrix(grid, options) {
|
|
37
|
+
// Optional parameters
|
|
38
|
+
options = options || {};
|
|
39
|
+
if (!isObject(options))
|
|
40
|
+
throw new Error("options is invalid");
|
|
41
|
+
var zProperty = options.zProperty || "elevation";
|
|
42
|
+
var flip = options.flip;
|
|
43
|
+
var flags = options.flags;
|
|
44
|
+
// validation
|
|
45
|
+
collectionOf(grid, "Point", "input must contain Points");
|
|
46
|
+
var pointsMatrix = sortPointsByLatLng(grid, flip);
|
|
47
|
+
var matrix = [];
|
|
48
|
+
// create property matrix from sorted points
|
|
49
|
+
// looping order matters here
|
|
50
|
+
for (var r = 0; r < pointsMatrix.length; r++) {
|
|
51
|
+
var pointRow = pointsMatrix[r];
|
|
52
|
+
var row = [];
|
|
53
|
+
for (var c = 0; c < pointRow.length; c++) {
|
|
54
|
+
var point = pointRow[c];
|
|
55
|
+
// Check if zProperty exist
|
|
56
|
+
if (point.properties[zProperty])
|
|
57
|
+
row.push(point.properties[zProperty]);
|
|
58
|
+
else
|
|
59
|
+
row.push(0);
|
|
60
|
+
// add flags
|
|
61
|
+
if (flags === true)
|
|
62
|
+
point.properties.matrixPosition = [r, c];
|
|
63
|
+
}
|
|
64
|
+
matrix.push(row);
|
|
65
|
+
}
|
|
66
|
+
return matrix;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Sorts points by latitude and longitude, creating a 2-dimensional array of points
|
|
70
|
+
*
|
|
71
|
+
* @private
|
|
72
|
+
* @param {FeatureCollection<Point>} points GeoJSON Point features
|
|
73
|
+
* @param {boolean} [flip=false] returns the matrix upside-down
|
|
74
|
+
* @returns {Array<Array<Point>>} points ordered by latitude and longitude
|
|
75
|
+
*/
|
|
76
|
+
function sortPointsByLatLng(points, flip) {
|
|
77
|
+
var pointsByLatitude = {};
|
|
78
|
+
// divide points by rows with the same latitude
|
|
79
|
+
featureEach(points, function (point) {
|
|
80
|
+
var lat = getCoords(point)[1];
|
|
81
|
+
if (!pointsByLatitude[lat])
|
|
82
|
+
pointsByLatitude[lat] = [];
|
|
83
|
+
pointsByLatitude[lat].push(point);
|
|
84
|
+
});
|
|
85
|
+
// sort points (with the same latitude) by longitude
|
|
86
|
+
var orderedRowsByLatitude = Object.keys(pointsByLatitude).map(function (lat) {
|
|
87
|
+
var row = pointsByLatitude[lat];
|
|
88
|
+
var rowOrderedByLongitude = row.sort(function (a, b) {
|
|
89
|
+
return getCoords(a)[0] - getCoords(b)[0];
|
|
90
|
+
});
|
|
91
|
+
return rowOrderedByLongitude;
|
|
92
|
+
});
|
|
93
|
+
// sort rows (of points with the same latitude) by latitude
|
|
94
|
+
var pointMatrix = orderedRowsByLatitude.sort(function (a, b) {
|
|
95
|
+
if (flip)
|
|
96
|
+
return getCoords(a[0])[1] - getCoords(b[0])[1];
|
|
97
|
+
else
|
|
98
|
+
return getCoords(b[0])[1] - getCoords(a[0])[1];
|
|
99
|
+
});
|
|
100
|
+
return pointMatrix;
|
|
101
|
+
}
|