Turf.jsBooleans

turf.booleanWithin

What is turf.booleanWithin?

turf.booleanWithin tests whether geometry a is completely inside geometry b. It is the inverse of turf.booleanContainsbooleanWithin(a, b) equals booleanContains(b, a).

JavaScript
turf.booleanWithin(feature1, feature2) → boolean

When would you use turf.booleanWithin?

Use turf.booleanWithin when filtering from the contained side — "is my point, line, or polygon entirely within this region?". In code that thinks from the inner feature's perspective (e.g. each order being tested against its zone) it reads more naturally than booleanContains.

JavaScript
const inBounds = customers.features.filter(c => turf.booleanWithin(c, cityPolygon));

FAQs

How do I install Turf.js to use this function?

Install npm install @turf/boolean-within and import import { booleanWithin } from '@turf/boolean-within', or use turf.booleanWithin from @turf/turf.

Does a point on the boundary count as within?

The OGC predicate requires the inner geometry's interior to be strictly inside the outer geometry's interior. Boundary-only contact is not enough. Use turf.booleanIntersects if touching counts.

Does it support MultiPolygon outer geometries?

Yes. A point or polygon inside any of a MultiPolygon's parts is within the whole.

When should I use booleanWithin vs booleanPointInPolygon?

For single-point checks, booleanPointInPolygon is faster and offers ignoreBoundary. Use booleanWithin for line-in-polygon or polygon-in-polygon checks.