Created
December 2, 2017 13:08
-
-
Save jsor/7149309ceaf7fdae1051a49a06797f63 to your computer and use it in GitHub Desktop.
Example setup for jsor/doctrine-postgis working with geometry objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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