Created
June 29, 2012 08:42
-
-
Save Koc/3016704 to your computer and use it in GitHub Desktop.
Doctrine2 DISTANCE function
This file contains 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 Myako\Geographical\Functions; | |
use Doctrine\ORM\Query\AST\Functions\FunctionNode; | |
use Doctrine\ORM\Query\Lexer; | |
/** | |
* "DISTANCE" "(" LatitudeFrom, LongitudetFrom, LatitudeTo, LongitudeTo ")" | |
* | |
* @author Konstantin.Myakshin <[email protected]> | |
*/ | |
class DistanceFunction extends FunctionNode | |
{ | |
protected $fromLat; | |
protected $fromLng; | |
protected $toLat; | |
protected $toLng; | |
public function parse(\Doctrine\ORM\Query\Parser $parser) | |
{ | |
$parser->match(Lexer::T_IDENTIFIER); | |
$parser->match(Lexer::T_OPEN_PARENTHESIS); | |
$this->fromLat = $parser->ArithmeticPrimary(); | |
$parser->match(Lexer::T_COMMA); | |
$this->fromLng = $parser->ArithmeticPrimary(); | |
$parser->match(Lexer::T_COMMA); | |
$this->toLat = $parser->ArithmeticPrimary(); | |
$parser->match(Lexer::T_COMMA); | |
$this->toLng = $parser->ArithmeticPrimary(); | |
$parser->match(Lexer::T_CLOSE_PARENTHESIS); | |
} | |
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) | |
{ | |
$earthDiameterInKM = 1.609344 * 3956 * 2; | |
$sql = '('.$earthDiameterInKM.' * ASIN(SQRT(POWER(' . | |
'SIN(('.$this->fromLat->dispatch($sqlWalker).' - ABS('.$this->toLat->dispatch($sqlWalker).')) * PI() / 180 / 2), 2) + ' . | |
'COS('.$this->fromLat->dispatch($sqlWalker).' * PI() / 180) * COS(ABS('.$this->toLat->dispatch($sqlWalker).') * PI() / 180) * ' . | |
'POWER(SIN(('.$this->fromLng->dispatch($sqlWalker).' - '.$this->toLng->dispatch($sqlWalker).') * PI() / 180 / 2), 2) ' . | |
')))'; | |
return $sql; | |
} | |
} |
Important: This function is currently wrong and won't work for any negative latitudes. The ABS-functions need to be removed and then it seems to work correctly. I compared it with the original formula for this calculation and there were no ABS-functions used. I am not sure if there is a good reason why you did use them, but after trying a lot of different geo positions and searching for this error for a few hours now I found out that removing them solves the issue.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i think you have to use ArithmeticExpression() instead of ArithmeticPrimary()
@see https://github.com/craue/CraueGeoBundle/blob/master/Doctrine/Query/Mysql/GeoDistance.php#L38