@turf/intersect
Advanced tools
Comparing version 6.1.1 to 6.1.2
56
index.js
"use strict"; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var martinez = require("martinez-polygon-clipping"); | ||
var helpers_1 = require("@turf/helpers"); | ||
var invariant_1 = require("@turf/invariant"); | ||
var helpers_1 = require("@turf/helpers"); | ||
var martinez = __importStar(require("martinez-polygon-clipping")); | ||
/** | ||
* Takes two {@link Polygon|polygon} or {@link MultiPolygon|multi-polygon} geometries and finds their polygonal intersection. If they don't intersect, returns null. | ||
* Takes two {@link Polygon|polygon} or {@link MultiPolygon|multi-polygon} geometries and | ||
* finds their polygonal intersection. If they don't intersect, returns null. | ||
* | ||
@@ -14,3 +22,4 @@ * @name intersect | ||
* @param {Object} [options.properties={}] Translate GeoJSON Properties to Feature | ||
* @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`. | ||
* @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,11 +54,13 @@ * var poly1 = turf.polygon([[ | ||
var geom2 = invariant_1.getGeom(poly2); | ||
if (geom1.type === 'Polygon' && geom2.type === 'Polygon') { | ||
if (geom1.type === "Polygon" && geom2.type === "Polygon") { | ||
var intersection = martinez.intersection(geom1.coordinates, geom2.coordinates); | ||
if (intersection === null || intersection.length === 0) | ||
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]) | ||
if (start[0] === end[0] && start[1] === end[1]) { | ||
return helpers_1.polygon(intersection[0], options.properties); | ||
} | ||
return null; | ||
@@ -59,27 +70,34 @@ } | ||
} | ||
else if (geom1.type === 'MultiPolygon') { | ||
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])); | ||
for (var _i = 0, _a = geom1.coordinates; _i < _a.length; _i++) { | ||
var coords = _a[_i]; | ||
var subGeom = invariant_1.getGeom(helpers_1.polygon(coords)); | ||
var subIntersection = intersect(subGeom, geom2); | ||
if (subIntersection) { | ||
var subIntGeom = invariant_1.getGeom(subIntersection); | ||
if (subIntGeom.type === 'Polygon') | ||
if (subIntGeom.type === "Polygon") { | ||
resultCoords.push(subIntGeom.coordinates); | ||
else if (subIntGeom.type === 'MultiPolygon') | ||
} | ||
else if (subIntGeom.type === "MultiPolygon") { | ||
resultCoords = resultCoords.concat(subIntGeom.coordinates); | ||
else | ||
throw new Error('intersection is invalid'); | ||
} | ||
else { | ||
throw new Error("intersection is invalid"); | ||
} | ||
} | ||
} | ||
// Make a polygon with the result | ||
if (resultCoords.length === 0) | ||
if (resultCoords.length === 0) { | ||
return null; | ||
if (resultCoords.length === 1) | ||
} | ||
if (resultCoords.length === 1) { | ||
return helpers_1.polygon(resultCoords[0], options.properties); | ||
else | ||
} | ||
else { | ||
return helpers_1.multiPolygon(resultCoords, options.properties); | ||
} | ||
} | ||
else if (geom2.type === 'MultiPolygon') { | ||
else if (geom2.type === "MultiPolygon") { | ||
// geom1 is a polygon and geom2 a multiPolygon, | ||
@@ -91,5 +109,5 @@ // put the multiPolygon first and fallback to the previous case. | ||
// handle invalid geometry types | ||
throw new Error('poly1 and poly2 must be either polygons or multiPolygons'); | ||
throw new Error("poly1 and poly2 must be either polygons or multiPolygons"); | ||
} | ||
} | ||
exports.default = intersect; |
{ | ||
"name": "@turf/intersect", | ||
"version": "6.1.1", | ||
"version": "6.1.2", | ||
"description": "turf intersect module", | ||
@@ -5,0 +5,0 @@ "main": "index", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10487
5
149