PHPackages                             jsor/doctrine-postgis - 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. [Database &amp; ORM](/categories/database)
4. /
5. jsor/doctrine-postgis

ActiveLibrary[Database &amp; ORM](/categories/database)

jsor/doctrine-postgis
=====================

Spatial and Geographic Data with PostGIS and Doctrine.

v2.4.0(7mo ago)2191.6M—0.7%60[4 issues](https://github.com/jsor/doctrine-postgis/issues)1MITPHPPHP ^8.1CI passing

Since Aug 11Pushed 4mo ago10 watchersCompare

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

READMEChangelog (10)Dependencies (9)Versions (20)Used By (1)

PostGIS extension for Doctrine
==============================

[](#postgis-extension-for-doctrine)

[![Build Status](https://github.com/jsor/doctrine-postgis/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/jsor/doctrine-postgis/actions/workflows/ci.yml)[![Coverage Status](https://camo.githubusercontent.com/2b8f9904afa5cda1c45766d89d783f98a6be63eba35df62215731e4bafa920ef/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6a736f722f646f637472696e652d706f73746769732f62616467652e7376673f6272616e63683d6d61696e26736572766963653d676974687562)](https://coveralls.io/github/jsor/doctrine-postgis?branch=main)

This library allows you to use [Doctrine](https://www.doctrine-project.org/)(ORM or DBAL) with [PostGIS](https://postgis.net/), the spatial database extension for [PostgreSQL](https://www.postgresql.org/).

- [Supported Versions](#supported-versions)
- [Installation](#installation)
- [Setup](#setup)
    - [Symfony](#symfony)
- [Property Mapping](#property-mapping)
- [Spatial Indexes](#spatial-indexes)
- [Schema Tool](#schema-tool)
- [DQL Functions](#dql-functions)
- [Known Problems](#known-problems)
- [Running the Tests](#running-the-tests)

Supported Versions
------------------

[](#supported-versions)

The following table shows the versions which are officially supported by this library.

DependencySupported VersionsPostGIS&gt;= 3.2PostgreSQL&gt;= 14Doctrine ORM^2.19 and ^3.0Doctrine DBAL^3.7 and ^4.0Installation
------------

[](#installation)

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

```
composer require jsor/doctrine-postgis
```

Check the [Packagist page](https://packagist.org/packages/jsor/doctrine-postgis)for all available versions.

Setup
-----

[](#setup)

Basic setup requires registering Middleware and the SchemaManagerFactory via the DBAL connection configuration.

```
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\PgSQL\Driver;
use Jsor\Doctrine\PostGIS\Driver\Middleware;
use Jsor\Doctrine\PostGIS\Event\ORMSchemaEventListener;
use Jsor\Doctrine\PostGIS\Schema\SchemaManagerFactory;

$params = [
    // your connection parameters …
];
$config = new Configuration();
$config->setMiddlewares([new Middleware]);
$config->setSchemaManagerFactory(new SchemaManagerFactory());

$connection = new Connection($params, new Driver(), $config);
```

Additionally, to also use the library with the Doctrine ORM, register the `ORMSchemaEventListener` event subscriber.

```
use Doctrine\ORM\Tools\ToolEvents;
use Jsor\Doctrine\PostGIS\Event\ORMSchemaEventListener;

$entityManager->getEventManager()->addEventListener(ToolEvents::postGenerateSchemaTable, new ORMSchemaEventListener());
```

### Symfony

[](#symfony)

For integrating this library into a Symfony project, read the dedicated [Symfony Documentation](docs/symfony.md).

Property Mapping
----------------

[](#property-mapping)

Once the event subscriber is registered, the column types `geometry` and `geography` can be used in property mappings (please read the [PostGIS docs](https://postgis.net/docs/using_postgis_dbmanagement.html#PostGIS_Geography)to understand the difference between these two types).

```
use Doctrine\ORM\Mapping as ORM;
use Jsor\Doctrine\PostGIS\Types\PostGISType;

#[ORM\Entity]
class MyEntity
{
    #[ORM\Column(type: PostGISType::GEOMETRY)]
    private string $geometry;

    #[ORM\Column(type: PostGISType::GEOGRAPHY)]
    private string $geography;
}
```

There are 2 options to configure the geometry.

- `geometry_type`This defines the type of the geometry, like `POINT`, `LINESTRING` etc. If you omit this option, the generic type `GEOMETRY` is used.
- `srid`This defines the Spatial Reference System Identifier (SRID) of the geometry.

### Example

[](#example)

```
use Doctrine\ORM\Mapping as ORM;
use Jsor\Doctrine\PostGIS\Types\PostGISType;

#[ORM\Entity]
class MyEntity
{
    #[ORM\Column(
        type: PostGISType::GEOMETRY,
        options: ['geometry_type' => 'POINT'],
    )]
    public string $point;

    #[ORM\Column(
        type: PostGISType::GEOMETRY,
        options: ['geometry_type' => 'POINTZM'],
   )]
    public string $point4D;

    #[ORM\Column(
        type: PostGISType::GEOMETRY,
        options: ['geometry_type' => 'POINT', 'srid' => 3785],
    )]
    public string $pointWithSRID;

    public function __construct(
        string $point,
        string $point4D,
        string $pointWithSRID,
    ) {
        $this->point = $point;
        $this->point4D = $point4D;
        $this->pointWithSRID = $pointWithSRID;
    }
}
```

Values provided for the properties must be in the [WKT](https://en.wikipedia.org/wiki/Well-known_text)format. Please note, that the values returned from database may differ from the values you have set. The library uses [ST\_AsEWKT](https://postgis.net/docs/ST_AsEWKT.html)to retain as much information as possible (like SRID's). Read more in the [PostGIS docs](https://postgis.net/docs/using_postgis_dbmanagement.html#RefObject).

### Example

[](#example-1)

```
$entity = new MyEntity(
    point: 'POINT(-122.0845187 37.4220761)',
    point4D: 'POINT(1 2 3 4)',
    pointWithSRID: 'SRID=3785;POINT(-122.0845187 37.4220761)',
);
```

Spatial Indexes
---------------

[](#spatial-indexes)

[Spatial indexes](https://postgis.net/docs/using_postgis_dbmanagement.html#gist_indexes)can be defined for geometry fields by setting the `spatial` flag.

```
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Index(
    fields: ['pointWithSRID'],
    flags: ['spatial'],
)]
class MyEntity
{
}
```

Schema Tool
-----------

[](#schema-tool)

Full support for the [ORM Schema Tool](https://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/tools.html)and the [DBAL Schema Manager](https://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/schema-manager.html)is provided.

DQL Functions
-------------

[](#dql-functions)

Most [PostGIS functions](https://postgis.net/docs/reference.html) are also available for the [Doctrine Query Language](https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html)(DQL) under the `Jsor\Doctrine\PostGIS\Functions` namespace.

For a full list of all supported functions, see the [Function Index](docs/function-index.md).

> Read the dedicated [Symfony documentation](docs/symfony.md#dql-functions) on how to configure the functions with Symfony.

The functions must be registered with the `Doctrine\ORM\Configuration` instance.

```
$configuration = new Doctrine\ORM\Configuration();

$configuration->addCustomStringFunction(
    'ST_Within',
    Jsor\Doctrine\PostGIS\Functions\ST_Within::class
);

$configuration->addCustomNumericFunction(
    'ST_Distance',
    Jsor\Doctrine\PostGIS\Functions\ST_Distance::class
);

$dbParams = [/***/];
$entityManager = Doctrine\ORM\EntityManager::create($dbParams, $configuration);
```

There's a convenience Configurator class which can be used to register all functions at once.

```
$configuration = new Doctrine\ORM\Configuration();

Jsor\Doctrine\PostGIS\Functions\Configurator::configure($configuration);

$dbParams = [/***/];
$entityManager = Doctrine\ORM\EntityManager::create($dbParams, $configuration);
```

Known Problems
--------------

[](#known-problems)

> Read the dedicated [Symfony documentation](docs/symfony.md#known-problems) on how to handle those problems with Symfony.

### PostGIS Schema Exclusion

[](#postgis-schema-exclusion)

Since PostGIS can add a few new schemas, like `topology`, `tiger` and `tiger_data`, you might want to exclude them from being handled by Doctrine.

This can be done by configuring a schema assets filter.

```
$configuration = new Doctrine\ORM\Configuration();

$configuration->setSchemaAssetsFilter(static function ($assetName): bool {
     if ($assetName instanceof AbstractAsset) {
         $assetName = $assetName->getName();
     }

     return (bool) preg_match('/^(?!tiger)(?!topology)/', $assetName);
});

$dbParams = [/***/];
$entityManager = Doctrine\ORM\EntityManager::create($dbParams, $configuration);
```

### Unknown Database Types

[](#unknown-database-types)

Sometimes, the schema tool stumbles upon database types it can't handle. A common exception is something like

```
Doctrine\DBAL\Exception: Unknown database type _text requested, Doctrine\DBAL\Platforms\PostgreSQL100Platform may not support it.

```

To solve this, the unknown database types can be mapped to known types.

```
$configuration = new Doctrine\ORM\Configuration();

$dbParams = [/***/];
$entityManager = Doctrine\ORM\EntityManager::create($dbParams, $configuration);

$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('_text', 'string');
```

**Note:** This type is then not suited to be used in entity mappings. It just prevents "Unknown database type..." exceptions thrown during database inspections by the schema tool.

If you want to use this type in your entities, you have to configure real database types, e.g. with the [PostgreSQL for Doctrine](https://github.com/martin-georgiev/postgresql-for-doctrine)package.

Running the Tests
-----------------

[](#running-the-tests)

A simple Docker setup is included to run the test suite against the different PostgreSQL / PostGIS combinations.

All commands here should be run from the project root.

First, build the PHP container. This must be done only once.

```
./docker/build-php.sh
```

Install dependencies via Composer.

```
./docker/run-php.sh composer install
```

Next, start the database containers.

```
docker compose -f ./docker/docker-compose.yml up -d
```

There are a number of shortcut scripts available to execute commands inside the PHP container connected to specific database containers.

The script names follow the pattern `run--.sh`.

To run the test suite against PostgreSQL 18 with PostGIS 3.6, use the script `./docker/run-18-36.sh`.

Tests are either PostGIS version specific (versioned) or agnostic, and are run separately using PHPUnit groups.

e.g. for PostGIS 3.6 and PostgreSQL 18

```
./docker/run-18-36.sh vendor/bin/phpunit --exclude-group=versioned
./docker/run-18-36.sh vendor/bin/phpunit --group=postgis-3.6
```

e.g. for PostGIS 3.2 and PostgreSQL 14

```
./docker/run-14-32.sh vendor/bin/phpunit --exclude-group=versioned
./docker/run-14-32.sh vendor/bin/phpunit --group=postgis-3.2
```

License
-------

[](#license)

Copyright (c) 2014-2024 Jan Sorgalla. Released under the [MIT License](LICENSE).

###  Health Score

63

—

FairBetter than 99% of packages

Maintenance69

Regular maintenance activity

Popularity59

Moderate usage in the ecosystem

Community28

Small or concentrated contributor base

Maturity81

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 92.8% 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 ~218 days

Recently: every ~365 days

Total

18

Last Release

221d ago

Major Versions

1.x-dev → v2.0.02021-10-07

PHP version history (5 changes)v1.0.0PHP &gt;=5.3.2

v1.7.0PHP ^5.6 || ^7.0

v1.8.0PHP ^5.6 || ^7.0 || ^8.0

v2.0.0PHP ^8.0

v2.4.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/5de14776bbddf901c6e24d35829fe66fe997c303d53aca83cc7d1a90bb0b7110?d=identicon)[jsor](/maintainers/jsor)

---

Top Contributors

[![jsor](https://avatars.githubusercontent.com/u/55574?v=4)](https://github.com/jsor "jsor (269 commits)")[![sasa-b](https://avatars.githubusercontent.com/u/18427949?v=4)](https://github.com/sasa-b "sasa-b (4 commits)")[![maxhelias](https://avatars.githubusercontent.com/u/12966574?v=4)](https://github.com/maxhelias "maxhelias (3 commits)")[![carusogabriel](https://avatars.githubusercontent.com/u/16328050?v=4)](https://github.com/carusogabriel "carusogabriel (2 commits)")[![dragosprotung](https://avatars.githubusercontent.com/u/1081073?v=4)](https://github.com/dragosprotung "dragosprotung (2 commits)")[![GwendolenLynch](https://avatars.githubusercontent.com/u/1427081?v=4)](https://github.com/GwendolenLynch "GwendolenLynch (2 commits)")[![paolostefan](https://avatars.githubusercontent.com/u/1904907?v=4)](https://github.com/paolostefan "paolostefan (2 commits)")[![maxperrimond](https://avatars.githubusercontent.com/u/2749829?v=4)](https://github.com/maxperrimond "maxperrimond (1 commits)")[![yfrommelt](https://avatars.githubusercontent.com/u/44576727?v=4)](https://github.com/yfrommelt "yfrommelt (1 commits)")[![cheack](https://avatars.githubusercontent.com/u/668493?v=4)](https://github.com/cheack "cheack (1 commits)")[![simonwelsh](https://avatars.githubusercontent.com/u/125915?v=4)](https://github.com/simonwelsh "simonwelsh (1 commits)")[![teohhanhui](https://avatars.githubusercontent.com/u/548843?v=4)](https://github.com/teohhanhui "teohhanhui (1 commits)")[![ltsstar](https://avatars.githubusercontent.com/u/1613646?v=4)](https://github.com/ltsstar "ltsstar (1 commits)")

---

Tags

databasedoctrinegeographygeometryphppostgispostgrespostgresqlspatialdatabaseormdoctrinedbalgeometrygeographypostgisgisspatialgeo

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jsor-doctrine-postgis/health.svg)

```
[![Health](https://phpackages.com/badges/jsor-doctrine-postgis/health.svg)](https://phpackages.com/packages/jsor-doctrine-postgis)
```

###  Alternatives

[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)[creof/doctrine2-spatial

Doctrine2 multi-platform support for spatial types and functions

2763.3M11](/packages/creof-doctrine2-spatial)[longitude-one/doctrine-spatial

Doctrine multi-platform support for spatial types and functions, compliant with Doctrine 2.19, 3.1, and dev ones (3.2 and 4.0).

891.4M1](/packages/longitude-one-doctrine-spatial)[vlucas/spot2

Simple DataMapper built on top of Doctrine DBAL

605392.8k7](/packages/vlucas-spot2)[williarin/wordpress-interop

Interoperability library to work with WordPress database in third party apps

6610.9k2](/packages/williarin-wordpress-interop)

PHPackages © 2026

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