index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // index.js
  2. import { center } from "@turf/center";
  3. import jsts from "@turf/jsts";
  4. import { geomEach, featureEach } from "@turf/meta";
  5. import { geoAzimuthalEquidistant } from "d3-geo";
  6. import {
  7. feature,
  8. featureCollection,
  9. radiansToLength,
  10. lengthToRadians,
  11. earthRadius
  12. } from "@turf/helpers";
  13. var { BufferOp, GeoJSONReader, GeoJSONWriter } = jsts;
  14. function buffer(geojson, radius, options) {
  15. options = options || {};
  16. var units = options.units || "kilometers";
  17. var steps = options.steps || 8;
  18. if (!geojson) throw new Error("geojson is required");
  19. if (typeof options !== "object") throw new Error("options must be an object");
  20. if (typeof steps !== "number") throw new Error("steps must be an number");
  21. if (radius === void 0) throw new Error("radius is required");
  22. if (steps <= 0) throw new Error("steps must be greater than 0");
  23. var results = [];
  24. switch (geojson.type) {
  25. case "GeometryCollection":
  26. geomEach(geojson, function(geometry) {
  27. var buffered = bufferFeature(geometry, radius, units, steps);
  28. if (buffered) results.push(buffered);
  29. });
  30. return featureCollection(results);
  31. case "FeatureCollection":
  32. featureEach(geojson, function(feature2) {
  33. var multiBuffered = bufferFeature(feature2, radius, units, steps);
  34. if (multiBuffered) {
  35. featureEach(multiBuffered, function(buffered) {
  36. if (buffered) results.push(buffered);
  37. });
  38. }
  39. });
  40. return featureCollection(results);
  41. }
  42. return bufferFeature(geojson, radius, units, steps);
  43. }
  44. function bufferFeature(geojson, radius, units, steps) {
  45. var properties = geojson.properties || {};
  46. var geometry = geojson.type === "Feature" ? geojson.geometry : geojson;
  47. if (geometry.type === "GeometryCollection") {
  48. var results = [];
  49. geomEach(geojson, function(geometry2) {
  50. var buffered2 = bufferFeature(geometry2, radius, units, steps);
  51. if (buffered2) results.push(buffered2);
  52. });
  53. return featureCollection(results);
  54. }
  55. var projection = defineProjection(geometry);
  56. var projected = {
  57. type: geometry.type,
  58. coordinates: projectCoords(geometry.coordinates, projection)
  59. };
  60. var reader = new GeoJSONReader();
  61. var geom = reader.read(projected);
  62. var distance = radiansToLength(lengthToRadians(radius, units), "meters");
  63. var buffered = BufferOp.bufferOp(geom, distance, steps);
  64. var writer = new GeoJSONWriter();
  65. buffered = writer.write(buffered);
  66. if (coordsIsNaN(buffered.coordinates)) return void 0;
  67. var result = {
  68. type: buffered.type,
  69. coordinates: unprojectCoords(buffered.coordinates, projection)
  70. };
  71. return feature(result, properties);
  72. }
  73. function coordsIsNaN(coords) {
  74. if (Array.isArray(coords[0])) return coordsIsNaN(coords[0]);
  75. return isNaN(coords[0]);
  76. }
  77. function projectCoords(coords, proj) {
  78. if (typeof coords[0] !== "object") return proj(coords);
  79. return coords.map(function(coord) {
  80. return projectCoords(coord, proj);
  81. });
  82. }
  83. function unprojectCoords(coords, proj) {
  84. if (typeof coords[0] !== "object") return proj.invert(coords);
  85. return coords.map(function(coord) {
  86. return unprojectCoords(coord, proj);
  87. });
  88. }
  89. function defineProjection(geojson) {
  90. var coords = center(geojson).geometry.coordinates;
  91. var rotation = [-coords[0], -coords[1]];
  92. return geoAzimuthalEquidistant().rotate(rotation).scale(earthRadius);
  93. }
  94. var turf_buffer_default = buffer;
  95. export {
  96. buffer,
  97. turf_buffer_default as default
  98. };
  99. //# sourceMappingURL=index.js.map