What is @turf/bbox-clip?
@turf/bbox-clip is a module from the Turf.js library that allows you to clip a GeoJSON feature to a bounding box. This is useful for tasks such as extracting a specific area from a larger dataset, ensuring that features fit within a certain geographic region, or preparing data for visualization.
What are @turf/bbox-clip's main functionalities?
Clip a Polygon to a Bounding Box
This feature allows you to clip a polygon to a specified bounding box. The code sample demonstrates how to clip a polygon that spans a larger area to fit within the bounding box defined by the coordinates [-3, 53, 3, 55].
const turf = require('@turf/turf');
const polygon = turf.polygon([[
[-5, 52], [-5, 56], [5, 56], [5, 52], [-5, 52]
]]);
const bbox = [-3, 53, 3, 55];
const clipped = turf.bboxClip(polygon, bbox);
console.log(JSON.stringify(clipped));
Clip a LineString to a Bounding Box
This feature allows you to clip a LineString to a specified bounding box. The code sample demonstrates how to clip a LineString that spans a larger area to fit within the bounding box defined by the coordinates [-3, 53, 3, 55].
const turf = require('@turf/turf');
const line = turf.lineString([
[-5, 52], [-5, 56], [5, 56], [5, 52]
]);
const bbox = [-3, 53, 3, 55];
const clipped = turf.bboxClip(line, bbox);
console.log(JSON.stringify(clipped));
Clip a MultiPolygon to a Bounding Box
This feature allows you to clip a MultiPolygon to a specified bounding box. The code sample demonstrates how to clip a MultiPolygon that spans a larger area to fit within the bounding box defined by the coordinates [-3, 53, 3, 55].
const turf = require('@turf/turf');
const multiPolygon = turf.multiPolygon([[
[[-5, 52], [-5, 56], [5, 56], [5, 52], [-5, 52]]
], [
[[-10, 50], [-10, 54], [0, 54], [0, 50], [-10, 50]]
]]);
const bbox = [-3, 53, 3, 55];
const clipped = turf.bboxClip(multiPolygon, bbox);
console.log(JSON.stringify(clipped));
Other packages similar to @turf/bbox-clip
jsts
The jsts (JavaScript Topology Suite) library offers a wide range of geospatial operations, including clipping geometries to a bounding box. While it provides more comprehensive geospatial processing capabilities, it is also more complex and has a steeper learning curve compared to @turf/bbox-clip.
@turf/bbox-clip
bboxClip
Takes a Feature and a bbox and clips the feature to the bbox using
lineclip.
May result in degenerate edges when clipping Polygons.
Parameters
Examples
var bbox = [0, 0, 10, 10];
var poly = turf.polygon([[[2, 2], [8, 4], [12, 8], [3, 7], [2, 2]]]);
var clipped = turf.bboxClip(poly, bbox);
var addToMap = [bbox, poly, clipped]
Returns Feature<(LineString | MultiLineString | Polygon | MultiPolygon)> clipped Feature
This module is part of the Turfjs project, an open source module collection dedicated to geographic algorithms. It is maintained in the Turfjs/turf repository, where you can create PRs and issues.
Installation
Install this single module individually:
$ npm install @turf/bbox-clip
Or install the all-encompassing @turf/turf module that includes all modules as functions:
$ npm install @turf/turf