🚀 Big News:Socket Has Acquired Secure Annex.Learn More
Socket
Book a DemoSign in
Socket

@turf/boolean-within

Package Overview
Dependencies
Maintainers
4
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@turf/boolean-within - npm Package Compare versions

Comparing version
4.7.3
to
5.0.4
+223
main.js
'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var calcBbox = _interopDefault(require('@turf/bbox'));
var booleanPointOnLine = _interopDefault(require('@turf/boolean-point-on-line'));
var booleanPointInPolygon = _interopDefault(require('@turf/boolean-point-in-polygon'));
var invariant = require('@turf/invariant');
/**
* Boolean-within returns true if the first geometry is completely within the second geometry.
* The interiors of both geometries must intersect and, the interior and boundary of the primary (geometry a)
* must not intersect the exterior of the secondary (geometry b).
* Boolean-within returns the exact opposite result of the `@turf/boolean-contains`.
*
* @name booleanWithin
* @param {Geometry|Feature<any>} feature1 GeoJSON Feature or Geometry
* @param {Geometry|Feature<any>} feature2 GeoJSON Feature or Geometry
* @returns {boolean} true/false
* @example
* var line = turf.lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
* var point = turf.point([1, 2]);
*
* turf.booleanWithin(point, line);
* //=true
*/
function booleanWithin(feature1, feature2) {
var type1 = invariant.getType(feature1);
var type2 = invariant.getType(feature2);
var geom1 = invariant.getGeom(feature1);
var geom2 = invariant.getGeom(feature2);
switch (type1) {
case 'Point':
switch (type2) {
case 'MultiPoint':
return isPointInMultiPoint(geom1, geom2);
case 'LineString':
return booleanPointOnLine(geom1, geom2, {ignoreEndVertices: true});
case 'Polygon':
return booleanPointInPolygon(geom1, geom2, {ignoreBoundary: true});
default:
throw new Error('feature2 ' + type2 + ' geometry not supported');
}
case 'MultiPoint':
switch (type2) {
case 'MultiPoint':
return isMultiPointInMultiPoint(geom1, geom2);
case 'LineString':
return isMultiPointOnLine(geom1, geom2);
case 'Polygon':
return isMultiPointInPoly(geom1, geom2);
default:
throw new Error('feature2 ' + type2 + ' geometry not supported');
}
case 'LineString':
switch (type2) {
case 'LineString':
return isLineOnLine(geom1, geom2);
case 'Polygon':
return isLineInPoly(geom1, geom2);
default:
throw new Error('feature2 ' + type2 + ' geometry not supported');
}
case 'Polygon':
switch (type2) {
case 'Polygon':
return isPolyInPoly(geom1, geom2);
default:
throw new Error('feature2 ' + type2 + ' geometry not supported');
}
default:
throw new Error('feature1 ' + type1 + ' geometry not supported');
}
}
function isPointInMultiPoint(point, multiPoint) {
var i;
var output = false;
for (i = 0; i < multiPoint.coordinates.length; i++) {
if (compareCoords(multiPoint.coordinates[i], point.coordinates)) {
output = true;
break;
}
}
return output;
}
function isMultiPointInMultiPoint(multiPoint1, multiPoint2) {
for (var i = 0; i < multiPoint1.coordinates.length; i++) {
var anyMatch = false;
for (var i2 = 0; i2 < multiPoint2.coordinates.length; i2++) {
if (compareCoords(multiPoint1.coordinates[i], multiPoint2.coordinates[i2])) {
anyMatch = true;
}
}
if (!anyMatch) {
return false;
}
}
return true;
}
function isMultiPointOnLine(multiPoint, lineString) {
var foundInsidePoint = false;
for (var i = 0; i < multiPoint.coordinates.length; i++) {
if (!booleanPointOnLine(multiPoint.coordinates[i], lineString)) {
return false;
}
if (!foundInsidePoint) {
foundInsidePoint = booleanPointOnLine(multiPoint.coordinates[i], lineString, {ignoreEndVertices: true});
}
}
return foundInsidePoint;
}
function isMultiPointInPoly(multiPoint, polygon) {
var output = true;
var oneInside = false;
for (var i = 0; i < multiPoint.coordinates.length; i++) {
var isInside = booleanPointInPolygon(multiPoint.coordinates[1], polygon);
if (!isInside) {
output = false;
break;
}
if (!oneInside) {
isInside = booleanPointInPolygon(multiPoint.coordinates[1], polygon, {ignoreBoundary: true});
}
}
return output && isInside;
}
function isLineOnLine(lineString1, lineString2) {
for (var i = 0; i < lineString1.coordinates.length; i++) {
if (!booleanPointOnLine(lineString1.coordinates[i], lineString2)) {
return false;
}
}
return true;
}
function isLineInPoly(linestring, polygon) {
var polyBbox = calcBbox(polygon);
var lineBbox = calcBbox(linestring);
if (!doBBoxOverlap(polyBbox, lineBbox)) {
return false;
}
var foundInsidePoint = false;
for (var i = 0; i < linestring.coordinates.length - 1; i++) {
if (!booleanPointInPolygon(linestring.coordinates[i], polygon)) {
return false;
}
if (!foundInsidePoint) {
foundInsidePoint = booleanPointInPolygon(linestring.coordinates[i], polygon, {ignoreBoundary: true});
}
if (!foundInsidePoint) {
var midpoint = getMidpoint(linestring.coordinates[i], linestring.coordinates[i + 1]);
foundInsidePoint = booleanPointInPolygon(midpoint, polygon, {ignoreBoundary: true});
}
}
return foundInsidePoint;
}
/**
* Is Polygon2 in Polygon1
* Only takes into account outer rings
*
* @private
* @param {Geometry|Feature<Polygon>} feature1 Polygon1
* @param {Geometry|Feature<Polygon>} feature2 Polygon2
* @returns {boolean} true/false
*/
function isPolyInPoly(feature1, feature2) {
var poly1Bbox = calcBbox(feature1);
var poly2Bbox = calcBbox(feature2);
if (!doBBoxOverlap(poly2Bbox, poly1Bbox)) {
return false;
}
for (var i = 0; i < feature1.coordinates[0].length; i++) {
if (!booleanPointInPolygon(feature1.coordinates[0][i], feature2)) {
return false;
}
}
return true;
}
function doBBoxOverlap(bbox1, bbox2) {
if (bbox1[0] > bbox2[0]) return false;
if (bbox1[2] < bbox2[2]) return false;
if (bbox1[1] > bbox2[1]) return false;
if (bbox1[3] < bbox2[3]) return false;
return true;
}
/**
* compareCoords
*
* @private
* @param {Array<number>} pair1 point [x,y]
* @param {Array<number>} pair2 point [x,y]
* @returns {boolean} true/false if coord pairs match
*/
function compareCoords(pair1, pair2) {
return pair1[0] === pair2[0] && pair1[1] === pair2[1];
}
/**
* getMidpoint
*
* @private
* @param {Array<number>} pair1 point [x,y]
* @param {Array<number>} pair2 point [x,y]
* @returns {Array<number>} midpoint of pair1 and pair2
*/
function getMidpoint(pair1, pair2) {
return [(pair1[0] + pair2[0]) / 2, (pair1[1] + pair2[1]) / 2];
}
module.exports = booleanWithin;
module.exports.default = booleanWithin;
+6
-7

