What is lineclip?
The lineclip npm package is a JavaScript library used for clipping lines and polygons to rectangular bounding boxes. It is useful in geographic information systems (GIS) and other applications where you need to clip geometries to a specific area.
What are lineclip's main functionalities?
Line Clipping
This feature allows you to clip a line to a rectangular bounding box. The code sample demonstrates how to clip a line that crosses the bounding box defined by the coordinates [-5, -5, 5, 5].
const lineclip = require('lineclip');
const line = [[-10, 10], [10, -10]];
const bbox = [-5, -5, 5, 5];
const clippedLine = lineclip(line, bbox);
console.log(clippedLine);
Polygon Clipping
This feature allows you to clip a polygon to a rectangular bounding box. The code sample demonstrates how to clip a polygon that extends beyond the bounding box defined by the coordinates [-5, -5, 5, 5].
const lineclip = require('lineclip');
const polygon = [[-10, 10], [10, 10], [10, -10], [-10, -10], [-10, 10]];
const bbox = [-5, -5, 5, 5];
const clippedPolygon = lineclip.polygon(polygon, bbox);
console.log(clippedPolygon);
Other packages similar to lineclip
martinez-polygon-clipping
The martinez-polygon-clipping package provides advanced polygon clipping capabilities, including union, intersection, difference, and XOR operations. It is more feature-rich compared to lineclip, which focuses primarily on clipping lines and polygons to rectangular bounding boxes.
turf
Turf is a comprehensive geospatial analysis library for JavaScript. It includes a wide range of functionalities, including clipping, buffering, and spatial analysis. While lineclip is specialized in clipping lines and polygons to bounding boxes, Turf offers a broader set of geospatial tools.
lineclip

A very fast JavaScript library for clipping polylines and polygons by a bounding box.
lineclip(
[[-10, 10], [10, 10], [10, -10]],
[0, 0, 20, 20]);
API
lineclip.polyline(points, bbox[, result])
points
— an array of [x, y]
pointsbbox
— a bounding box as [xmin, ymin, xmax, ymax]
result
— an array to append the results to
Returns an array of clipped lines.
lineclip
is an alias to lineclip.polyline
.
lineclip.polygon(points, bbox)
Returns a clipped polygon.
Install
Install with NPM:
npm install lineclip
To build a browser-compatible version, clone the repository locally, then run:
npm install -g browserify
browserify -s lineclip index.js > lineclip.js
Changelog
1.1.5 (Sep 23, 2015)
- Fixed a bug where polygon clip broke on out-of-bbox polygons.
1.1.4 (Sep 22, 2015)
- Fixed a bug where last point was omitted if the last two points are in bbox.
- Fixed a bug where a line outside of bbox would produce
[[]]
instead of []
.
1.1.3 (Sep 12, 2015)
- Fixed a polygon clipping race condition.
1.1.2 (Sep 11, 2015)
- Fixed a bug that completely broke the clipping on many cases. Sorry!
1.1.1 (Sep 11, 2015)
- Fixed a polyline clipping edge case.
1.1.0 (Sep 11, 2015)
- Added Sutherland-Hodgeman polygon clipping (
lineclip.polygon
).
1.0.1 (Sep 11, 2015)
- Minor code cleanup and optimizations.
1.0.0 (Sep 8, 2015)