index.cjs 3.8 KB

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