PHPackages                             willvincent/polyclip - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. willvincent/polyclip

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

willvincent/polyclip
====================

PHP port of polyclip-ts.

1.2(1y ago)05.3k↑12.3%11MITPHPPHP ^8.2

Since Feb 26Pushed 1y ago1 watchersCompare

[ Source](https://github.com/willvincent/polyclip)[ Packagist](https://packagist.org/packages/willvincent/polyclip)[ RSS](/packages/willvincent-polyclip/feed)WikiDiscussions master Synced yesterday

READMEChangelog (2)Dependencies (6)Versions (3)Used By (1)

Polyclip
========

[](#polyclip)

Polyclip is a PHP library designed, based on [polyclip-ts](https://github.com/luizbarboza/polyclip-ts)for performing geometric operations on polygons and multi-polygons, including union, intersection, XOR, and difference. It supports input and output in GeoJSON format and leverages a sweep line algorithm for efficient computation. The library uses Brick\\Math for high-precision arithmetic, ensuring accurate handling of large or small coordinates, and SplayTree for efficient data management.

Installation
------------

[](#installation)

Install Polyclip with composer. Run:

```
composer require willvincent/polyclip
```

Dependencies:

- PHP 8.2+ (due to strict typing)
- [brick/math](https://github.com/brick/math) for big decimal arithmetic
- [willvincent/splaytree](https://github.com/willvincent/splay-tree) for efficient splay tree operations

Usage
-----

[](#usage)

### Basic Operations

[](#basic-operations)

The Clipper class provides static methods to perform geometric operations. You can pass multiple geometries as separate arguments or as a single FeatureCollection. Each method returns a `GeoJson\Feature\Feature` object with the resulting geometry and empty properties.

#### Union: Combines multiple geometries into a single geometry.

[](#union-combines-multiple-geometries-into-a-single-geometry)

```
use Polyclip\Clipper;
use GeoJson\Geometry\Polygon;

$polygon1 = new Polygon([[[0, 0], [2, 0], [2, 2], [0, 2], [0, 0]]]);
$polygon2 = new Polygon([[[1, 1], [3, 1], [3, 3], [1, 3], [1, 1]]]);

$result = Clipper::union($polygon1, $polygon2);
echo json_encode($result); // Outputs the resulting GeoJSON Feature
```

#### Intersection: Computes the overlapping area of multiple geometries.

[](#intersection-computes-the-overlapping-area-of-multiple-geometries)

```
$result = Clipper::intersection($polygon1, $polygon2);
```

#### XOR: Computes the symmetric difference (exclusive or) between geometries.

[](#xor-computes-the-symmetric-difference-exclusive-or-between-geometries)

```
$result = Clipper::xor($polygon1, $polygon2);
```

#### Difference: Subtracts one or more geometries from a subject geometry.

[](#difference-subtracts-one-or-more-geometries-from-a-subject-geometry)

```
$subject = new Polygon([[[0, 0], [2, 0], [2, 2], [0, 2], [0, 0]]]);
$clip = new Polygon([[[1, 1], [3, 1], [3, 3], [1, 3], [1, 1]]]);

$result = Clipper::difference($subject, $clip);
```

Using a FeatureCollection:

```
use GeoJson\Feature\FeatureCollection;
use GeoJson\Feature\Feature;

$collection = new FeatureCollection([
    new Feature($polygon1),
    new Feature($polygon2),
]);

$result = Clipper::union($collection);
```

### Input Formats

[](#input-formats)

Geometries can be provided in the following formats:

#### GeoJson Objects: Use classes like GeoJson\\Geometry\\Polygon or GeoJson\\Geometry\\MultiPolygon.

[](#geojson-objects-use-classes-like-geojsongeometrypolygon-or-geojsongeometrymultipolygon)

```
$polygon = new Polygon([[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]);
```

#### GeoJSON Strings:

[](#geojson-strings)

```
$geojson = '{"type": "Polygon", "coordinates": [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]}';
```

#### Arrays: Coordinate arrays representing polygons or multi-polygons.

[](#arrays-coordinate-arrays-representing-polygons-or-multi-polygons)

```
$polygonArray = [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]; // Single polygon
$multiPolygonArray = [
    [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]], // Polygon 1
    [[[2, 2], [3, 2], [3, 3], [2, 3], [2, 2]]]  // Polygon 2
];
```

### Setting Precision

[](#setting-precision)

To handle floating-point precision issues (e.g., near-coincident points), set an epsilon value for comparisons:

```
Clipper::setPrecision(0.0001); // Sets the precision for all subsequent operations
```

This epsilon determines how close two points can be before they’re considered equal, which is crucial for robust geometric computations.

### Error Handling

[](#error-handling)

Ensure your input geometries are valid. The library throws \\InvalidArgumentException for:

- Invalid GeoJSON strings
- Features without geometries
- Unsupported geometry types (only Polygon and MultiPolygon are supported)
- Coordinate arrays that don’t form valid polygons

Wrap calls in a try-catch block if needed:

```
try {
    $result = Clipper::union($geom1, $geom2);
} catch (\InvalidArgumentException $e) {
    echo "Error: " . $e->getMessage();
}
```

Notes
-----

[](#notes)

- Input Requirements: Polygons must be simple (non-self-intersecting). Self-intersecting polygons may produce incorrect results.
- Holes: Polygons can include interior rings (holes).
- Properties: Output features have empty properties; input properties are not preserved.
- Precision: Internal calculations use BigDecimal for accuracy, but output coordinates are floats (as per GeoJSON standard).
- Performance: The sweep line algorithm is efficient, but performance varies with polygon size and complexity.
- There are still discrepancies between this package and the output from polyclip-ts, more tests need to be implemented, and additional logic may be needed for edge cases, but functionality is in place and working for not overly complex use cases.

Limitations
-----------

[](#limitations)

- Supports only Polygon and MultiPolygon geometries (no points, lines, etc.).
- Assumes simple polygons; self-intersections are not handled.
- Very complex polygons with many intersections may impact performance.

Contributing
------------

[](#contributing)

Contributions are welcome! Feel free to:

- Report issues or bugs on the GitHub repository.
- Submit pull requests with improvements or fixes.

Please include tests and documentation updates with your contributions.

License
-------

[](#license)

This project is [MIT Licensed](LICENSE.md)

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance41

Moderate activity, may be stable

Popularity24

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

2

Last Release

490d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0c6239b14bdb77aba00cd0bc65f561ad8dd694ec4a18508c69eff119352d54fa?d=identicon)[willvincent](/maintainers/willvincent)

---

Top Contributors

[![willvincent](https://avatars.githubusercontent.com/u/689891?v=4)](https://github.com/willvincent "willvincent (22 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/willvincent-polyclip/health.svg)

```
[![Health](https://phpackages.com/badges/willvincent-polyclip/health.svg)](https://phpackages.com/packages/willvincent-polyclip)
```

###  Alternatives

[laravel/framework

The Laravel Framework.

34.8k543.8M20.0k](/packages/laravel-framework)[ramsey/uuid

A PHP library for generating and working with universally unique identifiers (UUIDs).

12.6k745.0M4.0k](/packages/ramsey-uuid)[brick/money

Money and currency library

1.9k41.8M155](/packages/brick-money)[illuminate/database

The Illuminate Database package.

2.8k54.9M11.6k](/packages/illuminate-database)[flow-php/etl

PHP ETL - Extract Transform Load - Abstraction

378604.0k102](/packages/flow-php-etl)[illuminate/validation

The Illuminate Validation package.

18838.2M1.7k](/packages/illuminate-validation)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
