creof/doctrine2-spatial

Need ability to to convert GeometryInterface to WKT and WKT to GeometryInterface

Opened this issue · 3 comments

Basically this is dublicate of #29 but after refactor the way I used this is no longer available.
So maybe would be good to provide public methods like wktToObject() and objectToWkt() names does not matter :D

the use case is that when I get or send data trought rest API it is in WKT format then I need to convert wkt to GeometryInterface or vice versa

The code still exists but was moved into other packages in the CrEOF namespace. You may need to import one or more of those packages also.

This use case has been discussed and functionality to allow working with raw data (WKT, GeoJSON, etc.) is planned.

@djlambert yep, for now I wrote hydrator strategy for zf2 hydrator:

<?php
namespace BaseUtils\Hydrator\Strategy;

use CrEOF\Spatial\Exception\InvalidValueException;
use Zend\Stdlib\Hydrator\Strategy\StrategyInterface;
use CrEOF\Spatial\PHP\Types\Geography\GeographyInterface;
use CrEOF\Geo\WKT\Parser as WKTStringParser;

/**
 * Class SpatialGeographyStrategy
 * @package BaseUtils\Hydrator\Strategy
 * @deprecated
 */
class SpatialGeographyStrategy implements StrategyInterface
{
    public function extract($value)
    {
        if (!$value instanceof GeographyInterface) {
            throw new \InvalidArgumentException(
                sprintf(
                    '"value" must be an instance of "%s", "%s" given.',
                    GeographyInterface::class,
                    is_object($value) ? get_class($value) : gettype($value)
                )
            );
        }

        return sprintf('%s(%s)', strtoupper($value->getType()), $value);
    }

    public function hydrate($value)
    {
        $parser = new WKTStringParser($value);
        $value  = $parser->parse();

        $typeName  = strtoupper($value['type']);
        $constName = sprintf('CrEOF\Spatial\PHP\Types\Geometry\GeometryInterface::%s', $typeName);

        if (! defined($constName)) {
            throw new InvalidValueException(sprintf('Unsupported Geography type "%s".', $typeName));
        }

        $class = sprintf('CrEOF\Spatial\PHP\Types\Geography\%s', constant($constName));
        if (!class_exists($class)) {
            throw new InvalidValueException(sprintf('Unsupported Geography type "%s".', $typeName));
        }

        return new $class($value['value'], $value['srid']);
    }
}

not perfect, but well at least works :D (hope in all cases)

Hi,
Is there any update on this issue?
I have almost similar problem (https://stackoverflow.com/questions/45052641/doctrine-creof-convert-string-in-to-polygon-type-in-the-entity-setter-method), can any one please suggest me a full proof solution for the same?
Thanks,
Krish