Skip to content

Instantly share code, notes, and snippets.

@jsor
Created December 2, 2017 13:08
Show Gist options
  • Save jsor/7149309ceaf7fdae1051a49a06797f63 to your computer and use it in GitHub Desktop.
Save jsor/7149309ceaf7fdae1051a49a06797f63 to your computer and use it in GitHub Desktop.
Example setup for jsor/doctrine-postgis working with geometry objects
{
"autoload": {
"psr-4": {
"DoctrinePostGISExample\\": "./"
}
},
"require": {
"doctrine/orm": "^2.5",
"jsor/doctrine-postgis": "^1.5",
"geo-io/wkt-parser": "^1.0",
"geo-io/wkt-generator": "^1.0",
"geo-io/geometry": "^1.0"
}
}
<?php
namespace DoctrinePostGISExample;
use Doctrine\ORM\Mapping as ORM;
use GeoIO\Geometry\Point;
/**
* @ORM\Entity
* @ORM\Table(
* name="entities"
* )
*/
class Entity
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
private $id;
/**
* @ORM\Column(type="geometry", name="point", options={"geometry_type"="point"})
*/
private $point;
public function __construct(Point $point)
{
$this->point = $point;
}
public function getId()
{
return $this->id;
}
public function getPoint()
{
return $this->point;
}
}
<?php
require __DIR__.'/vendor/autoload.php';
Doctrine\DBAL\Types\Type::addType(
'geometry',
DoctrinePostGISExample\GeometryType::class
);
$entity = new DoctrinePostGISExample\Entity(
new GeoIO\Geometry\Point(
GeoIO\Dimension::DIMENSION_2D,
new GeoIO\Geometry\Coordinates(1, 2),
4326 // SRID (http://spatialreference.org/ref/epsg/wgs-84/)
)
);
$em->persist($entity);
$em->flush();
<?php
namespace DoctrinePostGISExample;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use GeoIO\Geometry\Extractor;
use GeoIO\Geometry\Factory;
use GeoIO\WKT\Generator\Generator;
use GeoIO\WKT\Parser\Parser;
use Jsor\Doctrine\PostGIS\Types\GeometryType as BaseGeometryType;
class GeometryType extends BaseGeometryType
{
private static $parser;
private static $generator;
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return self::getGenerator()->generate($value);
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return self::getParser()->parse($value);
}
private static function getParser()
{
if (!self::$parser) {
self::$parser = new Parser(new Factory());
}
return self::$parser;
}
private static function getGenerator()
{
if (!self::$generator) {
self::$generator = new Generator(
new Extractor(),
array(
'format' => Generator::FORMAT_EWKT,
'emit_srid' => true
)
);
}
return self::$generator;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment