This library allows you to use Doctrine with PostGIS, the spatial database extension for PostgreSQL.
Both PostGIS 1.5 and 2.x are supported as well as GiST-based spatial indexes.
Install through composer. Check the packagist page for all available versions.
{
"require": {
"jsor/doctrine-postgis": "~0.1.0@dev"
}
}
All you have to do is, to register an event subscriber.
use Jsor\Doctrine\PostGIS\Event\ORMSchemaEventSubscriber;
$entityManager->getEventManager()->addEventSubscriber(new ORMSchemaEventSubscriber());
You can also use this libray with the DBAL only.
use Jsor\Doctrine\PostGIS\Event\DBALSchemaEventSubscriber;
$connection->getEventManager()->addEventSubscriber(new DBALSchemaEventSubscriber());
Once the event subscriber is registered, you can use the column types
geometry
and geography
in your property mappings (please read the
PostGIS docs
to understand the difference between these two types).
/** @Entity */
class MyEntity
{
/**
* @Column(type="geometry")
*/
private $geometry;
/**
* @Column(type="geography")
*/
private $geography;
}
There are 2 options you can set to define the geometry.
spatial_type
This defines the type of the geometry, like POINT, LINESTRING etc.spatial_srid
This defines the Spatial Reference System Identifier (SRID) of the geometry.
/** @Entity */
class MyEntity
{
/**
* @Column(type="geometry", options={"spatial_type"="POINT"})
*/
private $point;
/**
* @Column(type="geometry", options={"spatial_type"="POINTZM"})
*/
private $point4D;
/**
* @Column(type="geometry", options={"spatial_type"="POINT", "spatial_srid"=3785})
*/
private $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.
$entity = new MyEntity();
$entity->setPoint('POINT(37.4220761 -122.0845187)');
$entity->setPoint4D('POINT(1 2 3 4)');
$entity->setPointWithSRID('SRID=3785;POINT(37.4220761 -122.0845187)');
You can define spatial indexes for your geometry columns.
In theory, you simply have to define a SPATIAL
flag in your index definition.
/**
* @Entity
* @Table(
* indexes={
* @Index(name="idx_point", columns={"point"}, flags={"SPATIAL"}))
* }
* )
*/
class MyEntity
{
}
In practice, the ORM doesn't support flags yet (but there is a pull request open to add support for it).
In the meantime you can use the following workaround to define spatial indexes.
/**
* @Entity
* @Table(
* options={"spatial_indexes"={"idx_point"}},
* indexes={
* @Index(name="idx_point", columns={"point"}))
* }
* )
*/
class MyEntity
{
}