Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33
GIS geometry library for PHP
License
brick/geo
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A GIS geometry library for PHP.
This library is a PHP implementation of theOpenGIS specification.
It providesGeometry classes (Point
,LineString
,Polygon
, etc.), and can natively read/write many formats: WKB, WKT, EWKB, EWKT, and GeoJSON.
It also provides aGeometryEngine
interface for advanced calculations (length
,area
,union
,intersection
, etc.),together with implementations that delegate these operations to a third-party GIS engine:
- theGEOS extension,
- thegeosop command-line tool,
- or a GIS-enabled database:
- MySQL
- MariaDB
- PostgreSQL with thePostGIS extension
- SQLite with theSpatiaLite extension
This library requires PHP 8.1.For PHP 8.0, you can use version0.9
.For PHP 7.4, you can use version0.7
.
Install the library withComposer:
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 aslength()
,union()
,intersection
, etc., head on to theConfiguration section to choose aGeometryEngine
implementation.
This library is still under development.
The current releases are numbered0.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 new0.x
version cycle is always started.
It is therefore safe to lock your project to a given release cycle, such as0.13.*
.
If you need to upgrade to a newer release cycle, check therelease history for a list of changes introduced by each further0.x.0
version.
useBrick\Geo\LineString;useBrick\Geo\Point;useBrick\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();// 1echo$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)
Advanced calculations are available through theGeometryEngine
interface. The library ships with the following implementations:
PdoEngine
: communicates with a GIS-compatible database over aPDO
connection.
This engine currently supports the following databases:- MySQL version 5.6 or greater (2D geometries only)
- MariaDB version 5.5 or greater
- PostgreSQL with thePostGIS extension
Sqlite3Engine
: communicates with anSQLite3 database with theSpatiaLite extensionGeosEngine
: uses theGEOS PHP extensionGeosOpEngine
: uses thegeosop command-line tool
Your choice for the right implementation should be guided by two criteria:
- availability: if you already use a GIS-enabled database such as MySQL, this may be an easy choice;
- capabilities: not all databases offer the same GIS capabilities:
- some functions may be available on PostgreSQL but not on other databases (see theGeometryEngine methods reference section)
- some functions may be restricted to certain geometry types and/or SRIDs; for example,
buffer()
works on MySQL, but would fail with aPolygon
on SRID 4326 (GPS coordinates, distance in meters) - some databases may return distances in meters on SRID 4326, while others may return distances in degrees
You should probably start with the easiest method that works for you, and test if this setup matches your expectations.
Click on one of the following configurations to see the setup instructions:
UsingPDO andMySQL
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:
useBrick\Geo\Engine\PdoEngine;$pdo =newPDO('mysql:host=localhost','root','');$geometryEngine =newPdoEngine($pdo);
Update the code with your own connection parameters, or use an existingPDO
connection if you have one (recommended).
UsingPDO andMariaDB
- Ensure that your MariaDB version is at least
5.5
. - MariaDB is a fork of MySQL, so you can follow the same procedure as for MySQL.
UsingPDO andPostgreSQL withPostGIS
Ensure thatPostGIS is installed on your server
Enable PostGIS on the database server if needed:
CREATE EXTENSION postgis;
Use this bootstrap code in your project:
useBrick\Geo\Engine\PdoEngine;$pdo =newPDO('pgsql:host=localhost','postgres','');$geometryEngine =newPdoEngine($pdo);
Update the code with your own connection parameters, or use an existingPDO
connection if you have one (recommended).
UsingPDO andSQLite withSpatiaLite
Due tolimitations in the pdo_sqlite driver, it is currently not possible* to load the SpatiaLite extension with aSELECT 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.
* It actuallyis possible, usingmoxio/sqlite-extended-api, which uses FFI andZ-Engine, but beware that this library is still experimental!
UsingSQLite3 withSpatiaLite
Ensure thatSpatiaLite is installed 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:
useBrick\Geo\Engine\Sqlite3Engine;$sqlite3 =newSQLite3(':memory:');$sqlite3->loadExtension('mod_spatialite.so');$geometryEngine =newSqlite3Engine($sqlite3);
Depending on the functions you use, you will probably need to initialize the spatial metadata by running this query:
SELECT InitSpatialMetaData();
You only need to run this query once if your database is persisted, butif your database is in-memory, you'll need to run it on every connection. Be aware that this may hurt performance.
In this example we have created an in-memory database for our GIS calculations, but you can also use an existingSQLite3
connection.
Using theGEOS extension
Ensure that theGEOS extension is installed on your server (GEOS 3.6.0 onwards; previous versions require compiling GEOS with the
--enable-php
flag).Ensure that the extension is enabled in your
php.ini
:extension=geos.so
Use this bootstrap code in your project:
useBrick\Geo\Engine\GeosEngine;$geometryEngine =newGeosEngine();
Using thegeosop
command-line tool
Ensure thatgeosop is installed on your server.You can install it on Fedora with the
geos
package, or on Ubuntu / Debian with thegeos-bin
package.Use this bootstrap code in your project:
useBrick\Geo\Engine\GeosOpEngine;$geometryEngine =newGeosOpEngine('/usr/bin/geosop');
Adjust the path to thegeosop
binary if needed.
Note that every call to theGeosOpEngine
will spawn a new process, which comes with a performance overhead compared to the other engines.
All geometry objects reside in theBrick\Geo
namespace, and extend a baseGeometry
class:
- Geometry
abstract
All geometry exceptions reside in theBrick\Geo\Exception
namespace, and extend a baseGeometryException
object.
Geometry exceptions are fine-grained: only subclasses of the baseGeometryException
class are thrown throughoutthe 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 geometryGeometryEngineException
is thrown when a functionality is not supported by the current geometry engineGeometryIoException
is thrown when an error occurs while reading or writing (E)WKB/T or GeoJSON dataInvalidGeometryException
is thrown when creating an invalid geometry, such as aLineString
with only onePoint
NoSuchGeometryException
is thrown when attempting to get a geometry at a non-existing index in a collectionUnexpectedGeometryException
is thrown when a geometry is not an instance of the expected subtype, for example whencallingPoint::fromText()
with aLineString
WKT.
This is a list of all methods available in theGeometryEngine
interface. Some methods are only availableif you use a specific geometry engine, sometimes with a minimum version.
Function Name | GEOS | geosop | PostGIS | MySQL | MariaDB | SpatiaLite |
---|---|---|---|---|---|---|
area | ✓ | ✓ | ✓ | ✓ | ✓ | |
azimuth | ✓ | ✓ | ||||
boundary | ✓ | ✓ | ✓ | ✓ | ||
buffer | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
centroid | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
contains | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
concaveHull | ✓ | ✓ | ✓ | |||
convexHull | ✓ | ✓ | ✓ | 5.7.6 | ✓ | |
crosses | ✓ | 3.12.0 | ✓ | ✓ | ✓ | ✓ |
difference | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
disjoint | ✓ | 3.12.0 | ✓ | ✓ | ✓ | ✓ |
distance | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
envelope | ✓ | 3.12.0 | ✓ | ✓ | ✓ | ✓ |
equals | ✓ | 3.11.0 | ✓ | ✓ | ✓ | ✓ |
intersection | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
intersects | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
isClosed | ✓ | ✓ | ✓ | ✓ | ✓ | |
isRing | ✓ | ✓ | ✓ | ✓ | ✓ | |
isSimple | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
isValid | ✓ | ✓ | ✓ | 5.7.6 | ✓ | |
length | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
lineInterpolatePoint | ✓ | ✓ | 8.0.24 | ✓ | ||
lineInterpolatePoints | ✓ | 8.0.24 | ||||
locateAlong | ✓ | ✓ | ||||
locateBetween | ✓ | ✓ | ||||
makeValid | ✓ | ✓ | ✓ | |||
maxDistance | ✓ | ✓ | ||||
overlaps | ✓ | 3.12.0 | ✓ | ✓ | ✓ | ✓ |
pointOnSurface | ✓ | ✓ | ✓ | |||
relate | ✓ | ✓ | ✓ | |||
simplify | ✓ | ✓ | 5.7.6 | 4.1.0 | ||
snapToGrid | ✓ | ✓ | ||||
split | ✓ | ✓ | ||||
symDifference | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
touches | ✓ | 3.12.0 | ✓ | ✓ | ✓ | ✓ |
transform | ✓ | ✓ | 8.0.13 | ✓ | ✓ | |
union | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
within | ✓ | 3.12.0 | ✓ | ✓ | ✓ | ✓ |
This library supports importing from and exporting to the following formats:
- WKT
- WKB
- EWKT
- EWKB
- GeoJSON
Well-Known Text is the standard text format for geometries.
Every Geometry class provides a convenience methodfromText()
, that accepts a WKT string and an optional SRID, andreturns a Geometry object:
useBrick\Geo\Point;$point = Point::fromText('POINT (1.5 2.5)',4326);
Geometries can be converted to WKT using the convenience methodasText()
:
echo$point->asText();// POINT (1.5 2.5)
You can alternatively use theWktReader andWktWriter classes directly; the latter allows you topretty-print the output.
Well-Known Binary is the standard binary format for geometries.
Every Geometry class provides a convenience methodfromBinary()
, that accepts a WKB binary string and an optionalSRID, and returns a Geometry object:
useBrick\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 methodasBinary()
:
echobin2hex($point->asBinary());// 0101000000000000000000f83f0000000000000440
You can alternatively use theWkbReader andWkbWriter classes directly; the latter allows you tochoose the endianness of the output (big endian or little endian).
Extended WKT is a PostGIS-specific text format that includes the SRID of the geometry object, which is missing from thestandard WKT format. You can import from and export to this format using theEwktReader andEwktWriter classes:
useBrick\Geo\Point;useBrick\Geo\Io\EwktReader;useBrick\Geo\Io\EwktWriter;$reader =newEwktReader();$point =$reader->read('SRID=4326; POINT (1.5 2.5)');echo$point->asText();// POINT (1.5 2.5)echo$point->srid();// 4326$writer =newEwktWriter();echo$writer->write($point);// SRID=4326; POINT (1.5 2.5)
Extended WKB is a PostGIS-specific binary format that includes the SRID of the geometry object, which is missing fromthe standard WKB format. You can import from and export to this format using theEwkbReader andEwkbWriter classes:
useBrick\Geo\Point;useBrick\Geo\Io\EwkbReader;useBrick\Geo\Io\EwkbWriter;$reader =newEwkbReader();$point =$reader->read(hex2bin('0101000020e6100000000000000000f83f0000000000000440'));echo$point->asText();// POINT (1.5 2.5)echo$point->srid();// 4326$writer =newEwkbWriter();echobin2hex($writer->write($point));// 0101000020e6100000000000000000f83f0000000000000440
GeoJSON is an open standard format designed for representing simple geographical features, based on JSON, andstandardized inRFC 7946.
This library supports importing geometries from, and exporting them to GeoJSON documents using theGeoJsonReader andGeoJsonWriter classes:
useBrick\Geo\Point;useBrick\Geo\Io\GeoJsonReader;useBrick\Geo\Io\GeoJsonWriter;$reader =newGeoJsonReader();$point =$reader->read('{ "type": "Point", "coordinates": [1, 2] }');echo$point->asText();// POINT (1 2)echo$point->srid();// 4326$writer =newGeoJsonWriter();echo$writer->write($point);// {"type":"Point","coordinates":[1,2]}
The library supports reading and writingFeature
andFeatureCollection
objects, together with custom properties.
GeoJSON aims to support WGS84 only, and as such all Geometries are imported usingSRID 4326.
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 thewithRoundedCoordinates()
method for this:
useBrick\Geo\Point;useBrick\Geo\Projector\RoundCoordinatesProjector;$point = Point::xy(1.2345678,2.3456789);echo$point->asText();// POINT (1.2345678 2.3456789)$roundedPoint =$point->withRoundedCoordinates(2);echo$roundedPoint->asText();// POINT (1.23 2.35)
You can usebrick/geo
types in your Doctrine entities using thebrick/geo-doctrine package.
About
GIS geometry library for PHP
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.