index.cjs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }// index.js
  2. var _rbush = require('rbush'); var _rbush2 = _interopRequireDefault(_rbush);
  3. var _helpers = require('@turf/helpers');
  4. var _meta = require('@turf/meta');
  5. var _bbox = require('@turf/bbox');
  6. function geojsonRbush(maxEntries) {
  7. var tree = new (0, _rbush2.default)(maxEntries);
  8. tree.insert = function(feature) {
  9. if (feature.type !== "Feature") throw new Error("invalid feature");
  10. feature.bbox = feature.bbox ? feature.bbox : _bbox.bbox.call(void 0, feature);
  11. return _rbush2.default.prototype.insert.call(this, feature);
  12. };
  13. tree.load = function(features) {
  14. var load = [];
  15. if (Array.isArray(features)) {
  16. features.forEach(function(feature) {
  17. if (feature.type !== "Feature") throw new Error("invalid features");
  18. feature.bbox = feature.bbox ? feature.bbox : _bbox.bbox.call(void 0, feature);
  19. load.push(feature);
  20. });
  21. } else {
  22. _meta.featureEach.call(void 0, features, function(feature) {
  23. if (feature.type !== "Feature") throw new Error("invalid features");
  24. feature.bbox = feature.bbox ? feature.bbox : _bbox.bbox.call(void 0, feature);
  25. load.push(feature);
  26. });
  27. }
  28. return _rbush2.default.prototype.load.call(this, load);
  29. };
  30. tree.remove = function(feature, equals) {
  31. if (feature.type !== "Feature") throw new Error("invalid feature");
  32. feature.bbox = feature.bbox ? feature.bbox : _bbox.bbox.call(void 0, feature);
  33. return _rbush2.default.prototype.remove.call(this, feature, equals);
  34. };
  35. tree.clear = function() {
  36. return _rbush2.default.prototype.clear.call(this);
  37. };
  38. tree.search = function(geojson) {
  39. var features = _rbush2.default.prototype.search.call(this, this.toBBox(geojson));
  40. return _helpers.featureCollection.call(void 0, features);
  41. };
  42. tree.collides = function(geojson) {
  43. return _rbush2.default.prototype.collides.call(this, this.toBBox(geojson));
  44. };
  45. tree.all = function() {
  46. var features = _rbush2.default.prototype.all.call(this);
  47. return _helpers.featureCollection.call(void 0, features);
  48. };
  49. tree.toJSON = function() {
  50. return _rbush2.default.prototype.toJSON.call(this);
  51. };
  52. tree.fromJSON = function(json) {
  53. return _rbush2.default.prototype.fromJSON.call(this, json);
  54. };
  55. tree.toBBox = function(geojson) {
  56. var bbox;
  57. if (geojson.bbox) bbox = geojson.bbox;
  58. else if (Array.isArray(geojson) && geojson.length === 4) bbox = geojson;
  59. else if (Array.isArray(geojson) && geojson.length === 6)
  60. bbox = [geojson[0], geojson[1], geojson[3], geojson[4]];
  61. else if (geojson.type === "Feature") bbox = _bbox.bbox.call(void 0, geojson);
  62. else if (geojson.type === "FeatureCollection") bbox = _bbox.bbox.call(void 0, geojson);
  63. else throw new Error("invalid geojson");
  64. return {
  65. minX: bbox[0],
  66. minY: bbox[1],
  67. maxX: bbox[2],
  68. maxY: bbox[3]
  69. };
  70. };
  71. return tree;
  72. }
  73. var turf_geojson_rbush_default = geojsonRbush;
  74. exports.default = turf_geojson_rbush_default; exports.geojsonRbush = geojsonRbush;
  75. //# sourceMappingURL=index.cjs.map