Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@turf/intersect

Package Overview
Dependencies
Maintainers
4
Versions
63
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@turf/intersect - npm Package Compare versions

Comparing version 6.0.1 to 6.1.0

67

index.js

@@ -7,10 +7,10 @@ "use strict";

/**
* Takes two {@link Polygon|polygons} and finds their intersection. If they share a border, returns the border; if they don't intersect, returns undefined.
* Takes two {@link Polygon|polygon} or {@link MultiPolygon|multi-polygon} geometries and finds their polygonal intersection. If they don't intersect, returns null.
*
* @name intersect
* @param {Feature<Polygon>} poly1 the first polygon
* @param {Feature<Polygon>} poly2 the second polygon
* @param {Feature<Polygon | MultiPolygon>} poly1 the first polygon or multipolygon
* @param {Feature<Polygon | MultiPolygon>} poly2 the second polygon or multipolygon
* @param {Object} [options={}] Optional Parameters
* @param {Object} [options.properties={}] Translate GeoJSON Properties to Feature
* @returns {Feature|null} returns a feature representing the point(s) they share (in case of a {@link Point} or {@link MultiPoint}), the borders they share (in case of a {@link LineString} or a {@link MultiLineString}), the area they share (in case of {@link Polygon} or {@link MultiPolygon}). If they do not share any point, returns `null`.
* @returns {Feature|null} returns a feature representing the area they share (either a {@link Polygon} or {@link MultiPolygon}). If they do not share any area, returns `null`.
* @example

@@ -45,18 +45,49 @@ * var poly1 = turf.polygon([[

var geom2 = invariant_1.getGeom(poly2);
if (geom1.type !== 'Polygon')
throw new Error('poly1 must be a Polygon');
if (geom2.type !== 'Polygon')
throw new Error('poly2 must be a Polygon');
var intersection = martinez.intersection(geom1.coordinates, geom2.coordinates);
if (intersection === null || intersection.length === 0)
return null;
if (intersection.length === 1) {
var start = intersection[0][0][0];
var end = intersection[0][0][intersection[0][0].length - 1];
if (start[0] === end[0] && start[1] === end[1])
return helpers_1.polygon(intersection[0], options.properties);
return null;
if (geom1.type === 'Polygon' && geom2.type === 'Polygon') {
var intersection = martinez.intersection(geom1.coordinates, geom2.coordinates);
if (intersection === null || intersection.length === 0)
return null;
if (intersection.length === 1) {
var start = intersection[0][0][0];
var end = intersection[0][0][intersection[0][0].length - 1];
if (start[0] === end[0] && start[1] === end[1])
return helpers_1.polygon(intersection[0], options.properties);
return null;
}
return helpers_1.multiPolygon(intersection, options.properties);
}
return helpers_1.multiPolygon(intersection, options.properties);
else if (geom1.type === 'MultiPolygon') {
var resultCoords = [];
// iterate through the polygon and run intersect with each part, adding to the resultCoords.
for (var i = 0; i < geom1.coordinates.length; i++) {
var subGeom = invariant_1.getGeom(helpers_1.polygon(geom1.coordinates[i]));
var subIntersection = intersect(subGeom, geom2);
if (subIntersection) {
var subIntGeom = invariant_1.getGeom(subIntersection);
if (subIntGeom.type === 'Polygon')
resultCoords.push(subIntGeom.coordinates);
else if (subIntGeom.type === 'MultiPolygon')
resultCoords = resultCoords.concat(subIntGeom.coordinates);
else
throw new Error('intersection is invalid');
}
}
// Make a polygon with the result
if (resultCoords.length === 0)
return null;
if (resultCoords.length === 1)
return helpers_1.polygon(resultCoords[0], options.properties);
else
return helpers_1.multiPolygon(resultCoords, options.properties);
}
else if (geom2.type === 'MultiPolygon') {
// geom1 is a polygon and geom2 a multiPolygon,
// put the multiPolygon first and fallback to the previous case.
return intersect(geom2, geom1);
}
else {
// handle invalid geometry types
throw new Error('poly1 and poly2 must be either polygons or multiPolygons');
}
}
exports.default = intersect;

@@ -6,10 +6,10 @@ import * as martinez from 'martinez-polygon-clipping';

/**
* Takes two {@link Polygon|polygons} and finds their intersection. If they share a border, returns the border; if they don't intersect, returns undefined.
* Takes two {@link Polygon|polygon} or {@link MultiPolygon|multi-polygon} geometries and finds their polygonal intersection. If they don't intersect, returns null.
*
* @name intersect
* @param {Feature<Polygon>} poly1 the first polygon
* @param {Feature<Polygon>} poly2 the second polygon
* @param {Feature<Polygon | MultiPolygon>} poly1 the first polygon or multipolygon
* @param {Feature<Polygon | MultiPolygon>} poly2 the second polygon or multipolygon
* @param {Object} [options={}] Optional Parameters
* @param {Object} [options.properties={}] Translate GeoJSON Properties to Feature
* @returns {Feature|null} returns a feature representing the point(s) they share (in case of a {@link Point} or {@link MultiPoint}), the borders they share (in case of a {@link LineString} or a {@link MultiLineString}), the area they share (in case of {@link Polygon} or {@link MultiPolygon}). If they do not share any point, returns `null`.
* @returns {Feature|null} returns a feature representing the area they share (either a {@link Polygon} or {@link MultiPolygon}). If they do not share any area, returns `null`.
* @example

@@ -41,4 +41,4 @@ * var poly1 = turf.polygon([[

function intersect<P = Properties>(
poly1: Feature<Polygon> | Polygon,
poly2: Feature<Polygon> | Polygon,
poly1: Feature<Polygon | MultiPolygon> | Polygon | MultiPolygon,
poly2: Feature<Polygon | MultiPolygon> | Polygon | MultiPolygon,
options: {

@@ -51,17 +51,47 @@ properties?: P,

if (geom1.type !== 'Polygon') throw new Error('poly1 must be a Polygon');
if (geom2.type !== 'Polygon') throw new Error('poly2 must be a Polygon');
if (geom1.type === 'Polygon' && geom2.type === 'Polygon') {
const intersection: any = martinez.intersection(geom1.coordinates, geom2.coordinates);
const intersection: any = martinez.intersection(geom1.coordinates, geom2.coordinates);
if (intersection === null || intersection.length === 0) return null;
if (intersection.length === 1) {
const start = intersection[0][0][0];
const end = intersection[0][0][intersection[0][0].length - 1];
if (start[0] === end[0] && start[1] === end[1]) return polygon(intersection[0], options.properties);
return null;
}
return multiPolygon(intersection, options.properties);
if (intersection === null || intersection.length === 0) return null;
if (intersection.length === 1) {
const start = intersection[0][0][0];
const end = intersection[0][0][intersection[0][0].length - 1];
if (start[0] === end[0] && start[1] === end[1]) return polygon(intersection[0], options.properties);
return null;
} else if (geom1.type === 'MultiPolygon') {
let resultCoords = [];
// iterate through the polygon and run intersect with each part, adding to the resultCoords.
for (let i = 0; i < geom1.coordinates.length; i++) {
const subGeom = getGeom(polygon(geom1.coordinates[i]));
const subIntersection = intersect(subGeom, geom2);
if (subIntersection) {
const subIntGeom = getGeom(subIntersection);
if (subIntGeom.type === 'Polygon') resultCoords.push(subIntGeom.coordinates);
else if (subIntGeom.type === 'MultiPolygon') resultCoords = resultCoords.concat(subIntGeom.coordinates);
else throw new Error('intersection is invalid');
}
}
// Make a polygon with the result
if (resultCoords.length === 0) return null;
if (resultCoords.length === 1) return polygon(resultCoords[0], options.properties);
else return multiPolygon(resultCoords, options.properties);
} else if (geom2.type === 'MultiPolygon') {
// geom1 is a polygon and geom2 a multiPolygon,
// put the multiPolygon first and fallback to the previous case.
return intersect(geom2, geom1);
} else {
// handle invalid geometry types
throw new Error('poly1 and poly2 must be either polygons or multiPolygons');
}
return multiPolygon(intersection, options.properties);
}
export default intersect;
{
"name": "@turf/intersect",
"version": "6.0.1",
"version": "6.1.0",
"description": "turf intersect module",

@@ -5,0 +5,0 @@ "main": "index",

@@ -7,8 +7,10 @@ # @turf/intersect

Takes two [polygons](https://tools.ietf.org/html/rfc7946#section-3.1.6) and finds their intersection. If they share a border, returns the border; if they don't intersect, returns undefined.
Takes two [polygon](https://tools.ietf.org/html/rfc7946#section-3.1.6) or [multi-polygon](https://tools.ietf.org/html/rfc7946#section-3.1.7) geometries and finds their polygonal intersection. If they don't intersect, returns null.
**Parameters**
- `poly1` **[Feature](https://tools.ietf.org/html/rfc7946#section-3.2)&lt;[Polygon](https://tools.ietf.org/html/rfc7946#section-3.1.6)>** the first polygon
- `poly2` **[Feature](https://tools.ietf.org/html/rfc7946#section-3.2)&lt;[Polygon](https://tools.ietf.org/html/rfc7946#section-3.1.6)>** the second polygon
- `poly1` **[Feature](https://tools.ietf.org/html/rfc7946#section-3.2)&lt;([Polygon](https://tools.ietf.org/html/rfc7946#section-3.1.6) \| [MultiPolygon](https://tools.ietf.org/html/rfc7946#section-3.1.7))>** the first polygon or multipolygon
- `poly2` **[Feature](https://tools.ietf.org/html/rfc7946#section-3.2)&lt;([Polygon](https://tools.ietf.org/html/rfc7946#section-3.1.6) \| [MultiPolygon](https://tools.ietf.org/html/rfc7946#section-3.1.7))>** the second polygon or multipolygon
- `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Optional Parameters (optional, default `{}`)
- `options.properties` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Translate GeoJSON Properties to Feature (optional, default `{}`)

@@ -43,3 +45,3 @@ **Examples**

Returns **([Feature](https://tools.ietf.org/html/rfc7946#section-3.2) | null)** returns a feature representing the point(s) they share (in case of a [Point](https://tools.ietf.org/html/rfc7946#section-3.1.2) or [MultiPoint](https://tools.ietf.org/html/rfc7946#section-3.1.3)), the borders they share (in case of a [LineString](https://tools.ietf.org/html/rfc7946#section-3.1.4) or a [MultiLineString](https://tools.ietf.org/html/rfc7946#section-3.1.5)), the area they share (in case of [Polygon](https://tools.ietf.org/html/rfc7946#section-3.1.6) or [MultiPolygon](https://tools.ietf.org/html/rfc7946#section-3.1.7)). If they do not share any point, returns `null`.
Returns **([Feature](https://tools.ietf.org/html/rfc7946#section-3.2) | null)** returns a feature representing the area they share (either a [Polygon](https://tools.ietf.org/html/rfc7946#section-3.1.6) or [MultiPolygon](https://tools.ietf.org/html/rfc7946#section-3.1.7)). If they do not share any area, returns `null`.

@@ -46,0 +48,0 @@ <!-- This file is automatically generated. Please don't edit it directly:

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc