PHPackages                             lyhty/geometry - 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. lyhty/geometry

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

lyhty/geometry
==============

Geometry for Laravel.

v0.7.0(1y ago)35.9k↓21.4%MITPHPPHP ^8.2

Since Sep 10Pushed 1y agoCompare

[ Source](https://github.com/lyhty/geometry)[ Packagist](https://packagist.org/packages/lyhty/geometry)[ RSS](/packages/lyhty-geometry/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (14)Used By (0)

 [![](https://camo.githubusercontent.com/b63d2735d2bc13e91a3e28b7dff7c3a01860a2bf4ea44d65bb0a05a8ceeaf511/68747470733a2f2f6d617474692e73756f72616e69656d692e636f6d2f73746f726167652f6c796874792d67656f6d657472792e706e67)](https://camo.githubusercontent.com/b63d2735d2bc13e91a3e28b7dff7c3a01860a2bf4ea44d65bb0a05a8ceeaf511/68747470733a2f2f6d617474692e73756f72616e69656d692e636f6d2f73746f726167652f6c796874792d67656f6d657472792e706e67)

[![Latest Version on Packagist](https://camo.githubusercontent.com/6a8bbf14b54785a7a58b963d7b2198eadc2ff44a1477d22130ff367314ba586a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c796874792f67656f6d657472792e7376673f6c6162656c3d267374796c653d666c61742d737175617265266c6f676f3d7061636b6167697374266c6f676f436f6c6f723d7768697465)](https://packagist.org/packages/lyhty/geometry)[![PHP](https://camo.githubusercontent.com/a84876d82c9bc7fb80d2c25c350649c66f6a53ae37d0a58247ed9c0ec94e574a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6c796874792f67656f6d657472793f7374796c653d666c61742d737175617265266c6162656c3d266c6f676f3d706870266c6f676f436f6c6f723d7768697465)](https://packagist.org/packages/lyhty/geometry)[![Laravel](https://camo.githubusercontent.com/ae586ea7917c7a64381ff9fefc6a11a1cd60534de92513caa5110c889504fcd0/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f6c6162656c3d266d6573736167653d2535453131253230253743253230253545313226636f6c6f723d726564267374796c653d666c61742d737175617265266c6f676f3d6c61726176656c266c6f676f436f6c6f723d7768697465)](https://packagist.org/packages/lyhty/geometry)[![Total Downloads](https://camo.githubusercontent.com/f9cbe7e4acc8d20a33a391173deb946a97a883f25d0f05c53990b8f595fc6520/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c796874792f67656f6d657472792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/lyhty/geometry)[![License](https://camo.githubusercontent.com/026b1d3c2f021cfa85ca404d62ea9f398773f708c6deeaf7e4d871ad8babd3a6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6c796874792f67656f6d657472792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/lyhty/geometry)

This package provides tools to parse and write various formats of Geometry data, with a possibility to extend it with custom adapters.

The package is greatly inspired by and many things are borrowed from the following packages: [geoPHP](https://github.com/phayes/geoPHP) by [@phayes](https://github.com/phayes) and [laravel-mysql-spatial](https://github.com/grimzy/laravel-mysql-spatial) by [@grimzy](https://github.com/grimzy).

The package also provides a Model trait, that adds the OGC standard spatial functions to the Model and its Query builder.

Many of the package's functionalities relies on [`geos`](https://libgeos.org) being installed on your system, along with [`php-geos`](https://git.osgeo.org/gitea/geos/php-geos.git) PHP extension. The package however DOES NOT require this, but many of the features will be unavailable.

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

[](#installation)

Install the package with Composer:

```
composer require lyhty/geometry

```

The package registers itself automatically.

Geometry Models
---------------

[](#geometry-models)

This package comes with classes for following geometry types:

- `Point`: `Lyhty\Geometry\Types\Point`
- `LineString`: `Lyhty\Geometry\Types\LineString`
- `Polygon`: `Lyhty\Geometry\Types\Polygon`
- `MultiPoint`: `Lyhty\Geometry\Types\MultiPoint`
- `MultiLineString`: `Lyhty\Geometry\Types\MultiLineString`
- `MultiPolygon`: `Lyhty\Geometry\Types\MultiPolygon`
- `GeometryCollection`: `Lyhty\Geometry\Types\GeometryCollection`

All these classes extend an abstract class `Lyhty\Geometry\Types\Geometry`. Additionally, all classes except for type `Point` extend `Lyhty\Geometry\Types\Collection` class.

With these classes you can easily create your own geometry objects. For example:

```
$p1 = new Point(0, 4);
$p2 = new Point(1, 5);
$p3 = new Point(2, 6);
$p4 = new Point(3, 7);

$ls1 = new LineString([$p1, $p2]);
$ls2 = new LineString([$p3, $p4]);
$mp = new MultiPoint([$p1, $p2, $p3, $p4]);

$g = new Polygon([$ls1, $ls2]);
$c = new GeometryCollection([$p1, $ls1, $mp, $p1]);
```

### Predicates

[](#predicates)

> ¹ Feature requires `geos`
> ² Feature utilizes `geos` if available

These models comes with various predicate methods:

- `Geometry::contains(Geometry $geometry): bool`¹
    - Return boolean value whether the geometry contains with the given geometry.
- `Geometry::covers(Geometry $geometry): bool`¹
    - Return boolean value whether the geometry covers the given geometry.
- `Geometry::coveredBy(Geometry $geometry): bool`¹
    - Return boolean value whether the geometry is covered by the given geometry.
- `Geometry::crosses(Geometry $geometry): bool`¹
    - Return boolean value whether the geometry crosses with the given geometry.
- `Geometry::disjoint(Geometry $geometry): bool`¹
    - Return boolean value whether the geometry disjoint with the given geometry.
- `Geometry::equals(Geometry $geometry): bool`¹
    - Return boolean value whether the geometry is “spatially equal” to the given Geometry.
- `Geometry::equalsExact(Geometry $geometry): bool`¹
    - Return boolean value whether this gemometric object is exactly the same as another object, including the ordering of component parts.
- `Geometry::intersects(Geometry $geometry): bool`¹
    - Return boolean value whether the geometry intersects with the given geometry.
- `Geometry::overlaps(Geometry $geometry): bool`¹
    - Return boolean value whether the geometry overlaps with the given geometry.
- `Geometry::touches(Geometry $geometry): bool`¹
    - Return boolean value whether the geometry touches with the given geometry.
- `Geometry::within(Geometry $geometry): bool`¹
    - Return boolean value whether the geometry is within the given geometry.

### Operations

[](#operations)

> ¹ Feature requires `geos`
> ² Feature utilizes `geos` if available

- `Geometry::area(): float`
    - The area of this Polygon (or GeometryCollection), as measured in the spatial reference system of the geometry.
- `Geometry::boundary(): LineString|Point`
    - Returns the closure of the combinatorial boundary of this geometric object.
- `Geometry::buffer(float $distance): Geometry`¹
    - Returns a buffer region around this Geometry having the given width.
- `Geometry::checkValidity(): bool`¹
    - Returns a boolean value whether the geometry is "valid".
- `Geometry::convexHull(): Geometry`
    - Returns a geometric object that represents the convex hull of this geometric object.
- `Geometry::difference(Geometry $geometry): Geometry`
    - Returns a geometric object that represents the Point set difference of this geometric object with the given geometry.
- `Geometry::distance(Geometry $geometry): float`
    - Returns the shortest distance between any two Points in the two geometric objects as calculated in the spatial reference system of this geometric object.
- `Geometry::envelope(): Polygon`²
    - The minimum bounding box for this Geometry, returned as a Geometry.
- `Geometry::greatCircleLength(): float`
    - [https://en.wikipedia.org/wiki/Great-circle\_distance](https://en.wikipedia.org/wiki/Great-circle_distance)
- `Geometry::hausdorffDistance(Geometry $geometry): float`¹
    - [http://en.wikipedia.org/wiki/Hausdorff\_distance](http://en.wikipedia.org/wiki/Hausdorff_distance)
- `Geometry::haversineLength(): float`
    - [https://en.wikipedia.org/wiki/Haversine\_formula](https://en.wikipedia.org/wiki/Haversine_formula)
- `Geometry::intersection(Geometry $geometry): Geometry`¹
    - Returns a geometric object that represents the point set intersection of this geometric object with the given geometry.
- `Geometry::length(): float`
    - Get the length of the geometry in its associated spatial reference.
- `Geometry::relate(Geometry $geometry, $pattern = null)`¹
    - Computes the intersection matrix for the spatial relationship with the given geometry.
- `Geometry::simplify(float $tolerance, bool $preserveTopology = false): Geometry`¹
    - Simplify the geometry using the standard Douglas-Peucker algorithm.
- `Geometry::symDifference(Geometry $geometry): Geometry`¹
    - Returns a geometric object that represents the point set symmetric difference of this geometric object with the given geometry.
- `Geometry::union(Geometry|array $geometry)`¹
    - Returns a geometric object that represents the Point set union of this geometric object with the given geometry.

### Formatting

[](#formatting)

- `Geometry::format(string $format, ...$args): mixed`
    - Outputs the geometry into the specified adapter format. See [adapters](#adapters).
- `Geometry::toWKT(): string`
    - Alias to `Geometry::format('wkt')`
- `Geometry::toText(): string`
    - Alias to `Geometry::toWKT()`
- `Geometry::toBinary(bool $asHex = false): string`
    - Alias to `Geometry::format('wkb', $asHes)`
- `Geometry::toArray()`
    - `Arrayable` interface method. Formats the instance to an array that follows the geojson structure standard.
- `Geometry::toJson($options = 0)`
    - `Jsonable` interface method. Returns a json string of the geometry in geojson format.
- `Geometry::jsonSerialize(): mixed`

### Other notable methods

[](#other-notable-methods)

- `Geometry::centroid(): Point|null`²
    - The mathematical centroid for this geometry as a Point. For polygons, the result is not guaranteed to be interior.
- `Geometry::getBBox(): array|null`²
    - The minimum bounding box for this Geometry, returned as an array.
- `Geometry::pointOnSurface(): Point`¹
    - A Point guaranteed to be within a polygon.
- `Geometry::explode(): array|null`
    - Explode the geometry into an array of LineString instances.

Geometry Factory
----------------

[](#geometry-factory)

The package also provides a powerful class that helps you parse, write and manipulate geometry data.

The class is accessible through a facade `Lyhty\Geometry\Geom`.

### Parsing &amp; Formatting

[](#parsing--formatting)

The parsing and formatting happens with the help of adapters.

- `Geom::parse($data, $type, ...$otherArgs)`
    - Load from an adapter format (like wkt) into a geometry.
- `Geom::format(Geometry $geometry, $format, ...$otherArgs)`
    - Outputs the geometry into the specified adapter format.

#### Adapters

[](#adapters)

Out of the box the package supports parsing and writing with following standards and services:

NameParse SyntaxFormat Syntax[WKT](https://bit.ly/3d1F8b8)`Geom::parse($data, 'wkt')``Geom::format($geom, 'wkt')`EWKT`Geom::parse($data, 'ewkt')``Geom::format($geom, 'ewkt')`[WKB](https://bit.ly/3RQTw4J)`Geom::parse($data, 'wkb', $isHex = false)``Geom::format($geom, 'wkb', $asHex = false)`[EWKB](https://bit.ly/3U1ykLc)`Geom::parse($data, 'ewkb', $isHex = false)``Geom::format($geom, 'ewkb', $asHex = false)`[GeoJSON](https://geojson.org)`Geom::parse($data, 'geo_json')``Geom::format($geom, 'geo_json', $asArray = false)`[KML](https://bit.ly/3RJ9PRg)`Geom::parse($data, 'kml')``Geom::format($geom, 'kml', $namespace = null)`[GPX](https://bit.ly/2vO2vw2)`Geom::parse($data, 'gpx')``Geom::format($geom, 'gpx', $namespace = null)`[GeoRSS](https://bit.ly/3RQGO6b)`Geom::parse($data, 'geo_rss')``Geom::format($geom, 'geo_rss', $namespace = null)`[Google Geocode](https://bit.ly/3eGE6lt)`Geom::parse($address, 'google_geocode', $token = '')``Geom::format($geom, 'google_geocode', $token = '')`[GeoHash](https://bit.ly/3eyAl1j)`Geom::parse($data, 'geo_hash', $asGrid = false)``Geom::format($geom, 'geo_hash', $precision = null)``Lyhty\Geometry\Eloquent\HasGeometryAttributes`
-----------------------------------------------

[](#lyhtygeometryeloquenthasgeometryattributes)

Sometimes in our applications we need to have models with geometry columns set to them. This package provides a trait you can add to your Model, which turns the binary string of a geometry column into an interactive `Lyhty\Geometry\Types\Geometry` instance! All the methods previously listed are now usable with the Model!

You have to list the attributes that are geometry in the Model the following way:

```
class Example extends Model
{
    use HasGeometryAttributes;

    protected array $geometryAttributes = [
        'home_yard_boundary'
    ];
}
```

The trait will override the `Illuminate\Database\Eloquent\Builder` and `Illuminate\Database\Query\Builder`instances within the Model utilizing the trait with the package's own respective Builder classes. This is required so the storing of geometry data to the database will work. Also several geometry query scopes are part of the base query builder.

### Query scopes

[](#query-scopes)

- `Builder::selectDistanceValue($column, Geometry $geometry)`
- `Builder::selectDistanceSphereValue($column, Geometry $geometry)`
- `Builder::whereDistance($column, Geometry $geometry, $distance)`
- `Builder::whereDistanceSphere($column, Geometry $geometry, $distance)`
- `Builder::whereDistanceExcludingSelf($column, Geometry $geometry, $distance)`
- `Builder::whereDistanceSphereExcludingSelf($column, Geometry $geometry, $distance)`
- `Builder::whereWithin($column, Geometry $geometry)`
- `Builder::whereCrosses($column, Geometry $geometry)`
- `Builder::whereContains($column, Geometry $geometry)`
- `Builder::whereDisjoint($column, Geometry $geometry)`
- `Builder::whereEquals($column, Geometry $geometry)`
- `Builder::whereIntersects($column, Geometry $geometry)`
- `Builder::whereOverlaps($column, Geometry $geometry)`
- `Builder::whereTouches($column, Geometry $geometry)`
- `Builder::orderByDistance($column, Geometry $geometry, $direction = 'asc')`
- `Builder::orderByDistanceSphere($column, Geometry $geometry, $direction = 'asc')`

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance48

Moderate activity, may be stable

Popularity26

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity57

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 ~80 days

Recently: every ~194 days

Total

13

Last Release

386d ago

PHP version history (4 changes)v0.1.0PHP ^8.1

v0.2.0PHP ^8.0.2

v0.6.0PHP ^8.0.2 | ^8.2

v0.7.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/2cd5d6f2caaea83278fd1552c5bdc1b30b4164890ba0801a718dc20efa93738c?d=identicon)[TruecapeDev](/maintainers/TruecapeDev)

---

Top Contributors

[![sirmathays](https://avatars.githubusercontent.com/u/37704147?v=4)](https://github.com/sirmathays "sirmathays (27 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/lyhty-geometry/health.svg)

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

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[orchestra/canvas

Code Generators for Laravel Applications and Packages

21017.2M158](/packages/orchestra-canvas)[kirschbaum-development/commentions

A package to allow you to create comments, tag users and more

12369.2k](/packages/kirschbaum-development-commentions)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)[glhd/special

1929.4k](/packages/glhd-special)[bjuppa/laravel-blog

Add blog functionality to your Laravel project

483.3k2](/packages/bjuppa-laravel-blog)

PHPackages © 2026

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