Skip to content

Commit

Permalink
Added ST_Distance_Sphere and ST_Distance to mysql (#191)
Browse files Browse the repository at this point in the history
* Added ST_Distance_Sphere to mysql

* Added ST_Distance_Test to mysql

* Updated travis ci config

* Updated a test case

* Fixed a typo

* Updated INSTALL.md

* Added STGeographyFromText to test case

* Added STGeographyFromText class

* Added STGeomFromText

* Fixed some tests

* Update phpunit.xml.dist
  • Loading branch information
0x6368 authored Sep 20, 2023
1 parent 8ce00f1 commit 99be6ec
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 1 deletion.
9 changes: 8 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@ language: php
sudo: false

php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- hhvm

env:
- ORM=2.3
- ORM=2.4
- ORM=2.5

addons:
postgresql: 9.6
apt:
packages:
- postgresql-9.6-postgis-2.3


before_script:
- composer self-update
- cp ./tests/travis/composer.orm$ORM.json ./composer.json
Expand Down
2 changes: 2 additions & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ doctrine:
st_contains: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STContains
st_crosses: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STCrosses
st_disjoint: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STDisjoint
st_distance: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STDistance
st_distance_sphere: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STDistanceSphere
st_equals: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STEquals
st_intersects: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STIntersects
st_overlaps: CrEOF\Spatial\ORM\Query\AST\Functions\MySql\STOverlaps
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* Copyright (C) 2013 Christian Bieg
*
* 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\MySql;

use CrEOF\Spatial\ORM\Query\AST\Functions\AbstractSpatialDQLFunction;

/**
* STDistance DQL function
*
* @author Christian Bieg <[email protected]>
* @license http://dlambert.mit-license.org MIT
*/
class STDistanceSphere extends AbstractSpatialDQLFunction {

protected $platforms = array('mysql');
protected $functionName = 'ST_Distance_SPHERE';
protected $minGeomExpr = 2;
protected $maxGeomExpr = 2;

}
42 changes: 42 additions & 0 deletions lib/CrEOF/Spatial/ORM/Query/AST/Functions/MySql/STGeomFromText.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright (C) 2012 Christian Bieg
*
* 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\MySql;

use CrEOF\Spatial\ORM\Query\AST\Functions\AbstractSpatialDQLFunction;

/**
* ST_GeomFromText DQL function
*
* @author Christian Bieg <[email protected]>
*/
class STGeomFromText extends AbstractSpatialDQLFunction
{
protected $platforms = array('mysql');

protected $functionName = 'ST_GeomFromText';

protected $minGeomExpr = 1;

protected $maxGeomExpr = 2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Copyright (C) 2015 Christian Bieg
*
* 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\Tests\ORM\Query\AST\Functions\MySql;

use CrEOF\Spatial\PHP\Types\Geography\Point as GeographyPoint;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
use CrEOF\Spatial\Tests\Fixtures\PointEntity;
use CrEOF\Spatial\Tests\Fixtures\GeographyEntity;
use CrEOF\Spatial\Tests\OrmTestCase;
use Doctrine\ORM\Query;

/**
* ST_Distance_Sphere DQL function tests
*
* @author Christian Bieg <[email protected]>
*
* @group dql
*/
class STDistanceSphereTest extends OrmTestCase
{
protected function setUp()
{
$this->usesEntity(self::POINT_ENTITY);
$this->supportsPlatform('mysql');

parent::setUp();
}

/**
* @group geometry
*/
public function testSelectSTDistanceSphereGeometry()
{
$newYork = new Point(-73.938611, 40.664167);
$losAngles = new Point(-118.2430, 34.0522);
$dallas = new Point(-96.803889, 32.782778);

$entity1 = new PointEntity();

$entity1->setPoint($newYork);
$this->getEntityManager()->persist($entity1);

$entity2 = new PointEntity();

$entity2->setPoint($losAngles);
$this->getEntityManager()->persist($entity2);

$entity3 = new PointEntity();

$entity3->setPoint($dallas);
$this->getEntityManager()->persist($entity3);
$this->getEntityManager()->flush();
$this->getEntityManager()->clear();

$query = $this->getEntityManager()->createQuery('SELECT p, ST_Distance_Sphere(p.point, ST_GeomFromText(:p1)) FROM CrEOF\Spatial\Tests\Fixtures\PointEntity p');

$query->setParameter('p1', 'POINT(-89.4 43.066667)', 'string');

$result = $query->getResult();

$this->assertCount(3, $result);
$this->assertEquals($entity1, $result[0][0]);
$this->assertEquals(1305895.94823465, $result[0][1]);
$this->assertEquals($entity2, $result[1][0]);
$this->assertEquals(2684082.08249337, $result[1][1]);
$this->assertEquals($entity3, $result[2][0]);
$this->assertEquals(1313754.60684762, $result[2][1]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Copyright (C) 2015 Christian Bieg
*
* 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\Tests\ORM\Query\AST\Functions\MySql;

use CrEOF\Spatial\PHP\Types\Geography\Point as GeographyPoint;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
use CrEOF\Spatial\Tests\Fixtures\PointEntity;
use CrEOF\Spatial\Tests\Fixtures\GeographyEntity;
use CrEOF\Spatial\Tests\OrmTestCase;
use Doctrine\ORM\Query;

/**
* ST_Distance DQL function tests
*
* @author Christian Bieg <[email protected]>
*
* @group dql
*/
class STDistanceTest extends OrmTestCase
{
protected function setUp()
{
$this->usesEntity(self::POINT_ENTITY);
$this->usesEntity(self::GEOGRAPHY_ENTITY);
$this->usesType('geopoint');
$this->supportsPlatform('mysql');

parent::setUp();
}

/**
* @group geometry
*/
public function testSelectSTDistanceGeometryCartesian()
{
$newYork = new Point(-73.938611, 40.664167);
$losAngles = new Point(-118.2430, 34.0522);
$dallas = new Point(-96.803889, 32.782778);

$entity1 = new PointEntity();

$entity1->setPoint($newYork);
$this->getEntityManager()->persist($entity1);

$entity2 = new PointEntity();

$entity2->setPoint($losAngles);
$this->getEntityManager()->persist($entity2);

$entity3 = new PointEntity();

$entity3->setPoint($dallas);
$this->getEntityManager()->persist($entity3);
$this->getEntityManager()->flush();
$this->getEntityManager()->clear();

$query = $this->getEntityManager()->createQuery('SELECT p, ST_Distance(p.point, ST_GeomFromText(:p1)) FROM CrEOF\Spatial\Tests\Fixtures\PointEntity p');

$query->setParameter('p1', 'POINT(-89.4 43.066667)', 'string');

$result = $query->getResult();

$this->assertCount(3, $result);
$this->assertEquals($entity1, $result[0][0]);
$this->assertEquals(15.646934398128, $result[0][1]);
$this->assertEquals($entity2, $result[1][0]);
$this->assertEquals(30.2188561049899, $result[1][1]);
$this->assertEquals($entity3, $result[2][0]);
$this->assertEquals(12.6718564262953, $result[2][1]);
}
}
Loading

0 comments on commit 99be6ec

Please sign in to comment.