@@ -1,10 +0,9 @@

/// <reference types="geojson" />
import { Feature, GeometryObject } from '@turf/helpers'
type Feature = GeoJSON.Feature<any> | GeoJSON.GeometryObject
/**
* http://turfjs.org/docs/#boolean-within
* http://turfjs.org/docs/#booleanwithin
*/
declare function within(feature1: Feature, feature2: Feature): boolean;
declare namespace within { }
export = within;
export default function (
feature1: Feature<any> | GeometryObject,
feature2: Feature<any> | GeometryObject
): boolean;
+28
-28

@@ -1,7 +0,5 @@

var inside = require('@turf/inside');
var calcBbox = require('@turf/bbox');
var invariant = require('@turf/invariant');
var isPointOnLine = require('@turf/boolean-point-on-line');
var getGeom = invariant.getGeom;
var getGeomType = invariant.getGeomType;
import calcBbox from '@turf/bbox';
import booleanPointOnLine from '@turf/boolean-point-on-line';
import booleanPointInPolygon from '@turf/boolean-point-in-polygon';
import { getGeom, getType } from '@turf/invariant';

@@ -19,4 +17,4 @@ /**

* @example
* const line = turf.lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
* const point = turf.point([1, 2]);
* var line = turf.lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
* var point = turf.point([1, 2]);
*

@@ -26,5 +24,5 @@ * turf.booleanWithin(point, line);

*/
module.exports = function (feature1, feature2) {
var type1 = getGeomType(feature1);
var type2 = getGeomType(feature2);
function booleanWithin(feature1, feature2) {
var type1 = getType(feature1);
var type2 = getType(feature2);
var geom1 = getGeom(feature1);

@@ -39,5 +37,5 @@ var geom2 = getGeom(feature2);

case 'LineString':
return isPointOnLine(geom1, geom2, true);
return booleanPointOnLine(geom1, geom2, {ignoreEndVertices: true});
case 'Polygon':
return inside(geom1, geom2, true);
return booleanPointInPolygon(geom1, geom2, {ignoreBoundary: true});
default:

@@ -76,3 +74,3 @@ throw new Error('feature2 ' + type2 + ' geometry not supported');

}
};
}

