Skip to content

Added ST_SnapToGrid support #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ doctrine:
# for postgresql
geometry: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\Geometry
stcollect: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STCollect
stsnaptogrid: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STSnapToGrid
numeric_functions:
# for postgresql
starea: CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STArea
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Copyright (C) 2016 Derek J. Lambert
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql;

use CrEOF\Spatial\ORM\Query\AST\Functions\AbstractSpatialDQLFunction;
use CrEOF\Spatial\ORM\Query\AST\Functions\ReturnsGeometryInterface;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;

/**
* ST_SnapToGrid DQL function.
*
* Possible signatures:
* geometry ST_SnapToGrid(geometry geomA, float size);
* geometry ST_SnapToGrid(geometry geomA, float sizeX, float sizeY);
* geometry ST_SnapToGrid(geometry geomA, float originX, float originY, float sizeX, float sizeY);
* geometry ST_SnapToGrid(geometry geomA, geometry pointOrigin, float sizeX, float sizeY, float sizeZ, float sizeM);
*
* @author Dragos Protung
* @license http://mit-license.org MIT
*/
class STSnapToGrid extends AbstractSpatialDQLFunction implements ReturnsGeometryInterface
{
protected $platforms = array('postgresql');

protected $functionName = 'ST_SnapToGrid';

protected $minGeomExpr = 2;

protected $maxGeomExpr = 6;

/**
* {@inheritdoc}
*/
public function parse(Parser $parser)
{
$lexer = $parser->getLexer();

$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);

// 1st signature
$this->geomExpr[] = $parser->ArithmeticFactor();
$parser->match(Lexer::T_COMMA);
$this->geomExpr[] = $parser->ArithmeticFactor();

// 2nd signature
if ($lexer->lookahead['type'] === Lexer::T_COMMA) {
$parser->match(Lexer::T_COMMA);
$this->geomExpr[] = $parser->ArithmeticFactor();
}

// 3rd signature
if ($lexer->lookahead['type'] === Lexer::T_COMMA) {
$parser->match(Lexer::T_COMMA);
$this->geomExpr[] = $parser->ArithmeticFactor();

$parser->match(Lexer::T_COMMA);
$this->geomExpr[] = $parser->ArithmeticFactor();

// 4th signature
if ($lexer->lookahead['type'] === Lexer::T_COMMA) {
// sizeM
$parser->match(Lexer::T_COMMA);
$this->geomExpr[] = $parser->ArithmeticFactor();
}
}

$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace CrEOF\Spatial\Tests\ORM\Query\AST\Functions\PostgreSql;

use CrEOF\Spatial\PHP\Types\Geometry\Point;
use CrEOF\Spatial\Tests\Fixtures\PointEntity;
use CrEOF\Spatial\Tests\OrmTestCase;

/**
* ST_SnapToGrid DQL function tests
*
* @author Dragos Protung
* @license http://dlambert.mit-license.org MIT
*
* @group dql
*/
class STSnapToGridTest extends OrmTestCase
{
protected function setUp()
{
$this->usesEntity(self::POINT_ENTITY);
$this->supportsPlatform('postgresql');

parent::setUp();
}

/**
* @group geometry
*/
public function testSelectSTSnapToGridSignature2Parameters()
{
$entity = new PointEntity();
$entity->setPoint(new Point(1.25, 2.55));
$this->getEntityManager()->persist($entity);

$this->getEntityManager()->flush();
$this->getEntityManager()->clear();

$query = $this->getEntityManager()->createQuery('SELECT ST_AsText(ST_SnapToGrid(geometry(p.point), 0.5)) FROM CrEOF\Spatial\Tests\Fixtures\PointEntity p');
$result = $query->getResult();

$expected = array(
array(1 => 'POINT(1 2.5)'),
);

$this->assertEquals($expected, $result);
}

/**
* @group geometry
*/
public function testSelectSTSnapToGridSignature3Parameters()
{
$entity = new PointEntity();
$entity->setPoint(new Point(1.25, 2.55));
$this->getEntityManager()->persist($entity);

$this->getEntityManager()->flush();
$this->getEntityManager()->clear();

$query = $this->getEntityManager()->createQuery('SELECT ST_AsText(ST_SnapToGrid(geometry(p.point), 0.5, 1)) FROM CrEOF\Spatial\Tests\Fixtures\PointEntity p');
$result = $query->getResult();

$expected = array(
array(1 => 'POINT(1 3)'),
);

$this->assertEquals($expected, $result);
}

/**
* @group geometry
*/
public function testSelectSTSnapToGridSignature5Parameters()
{
$entity = new PointEntity();
$entity->setPoint(new Point(5.25, 6.55));
$this->getEntityManager()->persist($entity);

$this->getEntityManager()->flush();
$this->getEntityManager()->clear();

$query = $this->getEntityManager()->createQuery('SELECT ST_AsText(ST_SnapToGrid(geometry(p.point), 5.55, 6.25, 0.5, 0.5)) FROM CrEOF\Spatial\Tests\Fixtures\PointEntity p');
$result = $query->getResult();

$expected = array(
array(1 => 'POINT(5.05 6.75)'),
);

$this->assertEquals($expected, $result);
}

/**
* @group geometry
*/
public function testSelectSTSnapToGridSignature6Parameters()
{
$entity = new PointEntity();
$entity->setPoint(new Point(5.25, 6.55));
$this->getEntityManager()->persist($entity);

$this->getEntityManager()->flush();
$this->getEntityManager()->clear();

$query = $this->getEntityManager()->createQuery('SELECT ST_AsText(ST_SnapToGrid(geometry(p.point), geometry(p.point), 0.005, 0.025, 0.5, 0.01)) FROM CrEOF\Spatial\Tests\Fixtures\PointEntity p');
$result = $query->getResult();

$expected = array(
array(1 => 'POINT(5.25 6.55)'),
);

$this->assertEquals($expected, $result);
}
}
1 change: 1 addition & 0 deletions tests/CrEOF/Spatial/Tests/OrmTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ protected function setUpFunctions()
$configuration->addCustomNumericFunction('st_length', 'CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STLength');
$configuration->addCustomNumericFunction('st_linecrossingdirection', 'CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STLineCrossingDirection');
$configuration->addCustomStringFunction('st_makeenvelope', 'CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STMakeEnvelope');
$configuration->addCustomStringFunction('st_snaptogrid', 'CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STSnapToGrid');
$configuration->addCustomStringFunction('st_startpoint', 'CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STStartPoint');
$configuration->addCustomStringFunction('st_summary', 'CrEOF\Spatial\ORM\Query\AST\Functions\PostgreSql\STSummary');
}
Expand Down