A JavaScript library for performing geospatial operations with GeoJSON data
—
Functions for testing spatial relationships between geometric features, including intersections, containment, topology validation, and directional analysis. These operations return boolean values based on geometric relationships.
Determine if a point is inside a polygon.
/**
* Takes a Point and a Polygon or MultiPolygon and determines if the point
* resides inside the polygon. The polygon can be convex or concave.
*/
function booleanPointInPolygon(
point: Coord,
polygon: Feature<Polygon | MultiPolygon> | Polygon | MultiPolygon,
options?: { ignoreBoundary?: boolean }
): boolean;Usage Examples:
import { booleanPointInPolygon, point, polygon } from "@turf/turf";
const pt = point([-77, 44]);
const poly = polygon([[
[-81, 41], [-81, 47], [-72, 47], [-72, 41], [-81, 41]
]]);
const inside = booleanPointInPolygon(pt, poly);
console.log(`Point is inside polygon: ${inside}`); // true
// Test point on boundary
const boundaryPt = point([-81, 44]);
const onBoundary = booleanPointInPolygon(boundaryPt, poly);
const ignoreBoundary = booleanPointInPolygon(boundaryPt, poly, { ignoreBoundary: true });
console.log(`On boundary (default): ${onBoundary}`); // true
console.log(`On boundary (ignored): ${ignoreBoundary}`); // falseDetermine if a point lies on a line.
/**
* Returns true if a point is on a line. Accepts a optional parameter to ignore the
* start and end vertices of the linestring.
*/
function booleanPointOnLine(
pt: Coord,
line: Feature<LineString> | LineString,
options?: { ignoreEndVertices?: boolean }
): boolean;Usage Examples:
import { booleanPointOnLine, point, lineString } from "@turf/turf";
const line = lineString([[-1, -1], [1, 1], [1.5, 2.2]]);
const pt1 = point([0, 0]); // On line
const pt2 = point([1.5, 2.2]); // End vertex
const pt3 = point([1, 2]); // Not on line
console.log(`Point 1 on line: ${booleanPointOnLine(pt1, line)}`); // true
console.log(`Point 2 on line: ${booleanPointOnLine(pt2, line)}`); // true
console.log(`Point 2 on line (ignore ends): ${booleanPointOnLine(pt2, line, { ignoreEndVertices: true })}`); // false
console.log(`Point 3 on line: ${booleanPointOnLine(pt3, line)}`); // falseDetermine if one feature completely contains another.
/**
* Boolean-contains returns True if the first geometry completely contains the second geometry.
*/
function booleanContains(feature1: Feature<any>, feature2: Feature<any>): boolean;
/**
* Boolean-within returns True if the first geometry is completely within the second geometry.
*/
function booleanWithin(feature1: Feature<any>, feature2: Feature<any>): boolean;Usage Examples:
import { booleanContains, booleanWithin, polygon, point } from "@turf/turf";
const outerPoly = polygon([[
[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]
]]);
const innerPoly = polygon([[
[2, 2], [2, 8], [8, 8], [8, 2], [2, 2]
]]);
const pt = point([5, 5]);
console.log(`Outer contains inner: ${booleanContains(outerPoly, innerPoly)}`); // true
console.log(`Inner within outer: ${booleanWithin(innerPoly, outerPoly)}`); // true
console.log(`Outer contains point: ${booleanContains(outerPoly, pt)}`); // trueDetermine if two features intersect.
/**
* Boolean-intersects returns true if the intersection of the two geometries
* is not empty.
*/
function booleanIntersects(feature1: Feature<any>, feature2: Feature<any>): boolean;
/**
* Boolean-disjoint returns true if the intersection of the two geometries is empty.
*/
function booleanDisjoint(feature1: Feature<any>, feature2: Feature<any>): boolean;Usage Examples:
import { booleanIntersects, booleanDisjoint, polygon, lineString } from "@turf/turf";
const poly1 = polygon([[[0, 0], [0, 5], [5, 5], [5, 0], [0, 0]]]);
const poly2 = polygon([[[3, 3], [3, 8], [8, 8], [8, 3], [3, 3]]]);
const poly3 = polygon([[[10, 10], [10, 15], [15, 15], [15, 10], [10, 10]]]);
console.log(`Poly1 intersects Poly2: ${booleanIntersects(poly1, poly2)}`); // true (overlapping)
console.log(`Poly1 intersects Poly3: ${booleanIntersects(poly1, poly3)}`); // false (separate)
console.log(`Poly1 disjoint from Poly3: ${booleanDisjoint(poly1, poly3)}`); // true (separate)Determine if two features overlap.
/**
* Boolean-overlap returns True if the intersection of the two geometries
* is of the same dimension but is not equal to either of the given geometries.
*/
function booleanOverlap(feature1: Feature<any>, feature2: Feature<any>): boolean;Usage Examples:
import { booleanOverlap, polygon } from "@turf/turf";
const poly1 = polygon([[[0, 0], [0, 5], [5, 5], [5, 0], [0, 0]]]);
const poly2 = polygon([[[3, 3], [3, 8], [8, 8], [8, 3], [3, 3]]]);
const poly3 = polygon([[[1, 1], [1, 4], [4, 4], [4, 1], [1, 1]]]);
console.log(`Poly1 overlaps Poly2: ${booleanOverlap(poly1, poly2)}`); // true (partial overlap)
console.log(`Poly1 overlaps Poly3: ${booleanOverlap(poly1, poly3)}`); // false (contains, not overlap)Test various topological relationships.
/**
* Boolean-crosses returns True if the intersection results in a geometry whose
* dimension is one less than the maximum dimension of the two source Geometries.
*/
function booleanCrosses(feature1: Feature<any>, feature2: Feature<any>): boolean;
/**
* Boolean-touches returns True if the geometries have at least one point in common,
* but their interiors do not intersect.
*/
function booleanTouches(feature1: Feature<any>, feature2: Feature<any>): boolean;
/**
* Boolean-equal returns true if the two geometries are exactly equal.
*/
function booleanEqual(feature1: Feature<any>, feature2: Feature<any>): boolean;Usage Examples:
import { booleanCrosses, booleanTouches, booleanEqual, lineString, polygon } from "@turf/turf";
const line1 = lineString([[0, 0], [5, 5]]);
const line2 = lineString([[0, 5], [5, 0]]);
const poly = polygon([[[0, 0], [0, 5], [5, 5], [5, 0], [0, 0]]]);
console.log(`Lines cross: ${booleanCrosses(line1, line2)}`); // true
console.log(`Line touches polygon: ${booleanTouches(line1, poly)}`); // depends on exact position
console.log(`Lines are equal: ${booleanEqual(line1, line2)}`); // falseTest geometric properties of features.
/**
* Takes a ring and return true or false whether or not the ring is clockwise or counter-clockwise.
*/
function booleanClockwise(line: Feature<LineString> | LineString): boolean;
/**
* Takes a polygon and returns true or false as to whether it is concave or not.
*/
function booleanConcave(polygon: Feature<Polygon>): boolean;
/**
* Boolean-valid checks if the geometry is a valid according to the OGC Simple Feature Specification.
*/
function booleanValid(feature: Feature<any>): boolean;Usage Examples:
import { booleanClockwise, booleanConcave, booleanValid, polygon, lineString } from "@turf/turf";
// Test clockwise orientation
const clockwiseRing = lineString([[0, 0], [5, 0], [5, 5], [0, 5], [0, 0]]);
const counterClockwiseRing = lineString([[0, 0], [0, 5], [5, 5], [5, 0], [0, 0]]);
console.log(`Ring 1 is clockwise: ${booleanClockwise(clockwiseRing)}`);
console.log(`Ring 2 is clockwise: ${booleanClockwise(counterClockwiseRing)}`);
// Test concave polygon
const concavePoly = polygon([[[0, 0], [2, 0], [1, 1], [2, 2], [0, 2], [0, 0]]]);
const convexPoly = polygon([[[0, 0], [2, 0], [2, 2], [0, 2], [0, 0]]]);
console.log(`Polygon 1 is concave: ${booleanConcave(concavePoly)}`);
console.log(`Polygon 2 is concave: ${booleanConcave(convexPoly)}`);
// Test validity
console.log(`Polygon is valid: ${booleanValid(convexPoly)}`);Test relationships between linear features.
/**
* Boolean-parallel returns True if each segment of `line1` is parallel to the correspondent
* segment of `line2`.
*/
function booleanParallel(
line1: Feature<LineString> | LineString,
line2: Feature<LineString> | LineString
): boolean;Usage Examples:
import { booleanParallel, lineString } from "@turf/turf";
const line1 = lineString([[0, 0], [0, 1]]);
const line2 = lineString([[1, 0], [1, 1]]);
const line3 = lineString([[0, 0], [1, 1]]);
console.log(`Lines 1 and 2 are parallel: ${booleanParallel(line1, line2)}`); // true
console.log(`Lines 1 and 3 are parallel: ${booleanParallel(line1, line3)}`); // falseInstall with Tessl CLI
npx tessl i tessl/npm-turf--turf