index.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 invariant_1 = require("@turf/invariant");
  8. var polygon_clipping_1 = __importDefault(require("polygon-clipping"));
  9. /**
  10. * Takes two {@link Polygon|polygon} or {@link MultiPolygon|multi-polygon} geometries and
  11. * finds their polygonal intersection. If they don't intersect, returns null.
  12. *
  13. * @name intersect
  14. * @param {Feature<Polygon | MultiPolygon>} poly1 the first polygon or multipolygon
  15. * @param {Feature<Polygon | MultiPolygon>} poly2 the second polygon or multipolygon
  16. * @param {Object} [options={}] Optional Parameters
  17. * @param {Object} [options.properties={}] Translate GeoJSON Properties to Feature
  18. * @returns {Feature|null} returns a feature representing the area they share (either a {@link Polygon} or
  19. * {@link MultiPolygon}). If they do not share any area, returns `null`.
  20. * @example
  21. * var poly1 = turf.polygon([[
  22. * [-122.801742, 45.48565],
  23. * [-122.801742, 45.60491],
  24. * [-122.584762, 45.60491],
  25. * [-122.584762, 45.48565],
  26. * [-122.801742, 45.48565]
  27. * ]]);
  28. *
  29. * var poly2 = turf.polygon([[
  30. * [-122.520217, 45.535693],
  31. * [-122.64038, 45.553967],
  32. * [-122.720031, 45.526554],
  33. * [-122.669906, 45.507309],
  34. * [-122.723464, 45.446643],
  35. * [-122.532577, 45.408574],
  36. * [-122.487258, 45.477466],
  37. * [-122.520217, 45.535693]
  38. * ]]);
  39. *
  40. * var intersection = turf.intersect(poly1, poly2);
  41. *
  42. * //addToMap
  43. * var addToMap = [poly1, poly2, intersection];
  44. */
  45. function intersect(poly1, poly2, options) {
  46. if (options === void 0) { options = {}; }
  47. var geom1 = invariant_1.getGeom(poly1);
  48. var geom2 = invariant_1.getGeom(poly2);
  49. var intersection = polygon_clipping_1.default.intersection(geom1.coordinates, geom2.coordinates);
  50. if (intersection.length === 0)
  51. return null;
  52. if (intersection.length === 1)
  53. return helpers_1.polygon(intersection[0], options.properties);
  54. return helpers_1.multiPolygon(intersection, options.properties);
  55. }
  56. exports.default = intersect;