index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use strict';
  2. var bbox = require('@turf/bbox');
  3. var hexGrid = require('@turf/hex-grid');
  4. var pointGrid = require('@turf/point-grid');
  5. var distance = require('@turf/distance');
  6. var centroid = require('@turf/centroid');
  7. var squareGrid = require('@turf/square-grid');
  8. var triangleGrid = require('@turf/triangle-grid');
  9. var clone = require('@turf/clone');
  10. var helpers = require('@turf/helpers');
  11. var meta = require('@turf/meta');
  12. var invariant = require('@turf/invariant');
  13. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  14. var bbox__default = /*#__PURE__*/_interopDefaultLegacy(bbox);
  15. var hexGrid__default = /*#__PURE__*/_interopDefaultLegacy(hexGrid);
  16. var pointGrid__default = /*#__PURE__*/_interopDefaultLegacy(pointGrid);
  17. var distance__default = /*#__PURE__*/_interopDefaultLegacy(distance);
  18. var centroid__default = /*#__PURE__*/_interopDefaultLegacy(centroid);
  19. var squareGrid__default = /*#__PURE__*/_interopDefaultLegacy(squareGrid);
  20. var triangleGrid__default = /*#__PURE__*/_interopDefaultLegacy(triangleGrid);
  21. var clone__default = /*#__PURE__*/_interopDefaultLegacy(clone);
  22. /**
  23. * Takes a set of points and estimates their 'property' values on a grid using the [Inverse Distance Weighting (IDW) method](https://en.wikipedia.org/wiki/Inverse_distance_weighting).
  24. *
  25. * @name interpolate
  26. * @param {FeatureCollection<Point>} points with known value
  27. * @param {number} cellSize the distance across each grid point
  28. * @param {Object} [options={}] Optional parameters
  29. * @param {string} [options.gridType='square'] defines the output format based on a Grid Type (options: 'square' | 'point' | 'hex' | 'triangle')
  30. * @param {string} [options.property='elevation'] the property name in `points` from which z-values will be pulled, zValue fallbacks to 3rd coordinate if no property exists.
  31. * @param {string} [options.units='kilometers'] used in calculating cellSize, can be degrees, radians, miles, or kilometers
  32. * @param {number} [options.weight=1] exponent regulating the distance-decay weighting
  33. * @returns {FeatureCollection<Point|Polygon>} grid of points or polygons with interpolated 'property'
  34. * @example
  35. * var points = turf.randomPoint(30, {bbox: [50, 30, 70, 50]});
  36. *
  37. * // add a random property to each point
  38. * turf.featureEach(points, function(point) {
  39. * point.properties.solRad = Math.random() * 50;
  40. * });
  41. * var options = {gridType: 'points', property: 'solRad', units: 'miles'};
  42. * var grid = turf.interpolate(points, 100, options);
  43. *
  44. * //addToMap
  45. * var addToMap = [grid];
  46. */
  47. function interpolate(points, cellSize, options) {
  48. // Optional parameters
  49. options = options || {};
  50. if (typeof options !== "object") throw new Error("options is invalid");
  51. var gridType = options.gridType;
  52. var property = options.property;
  53. var weight = options.weight;
  54. // validation
  55. if (!points) throw new Error("points is required");
  56. invariant.collectionOf(points, "Point", "input must contain Points");
  57. if (!cellSize) throw new Error("cellSize is required");
  58. if (weight !== undefined && typeof weight !== "number")
  59. throw new Error("weight must be a number");
  60. // default values
  61. property = property || "elevation";
  62. gridType = gridType || "square";
  63. weight = weight || 1;
  64. var box = bbox__default['default'](points);
  65. var grid;
  66. switch (gridType) {
  67. case "point":
  68. case "points":
  69. grid = pointGrid__default['default'](box, cellSize, options);
  70. break;
  71. case "square":
  72. case "squares":
  73. grid = squareGrid__default['default'](box, cellSize, options);
  74. break;
  75. case "hex":
  76. case "hexes":
  77. grid = hexGrid__default['default'](box, cellSize, options);
  78. break;
  79. case "triangle":
  80. case "triangles":
  81. grid = triangleGrid__default['default'](box, cellSize, options);
  82. break;
  83. default:
  84. throw new Error("invalid gridType");
  85. }
  86. var results = [];
  87. meta.featureEach(grid, function (gridFeature) {
  88. var zw = 0;
  89. var sw = 0;
  90. // calculate the distance from each input point to the grid points
  91. meta.featureEach(points, function (point) {
  92. var gridPoint =
  93. gridType === "point" ? gridFeature : centroid__default['default'](gridFeature);
  94. var d = distance__default['default'](gridPoint, point, options);
  95. var zValue;
  96. // property has priority for zValue, fallbacks to 3rd coordinate from geometry
  97. if (property !== undefined) zValue = point.properties[property];
  98. if (zValue === undefined) zValue = point.geometry.coordinates[2];
  99. if (zValue === undefined) throw new Error("zValue is missing");
  100. if (d === 0) zw = zValue;
  101. var w = 1.0 / Math.pow(d, weight);
  102. sw += w;
  103. zw += w * zValue;
  104. });
  105. // write interpolated value for each grid point
  106. var newFeature = clone__default['default'](gridFeature);
  107. newFeature.properties[property] = zw / sw;
  108. results.push(newFeature);
  109. });
  110. return helpers.featureCollection(results);
  111. }
  112. module.exports = interpolate;
  113. module.exports.default = interpolate;