@@ -110,7 +108,7 @@ function isPointInMultiPoint(point, multiPoint) {

for (var i = 0; i < multiPoint.coordinates.length; i++) {
if (!isPointOnLine(multiPoint.coordinates[i], lineString)) {
if (!booleanPointOnLine(multiPoint.coordinates[i], lineString)) {
return false;
}
if (!foundInsidePoint) {
foundInsidePoint = isPointOnLine(multiPoint.coordinates[i], lineString, true);
foundInsidePoint = booleanPointOnLine(multiPoint.coordinates[i], lineString, {ignoreEndVertices: true});
}

@@ -125,3 +123,3 @@ }

for (var i = 0; i < multiPoint.coordinates.length; i++) {
var isInside = inside(multiPoint.coordinates[1], polygon);
var isInside = booleanPointInPolygon(multiPoint.coordinates[1], polygon);
if (!isInside) {

@@ -132,3 +130,3 @@ output = false;

if (!oneInside) {
isInside = inside(multiPoint.coordinates[1], polygon, true);
isInside = booleanPointInPolygon(multiPoint.coordinates[1], polygon, {ignoreBoundary: true});
}

@@ -141,3 +139,3 @@ }

for (var i = 0; i < lineString1.coordinates.length; i++) {
if (!isPointOnLine(lineString1.coordinates[i], lineString2)) {
if (!booleanPointOnLine(lineString1.coordinates[i], lineString2)) {
return false;

@@ -158,11 +156,11 @@ }

for (var i = 0; i < linestring.coordinates.length - 1; i++) {
if (!inside(linestring.coordinates[i], polygon)) {
if (!booleanPointInPolygon(linestring.coordinates[i], polygon)) {
return false;
}
if (!foundInsidePoint) {
foundInsidePoint = inside(linestring.coordinates[i], polygon, true);
foundInsidePoint = booleanPointInPolygon(linestring.coordinates[i], polygon, {ignoreBoundary: true});
}
if (!foundInsidePoint) {
var midpoint = getMidpoint(linestring.coordinates[i], linestring.coordinates[i + 1]);
foundInsidePoint = inside(midpoint, polygon, true);
foundInsidePoint = booleanPointInPolygon(midpoint, polygon, {ignoreBoundary: true});

@@ -190,3 +188,3 @@ }

for (var i = 0; i < feature1.coordinates[0].length; i++) {
if (!inside(feature1.coordinates[0][i], feature2)) {
if (!booleanPointInPolygon(feature1.coordinates[0][i], feature2)) {
return false;

@@ -210,4 +208,4 @@ }

* @private
* @param {[number, number]} pair1 point [x,y]
* @param {[number, number]} pair2 point [x,y]
* @param {Array<number>} pair1 point [x,y]
* @param {Array<number>} pair2 point [x,y]
* @returns {boolean} true/false if coord pairs match

@@ -223,5 +221,5 @@ */

* @private
* @param {[number, number]} pair1 point [x,y]
* @param {[number, number]} pair2 point [x,y]
* @returns {[number, number]} midpoint of pair1 and pair2
* @param {Array<number>} pair1 point [x,y]
* @param {Array<number>} pair2 point [x,y]
* @returns {Array<number>} midpoint of pair1 and pair2
*/

@@ -231,1 +229,3 @@ function getMidpoint(pair1, pair2) {

}
export default booleanWithin;
{
"name": "@turf/boolean-within",
"version": "4.7.3",
"version": "5.0.4",
"description": "turf boolean-within module",
"main": "index.js",
"main": "main",
"module": "index",
"jsnext:main": "index",
"types": "index.d.ts",
"files": [
"index.js",
"index.d.ts"
"index.d.ts",
"main.js"
],
"scripts": {
"test": "node test.js",
"bench": "node bench.js"
"pretest": "rollup -c ../../rollup.config.js",
"test": "node -r @std/esm test.js",
"bench": "node -r @std/esm bench.js"
},

@@ -37,16 +41,22 @@ "repository": {

"devDependencies": {
"@turf/helpers": "^4.7.3",
"benchmark": "2.1.4",
"boolean-jsts": "0.0.1",
"boolean-shapely": "0.1.2",
"glob": "7.1.2",
"load-json-file": "2.0.0",
"tape": "4.8.0"
"@std/esm": "*",
"benchmark": "*",
"boolean-jsts": "*",
"boolean-shapely": "*",
"glob": "*",
"load-json-file": "*",
"rollup": "*",
"tape": "*"
},
"dependencies": {
"@turf/bbox": "^4.7.3",
"@turf/boolean-point-on-line": "^4.7.3",
"@turf/inside": "^4.7.3",
"@turf/invariant": "^4.7.3"
"@turf/bbox": "^5.0.4",
"@turf/boolean-point-in-polygon": "^5.0.4",
"@turf/boolean-point-on-line": "^5.0.4",
"@turf/helpers": "^5.0.4",
"@turf/invariant": "^5.0.4"
},
"@std/esm": {
"esm": "js",
"cjs": true
}
}
# @turf/boolean-within
# booleanWithin
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
## booleanWithin
Boolean-within returns true if the first geometry is completely within the second geometry.

@@ -18,4 +20,4 @@ The interiors of both geometries must intersect and, the interior and boundary of the primary (geometry a)

```javascript
const line = turf.lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
const point = turf.point([1, 2]);
var line = turf.lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
var point = turf.point([1, 2]);

@@ -22,0 +24,0 @@ turf.booleanWithin(point, line);