index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // index.js
  2. import { bbox } from "@turf/bbox";
  3. import { hexGrid } from "@turf/hex-grid";
  4. import { pointGrid } from "@turf/point-grid";
  5. import { distance } from "@turf/distance";
  6. import { centroid } from "@turf/centroid";
  7. import { squareGrid } from "@turf/square-grid";
  8. import { triangleGrid } from "@turf/triangle-grid";
  9. import { clone } from "@turf/clone";
  10. import { featureCollection, validateBBox } from "@turf/helpers";
  11. import { featureEach } from "@turf/meta";
  12. import { collectionOf } from "@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. collectionOf(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(points);
  29. validateBBox(box);
  30. var grid;
  31. switch (gridType) {
  32. case "point":
  33. case "points":
  34. grid = pointGrid(box, cellSize, options);
  35. break;
  36. case "square":
  37. case "squares":
  38. grid = squareGrid(box, cellSize, options);
  39. break;
  40. case "hex":
  41. case "hexes":
  42. grid = hexGrid(box, cellSize, options);
  43. break;
  44. case "triangle":
  45. case "triangles":
  46. grid = triangleGrid(box, cellSize, options);
  47. break;
  48. default:
  49. throw new Error("invalid gridType");
  50. }
  51. var results = [];
  52. featureEach(grid, function(gridFeature) {
  53. var zw = 0;
  54. var sw = 0;
  55. featureEach(points, function(point) {
  56. var gridPoint = gridType === "point" ? gridFeature : centroid(gridFeature);
  57. var d = distance(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(gridFeature);
  68. newFeature.properties[property] = zw / sw;
  69. results.push(newFeature);
  70. });
  71. return featureCollection(results);
  72. }
  73. var turf_interpolate_default = interpolate;
  74. export {
  75. turf_interpolate_default as default,
  76. interpolate
  77. };
  78. //# sourceMappingURL=index.js.map