"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var helpers_1 = require("@turf/helpers"); var meta_1 = require("@turf/meta"); var concaveman_1 = __importDefault(require("concaveman")); /** * Takes a {@link Feature} or a {@link FeatureCollection} and returns a convex hull {@link Polygon}. * * Internally this uses * the [convex-hull](https://github.com/mikolalysenko/convex-hull) module that implements a * [monotone chain hull](http://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain). * * @name convex * @param {GeoJSON} geojson input Feature or FeatureCollection * @param {Object} [options={}] Optional parameters * @param {number} [options.concavity=Infinity] 1 - thin shape. Infinity - convex hull. * @param {Object} [options.properties={}] Translate Properties to Feature * @returns {Feature} a convex hull * @example * var points = turf.featureCollection([ * turf.point([10.195312, 43.755225]), * turf.point([10.404052, 43.8424511]), * turf.point([10.579833, 43.659924]), * turf.point([10.360107, 43.516688]), * turf.point([10.14038, 43.588348]), * turf.point([10.195312, 43.755225]) * ]); * * var hull = turf.convex(points); * * //addToMap * var addToMap = [points, hull] */ function convex(geojson, options) { if (options === void 0) { options = {}; } // Default parameters options.concavity = options.concavity || Infinity; // Container var points = []; // Convert all points to flat 2D coordinate Array meta_1.coordEach(geojson, function (coord) { points.push([coord[0], coord[1]]); }); if (!points.length) { return null; } var convexHull = concaveman_1.default(points, options.concavity); // Convex hull should have at least 3 different vertices in order to create a valid polygon if (convexHull.length > 3) { return helpers_1.polygon([convexHull]); } return null; } exports.default = convex;