2017 © Pedro Peláez
 

library doctrine-postgis

Spatial and Geographic Data with PostGIS and Doctrine.

image

jsor/doctrine-postgis

Spatial and Geographic Data with PostGIS and Doctrine.

  • Sunday, July 15, 2018
  • by jsor
  • Repository
  • 9 Watchers
  • 98 Stars
  • 131,451 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 12 Forks
  • 0 Open issues
  • 14 Versions
  • 10 % Grown

The README.md

PostGIS extension for Doctrine

Build Status Coverage Status, (*1)

This library allows you to use Doctrine (ORM or DBAL) with PostGIS, the spatial database extension for PostgreSQL., (*2)

Supported Versions

The following table shows the versions which are officially supported by this library., (*3)

Dependency Supported Versions
PostGIS 3.0 and 3.1
PostgreSQL 11, 12 and 13
Doctrine ORM ^2.9
Doctrine DBAL ^2.13 and ^3.1

Installation

Install the latest version with Composer., (*4)

composer require jsor/doctrine-postgis

Check the Packagist page for all available versions., (*5)

Setup

To use the library with the Doctrine ORM, register the ORMSchemaEventSubscriber event subscriber., (*6)

use Jsor\Doctrine\PostGIS\Event\ORMSchemaEventSubscriber;

$entityManager->getEventManager()->addEventSubscriber(new ORMSchemaEventSubscriber());

To use it with the DBAL only, register the DBALSchemaEventSubscriber event subscriber., (*7)

use Jsor\Doctrine\PostGIS\Event\DBALSchemaEventSubscriber;

$connection->getEventManager()->addEventSubscriber(new DBALSchemaEventSubscriber());

Symfony

For integrating this library into a Symfony project, read the dedicated Symfony Documentation., (*8)

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 to understand the difference between these two types)., (*9)

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., (*10)

  • 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

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 format. Please note, that the values returned from database may differ from the values you have set. The library uses ST_AsEWKT to retain as much information as possible (like SRID's). Read more in the PostGIS docs., (*11)

Example

$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 can be defined for geometry fields by setting the spatial flag., (*12)

use Doctrine\ORM\Mapping as ORM;

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

Schema Tool

Full support for the ORM Schema Tool and the DBAL Schema Manager is provided., (*13)

DQL Functions

Most PostGIS functions are also available for the Doctrine Query Language (DQL) under the Jsor\Doctrine\PostGIS\Functions namespace., (*14)

For a full list of all supported functions, see the Function Index., (*15)

Read the dedicated Symfony documentation on how to configure the functions with Symfony., (*16)

The functions must be registered with the Doctrine\ORM\Configuration instance., (*17)

$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., (*18)

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

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

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

Known Problems

Read the dedicated Symfony documentation on how to handle those problems with Symfony., (*19)

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., (*20)

This can be done by configuring a schema assets filter., (*21)

$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

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

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., (*23)

$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., (*24)

If you want to use this type in your entities, you have to configure real database types, e.g. with the PostgreSQL for Doctrine package., (*25)

Running the Tests

A simple Docker setup is included to run the test suite against the different PostgreSQL / PostGIS combinations., (*26)

All commands here should be run from the project root., (*27)

First, build the PHP container. This must be done only once., (*28)

./docker/build-php.sh

Install dependencies via Composer., (*29)

./docker/run-php.sh composer install

Next, start the database containers., (*30)

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., (*31)

The script names follow the pattern run-<POSTGRESQL_VERSION>-<POSTGIS_VERSION>.sh., (*32)

To run the test suite against PostgreSQL 13 with PostGIS 3.1, use the script ./docker/run-13-31.sh., (*33)

./docker/run-13-31.sh vendor/bin/phpunit --exclude-group=postgis-3.0

Note, that we exclude tests targeted at PostGIS 3.0 here. When running tests against PostGIS 3.0, exclude the tests for 3.1., (*34)

./docker/run-13-30.sh vendor/bin/phpunit --exclude-group=postgis-3.1

License

Copyright (c) 2014-2024 Jan Sorgalla. Released under the MIT License., (*35)

The Versions

15/07 2018

dev-test-matrix

dev-test-matrix

Spatial and Geographic Data with PostGIS and Doctrine.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database doctrine geo dbal geometry geography gis postgis spatial

15/07 2018

dev-master

9999999-dev

Spatial and Geographic Data with PostGIS and Doctrine.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database doctrine geo dbal geometry geography gis postgis spatial

15/07 2018

dev-drop-unsupported-php-versions

dev-drop-unsupported-php-versions

Spatial and Geographic Data with PostGIS and Doctrine.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database doctrine geo dbal geometry geography gis postgis spatial

13/07 2018

v1.6.0

1.6.0.0

Spatial and Geographic Data with PostGIS and Doctrine.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database doctrine geo dbal geometry geography gis postgis spatial

13/11 2017

v1.5.0

1.5.0.0

Spatial and Geographic Data with PostGIS and Doctrine.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database doctrine geo dbal geometry geography gis postgis spatial

25/09 2017

dev-update-tested-version

dev-update-tested-version

Spatial and Geographic Data with PostGIS and Doctrine.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database doctrine geo dbal geometry geography gis postgis spatial

12/07 2017

v1.4.1

1.4.1.0

Spatial and Geographic Data with PostGIS and Doctrine.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database doctrine geo dbal geometry geography gis postgis spatial

11/01 2017

v1.4.0

1.4.0.0

Spatial and Geographic Data with PostGIS and Doctrine.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database doctrine geo dbal geometry geography gis postgis spatial

16/12 2016

v1.3.0

1.3.0.0

Spatial and Geographic Data with PostGIS and Doctrine.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database doctrine geo dbal geometry geography gis postgis spatial

15/12 2016

v1.2.0

1.2.0.0

Spatial and Geographic Data with PostGIS and Doctrine.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database doctrine geo dbal geometry geography gis postgis spatial

28/11 2016

v1.1.2

1.1.2.0

Spatial and Geographic Data with PostGIS and Doctrine.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database doctrine geo dbal geometry geography gis postgis spatial

11/10 2016

v1.1.1

1.1.1.0

Spatial and Geographic Data with PostGIS and Doctrine.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database doctrine geo dbal geometry geography gis postgis spatial

08/04 2016

v1.1.0

1.1.0.0

Spatial and Geographic Data with PostGIS and Doctrine.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database doctrine geo dbal geometry geography gis postgis spatial

11/08 2015

v1.0.0

1.0.0.0

Spatial and Geographic Data with PostGIS and Doctrine.

  Sources   Download

MIT

The Requires

 

The Development Requires

orm database doctrine geo dbal geometry geography gis postgis spatial