index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 polygon_clipping_1 = __importDefault(require("polygon-clipping"));
  7. var invariant_1 = require("@turf/invariant");
  8. var helpers_1 = require("@turf/helpers");
  9. /**
  10. * Takes two {@link (Multi)Polygon(s)} and returns a combined polygon. If the input polygons are not contiguous, this function returns a {@link MultiPolygon} feature.
  11. *
  12. * @name union
  13. * @param {Feature<Polygon|MultiPolygon>} polygon1 input Polygon feature
  14. * @param {Feature<Polygon|MultiPolygon>} polygon2 Polygon feature to difference from polygon1
  15. * @param {Object} [options={}] Optional Parameters
  16. * @param {Object} [options.properties={}] Translate Properties to output Feature
  17. * @returns {Feature<(Polygon|MultiPolygon)>} a combined {@link Polygon} or {@link MultiPolygon} feature, or null if the inputs are empty
  18. * @example
  19. * var poly1 = turf.polygon([[
  20. * [-82.574787, 35.594087],
  21. * [-82.574787, 35.615581],
  22. * [-82.545261, 35.615581],
  23. * [-82.545261, 35.594087],
  24. * [-82.574787, 35.594087]
  25. * ]], {"fill": "#0f0"});
  26. * var poly2 = turf.polygon([[
  27. * [-82.560024, 35.585153],
  28. * [-82.560024, 35.602602],
  29. * [-82.52964, 35.602602],
  30. * [-82.52964, 35.585153],
  31. * [-82.560024, 35.585153]
  32. * ]], {"fill": "#00f"});
  33. *
  34. * var union = turf.union(poly1, poly2);
  35. *
  36. * //addToMap
  37. * var addToMap = [poly1, poly2, union];
  38. */
  39. function union(poly1, poly2, options) {
  40. if (options === void 0) { options = {}; }
  41. var geom1 = invariant_1.getGeom(poly1);
  42. var geom2 = invariant_1.getGeom(poly2);
  43. var unioned = polygon_clipping_1.default.union(geom1.coordinates, geom2.coordinates);
  44. if (unioned.length === 0)
  45. return null;
  46. if (unioned.length === 1)
  47. return helpers_1.polygon(unioned[0], options.properties);
  48. else
  49. return helpers_1.multiPolygon(unioned, options.properties);
  50. }
  51. exports.default = union;