PHPackages                             webnuvola/geo - 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. webnuvola/geo

ActiveLibrary

webnuvola/geo
=============

GIS geometry library

0.2.3(7y ago)011MITPHPPHP &gt;=7.1

Since Oct 3Pushed 7y ago1 watchersCompare

[ Source](https://github.com/Webnuvola/geo)[ Packagist](https://packagist.org/packages/webnuvola/geo)[ RSS](/packages/webnuvola-geo/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (5)Versions (7)Used By (0)

Brick\\Geo
==========

[](#brickgeo)

[![](https://raw.githubusercontent.com/brick/brick/master/logo.png)](https://raw.githubusercontent.com/brick/brick/master/logo.png)

A collection of classes to work with GIS geometries.

[![Build Status](https://camo.githubusercontent.com/660ca2ff8c6827d51a53f70598bce5c1348cefba346ce8d0478ffac3d00656d7/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f627269636b2f67656f2e7376673f6272616e63683d6d6173746572)](http://travis-ci.org/brick/geo)[![Coverage Status](https://camo.githubusercontent.com/a3c8e7cabba8b203fb43638b02ef4e7031908722ca649c8857106efbf20cb310/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f627269636b2f67656f2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/r/brick/geo?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/50e15ca52cfa5d04971898044f09c520e05fdfe07652d43228ecc6c3bd14e2db/68747470733a2f2f706f7365722e707567782e6f72672f627269636b2f67656f2f762f737461626c65)](https://packagist.org/packages/brick/geo)[![Total Downloads](https://camo.githubusercontent.com/dcc27dc1358372c1568464adf3e9fe18ee01c8b2fb6b40f576c3edd95889a45a/68747470733a2f2f706f7365722e707567782e6f72672f627269636b2f67656f2f646f776e6c6f616473)](https://packagist.org/packages/brick/geo)[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](http://opensource.org/licenses/MIT)

Introduction
------------

[](#introduction)

This library is a PHP implementation of the [OpenGIS specification](http://www.opengeospatial.org/standards/sfa).

It is essentially a wrapper around a third-party GIS engine, to which it delegates most of the complexity of the geometry calculations. Several engines are supported, from native PHP extensions such as GEOS to GIS-compatible databases such as MySQL or PostgreSQL.

Requirements and installation
-----------------------------

[](#requirements-and-installation)

This library requires 7.1 or later. for PHP 5.6 and PHP 7.0 support, use version `0.1`.

Install the library with [Composer](https://getcomposer.org/):

```
composer require brick/geo
```

Then head on to the [Configuration](#configuration) section to configure a GIS geometry engine.

Failure to configure a geometry engine would result in a `GeometryEngineException` being thrown when trying to use a method that requires one.

Project status &amp; release process
------------------------------------

[](#project-status--release-process)

This library is still under development.

The current releases are numbered `0.x.y`. When a non-breaking change is introduced (adding new methods, optimizing existing code, etc.), `y` is incremented.

**When a breaking change is introduced, a new `0.x` version cycle is always started.**

It is therefore safe to lock your project to a given release cycle, such as `0.2.*`.

If you need to upgrade to a newer release cycle, check the [release history](https://github.com/brick/geo/releases) for a list of changes introduced by each further `0.x.0` version.

Configuration
-------------

[](#configuration)

Configuring the library consists in choosing the most convenient `GeometryEngine` implementation for your installation. The following implementations are available:

- `PDOEngine`: communicates with a GIS-compatible database over a `PDO` connection.
    This engine currently supports the following databases:
    - [MySQL](http://php.net/manual/en/ref.pdo-mysql.php) version 5.6 or greater.
        *Note: MySQL currently only supports 2D geometries.*
    - MariaDB version 5.5 or greater.
    - [PostgreSQL](http://php.net/manual/en/ref.pdo-pgsql.php) with the [PostGIS](http://postgis.net/install) extension.
- `SQLite3Engine`: communicates with a [SQLite3](http://php.net/manual/en/book.sqlite3.php) database with the [SpatiaLite](https://www.gaia-gis.it/fossil/libspatialite/index) extension.
- `GEOSEngine`: uses the [GEOS](https://github.com/libgeos/libgeos) PHP bindings.

Following is a step-by-step guide for all the possible configurations:

### Using PDO and MySQL 5.6 or greater

[](#using-pdo-and-mysql-56-or-greater)

- Ensure that your MySQL version is at least `5.6`.
    Earlier versions only have partial GIS support based on bounding boxes and are not supported.
- Use this bootstrap code in your project:

    ```
      use Brick\Geo\Engine\GeometryEngineRegistry;
      use Brick\Geo\Engine\PDOEngine;

      $pdo = new PDO('mysql:host=localhost', 'root', '');
      GeometryEngineRegistry::set(new PDOEngine($pdo));

    ```

Update the code with your own connection parameters, or use an existing `PDO` connection if you have one (recommended).

### Using PDO and MariaDB 5.5 or greater

[](#using-pdo-and-mariadb-55-or-greater)

MariaDB is a fork of MySQL, so you can follow the same procedure as for MySQL. Just ensure that your MariaDB version is `5.5` or greater.

### Using PDO and PostgreSQL with PostGIS

[](#using-pdo-and-postgresql-with-postgis)

- Ensure that [PostGIS is installed](http://postgis.net/install/) on your server
- Enable PostGIS on the database server if needed:

    ```
      CREATE EXTENSION postgis;

    ```
- Use this bootstrap code in your project:

    ```
      use Brick\Geo\Engine\GeometryEngineRegistry;
      use Brick\Geo\Engine\PDOEngine;

      $pdo = new PDO('pgsql:host=localhost', 'postgres', '');
      GeometryEngineRegistry::set(new PDOEngine($pdo));

    ```

Update the code with your own connection parameters, or use an existing `PDO` connection if you have one (recommended).

### Using PDO and SQLite with SpatiaLite

[](#using-pdo-and-sqlite-with-spatialite)

Due to [limitations in the PDO\_SQLITE driver](https://bugs.php.net/bug.php?id=64810), it is currently not possible to load the SpatiaLite extension with a `SELECT LOAD_EXTENSION()` query, hence you cannot use SpatiaLite with the PDO driver.

You need to use the SQLite3 driver instead. Note that you can keep using your existing PDO\_SQLITE code, all you need to do is create an additional in-memory SQLite3 database just to power the geometry engine.

### Using SQLite3 with SpatiaLite

[](#using-sqlite3-with-spatialite)

- Ensure that [SpatiaLite is installed](https://www.gaia-gis.it/fossil/libspatialite/index) on your system.
- Ensure that the SQLite3 extension is enabled in your `php.ini`:

    ```
      extension=sqlite3.so

    ```
- Ensure that the SQLite3 extension dir where SpatiaLite is installed is configured in your `php.ini`:

    ```
      [sqlite3]
      sqlite3.extension_dir = /usr/lib

    ```
- Use this bootstrap code in your project:

    ```
      use Brick\Geo\Engine\GeometryEngineRegistry;
      use Brick\Geo\Engine\SQLite3Engine;

      $sqlite3 = new SQLite3(':memory:');
      $sqlite3->loadExtension('mod_spatialite.so');
      GeometryEngineRegistry::set(new SQLite3Engine($sqlite3));

    ```

In this example we have created an in-memory database for our GIS calculations, but you can also use an existing `SQLite3` connection.

### Using GEOS PHP bindings

[](#using-geos-php-bindings)

- Ensure that [the PHP bindings for GEOS](https://git.osgeo.org/gogs/geos/php-geos) are installed on your server (GEOS 3.6.0 onwards; previous versions require compiling GEOS with the `--enable-php` flag).
- Ensure that the GEOS extension is enabled in your `php.ini`:

    ```
      extension=geos.so

    ```
- Use this bootstrap code in your project:

    ```
      use Brick\Geo\Engine\GeometryEngineRegistry;
      use Brick\Geo\Engine\GEOSEngine;

      GeometryEngineRegistry::set(new GEOSEngine());

    ```

Geometry hierarchy
------------------

[](#geometry-hierarchy)

All geometry objects reside in the `Brick\Geo` namespace, and extend a base `Geometry` class:

- [Geometry](https://github.com/brick/geo/blob/master/src/Geometry.php) `abstract`
    - [Point](https://github.com/brick/geo/blob/master/src/Point.php)
    - [Curve](https://github.com/brick/geo/blob/master/src/Curve.php) `abstract`
        - [LineString](https://github.com/brick/geo/blob/master/src/LineString.php)
        - [CompoundCurve](https://github.com/brick/geo/blob/master/src/CompoundCurve.php)
        - [CircularString](https://github.com/brick/geo/blob/master/src/CircularString.php)
    - [Surface](https://github.com/brick/geo/blob/master/src/Surface.php) `abstract`
        - [Polygon](https://github.com/brick/geo/blob/master/src/Polygon.php)
            - [Triangle](https://github.com/brick/geo/blob/master/src/Triangle.php)
        - [CurvePolygon](https://github.com/brick/geo/blob/master/src/CurvePolygon.php)
        - [PolyhedralSurface](https://github.com/brick/geo/blob/master/src/PolyhedralSurface.php)
            - [TIN](https://github.com/brick/geo/blob/master/src/TIN.php)
    - [GeometryCollection](https://github.com/brick/geo/blob/master/src/GeometryCollection.php)
        - [MultiPoint](https://github.com/brick/geo/blob/master/src/MultiPoint.php)
        - [MultiCurve](https://github.com/brick/geo/blob/master/src/MultiCurve.php) `abstract`
            - [MultiLineString](https://github.com/brick/geo/blob/master/src/MultiLineString.php)
        - [MultiSurface](https://github.com/brick/geo/blob/master/src/MultiSurface.php) `abstract`
            - [MultiPolygon](https://github.com/brick/geo/blob/master/src/MultiPolygon.php)

Geometry exceptions
-------------------

[](#geometry-exceptions)

All geometry exceptions reside in the `Brick\Geo\Exception` namespace, and extend a base `GeometryException` object.

Geometry exceptions are fine-grained: only subclasses of the base `GeometryException` class are thrown throughout the project. This leaves to the user the choice to catch only specific exceptions, or all geometry-related exceptions.

Here is a list of all exceptions:

- `CoordinateSystemException` is thrown when mixing objects with different SRID or dimensionality (e.g. XY with XYZ)
- `EmptyGeometryException` is thrown when trying to access a non-existent property on an empty geometry
- `GeometryEngineException` is thrown when a functionality is not supported by the current geometry engine
- `GeometryIOException` is thrown when an error occurs while reading or writing (E)WKB/T data
- `InvalidGeometryException` is thrown when creating an invalid geometry, such as a `LineString` with only one `Point`
- `NoSuchGeometryException` is thrown when attempting to get a geometry at an non-existing index in a collection
- `UnexpectedGeometryException` is thrown when a geometry is not an instance of the expected sub-type, for example when calling `Point::fromText()` with a `LineString` WKT.

Example
-------

[](#example)

```
use Brick\Geo\Polygon;

$polygon = Polygon::fromText('POLYGON ((0 0, 0 3, 3 3, 0 0))');
echo $polygon->area(); // 4.5

$centroid = $polygon->centroid();
echo $centroid->asText(); // POINT (1 2)

```

Spatial Function Reference
--------------------------

[](#spatial-function-reference)

This is a list of all functions which are currently implemented in the geo project. Some functions are only available if you use a specific geometry engine, sometimes with a minimum version. This table also shows which functions are part of the OpenGIS standard.

Function NameGEOSPostGISMySQLMariaDBSpatiaLiteOpenGIS standard`area`✓✓✓✓✓✓`boundary`✓✓✓✓`buffer`✓✓✓✓✓✓`centroid`✓✓✓✓✓✓`contains`✓✓✓✓✓✓`convexHull`✓✓5.7.6✓✓`crosses`✓✓✓✓✓✓`difference`✓✓✓✓✓✓`disjoint`✓✓✓✓✓✓`distance`✓✓✓✓✓✓`envelope`✓✓✓✓✓✓`equals`✓✓✓✓✓✓`intersects`✓✓✓✓✓✓`intersection`✓✓✓✓✓✓`isSimple`✓✓✓✓✓✓`isValid`✓✓5.7.6✓`length`✓✓✓✓✓✓`locateAlong`✓✓`locateBetween`✓✓`maxDistance`✓✓`overlaps`✓✓✓✓✓✓`pointOnSurface`✓✓✓✓`relate`✓✓✓✓`simplify`✓✓5.7.64.1.0`snapToGrid`✓✓`symDifference`✓✓✓✓✓✓`touches`✓✓✓✓✓✓`union`✓✓✓✓✓✓`within`✓✓✓✓✓✓Importing and exporting geometries
----------------------------------

[](#importing-and-exporting-geometries)

This library supports importing from and exporting to the following formats:

- WKT
- WKB
- EWKT
- EWKB
- GeoJSON

### WKT

[](#wkt)

Well-Known Text is the standard text format for geometries.

Every Geometry class provides a convenience method `fromText()`, that accepts a WKT string and an optional SRID, and returns a Geometry object:

```
use Brick\Geo\Point;

$point = Point::fromText('POINT (1.5 2.5)', 4326);
```

Geometries can be converted to WKT using the convenience method `asText()`:

```
echo $point->asText(); // POINT (1.5 2.5)
```

You can alternatively use the [WKTReader](https://github.com/brick/geo/blob/master/src/IO/WKTReader.php) and [WKTWriter](https://github.com/brick/geo/blob/master/src/IO/WKTWriter.php) classes directly; the latter allows you to pretty-print the output.

### WKB

[](#wkb)

Well-Known Binary is the standard binary format for geometries.

Every Geometry class provides a convenience method `fromBinary()`, that accepts a WKB binary string and an optional SRID, and returns a Geometry object:

```
use Brick\Geo\Point;

$point = Point::fromBinary(hex2bin('0101000000000000000000f83f0000000000000440'), 4326);

echo $point->asText(); // POINT (1.5 2.5)
echo $point->SRID(); // 4326
```

Geometries can be converted to WKB using the convenience method `asBinary()`:

```
echo bin2hex($point->asBinary()); // 0101000000000000000000f83f0000000000000440
```

You can alternatively use the [WKBReader](https://github.com/brick/geo/blob/master/src/IO/WKBReader.php) and [WKBWriter](https://github.com/brick/geo/blob/master/src/IO/WKBWriter.php) classes directly; the latter allows you to choose the endianness of the output (big endian or little endian).

### EWKT

[](#ewkt)

Extended WKT is a PostGIS-specific text format that includes the SRID of the geometry object, which is missing from the standard WKT format. You can import from and export to this format using the [EWKTReader](https://github.com/brick/geo/blob/master/src/IO/EWKTReader.php) and [EWKTWriter](https://github.com/brick/geo/blob/master/src/IO/EWKTWriter.php) classes:

```
use Brick\Geo\Point;
use Brick\Geo\IO\EWKTReader;
use Brick\Geo\IO\EWKTWriter;

$reader = new EWKTReader();
$point = $reader->read('SRID=4326; POINT (1.5 2.5)');

echo $point->asText(); // POINT (1.5 2.5)
echo $point->SRID(); // 4326

$writer = new EWKTWriter();
echo $writer->write($point); // SRID=4326; POINT (1.5 2.5)
```

### EWKB

[](#ewkb)

Extended WKB is a PostGIS-specific binary format that includes the SRID of the geometry object, which is missing from the standard WKB format. You can import from and export to this format using the [EWKBReader](https://github.com/brick/geo/blob/master/src/IO/EWKBReader.php) and [EWKBWriter](https://github.com/brick/geo/blob/master/src/IO/EWKBWriter.php) classes:

```
use Brick\Geo\Point;
use Brick\Geo\IO\EWKBReader;
use Brick\Geo\IO\EWKBWriter;

$reader = new EWKBReader();
$point = $reader->read(hex2bin('0101000020e6100000000000000000f83f0000000000000440'));

echo $point->asText(); // POINT (1.5 2.5)
echo $point->SRID(); // 4326

$writer = new EWKBWriter();
echo bin2hex($writer->write($point)); // 0101000020e6100000000000000000f83f0000000000000440
```

### GeoJSON

[](#geojson)

GeoJSON is an open standard format designed for representing simple geographical features, based on JSON, and standardized in [RFC 7946](https://tools.ietf.org/html/rfc7946).

This library supports importing geometries from, and exporting them to GeoJSON documents using the [GeoJSONReader](https://github.com/brick/geo/blob/master/src/IO/GeoJSONReader.php) and [GeoJSONWriter](https://github.com/brick/geo/blob/master/src/IO/GeoJSONWriter.php) classes:

```
use Brick\Geo\Point;
use Brick\Geo\IO\GeoJSONReader;
use Brick\Geo\IO\GeoJSONWriter;

$reader = new GeoJSONReader();
$point = $reader->read('{ "type": "Point", "coordinates": [1, 2] }');

echo $point->asText(); // POINT (1 2)
echo $point->SRID(); // 4326

$writer = new GeoJSONWriter();
echo $writer->write($point); // {"type":"Point","coordinates":[1,2]}
```

Note that `Feature`s are imported as `Geometry` objects, and `FeatureCollection`s are imported as `GeometryCollection`objects. Non-spatial attributes are ignored.

GeoJSON aims to support WGS84 only, and as such all Geometries are imported using [SRID 4326](https://epsg.io/4326).

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 94.7% 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 ~107 days

Recently: every ~134 days

Total

6

Last Release

2602d ago

PHP version history (2 changes)0.1.0PHP &gt;=5.6

0.2.0PHP &gt;=7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/bee6fac3ff235d06469b2a8a3afa880ec3e48807f520fa2b98fb9f8a761bcc16?d=identicon)[fab120](/maintainers/fab120)

---

Top Contributors

[![BenMorel](https://avatars.githubusercontent.com/u/1952838?v=4)](https://github.com/BenMorel "BenMorel (446 commits)")[![cevou](https://avatars.githubusercontent.com/u/1201681?v=4)](https://github.com/cevou "cevou (22 commits)")[![fab120](https://avatars.githubusercontent.com/u/293368?v=4)](https://github.com/fab120 "fab120 (2 commits)")[![michaelcurry](https://avatars.githubusercontent.com/u/2658933?v=4)](https://github.com/michaelcurry "michaelcurry (1 commits)")

---

Tags

brickgeometrygeographygisgeo

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/webnuvola-geo/health.svg)

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

###  Alternatives

[brick/geo

GIS geometry library

245862.1k13](/packages/brick-geo)[martin-georgiev/postgresql-for-doctrine

Extends Doctrine with native PostgreSQL support for arrays, JSONB, ranges, PostGIS geometries, text search, ltree, uuid, and 100+ PostgreSQL-specific functions.

4485.3M4](/packages/martin-georgiev-postgresql-for-doctrine)[jsor/doctrine-postgis

Spatial and Geographic Data with PostGIS and Doctrine.

2191.6M1](/packages/jsor-doctrine-postgis)[creof/doctrine2-spatial

Doctrine2 multi-platform support for spatial types and functions

2763.3M11](/packages/creof-doctrine2-spatial)[geokit/geokit

Geo-Toolkit for PHP

251924.7k6](/packages/geokit-geokit)[creof/geo-parser

Parser for geography coordinate strings

624.4M15](/packages/creof-geo-parser)

PHPackages © 2026

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