index.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var helpers_1 = require("@turf/helpers");
  7. var meta_1 = require("@turf/meta");
  8. var concaveman_1 = __importDefault(require("concaveman"));
  9. /**
  10. * Takes a {@link Feature} or a {@link FeatureCollection} and returns a convex hull {@link Polygon}.
  11. *
  12. * Internally this uses
  13. * the [convex-hull](https://github.com/mikolalysenko/convex-hull) module that implements a
  14. * [monotone chain hull](http://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain).
  15. *
  16. * @name convex
  17. * @param {GeoJSON} geojson input Feature or FeatureCollection
  18. * @param {Object} [options={}] Optional parameters
  19. * @param {number} [options.concavity=Infinity] 1 - thin shape. Infinity - convex hull.
  20. * @param {Object} [options.properties={}] Translate Properties to Feature
  21. * @returns {Feature<Polygon>} a convex hull
  22. * @example
  23. * var points = turf.featureCollection([
  24. * turf.point([10.195312, 43.755225]),
  25. * turf.point([10.404052, 43.8424511]),
  26. * turf.point([10.579833, 43.659924]),
  27. * turf.point([10.360107, 43.516688]),
  28. * turf.point([10.14038, 43.588348]),
  29. * turf.point([10.195312, 43.755225])
  30. * ]);
  31. *
  32. * var hull = turf.convex(points);
  33. *
  34. * //addToMap
  35. * var addToMap = [points, hull]
  36. */
  37. function convex(geojson, options) {
  38. if (options === void 0) { options = {}; }
  39. // Default parameters
  40. options.concavity = options.concavity || Infinity;
  41. // Container
  42. var points = [];
  43. // Convert all points to flat 2D coordinate Array
  44. meta_1.coordEach(geojson, function (coord) {
  45. points.push([coord[0], coord[1]]);
  46. });
  47. if (!points.length) {
  48. return null;
  49. }
  50. var convexHull = concaveman_1.default(points, options.concavity);
  51. // Convex hull should have at least 3 different vertices in order to create a valid polygon
  52. if (convexHull.length > 3) {
  53. return helpers_1.polygon([convexHull]);
  54. }
  55. return null;
  56. }
  57. exports.default = convex;