@turf/isobands 7.0.0-alpha.2 → 7.0.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.
@@ -1 +0,0 @@
1
- {"type":"module"}
package/dist/js/index.js DELETED
@@ -1,232 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- const bbox_1 = tslib_1.__importDefault(require("@turf/bbox"));
5
- const area_1 = tslib_1.__importDefault(require("@turf/area"));
6
- const boolean_point_in_polygon_1 = tslib_1.__importDefault(require("@turf/boolean-point-in-polygon"));
7
- const explode_1 = tslib_1.__importDefault(require("@turf/explode"));
8
- const invariant_1 = require("@turf/invariant");
9
- const helpers_1 = require("@turf/helpers");
10
- const grid_to_matrix_1 = tslib_1.__importDefault(require("./lib/grid-to-matrix"));
11
- const marchingsquares_isobands_1 = tslib_1.__importDefault(require("./lib/marchingsquares-isobands"));
12
- /**
13
- * Takes a square or rectangular grid {@link FeatureCollection} of {@link Point} features with z-values and an array of
14
- * value breaks and generates filled contour isobands.
15
- *
16
- * @name isobands
17
- * @param {FeatureCollection<Point>} pointGrid input points - must be square or rectangular
18
- * @param {Array<number>} breaks where to draw contours
19
- * @param {Object} [options={}] options on output
20
- * @param {string} [options.zProperty='elevation'] the property name in `points` from which z-values will be pulled
21
- * @param {Object} [options.commonProperties={}] GeoJSON properties passed to ALL isobands
22
- * @param {Array<Object>} [options.breaksProperties=[]] GeoJSON properties passed, in order, to the correspondent isoband (order defined by breaks)
23
- * @returns {FeatureCollection<MultiPolygon>} a FeatureCollection of {@link MultiPolygon} features representing isobands
24
- */
25
- function isobands(pointGrid, breaks, options) {
26
- // Optional parameters
27
- options = options || {};
28
- if (!helpers_1.isObject(options))
29
- throw new Error("options is invalid");
30
- const zProperty = options.zProperty || "elevation";
31
- const commonProperties = options.commonProperties || {};
32
- const breaksProperties = options.breaksProperties || [];
33
- // Validation
34
- invariant_1.collectionOf(pointGrid, "Point", "Input must contain Points");
35
- if (!breaks)
36
- throw new Error("breaks is required");
37
- if (!Array.isArray(breaks))
38
- throw new Error("breaks is not an Array");
39
- if (!helpers_1.isObject(commonProperties))
40
- throw new Error("commonProperties is not an Object");
41
- if (!Array.isArray(breaksProperties))
42
- throw new Error("breaksProperties is not an Array");
43
- // Isoband methods
44
- const matrix = grid_to_matrix_1.default(pointGrid, { zProperty: zProperty, flip: true });
45
- let contours = createContourLines(matrix, breaks, zProperty);
46
- contours = rescaleContours(contours, matrix, pointGrid);
47
- const multipolygons = contours.map((contour, index) => {
48
- if (breaksProperties[index] && !helpers_1.isObject(breaksProperties[index])) {
49
- throw new Error("Each mappedProperty is required to be an Object");
50
- }
51
- // collect all properties
52
- const contourProperties = Object.assign(Object.assign({}, commonProperties), breaksProperties[index]);
53
- contourProperties[zProperty] = contour[zProperty];
54
- const multiP = helpers_1.multiPolygon(contour.groupedRings, contourProperties);
55
- return multiP;
56
- });
57
- return helpers_1.featureCollection(multipolygons);
58
- }
59
- /**
60
- * Creates the contours lines (featuresCollection of polygon features) from the 2D data grid
61
- *
62
- * Marchingsquares process the grid data as a 3D representation of a function on a 2D plane, therefore it
63
- * assumes the points (x-y coordinates) are one 'unit' distance. The result of the IsoBands function needs to be
64
- * rescaled, with turfjs, to the original area and proportions on the map
65
- *
66
- * @private
67
- * @param {Array<Array<number>>} matrix Grid Data
68
- * @param {Array<number>} breaks Breaks
69
- * @param {string} [property='elevation'] Property
70
- * @returns {Array<any>} contours
71
- */
72
- function createContourLines(matrix, breaks, property) {
73
- const contours = [];
74
- for (let i = 1; i < breaks.length; i++) {
75
- const lowerBand = +breaks[i - 1]; // make sure the breaks value is a number
76
- const upperBand = +breaks[i];
77
- const isobandsCoords = marchingsquares_isobands_1.default(matrix, lowerBand, upperBand - lowerBand);
78
- // as per GeoJson rules for creating a Polygon, make sure the first element
79
- // in the array of LinearRings represents the exterior ring (i.e. biggest area),
80
- // and any subsequent elements represent interior rings (i.e. smaller area);
81
- // this avoids rendering issues of the MultiPolygons on the map
82
- const nestedRings = orderByArea(isobandsCoords);
83
- const groupedRings = groupNestedRings(nestedRings);
84
- contours.push({
85
- groupedRings: groupedRings,
86
- [property]: lowerBand + "-" + upperBand,
87
- });
88
- }
89
- return contours;
90
- }
91
- /**
92
- * Transform isobands of 2D grid to polygons for the map
93
- *
94
- * @private
95
- * @param {Array<any>} contours Contours
96
- * @param {Array<Array<number>>} matrix Grid Data
97
- * @param {Object} points Points by Latitude
98
- * @returns {Array<any>} contours
99
- */
100
- function rescaleContours(contours, matrix, points) {
101
- // get dimensions (on the map) of the original grid
102
- const gridBbox = bbox_1.default(points); // [ minX, minY, maxX, maxY ]
103
- const originalWidth = gridBbox[2] - gridBbox[0];
104
- const originalHeigth = gridBbox[3] - gridBbox[1];
105
- // get origin, which is the first point of the last row on the rectangular data on the map
106
- const x0 = gridBbox[0];
107
- const y0 = gridBbox[1];
108
- // get number of cells per side
109
- const matrixWidth = matrix[0].length - 1;
110
- const matrixHeight = matrix.length - 1;
111
- // calculate the scaling factor between matrix and rectangular grid on the map
112
- const scaleX = originalWidth / matrixWidth;
113
- const scaleY = originalHeigth / matrixHeight;
114
- const resize = (point) => {
115
- point[0] = point[0] * scaleX + x0;
116
- point[1] = point[1] * scaleY + y0;
117
- };
118
- // resize and shift each point/line of the isobands
119
- contours.forEach(function (contour) {
120
- contour.groupedRings.forEach(function (lineRingSet) {
121
- lineRingSet.forEach(function (lineRing) {
122
- lineRing.forEach(resize);
123
- });
124
- });
125
- });
126
- return contours;
127
- }
128
- /* utility functions */
129
- /**
130
- * Returns an array of coordinates (of LinearRings) in descending order by area
131
- *
132
- * @private
133
- * @param {Array<LineString>} ringsCoords array of closed LineString
134
- * @returns {Array} array of the input LineString ordered by area
135
- */
136
- function orderByArea(ringsCoords) {
137
- const ringsWithArea = [];
138
- const areas = [];
139
- ringsCoords.forEach(function (coords) {
140
- // const poly = polygon([points]);
141
- const ringArea = area_1.default(helpers_1.polygon([coords]));
142
- // create an array of areas value
143
- areas.push(ringArea);
144
- // associate each lineRing with its area
145
- ringsWithArea.push({ ring: coords, area: ringArea });
146
- });
147
- areas.sort(function (a, b) {
148
- // bigger --> smaller
149
- return b - a;
150
- });
151
- // create a new array of linearRings coordinates ordered by their area
152
- const orderedByArea = [];
153
- areas.forEach(function (area) {
154
- for (let lr = 0; lr < ringsWithArea.length; lr++) {
155
- if (ringsWithArea[lr].area === area) {
156
- orderedByArea.push(ringsWithArea[lr].ring);
157
- ringsWithArea.splice(lr, 1);
158
- break;
159
- }
160
- }
161
- });
162
- return orderedByArea;
163
- }
164
- /**
165
- * Returns an array of arrays of coordinates, each representing
166
- * a set of (coordinates of) nested LinearRings,
167
- * i.e. the first ring contains all the others
168
- *
169
- * @private
170
- * @param {Array} orderedLinearRings array of coordinates (of LinearRings) in descending order by area
171
- * @returns {Array<Array>} Array of coordinates of nested LinearRings
172
- */
173
- function groupNestedRings(orderedLinearRings) {
174
- // create a list of the (coordinates of) LinearRings
175
- const lrList = orderedLinearRings.map((lr) => {
176
- return { lrCoordinates: lr, grouped: false };
177
- });
178
- const groupedLinearRingsCoords = [];
179
- while (!allGrouped(lrList)) {
180
- for (let i = 0; i < lrList.length; i++) {
181
- if (!lrList[i].grouped) {
182
- // create new group starting with the larger not already grouped ring
183
- const group = [];
184
- group.push(lrList[i].lrCoordinates);
185
- lrList[i].grouped = true;
186
- const outerMostPoly = helpers_1.polygon([lrList[i].lrCoordinates]);
187
- // group all the rings contained by the outermost ring
188
- for (let j = i + 1; j < lrList.length; j++) {
189
- if (!lrList[j].grouped) {
190
- const lrPoly = helpers_1.polygon([lrList[j].lrCoordinates]);
191
- if (isInside(lrPoly, outerMostPoly)) {
192
- group.push(lrList[j].lrCoordinates);
193
- lrList[j].grouped = true;
194
- }
195
- }
196
- }
197
- // insert the new group
198
- groupedLinearRingsCoords.push(group);
199
- }
200
- }
201
- }
202
- return groupedLinearRingsCoords;
203
- }
204
- /**
205
- * @private
206
- * @param {Polygon} testPolygon polygon of interest
207
- * @param {Polygon} targetPolygon polygon you want to compare with
208
- * @returns {boolean} true if test-Polygon is inside target-Polygon
209
- */
210
- function isInside(testPolygon, targetPolygon) {
211
- const points = explode_1.default(testPolygon);
212
- for (let i = 0; i < points.features.length; i++) {
213
- if (!boolean_point_in_polygon_1.default(points.features[i], targetPolygon)) {
214
- return false;
215
- }
216
- }
217
- return true;
218
- }
219
- /**
220
- * @private
221
- * @param {Array<Object>} list list of objects which might contain the 'group' attribute
222
- * @returns {boolean} true if all the objects in the list are marked as grouped
223
- */
224
- function allGrouped(list) {
225
- for (let i = 0; i < list.length; i++) {
226
- if (list[i].grouped === false) {
227
- return false;
228
- }
229
- }
230
- return true;
231
- }
232
- exports.default = isobands;
@@ -1,37 +0,0 @@
1
- /**
2
- * Takes a {@link Point} grid and returns a correspondent matrix {Array<Array<number>>}
3
- * of the 'property' values
4
- *
5
- * @name gridToMatrix
6
- * @param {FeatureCollection<Point>} grid of points
7
- * @param {Object} [options={}] Optional parameters
8
- * @param {string} [options.zProperty='elevation'] the property name in `points` from which z-values will be pulled
9
- * @param {boolean} [options.flip=false] returns the matrix upside-down
10
- * @param {boolean} [options.flags=false] flags, adding a `matrixPosition` array field ([row, column]) to its properties,
11
- * the grid points with coordinates on the matrix
12
- * @returns {Array<Array<number>>} matrix of property values
13
- * @example
14
- * var extent = [-70.823364, -33.553984, -70.473175, -33.302986];
15
- * var cellSize = 3;
16
- * var grid = turf.pointGrid(extent, cellSize);
17
- * // add a random property to each point between 0 and 60
18
- * for (var i = 0; i < grid.features.length; i++) {
19
- * grid.features[i].properties.elevation = (Math.random() * 60);
20
- * }
21
- * gridToMatrix(grid);
22
- * //= [
23
- * [ 1, 13, 10, 9, 10, 13, 18],
24
- * [34, 8, 5, 4, 5, 8, 13],
25
- * [10, 5, 2, 1, 2, 5, 4],
26
- * [ 0, 4, 56, 19, 1, 4, 9],
27
- * [10, 5, 2, 1, 2, 5, 10],
28
- * [57, 8, 5, 4, 5, 0, 57],
29
- * [ 3, 13, 10, 9, 5, 13, 18],
30
- * [18, 13, 10, 9, 78, 13, 18]
31
- * ]
32
- */
33
- export default function gridToMatrix(grid: any, options?: {
34
- zProperty?: string;
35
- flip?: boolean;
36
- flags?: boolean;
37
- } | undefined): number[][];
@@ -1,104 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const invariant_1 = require("@turf/invariant");
4
- const meta_1 = require("@turf/meta");
5
- const helpers_1 = require("@turf/helpers");
6
- /**
7
- * Takes a {@link Point} grid and returns a correspondent matrix {Array<Array<number>>}
8
- * of the 'property' values
9
- *
10
- * @name gridToMatrix
11
- * @param {FeatureCollection<Point>} grid of points
12
- * @param {Object} [options={}] Optional parameters
13
- * @param {string} [options.zProperty='elevation'] the property name in `points` from which z-values will be pulled
14
- * @param {boolean} [options.flip=false] returns the matrix upside-down
15
- * @param {boolean} [options.flags=false] flags, adding a `matrixPosition` array field ([row, column]) to its properties,
16
- * the grid points with coordinates on the matrix
17
- * @returns {Array<Array<number>>} matrix of property values
18
- * @example
19
- * var extent = [-70.823364, -33.553984, -70.473175, -33.302986];
20
- * var cellSize = 3;
21
- * var grid = turf.pointGrid(extent, cellSize);
22
- * // add a random property to each point between 0 and 60
23
- * for (var i = 0; i < grid.features.length; i++) {
24
- * grid.features[i].properties.elevation = (Math.random() * 60);
25
- * }
26
- * gridToMatrix(grid);
27
- * //= [
28
- * [ 1, 13, 10, 9, 10, 13, 18],
29
- * [34, 8, 5, 4, 5, 8, 13],
30
- * [10, 5, 2, 1, 2, 5, 4],
31
- * [ 0, 4, 56, 19, 1, 4, 9],
32
- * [10, 5, 2, 1, 2, 5, 10],
33
- * [57, 8, 5, 4, 5, 0, 57],
34
- * [ 3, 13, 10, 9, 5, 13, 18],
35
- * [18, 13, 10, 9, 78, 13, 18]
36
- * ]
37
- */
38
- function gridToMatrix(grid, options) {
39
- // Optional parameters
40
- options = options || {};
41
- if (!helpers_1.isObject(options))
42
- throw new Error("options is invalid");
43
- var zProperty = options.zProperty || "elevation";
44
- var flip = options.flip;
45
- var flags = options.flags;
46
- // validation
47
- invariant_1.collectionOf(grid, "Point", "input must contain Points");
48
- var pointsMatrix = sortPointsByLatLng(grid, flip);
49
- var matrix = [];
50
- // create property matrix from sorted points
51
- // looping order matters here
52
- for (var r = 0; r < pointsMatrix.length; r++) {
53
- var pointRow = pointsMatrix[r];
54
- var row = [];
55
- for (var c = 0; c < pointRow.length; c++) {
56
- var point = pointRow[c];
57
- // Check if zProperty exist
58
- if (point.properties[zProperty])
59
- row.push(point.properties[zProperty]);
60
- else
61
- row.push(0);
62
- // add flags
63
- if (flags === true)
64
- point.properties.matrixPosition = [r, c];
65
- }
66
- matrix.push(row);
67
- }
68
- return matrix;
69
- }
70
- exports.default = gridToMatrix;
71
- /**
72
- * Sorts points by latitude and longitude, creating a 2-dimensional array of points
73
- *
74
- * @private
75
- * @param {FeatureCollection<Point>} points GeoJSON Point features
76
- * @param {boolean} [flip=false] returns the matrix upside-down
77
- * @returns {Array<Array<Point>>} points ordered by latitude and longitude
78
- */
79
- function sortPointsByLatLng(points, flip) {
80
- var pointsByLatitude = {};
81
- // divide points by rows with the same latitude
82
- meta_1.featureEach(points, function (point) {
83
- var lat = invariant_1.getCoords(point)[1];
84
- if (!pointsByLatitude[lat])
85
- pointsByLatitude[lat] = [];
86
- pointsByLatitude[lat].push(point);
87
- });
88
- // sort points (with the same latitude) by longitude
89
- var orderedRowsByLatitude = Object.keys(pointsByLatitude).map(function (lat) {
90
- var row = pointsByLatitude[lat];
91
- var rowOrderedByLongitude = row.sort(function (a, b) {
92
- return invariant_1.getCoords(a)[0] - invariant_1.getCoords(b)[0];
93
- });
94
- return rowOrderedByLongitude;
95
- });
96
- // sort rows (of points with the same latitude) by latitude
97
- var pointMatrix = orderedRowsByLatitude.sort(function (a, b) {
98
- if (flip)
99
- return invariant_1.getCoords(a[0])[1] - invariant_1.getCoords(b[0])[1];
100
- else
101
- return invariant_1.getCoords(b[0])[1] - invariant_1.getCoords(a[0])[1];
102
- });
103
- return pointMatrix;
104
- }
@@ -1 +0,0 @@
1
- export default function isoBands(data: any, minV: any, bandwidth: any, options: any): any[];