@turf/intersect
Advanced tools
Comparing version 6.2.0-alpha.1 to 6.2.0-alpha.2
import { multiPolygon, polygon } from "@turf/helpers"; | ||
import { getGeom } from "@turf/invariant"; | ||
import * as martinez from "martinez-polygon-clipping"; | ||
import polygonClipping from 'polygon-clipping'; | ||
/** | ||
@@ -44,57 +44,8 @@ * Takes two {@link Polygon|polygon} or {@link MultiPolygon|multi-polygon} geometries and | ||
var geom2 = getGeom(poly2); | ||
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 polygon(intersection[0], options.properties); | ||
} | ||
return null; | ||
} | ||
return 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, _a = geom1.coordinates; _i < _a.length; _i++) { | ||
var coords = _a[_i]; | ||
var subGeom = getGeom(polygon(coords)); | ||
var subIntersection = intersect(subGeom, geom2); | ||
if (subIntersection) { | ||
var 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"); | ||
} | ||
var intersection = polygonClipping.intersection(geom1.coordinates, geom2.coordinates); | ||
if (intersection.length === 0) | ||
return null; | ||
if (intersection.length === 1) | ||
return polygon(intersection[0], options.properties); | ||
return multiPolygon(intersection, options.properties); | ||
} |
"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; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
@@ -12,3 +8,3 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
var invariant_1 = require("@turf/invariant"); | ||
var martinez = __importStar(require("martinez-polygon-clipping")); | ||
var polygon_clipping_1 = __importDefault(require("polygon-clipping")); | ||
/** | ||
@@ -54,58 +50,9 @@ * Takes two {@link Polygon|polygon} or {@link MultiPolygon|multi-polygon} geometries and | ||
var geom2 = invariant_1.getGeom(poly2); | ||
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); | ||
} | ||
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, _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") { | ||
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"); | ||
} | ||
var intersection = polygon_clipping_1.default.intersection(geom1.coordinates, geom2.coordinates); | ||
if (intersection.length === 0) | ||
return null; | ||
if (intersection.length === 1) | ||
return helpers_1.polygon(intersection[0], options.properties); | ||
return helpers_1.multiPolygon(intersection, options.properties); | ||
} | ||
exports.default = intersect; |
{ | ||
"name": "@turf/intersect", | ||
"version": "6.2.0-alpha.1", | ||
"version": "6.2.0-alpha.2", | ||
"description": "turf intersect module", | ||
@@ -46,2 +46,3 @@ "author": "Turf Authors", | ||
"load-json-file": "*", | ||
"npm-run-all": "*", | ||
"tape": "*", | ||
@@ -53,7 +54,7 @@ "tslint": "*", | ||
"dependencies": { | ||
"@turf/helpers": "^6.2.0-alpha.1", | ||
"@turf/invariant": "^6.2.0-alpha.1", | ||
"martinez-polygon-clipping": "^0.6.2" | ||
"@turf/helpers": "^6.2.0-alpha.2", | ||
"@turf/invariant": "^6.2.0-alpha.2", | ||
"polygon-clipping": "^0.14.3" | ||
}, | ||
"gitHead": "fc519c045a8931c1e14eab9160a7e28391f8da02" | ||
"gitHead": "23d5cb91d77e0c1e2e903a2252f525797f1d0d09" | ||
} |
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
10699
9
146
+ Addedpolygon-clipping@^0.14.3
+ Addedpolygon-clipping@0.14.3(transitive)
+ Addedsplaytree@3.1.2(transitive)
- Removedmartinez-polygon-clipping@^0.6.2
- Removedmartinez-polygon-clipping@0.6.2(transitive)
- Removedrobust-predicates@2.0.4(transitive)
- Removedsplaytree@0.1.4(transitive)
- Removedtinyqueue@1.2.3(transitive)
Updated@turf/helpers@^6.2.0-alpha.2