Home

Awesome

Brick\Geo

<img src="https://raw.githubusercontent.com/brick/brick/master/logo.png" alt="" align="left" height="64">

A GIS geometry library for PHP.

Build Status Coverage Status Latest Stable Version Total Downloads License

Introduction

This library is a PHP implementation of the OpenGIS specification.

It provides Geometry classes (Point, LineString, Polygon, etc.), and can natively read/write many formats: WKB, WKT, EWKB, EWKT, and GeoJSON.

It also provides a GeometryEngine interface for advanced calculations (length, area, union, intersection, etc.), together with implementations that delegate these operations to a third-party GIS engine: the GEOS extension, or a GIS-enabled database such as MySQL or PostgreSQL.

Requirements and installation

This library requires PHP 8.1. For PHP 8.0, you can use version 0.9. For PHP 7.4, you can use version 0.7.

Install the library with Composer:

composer require brick/geo

If you only need basic operations such as building Geometry objects, importing from / exporting to one of the supported formats (WKB, WKT, EWKB, EWKT, or GeoJSON), then you're all set.

If you need advanced features, such as length(), union(), intersection, etc., head on to the Configuration section to choose a GeometryEngine implementation.

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.11.*.

If you need to upgrade to a newer release cycle, check the release history for a list of changes introduced by each further 0.x.0 version.

Quick start

use Brick\Geo\LineString;
use Brick\Geo\Point;
use Brick\Geo\Polygon;

// Building geometries from coordinates

$lineString = LineString::of(
    Point::xy(1, 2),
    Point::xy(3, 4),
);

echo $lineString->asText(); // LINESTRING (1 2, 3 4)

// Importing geometries

$point = Point::fromText('POINT (1 2)');

echo $point->x(); // 1
echo $point->y(); // 2

// Using advanced calculations from a GeometryEngine
// (see the Configuration section)

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

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

Configuration

Advanced calculations are available through the GeometryEngine interface. The library ships with the following implementations:

Your choice for the right implementation should be guided by two criteria:

You should probably start with the easiest method that works for you, and test if this setup matches your expectations.

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

Using PDO and MySQL 5.6 or greater

<details> <summary>Click to expand</summary>

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

</details>

Using PDO and MariaDB 5.5 or greater

<details> <summary>Click to expand</summary>

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.

</details>

Using PDO and PostgreSQL with PostGIS

<details> <summary>Click to expand</summary>

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

</details>

Using PDO and SQLite with SpatiaLite

<details> <summary>Click to expand</summary>

Due to limitations in the PDO_SQLITE driver, it is currently not possible<sup>*</sup> 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.

<sup>* It actually is possible, using moxio/sqlite-extended-api, which uses FFI and Z-Engine, but beware that this library is still experimental!</sup>

</details>

Using SQLite3 with SpatiaLite

<details> <summary>Click to expand</summary>

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

</details>

Using GEOS PHP bindings

<details> <summary>Click to expand</summary> </details>

Geometry hierarchy

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

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:

GeometryEngine methods reference

This is a list of all methods available in the GeometryEngine interface. Some methods are only available if you use a specific geometry engine, sometimes with a minimum version.

Function NameGEOSPostGISMySQLMariaDBSpatiaLite
area
azimuth
boundary
buffer
centroid
contains
convexHull5.7.6
crosses
difference
disjoint
distance
envelope
equals
intersection
intersects
isClosed
isRing
isSimple
isValid5.7.6
length
locateAlong
locateBetween
makeValid
maxDistance
overlaps
pointOnSurface
relate
simplify5.7.64.1.0
snapToGrid
split
symDifference
touches
transform8.0.13
union
within

Importing and exporting geometries

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

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 and WKTWriter classes directly; the latter allows you to pretty-print the output.

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 and WKBWriter classes directly; the latter allows you to choose the endianness of the output (big endian or little endian).

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 and EWKTWriter 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

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 and EWKBWriter 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 is an open standard format designed for representing simple geographical features, based on JSON, and standardized in RFC 7946.

This library supports importing geometries from, and exporting them to GeoJSON documents using the GeoJSONReader and GeoJSONWriter 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]}

The library supports reading and writing Feature and FeatureCollection objects, together with custom properties.

GeoJSON aims to support WGS84 only, and as such all Geometries are imported using SRID 4326.

Reducing coordinate precision

Before exporting geometries in a text format, you may need to reduce the precision of the coordinates to keep the output small, while retaining a sufficient precision. You can use the RoundCoordinatesProjector for this:

use Brick\Geo\Point;
use Brick\Geo\Projector\RoundCoordinatesProjector;

$roundProjector = new RoundCoordinatesProjector(2);

$point = Point::xy(1.2345678, 2.3456789);
echo $point->asText(); // POINT (1.2345678 2.3456789)

$roundedPoint = $point->project($roundProjector);
echo $roundedPoint->asText(); // POINT (1.23 2.35)

Doctrine mappings

You can use brick/geo types in your Doctrine entities using the brick/geo-doctrine package.