Skip to content
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

Added ST_SnapToGrid #21

Merged
merged 1 commit into from
Dec 15, 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 docs/function-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Function Index
* [ST_Multi](http://postgis.net/docs/ST_Multi.html)
* [ST_Scale](http://postgis.net/docs/ST_Scale.html)
* [ST_SetSRID](http://postgis.net/docs/ST_SetSRID.html)
* [ST_SnapToGrid](http://postgis.net/docs/ST_SnapToGrid.html)
* [ST_Transform](http://postgis.net/docs/ST_Transform.html)
* [ST_Translate](http://postgis.net/docs/ST_Translate.html)
* [ST_TransScale](http://postgis.net/docs/ST_TransScale.html)
Expand Down
1 change: 1 addition & 0 deletions src/Functions/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public static function configure(Configuration $configuration)
$configuration->addCustomStringFunction('ST_Multi', 'Jsor\Doctrine\PostGIS\Functions\ST_Multi');
$configuration->addCustomStringFunction('ST_Scale', 'Jsor\Doctrine\PostGIS\Functions\ST_Scale');
$configuration->addCustomStringFunction('ST_SetSRID', 'Jsor\Doctrine\PostGIS\Functions\ST_SetSRID');
$configuration->addCustomStringFunction('ST_SnapToGrid', 'Jsor\Doctrine\PostGIS\Functions\ST_SnapToGrid');
$configuration->addCustomStringFunction('ST_Transform', 'Jsor\Doctrine\PostGIS\Functions\ST_Transform');
$configuration->addCustomStringFunction('ST_Translate', 'Jsor\Doctrine\PostGIS\Functions\ST_Translate');
$configuration->addCustomStringFunction('ST_TransScale', 'Jsor\Doctrine\PostGIS\Functions\ST_TransScale');
Expand Down
62 changes: 62 additions & 0 deletions src/Functions/ST_SnapToGrid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/* This file is auto-generated. Don't edit directly! */

namespace Jsor\Doctrine\PostGIS\Functions;

use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;

class ST_SnapToGrid extends FunctionNode
{
protected $expressions = array();

public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);

$this->expressions[] = $parser->ArithmeticFactor();

$parser->match(Lexer::T_COMMA);

$this->expressions[] = $parser->ArithmeticFactor();

$lexer = $parser->getLexer();

if ($lexer->lookahead['type'] === Lexer::T_COMMA) {
$parser->match(Lexer::T_COMMA);
$this->expressions[] = $parser->ArithmeticFactor();
}

if ($lexer->lookahead['type'] === Lexer::T_COMMA) {
$parser->match(Lexer::T_COMMA);
$this->expressions[] = $parser->ArithmeticFactor();
}

if ($lexer->lookahead['type'] === Lexer::T_COMMA) {
$parser->match(Lexer::T_COMMA);
$this->expressions[] = $parser->ArithmeticFactor();
}

if ($lexer->lookahead['type'] === Lexer::T_COMMA) {
$parser->match(Lexer::T_COMMA);
$this->expressions[] = $parser->ArithmeticFactor();
}

$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}

public function getSql(SqlWalker $sqlWalker)
{
$arguments = array();

foreach ($this->expressions as $expression) {
$arguments[] = $expression->dispatch($sqlWalker);
}

return 'ST_SnapToGrid(' . implode(', ', $arguments) . ')';
}
}
74 changes: 74 additions & 0 deletions tests/Functions/ST_SnapToGridTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/* This file is auto-generated. Don't edit directly! */

namespace Jsor\Doctrine\PostGIS\Functions;

use Jsor\Doctrine\PostGIS\AbstractFunctionalTestCase;
use Jsor\Doctrine\PostGIS\PointsEntity;

class ST_SnapToGridTest extends AbstractFunctionalTestCase
{
protected function setUp()
{
parent::setUp();

$this->_setUpEntitySchema(array(
'Jsor\Doctrine\PostGIS\PointsEntity'
));

$em = $this->_getEntityManager();

$entity = new PointsEntity(array(
'text' => 'foo',
'geometry' => 'POINT(1 1)',
'point' => 'POINT(1 1)',
'point2D' => 'SRID=3785;POINT(1 1)',
'point3DZ' => 'SRID=3785;POINT(1 1 1)',
'point3DM' => 'SRID=3785;POINTM(1 1 1)',
'point4D' => 'SRID=3785;POINT(1 1 1 1)',
'point2DNullable' => null,
'point2DNoSrid' => 'POINT(1 1)',
'geography' => 'SRID=4326;POINT(1 1)',
'pointGeography2d' => 'SRID=4326;POINT(1 1)',
'pointGeography2dSrid' => 'POINT(1 1)',
));

$em->persist($entity);
$em->flush();
$em->clear();
}

public function testQuery1()
{
$query = $this->_getEntityManager()->createQuery('SELECT
ST_AsText(ST_SnapToGrid(ST_GeomFromText(\'LINESTRING(1.1115678 2.123, 4.111111 3.2374897, 4.11112 3.23748667)\'),0.001)),
ST_AsEWKT(ST_SnapToGrid(ST_GeomFromEWKT(\'LINESTRING(-1.1115678 2.123 2.3456 1.11111, 4.111111 3.2374897 3.1234 1.1111, -1.11111112 2.123 2.3456 1.1111112)\'), ST_GeomFromEWKT(\'POINT(1.12 2.22 3.2 4.4444)\'), 0.1, 0.1, 0.1, 0.01)),
ST_AsEWKT(ST_SnapToGrid(ST_GeomFromEWKT(\'LINESTRING(-1.1115678 2.123 3 2.3456, 4.111111 3.2374897 3.1234 1.1111)\'), 0.01))
FROM Jsor\\Doctrine\\PostGIS\\PointsEntity');

$result = $query->getSingleResult();

array_walk_recursive($result, function (&$data) {
if (is_resource($data)) {
$data = stream_get_contents($data);

if (false !== ($pos = strpos($data, 'x'))) {
$data = substr($data, $pos + 1);
}
}

if (is_string($data)) {
$data = trim($data);
}
});

$expected = array(
1 => 'LINESTRING(1.112 2.123,4.111 3.237)',
2 => 'LINESTRING(-1.08 2.12 2.3 1.1144,4.12 3.22 3.1 1.1144,-1.08 2.12 2.3 1.1144)',
3 => 'LINESTRING(-1.11 2.12 3 2.3456,4.11 3.24 3.1234 1.1111)',
);

$this->assertEquals($expected, $result, '', 0.0001);
}
}
20 changes: 19 additions & 1 deletion tools/functions/geometry-editors.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,27 @@
)
)
),
/*
'ST_SnapToGrid' => array(
'required_arguments' => 2,
'total_arguments' => 6,
'tests' => array(
'queries' => array(
array(
'sql' => "SELECT
ST_AsText({function}(ST_GeomFromText('LINESTRING(1.1115678 2.123, 4.111111 3.2374897, 4.11112 3.23748667)'),0.001)),
ST_AsEWKT(ST_SnapToGrid(ST_GeomFromEWKT('LINESTRING(-1.1115678 2.123 2.3456 1.11111, 4.111111 3.2374897 3.1234 1.1111, -1.11111112 2.123 2.3456 1.1111112)'), ST_GeomFromEWKT('POINT(1.12 2.22 3.2 4.4444)'), 0.1, 0.1, 0.1, 0.01)),
ST_AsEWKT(ST_SnapToGrid(ST_GeomFromEWKT('LINESTRING(-1.1115678 2.123 3 2.3456, 4.111111 3.2374897 3.1234 1.1111)'), 0.01))
",
'result' => array(
1 => 'LINESTRING(1.112 2.123,4.111 3.237)',
2 => 'LINESTRING(-1.08 2.12 2.3 1.1144,4.12 3.22 3.1 1.1144,-1.08 2.12 2.3 1.1144)',
3 => 'LINESTRING(-1.11 2.12 3 2.3456,4.11 3.24 3.1234 1.1111)'
)
),
)
)
),
/*
'ST_Snap' => array(
),
*/
Expand Down