[TYPO3-mvc] How would you realize a radius search

bernd wilke t3ng at bernd-wilke.net
Tue Feb 19 15:34:35 CET 2013


Am 19.02.2013 13:11, schrieb Philipp:
> Okay, i think ive got some research to do :-)
>
> At the moment my approach is a single sql query for my results. It works
> for the moment but i dont like it.
> public function findClosest($regionalObject,
> $excludeAllreadyDisplayed=FALSE, $andWhere = '',$limit=6) {
>
>          if ($regionalObject instanceof
> \Pixelpoint\RegionalObject\Domain\Model\RegionalObject) {
>
>              $lat = $regionalObject->getLatitude();
>              $lng = $regionalObject->getLongitude();
>              $table = $this->getTableName();
>
>              $sql =
>              "SELECT *,
>              (
>                  acos(
>                        sin(latitude)
>                      * sin($lat)
>                      + cos(latitude)
>                      * cos($lat)
>                      * cos(longitude - $lng)
>                  ) * 6371
>              ) AS distance
>              FROM $table
>              WHERE deleted = '0'
>              AND hidden = '0'
>              AND t3ver_oid = '0'
>              $andWhere
>              ORDER BY distance
>              LIMIT $limit";
>
>              $query = $this->createRawQuery();
>              $query->getQuerySettings()->setReturnRawQueryResult(TRUE);
>              $results = $query->statement($sql)->execute();
>
>              return $this->mapResults($results);
>
>          } else {
>              throw new \UnexpectedValueException("regionalObject was not
> of type \\Pixelpoint\\RegionalObject\\Domain\\Model\\RegionalObject");
>          }
>          return NULL;
>      }

depending on the data you may get much faster result by ignoring the 
spherical world-view.
if your locations reside not in a great variaty from pole to equator 
(like in a small country like germany) you may calculate the distance on 
a flat earth without trigonometric functions:

dx² + dy² = dz²:

select *, (sqr(latitude-$lat)*corr1+sqr(longitude-$lng))*factor as 
distsquare FROM ...

with a medium correction depending on latitude

bernd
-- 
http://www.pi-phi.de/cheatsheet.html


More information about the TYPO3-project-typo3v4mvc mailing list