index.js 20 KB

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