@turf/isolines 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 +12 -11
- package/dist/es/index.js +61 -589
- package/dist/es/lib/grid-to-matrix.js +101 -0
- package/dist/es/lib/marchingsquares-isocontours.js +385 -0
- package/dist/js/index.d.ts +36 -0
- package/dist/js/index.js +65 -598
- 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-isocontours.d.ts +1 -0
- package/dist/js/lib/marchingsquares-isocontours.js +388 -0
- package/package.json +23 -19
- package/index.d.ts +0 -19
package/dist/js/index.js
CHANGED
|
@@ -1,522 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
var bbox__default = /*#__PURE__*/_interopDefaultLegacy(bbox);
|
|
12
|
-
var objectAssign__default = /*#__PURE__*/_interopDefaultLegacy(objectAssign);
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @license GNU Affero General Public License.
|
|
16
|
-
* Copyright (c) 2015, 2015 Ronny Lorenz <ronny@tbi.univie.ac.at>
|
|
17
|
-
* v. 1.2.0
|
|
18
|
-
* https://github.com/RaumZeit/MarchingSquares.js
|
|
19
|
-
*
|
|
20
|
-
* MarchingSquaresJS is free software: you can redistribute it and/or modify
|
|
21
|
-
* it under the terms of the GNU Affero General Public License as published by
|
|
22
|
-
* the Free Software Foundation, either version 3 of the License, or
|
|
23
|
-
* (at your option) any later version.
|
|
24
|
-
*
|
|
25
|
-
* MarchingSquaresJS is distributed in the hope that it will be useful,
|
|
26
|
-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
27
|
-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
28
|
-
* GNU Affero General Public License for more details.
|
|
29
|
-
*
|
|
30
|
-
* As additional permission under GNU Affero General Public License version 3
|
|
31
|
-
* section 7, third-party projects (personal or commercial) may distribute,
|
|
32
|
-
* include, or link against UNMODIFIED VERSIONS of MarchingSquaresJS without the
|
|
33
|
-
* requirement that said third-party project for that reason alone becomes
|
|
34
|
-
* subject to any requirement of the GNU Affero General Public License version 3.
|
|
35
|
-
* Any modifications to MarchingSquaresJS, however, must be shared with the public
|
|
36
|
-
* and made available.
|
|
37
|
-
*
|
|
38
|
-
* In summary this:
|
|
39
|
-
* - allows you to use MarchingSquaresJS at no cost
|
|
40
|
-
* - allows you to use MarchingSquaresJS for both personal and commercial purposes
|
|
41
|
-
* - allows you to distribute UNMODIFIED VERSIONS of MarchingSquaresJS under any
|
|
42
|
-
* license as long as this license notice is included
|
|
43
|
-
* - enables you to keep the source code of your program that uses MarchingSquaresJS
|
|
44
|
-
* undisclosed
|
|
45
|
-
* - forces you to share any modifications you have made to MarchingSquaresJS,
|
|
46
|
-
* e.g. bug-fixes
|
|
47
|
-
*
|
|
48
|
-
* You should have received a copy of the GNU Affero General Public License
|
|
49
|
-
* along with MarchingSquaresJS. If not, see <http://www.gnu.org/licenses/>.
|
|
50
|
-
*/
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Compute the isocontour(s) of a scalar 2D field given
|
|
54
|
-
* a certain threshold by applying the Marching Squares
|
|
55
|
-
* Algorithm. The function returns a list of path coordinates
|
|
56
|
-
*/
|
|
57
|
-
var defaultSettings = {
|
|
58
|
-
successCallback: null,
|
|
59
|
-
verbose: false,
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
var settings = {};
|
|
63
|
-
|
|
64
|
-
function isoContours(data, threshold, options) {
|
|
65
|
-
/* process options */
|
|
66
|
-
options = options ? options : {};
|
|
67
|
-
|
|
68
|
-
var optionKeys = Object.keys(defaultSettings);
|
|
69
|
-
|
|
70
|
-
for (var i = 0; i < optionKeys.length; i++) {
|
|
71
|
-
var key = optionKeys[i];
|
|
72
|
-
var val = options[key];
|
|
73
|
-
val =
|
|
74
|
-
typeof val !== "undefined" && val !== null ? val : defaultSettings[key];
|
|
75
|
-
|
|
76
|
-
settings[key] = val;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (settings.verbose)
|
|
80
|
-
console.log(
|
|
81
|
-
"MarchingSquaresJS-isoContours: computing isocontour for " + threshold
|
|
82
|
-
);
|
|
83
|
-
|
|
84
|
-
var ret = contourGrid2Paths(computeContourGrid(data, threshold));
|
|
85
|
-
|
|
86
|
-
if (typeof settings.successCallback === "function")
|
|
87
|
-
settings.successCallback(ret);
|
|
88
|
-
|
|
89
|
-
return ret;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/*
|
|
93
|
-
Thats all for the public interface, below follows the actual
|
|
94
|
-
implementation
|
|
95
|
-
*/
|
|
96
|
-
|
|
97
|
-
/*
|
|
98
|
-
################################
|
|
99
|
-
Isocontour implementation below
|
|
100
|
-
################################
|
|
101
|
-
*/
|
|
102
|
-
|
|
103
|
-
/* assume that x1 == 1 && x0 == 0 */
|
|
104
|
-
function interpolateX(y, y0, y1) {
|
|
105
|
-
return (y - y0) / (y1 - y0);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/* compute the isocontour 4-bit grid */
|
|
109
|
-
function computeContourGrid(data, threshold) {
|
|
110
|
-
var rows = data.length - 1;
|
|
111
|
-
var cols = data[0].length - 1;
|
|
112
|
-
var ContourGrid = { rows: rows, cols: cols, cells: [] };
|
|
113
|
-
|
|
114
|
-
for (var j = 0; j < rows; ++j) {
|
|
115
|
-
ContourGrid.cells[j] = [];
|
|
116
|
-
for (var i = 0; i < cols; ++i) {
|
|
117
|
-
/* compose the 4-bit corner representation */
|
|
118
|
-
var cval = 0;
|
|
119
|
-
|
|
120
|
-
var tl = data[j + 1][i];
|
|
121
|
-
var tr = data[j + 1][i + 1];
|
|
122
|
-
var br = data[j][i + 1];
|
|
123
|
-
var bl = data[j][i];
|
|
124
|
-
|
|
125
|
-
if (isNaN(tl) || isNaN(tr) || isNaN(br) || isNaN(bl)) {
|
|
126
|
-
continue;
|
|
127
|
-
}
|
|
128
|
-
cval |= tl >= threshold ? 8 : 0;
|
|
129
|
-
cval |= tr >= threshold ? 4 : 0;
|
|
130
|
-
cval |= br >= threshold ? 2 : 0;
|
|
131
|
-
cval |= bl >= threshold ? 1 : 0;
|
|
132
|
-
|
|
133
|
-
/* resolve ambiguity for cval == 5 || 10 via averaging */
|
|
134
|
-
var flipped = false;
|
|
135
|
-
if (cval === 5 || cval === 10) {
|
|
136
|
-
var average = (tl + tr + br + bl) / 4;
|
|
137
|
-
if (cval === 5 && average < threshold) {
|
|
138
|
-
cval = 10;
|
|
139
|
-
flipped = true;
|
|
140
|
-
} else if (cval === 10 && average < threshold) {
|
|
141
|
-
cval = 5;
|
|
142
|
-
flipped = true;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/* add cell to ContourGrid if it contains edges */
|
|
147
|
-
if (cval !== 0 && cval !== 15) {
|
|
148
|
-
var top, bottom, left, right;
|
|
149
|
-
top = bottom = left = right = 0.5;
|
|
150
|
-
/* interpolate edges of cell */
|
|
151
|
-
if (cval === 1) {
|
|
152
|
-
left = 1 - interpolateX(threshold, tl, bl);
|
|
153
|
-
bottom = 1 - interpolateX(threshold, br, bl);
|
|
154
|
-
} else if (cval === 2) {
|
|
155
|
-
bottom = interpolateX(threshold, bl, br);
|
|
156
|
-
right = 1 - interpolateX(threshold, tr, br);
|
|
157
|
-
} else if (cval === 3) {
|
|
158
|
-
left = 1 - interpolateX(threshold, tl, bl);
|
|
159
|
-
right = 1 - interpolateX(threshold, tr, br);
|
|
160
|
-
} else if (cval === 4) {
|
|
161
|
-
top = interpolateX(threshold, tl, tr);
|
|
162
|
-
right = interpolateX(threshold, br, tr);
|
|
163
|
-
} else if (cval === 5) {
|
|
164
|
-
top = interpolateX(threshold, tl, tr);
|
|
165
|
-
right = interpolateX(threshold, br, tr);
|
|
166
|
-
bottom = 1 - interpolateX(threshold, br, bl);
|
|
167
|
-
left = 1 - interpolateX(threshold, tl, bl);
|
|
168
|
-
} else if (cval === 6) {
|
|
169
|
-
bottom = interpolateX(threshold, bl, br);
|
|
170
|
-
top = interpolateX(threshold, tl, tr);
|
|
171
|
-
} else if (cval === 7) {
|
|
172
|
-
left = 1 - interpolateX(threshold, tl, bl);
|
|
173
|
-
top = interpolateX(threshold, tl, tr);
|
|
174
|
-
} else if (cval === 8) {
|
|
175
|
-
left = interpolateX(threshold, bl, tl);
|
|
176
|
-
top = 1 - interpolateX(threshold, tr, tl);
|
|
177
|
-
} else if (cval === 9) {
|
|
178
|
-
bottom = 1 - interpolateX(threshold, br, bl);
|
|
179
|
-
top = 1 - interpolateX(threshold, tr, tl);
|
|
180
|
-
} else if (cval === 10) {
|
|
181
|
-
top = 1 - interpolateX(threshold, tr, tl);
|
|
182
|
-
right = 1 - interpolateX(threshold, tr, br);
|
|
183
|
-
bottom = interpolateX(threshold, bl, br);
|
|
184
|
-
left = interpolateX(threshold, bl, tl);
|
|
185
|
-
} else if (cval === 11) {
|
|
186
|
-
top = 1 - interpolateX(threshold, tr, tl);
|
|
187
|
-
right = 1 - interpolateX(threshold, tr, br);
|
|
188
|
-
} else if (cval === 12) {
|
|
189
|
-
left = interpolateX(threshold, bl, tl);
|
|
190
|
-
right = interpolateX(threshold, br, tr);
|
|
191
|
-
} else if (cval === 13) {
|
|
192
|
-
bottom = 1 - interpolateX(threshold, br, bl);
|
|
193
|
-
right = interpolateX(threshold, br, tr);
|
|
194
|
-
} else if (cval === 14) {
|
|
195
|
-
left = interpolateX(threshold, bl, tl);
|
|
196
|
-
bottom = interpolateX(threshold, bl, br);
|
|
197
|
-
} else {
|
|
198
|
-
console.log(
|
|
199
|
-
"MarchingSquaresJS-isoContours: Illegal cval detected: " + cval
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
ContourGrid.cells[j][i] = {
|
|
203
|
-
cval: cval,
|
|
204
|
-
flipped: flipped,
|
|
205
|
-
top: top,
|
|
206
|
-
right: right,
|
|
207
|
-
bottom: bottom,
|
|
208
|
-
left: left,
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
return ContourGrid;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
function isSaddle(cell) {
|
|
218
|
-
return cell.cval === 5 || cell.cval === 10;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
function isTrivial(cell) {
|
|
222
|
-
return cell.cval === 0 || cell.cval === 15;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
function clearCell(cell) {
|
|
226
|
-
if (!isTrivial(cell) && cell.cval !== 5 && cell.cval !== 10) {
|
|
227
|
-
cell.cval = 15;
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
function getXY(cell, edge) {
|
|
232
|
-
if (edge === "top") {
|
|
233
|
-
return [cell.top, 1.0];
|
|
234
|
-
} else if (edge === "bottom") {
|
|
235
|
-
return [cell.bottom, 0.0];
|
|
236
|
-
} else if (edge === "right") {
|
|
237
|
-
return [1.0, cell.right];
|
|
238
|
-
} else if (edge === "left") {
|
|
239
|
-
return [0.0, cell.left];
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
function contourGrid2Paths(grid) {
|
|
244
|
-
var paths = [];
|
|
245
|
-
var path_idx = 0;
|
|
246
|
-
var epsilon = 1e-7;
|
|
247
|
-
|
|
248
|
-
grid.cells.forEach(function (g, j) {
|
|
249
|
-
g.forEach(function (gg, i) {
|
|
250
|
-
if (typeof gg !== "undefined" && !isSaddle(gg) && !isTrivial(gg)) {
|
|
251
|
-
var p = tracePath(grid.cells, j, i);
|
|
252
|
-
var merged = false;
|
|
253
|
-
/* we may try to merge paths at this point */
|
|
254
|
-
if (p.info === "mergeable") {
|
|
255
|
-
/*
|
|
256
|
-
search backwards through the path array to find an entry
|
|
257
|
-
that starts with where the current path ends...
|
|
258
|
-
*/
|
|
259
|
-
var x = p.path[p.path.length - 1][0],
|
|
260
|
-
y = p.path[p.path.length - 1][1];
|
|
261
|
-
|
|
262
|
-
for (var k = path_idx - 1; k >= 0; k--) {
|
|
263
|
-
if (
|
|
264
|
-
Math.abs(paths[k][0][0] - x) <= epsilon &&
|
|
265
|
-
Math.abs(paths[k][0][1] - y) <= epsilon
|
|
266
|
-
) {
|
|
267
|
-
for (var l = p.path.length - 2; l >= 0; --l) {
|
|
268
|
-
paths[k].unshift(p.path[l]);
|
|
269
|
-
}
|
|
270
|
-
merged = true;
|
|
271
|
-
break;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
if (!merged) paths[path_idx++] = p.path;
|
|
276
|
-
}
|
|
277
|
-
});
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
return paths;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/*
|
|
284
|
-
construct consecutive line segments from starting cell by
|
|
285
|
-
walking arround the enclosed area clock-wise
|
|
286
|
-
*/
|
|
287
|
-
function tracePath(grid, j, i) {
|
|
288
|
-
var maxj = grid.length;
|
|
289
|
-
var p = [];
|
|
290
|
-
var dxContour = [0, 0, 1, 1, 0, 0, 0, 0, -1, 0, 1, 1, -1, 0, -1, 0];
|
|
291
|
-
var dyContour = [0, -1, 0, 0, 1, 1, 1, 1, 0, -1, 0, 0, 0, -1, 0, 0];
|
|
292
|
-
var dx, dy;
|
|
293
|
-
var startEdge = [
|
|
294
|
-
"none",
|
|
295
|
-
"left",
|
|
296
|
-
"bottom",
|
|
297
|
-
"left",
|
|
298
|
-
"right",
|
|
299
|
-
"none",
|
|
300
|
-
"bottom",
|
|
301
|
-
"left",
|
|
302
|
-
"top",
|
|
303
|
-
"top",
|
|
304
|
-
"none",
|
|
305
|
-
"top",
|
|
306
|
-
"right",
|
|
307
|
-
"right",
|
|
308
|
-
"bottom",
|
|
309
|
-
"none",
|
|
310
|
-
];
|
|
311
|
-
var nextEdge = [
|
|
312
|
-
"none",
|
|
313
|
-
"bottom",
|
|
314
|
-
"right",
|
|
315
|
-
"right",
|
|
316
|
-
"top",
|
|
317
|
-
"top",
|
|
318
|
-
"top",
|
|
319
|
-
"top",
|
|
320
|
-
"left",
|
|
321
|
-
"bottom",
|
|
322
|
-
"right",
|
|
323
|
-
"right",
|
|
324
|
-
"left",
|
|
325
|
-
"bottom",
|
|
326
|
-
"left",
|
|
327
|
-
"none",
|
|
328
|
-
];
|
|
329
|
-
var edge;
|
|
330
|
-
|
|
331
|
-
var currentCell = grid[j][i];
|
|
332
|
-
|
|
333
|
-
var cval = currentCell.cval;
|
|
334
|
-
var edge = startEdge[cval];
|
|
335
|
-
|
|
336
|
-
var pt = getXY(currentCell, edge);
|
|
337
|
-
|
|
338
|
-
/* push initial segment */
|
|
339
|
-
p.push([i + pt[0], j + pt[1]]);
|
|
340
|
-
edge = nextEdge[cval];
|
|
341
|
-
pt = getXY(currentCell, edge);
|
|
342
|
-
p.push([i + pt[0], j + pt[1]]);
|
|
343
|
-
clearCell(currentCell);
|
|
344
|
-
|
|
345
|
-
/* now walk arround the enclosed area in clockwise-direction */
|
|
346
|
-
var k = i + dxContour[cval];
|
|
347
|
-
var l = j + dyContour[cval];
|
|
348
|
-
var prev_cval = cval;
|
|
349
|
-
|
|
350
|
-
while (k >= 0 && l >= 0 && l < maxj && (k != i || l != j)) {
|
|
351
|
-
currentCell = grid[l][k];
|
|
352
|
-
if (typeof currentCell === "undefined") {
|
|
353
|
-
/* path ends here */
|
|
354
|
-
//console.log(k + " " + l + " is undefined, stopping path!");
|
|
355
|
-
break;
|
|
356
|
-
}
|
|
357
|
-
cval = currentCell.cval;
|
|
358
|
-
if (cval === 0 || cval === 15) {
|
|
359
|
-
return { path: p, info: "mergeable" };
|
|
360
|
-
}
|
|
361
|
-
edge = nextEdge[cval];
|
|
362
|
-
dx = dxContour[cval];
|
|
363
|
-
dy = dyContour[cval];
|
|
364
|
-
if (cval === 5 || cval === 10) {
|
|
365
|
-
/* select upper or lower band, depending on previous cells cval */
|
|
366
|
-
if (cval === 5) {
|
|
367
|
-
if (currentCell.flipped) {
|
|
368
|
-
/* this is actually a flipped case 10 */
|
|
369
|
-
if (dyContour[prev_cval] === -1) {
|
|
370
|
-
edge = "left";
|
|
371
|
-
dx = -1;
|
|
372
|
-
dy = 0;
|
|
373
|
-
} else {
|
|
374
|
-
edge = "right";
|
|
375
|
-
dx = 1;
|
|
376
|
-
dy = 0;
|
|
377
|
-
}
|
|
378
|
-
} else {
|
|
379
|
-
/* real case 5 */
|
|
380
|
-
if (dxContour[prev_cval] === -1) {
|
|
381
|
-
edge = "bottom";
|
|
382
|
-
dx = 0;
|
|
383
|
-
dy = -1;
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
} else if (cval === 10) {
|
|
387
|
-
if (currentCell.flipped) {
|
|
388
|
-
/* this is actually a flipped case 5 */
|
|
389
|
-
if (dxContour[prev_cval] === -1) {
|
|
390
|
-
edge = "top";
|
|
391
|
-
dx = 0;
|
|
392
|
-
dy = 1;
|
|
393
|
-
} else {
|
|
394
|
-
edge = "bottom";
|
|
395
|
-
dx = 0;
|
|
396
|
-
dy = -1;
|
|
397
|
-
}
|
|
398
|
-
} else {
|
|
399
|
-
/* real case 10 */
|
|
400
|
-
if (dyContour[prev_cval] === 1) {
|
|
401
|
-
edge = "left";
|
|
402
|
-
dx = -1;
|
|
403
|
-
dy = 0;
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
pt = getXY(currentCell, edge);
|
|
409
|
-
p.push([k + pt[0], l + pt[1]]);
|
|
410
|
-
clearCell(currentCell);
|
|
411
|
-
k += dx;
|
|
412
|
-
l += dy;
|
|
413
|
-
prev_cval = cval;
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
return { path: p, info: "closed" };
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
/**
|
|
420
|
-
* Takes a {@link Point} grid and returns a correspondent matrix {Array<Array<number>>}
|
|
421
|
-
* of the 'property' values
|
|
422
|
-
*
|
|
423
|
-
* @name gridToMatrix
|
|
424
|
-
* @param {FeatureCollection<Point>} grid of points
|
|
425
|
-
* @param {Object} [options={}] Optional parameters
|
|
426
|
-
* @param {string} [options.zProperty='elevation'] the property name in `points` from which z-values will be pulled
|
|
427
|
-
* @param {boolean} [options.flip=false] returns the matrix upside-down
|
|
428
|
-
* @param {boolean} [options.flags=false] flags, adding a `matrixPosition` array field ([row, column]) to its properties,
|
|
429
|
-
* the grid points with coordinates on the matrix
|
|
430
|
-
* @returns {Array<Array<number>>} matrix of property values
|
|
431
|
-
* @example
|
|
432
|
-
* var extent = [-70.823364, -33.553984, -70.473175, -33.302986];
|
|
433
|
-
* var cellSize = 3;
|
|
434
|
-
* var grid = turf.pointGrid(extent, cellSize);
|
|
435
|
-
* // add a random property to each point between 0 and 60
|
|
436
|
-
* for (var i = 0; i < grid.features.length; i++) {
|
|
437
|
-
* grid.features[i].properties.elevation = (Math.random() * 60);
|
|
438
|
-
* }
|
|
439
|
-
* gridToMatrix(grid);
|
|
440
|
-
* //= [
|
|
441
|
-
* [ 1, 13, 10, 9, 10, 13, 18],
|
|
442
|
-
* [34, 8, 5, 4, 5, 8, 13],
|
|
443
|
-
* [10, 5, 2, 1, 2, 5, 4],
|
|
444
|
-
* [ 0, 4, 56, 19, 1, 4, 9],
|
|
445
|
-
* [10, 5, 2, 1, 2, 5, 10],
|
|
446
|
-
* [57, 8, 5, 4, 5, 0, 57],
|
|
447
|
-
* [ 3, 13, 10, 9, 5, 13, 18],
|
|
448
|
-
* [18, 13, 10, 9, 78, 13, 18]
|
|
449
|
-
* ]
|
|
450
|
-
*/
|
|
451
|
-
function gridToMatrix(grid, options) {
|
|
452
|
-
// Optional parameters
|
|
453
|
-
options = options || {};
|
|
454
|
-
if (!helpers.isObject(options)) throw new Error("options is invalid");
|
|
455
|
-
var zProperty = options.zProperty || "elevation";
|
|
456
|
-
var flip = options.flip;
|
|
457
|
-
var flags = options.flags;
|
|
458
|
-
|
|
459
|
-
// validation
|
|
460
|
-
invariant.collectionOf(grid, "Point", "input must contain Points");
|
|
461
|
-
|
|
462
|
-
var pointsMatrix = sortPointsByLatLng(grid, flip);
|
|
463
|
-
|
|
464
|
-
var matrix = [];
|
|
465
|
-
// create property matrix from sorted points
|
|
466
|
-
// looping order matters here
|
|
467
|
-
for (var r = 0; r < pointsMatrix.length; r++) {
|
|
468
|
-
var pointRow = pointsMatrix[r];
|
|
469
|
-
var row = [];
|
|
470
|
-
for (var c = 0; c < pointRow.length; c++) {
|
|
471
|
-
var point = pointRow[c];
|
|
472
|
-
// Check if zProperty exist
|
|
473
|
-
if (point.properties[zProperty]) row.push(point.properties[zProperty]);
|
|
474
|
-
else row.push(0);
|
|
475
|
-
// add flags
|
|
476
|
-
if (flags === true) point.properties.matrixPosition = [r, c];
|
|
477
|
-
}
|
|
478
|
-
matrix.push(row);
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
return matrix;
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
/**
|
|
485
|
-
* Sorts points by latitude and longitude, creating a 2-dimensional array of points
|
|
486
|
-
*
|
|
487
|
-
* @private
|
|
488
|
-
* @param {FeatureCollection<Point>} points GeoJSON Point features
|
|
489
|
-
* @param {boolean} [flip=false] returns the matrix upside-down
|
|
490
|
-
* @returns {Array<Array<Point>>} points ordered by latitude and longitude
|
|
491
|
-
*/
|
|
492
|
-
function sortPointsByLatLng(points, flip) {
|
|
493
|
-
var pointsByLatitude = {};
|
|
494
|
-
|
|
495
|
-
// divide points by rows with the same latitude
|
|
496
|
-
meta.featureEach(points, function (point) {
|
|
497
|
-
var lat = invariant.getCoords(point)[1];
|
|
498
|
-
if (!pointsByLatitude[lat]) pointsByLatitude[lat] = [];
|
|
499
|
-
pointsByLatitude[lat].push(point);
|
|
500
|
-
});
|
|
501
|
-
|
|
502
|
-
// sort points (with the same latitude) by longitude
|
|
503
|
-
var orderedRowsByLatitude = Object.keys(pointsByLatitude).map(function (lat) {
|
|
504
|
-
var row = pointsByLatitude[lat];
|
|
505
|
-
var rowOrderedByLongitude = row.sort(function (a, b) {
|
|
506
|
-
return invariant.getCoords(a)[0] - invariant.getCoords(b)[0];
|
|
507
|
-
});
|
|
508
|
-
return rowOrderedByLongitude;
|
|
509
|
-
});
|
|
510
|
-
|
|
511
|
-
// sort rows (of points with the same latitude) by latitude
|
|
512
|
-
var pointMatrix = orderedRowsByLatitude.sort(function (a, b) {
|
|
513
|
-
if (flip) return invariant.getCoords(a[0])[1] - invariant.getCoords(b[0])[1];
|
|
514
|
-
else return invariant.getCoords(b[0])[1] - invariant.getCoords(a[0])[1];
|
|
515
|
-
});
|
|
516
|
-
|
|
517
|
-
return pointMatrix;
|
|
518
|
-
}
|
|
519
|
-
|
|
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 meta_1 = require("@turf/meta");
|
|
6
|
+
const invariant_1 = require("@turf/invariant");
|
|
7
|
+
const helpers_1 = require("@turf/helpers");
|
|
8
|
+
const marchingsquares_isocontours_1 = tslib_1.__importDefault(require("./lib/marchingsquares-isocontours"));
|
|
9
|
+
const grid_to_matrix_1 = tslib_1.__importDefault(require("./lib/grid-to-matrix"));
|
|
520
10
|
/**
|
|
521
11
|
* Takes a grid {@link FeatureCollection} of {@link Point} features with z-values and an array of
|
|
522
12
|
* value breaks and generates [isolines](https://en.wikipedia.org/wiki/Contour_line).
|
|
@@ -547,36 +37,29 @@ function sortPointsByLatLng(points, flip) {
|
|
|
547
37
|
* var addToMap = [lines];
|
|
548
38
|
*/
|
|
549
39
|
function isolines(pointGrid, breaks, options) {
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
matrix,
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
commonProperties,
|
|
573
|
-
breaksProperties
|
|
574
|
-
);
|
|
575
|
-
var scaledIsolines = rescaleIsolines(createdIsoLines, matrix, pointGrid);
|
|
576
|
-
|
|
577
|
-
return helpers.featureCollection(scaledIsolines);
|
|
40
|
+
// Optional parameters
|
|
41
|
+
options = options || {};
|
|
42
|
+
if (!helpers_1.isObject(options))
|
|
43
|
+
throw new Error("options is invalid");
|
|
44
|
+
const zProperty = options.zProperty || "elevation";
|
|
45
|
+
const commonProperties = options.commonProperties || {};
|
|
46
|
+
const breaksProperties = options.breaksProperties || [];
|
|
47
|
+
// Input validation
|
|
48
|
+
invariant_1.collectionOf(pointGrid, "Point", "Input must contain Points");
|
|
49
|
+
if (!breaks)
|
|
50
|
+
throw new Error("breaks is required");
|
|
51
|
+
if (!Array.isArray(breaks))
|
|
52
|
+
throw new Error("breaks must be an Array");
|
|
53
|
+
if (!helpers_1.isObject(commonProperties))
|
|
54
|
+
throw new Error("commonProperties must be an Object");
|
|
55
|
+
if (!Array.isArray(breaksProperties))
|
|
56
|
+
throw new Error("breaksProperties must be an Array");
|
|
57
|
+
// Isoline methods
|
|
58
|
+
const matrix = grid_to_matrix_1.default(pointGrid, { zProperty: zProperty, flip: true });
|
|
59
|
+
const createdIsoLines = createIsoLines(matrix, breaks, zProperty, commonProperties, breaksProperties);
|
|
60
|
+
const scaledIsolines = rescaleIsolines(createdIsoLines, matrix, pointGrid);
|
|
61
|
+
return helpers_1.featureCollection(scaledIsolines);
|
|
578
62
|
}
|
|
579
|
-
|
|
580
63
|
/**
|
|
581
64
|
* Creates the isolines lines (featuresCollection of MultiLineString features) from the 2D data grid
|
|
582
65
|
*
|
|
@@ -586,32 +69,23 @@ function isolines(pointGrid, breaks, options) {
|
|
|
586
69
|
*
|
|
587
70
|
* @private
|
|
588
71
|
* @param {Array<Array<number>>} matrix Grid Data
|
|
589
|
-
* @param {Array<number>} breaks
|
|
72
|
+
* @param {Array<number>} breaks BreakProps
|
|
590
73
|
* @param {string} zProperty name of the z-values property
|
|
591
74
|
* @param {Object} [commonProperties={}] GeoJSON properties passed to ALL isolines
|
|
592
75
|
* @param {Object} [breaksProperties=[]] GeoJSON properties passed to the correspondent isoline
|
|
593
76
|
* @returns {Array<MultiLineString>} isolines
|
|
594
77
|
*/
|
|
595
|
-
function createIsoLines(
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
)
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
var properties = objectAssign__default['default']({}, commonProperties, breaksProperties[i]);
|
|
607
|
-
properties[zProperty] = threshold;
|
|
608
|
-
var isoline = helpers.multiLineString(isoContours(matrix, threshold), properties);
|
|
609
|
-
|
|
610
|
-
results.push(isoline);
|
|
611
|
-
}
|
|
612
|
-
return results;
|
|
78
|
+
function createIsoLines(matrix, breaks, zProperty, commonProperties, breaksProperties) {
|
|
79
|
+
const results = [];
|
|
80
|
+
for (let i = 1; i < breaks.length; i++) {
|
|
81
|
+
const threshold = +breaks[i]; // make sure it's a number
|
|
82
|
+
const properties = Object.assign(Object.assign({}, commonProperties), breaksProperties[i]);
|
|
83
|
+
properties[zProperty] = threshold;
|
|
84
|
+
const isoline = helpers_1.multiLineString(marchingsquares_isocontours_1.default(matrix, threshold), properties);
|
|
85
|
+
results.push(isoline);
|
|
86
|
+
}
|
|
87
|
+
return results;
|
|
613
88
|
}
|
|
614
|
-
|
|
615
89
|
/**
|
|
616
90
|
* Translates and scales isolines
|
|
617
91
|
*
|
|
@@ -622,34 +96,27 @@ function createIsoLines(
|
|
|
622
96
|
* @returns {Array<MultiLineString>} isolines
|
|
623
97
|
*/
|
|
624
98
|
function rescaleIsolines(createdIsoLines, matrix, points) {
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
// resize and shift each point/line of the createdIsoLines
|
|
648
|
-
createdIsoLines.forEach(function (isoline) {
|
|
649
|
-
meta.coordEach(isoline, resize);
|
|
650
|
-
});
|
|
651
|
-
return createdIsoLines;
|
|
99
|
+
// get dimensions (on the map) of the original grid
|
|
100
|
+
const gridBbox = bbox_1.default(points); // [ minX, minY, maxX, maxY ]
|
|
101
|
+
const originalWidth = gridBbox[2] - gridBbox[0];
|
|
102
|
+
const originalHeigth = gridBbox[3] - gridBbox[1];
|
|
103
|
+
// get origin, which is the first point of the last row on the rectangular data on the map
|
|
104
|
+
const x0 = gridBbox[0];
|
|
105
|
+
const y0 = gridBbox[1];
|
|
106
|
+
// get number of cells per side
|
|
107
|
+
const matrixWidth = matrix[0].length - 1;
|
|
108
|
+
const matrixHeight = matrix.length - 1;
|
|
109
|
+
// calculate the scaling factor between matrix and rectangular grid on the map
|
|
110
|
+
const scaleX = originalWidth / matrixWidth;
|
|
111
|
+
const scaleY = originalHeigth / matrixHeight;
|
|
112
|
+
const resize = (point) => {
|
|
113
|
+
point[0] = point[0] * scaleX + x0;
|
|
114
|
+
point[1] = point[1] * scaleY + y0;
|
|
115
|
+
};
|
|
116
|
+
// resize and shift each point/line of the createdIsoLines
|
|
117
|
+
createdIsoLines.forEach((isoline) => {
|
|
118
|
+
meta_1.coordEach(isoline, resize);
|
|
119
|
+
});
|
|
120
|
+
return createdIsoLines;
|
|
652
121
|
}
|
|
653
|
-
|
|
654
|
-
module.exports = isolines;
|
|
655
|
-
module.exports.default = isolines;
|
|
122
|
+
exports.default = isolines;
|