index.cjs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.js
  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 interpolate(points, cellSize, options) {
  14. options = options || {};
  15. if (typeof options !== "object") throw new Error("options is invalid");
  16. var gridType = options.gridType;
  17. var property = options.property;
  18. var weight = options.weight;
  19. var box = options.bbox;
  20. if (!points) throw new Error("points is required");
  21. _invariant.collectionOf.call(void 0, points, "Point", "input must contain Points");
  22. if (!cellSize) throw new Error("cellSize is required");
  23. if (weight !== void 0 && typeof weight !== "number")
  24. throw new Error("weight must be a number");
  25. property = property || "elevation";
  26. gridType = gridType || "square";
  27. weight = weight || 1;
  28. box = box != null ? box : _bbox.bbox.call(void 0, points);
  29. _helpers.validateBBox.call(void 0, box);
  30. var grid;
  31. switch (gridType) {
  32. case "point":
  33. case "points":
  34. grid = _pointgrid.pointGrid.call(void 0, box, cellSize, options);
  35. break;
  36. case "square":
  37. case "squares":
  38. grid = _squaregrid.squareGrid.call(void 0, box, cellSize, options);
  39. break;
  40. case "hex":
  41. case "hexes":
  42. grid = _hexgrid.hexGrid.call(void 0, box, cellSize, options);
  43. break;
  44. case "triangle":
  45. case "triangles":
  46. grid = _trianglegrid.triangleGrid.call(void 0, box, cellSize, options);
  47. break;
  48. default:
  49. throw new Error("invalid gridType");
  50. }
  51. var results = [];
  52. _meta.featureEach.call(void 0, grid, function(gridFeature) {
  53. var zw = 0;
  54. var sw = 0;
  55. _meta.featureEach.call(void 0, points, function(point) {
  56. var gridPoint = gridType === "point" ? gridFeature : _centroid.centroid.call(void 0, gridFeature);
  57. var d = _distance.distance.call(void 0, gridPoint, point, options);
  58. var zValue;
  59. if (property !== void 0) zValue = point.properties[property];
  60. if (zValue === void 0) zValue = point.geometry.coordinates[2];
  61. if (zValue === void 0) throw new Error("zValue is missing");
  62. if (d === 0) zw = zValue;
  63. var w = 1 / Math.pow(d, weight);
  64. sw += w;
  65. zw += w * zValue;
  66. });
  67. var newFeature = _clone.clone.call(void 0, gridFeature);
  68. newFeature.properties[property] = zw / sw;
  69. results.push(newFeature);
  70. });
  71. return _helpers.featureCollection.call(void 0, results);
  72. }
  73. var turf_interpolate_default = interpolate;
  74. exports.default = turf_interpolate_default; exports.interpolate = interpolate;
  75. //# sourceMappingURL=index.cjs.map