]> git.openstreetmap.org Git - nominatim.git/commitdiff
Merge pull request #667 from lonvia/refactor-nearpoint
authorSarah Hoffmann <lonvia@denofr.de>
Sat, 18 Mar 2017 08:25:07 +0000 (09:25 +0100)
committerGitHub <noreply@github.com>
Sat, 18 Mar 2017 08:25:07 +0000 (09:25 +0100)
Factor out near point into class

lib/Geocode.php
lib/NearPoint.php [new file with mode: 0644]
lib/lib.php
test/php/Nominatim/NearPointTest.php [new file with mode: 0644]
test/php/Nominatim/NominatimTest.php

index b12f96079c5d9f9295d6c15ca192bd0dce72a227..3726f3379600675a00196020f7d18d7e37741c3f 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace Nominatim;
 
+require_once(CONST_BasePath.'/lib/NearPoint.php');
 require_once(CONST_BasePath.'/lib/PlaceLookup.php');
 require_once(CONST_BasePath.'/lib/ReverseGeocode.php');
 
@@ -32,7 +33,6 @@ class Geocode
     protected $bFallback = false;
 
     protected $aCountryCodes = false;
-    protected $aNearPoint = false;
 
     protected $bBoundedSearch = false;
     protected $aViewBox = false;
@@ -221,11 +221,6 @@ class Geocode
         );
     }
 
-    public function setNearPoint($aNearPoint, $fRadiusDeg = 0.1)
-    {
-        $this->aNearPoint = array((float)$aNearPoint[0], (float)$aNearPoint[1], (float)$fRadiusDeg);
-    }
-
     public function setQuery($sQueryString)
     {
         $this->sQuery = $sQueryString;
@@ -698,10 +693,12 @@ class Geocode
                                         if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
                                     }
                                 } elseif (isset($aSearchTerm['lat']) && $aSearchTerm['lat'] !== '' && $aSearchTerm['lat'] !== null) {
-                                    if ($aSearch['fLat'] === '') {
-                                        $aSearch['fLat'] = $aSearchTerm['lat'];
-                                        $aSearch['fLon'] = $aSearchTerm['lon'];
-                                        $aSearch['fRadius'] = $aSearchTerm['radius'];
+                                    if ($aSearch['oNear'] === false) {
+                                        $aSearch['oNear'] = new NearPoint(
+                                            $aSearchTerm['lat'],
+                                            $aSearchTerm['lon'],
+                                            $aSearchTerm['radius']
+                                        );
                                         if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
                                     }
                                 } elseif ($sPhraseType == 'postalcode') {
@@ -942,8 +939,9 @@ class Geocode
         }
 
         // Do we have anything that looks like a lat/lon pair?
-        if ($aLooksLike = looksLikeLatLonPair($sQuery)) {
-            $this->setNearPoint(array($aLooksLike['lat'], $aLooksLike['lon']));
+        $oNearPoint = false;
+        if ($aLooksLike = NearPoint::extractFromQuery($sQuery)) {
+            $oNearPoint = $aLooksLike['pt'];
             $sQuery = $aLooksLike['query'];
         }
 
@@ -965,21 +963,10 @@ class Geocode
                            'sClass' => '',
                            'sType' => '',
                            'sHouseNumber' => '',
-                           'fLat' => '',
-                           'fLon' => '',
-                           'fRadius' => ''
+                           'oNear' => $oNearPoint
                           )
                          );
 
-            // Do we have a radius search?
-            $sNearPointSQL = false;
-            if ($this->aNearPoint) {
-                $sNearPointSQL = "ST_SetSRID(ST_Point(".(float)$this->aNearPoint[1].",".(float)$this->aNearPoint[0]."),4326)";
-                $aSearches[0]['fLat'] = (float)$this->aNearPoint[0];
-                $aSearches[0]['fLon'] = (float)$this->aNearPoint[1];
-                $aSearches[0]['fRadius'] = (float)$this->aNearPoint[2];
-            }
-
             // Any 'special' terms in the search?
             $bSpecialTerms = false;
             preg_match_all('/\\[(.*)=(.*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
@@ -1239,7 +1226,7 @@ class Geocode
                     if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);
 
                     // No location term?
-                    if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon']) {
+                    if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['oNear']) {
                         if ($aSearch['sCountryCode'] && !$aSearch['sClass'] && !$aSearch['sHouseNumber']) {
                             // Just looking for a country by code - look it up
                             if (4 >= $this->iMinAddressRank && 4 <= $this->iMaxAddressRank) {
@@ -1254,7 +1241,7 @@ class Geocode
                                 $aPlaceIDs = array();
                             }
                         } else {
-                            if (!$bBoundingBoxSearch && !$aSearch['fLon']) continue;
+                            if (!$bBoundingBoxSearch && !$aSearch['oNear']) continue;
                             if (!$aSearch['sClass']) continue;
 
                             $sSQL = "SELECT COUNT(*) FROM pg_tables WHERE tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
@@ -1299,7 +1286,7 @@ class Geocode
                                 $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
                             }
                         }
-                    } elseif ($aSearch['fLon'] && !sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['sClass']) {
+                    } elseif ($aSearch['oNear'] && !sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['sClass']) {
                         // If a coordinate is given, the search must either
                         // be for a name or a special search. Ignore everythin else.
                         $aPlaceIDs = array();
@@ -1364,15 +1351,10 @@ class Geocode
                                 $aTerms[] = "address_rank <= ".$this->iMaxAddressRank;
                             }
                         }
-                        if ($aSearch['fLon'] && $aSearch['fLat']) {
-                            $aTerms[] = sprintf(
-                                'ST_DWithin(centroid, ST_SetSRID(ST_Point(%F,%F),4326), %F)',
-                                $aSearch['fLon'],
-                                $aSearch['fLat'],
-                                $aSearch['fRadius']
-                            );
+                        if ($aSearch['oNear']) {
+                            $aTerms[] = $aSearch['oNear']->withinSQL('centroid');
 
-                            $aOrder[] = "ST_Distance(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326)) ASC";
+                            $aOrder[] = $aSearch['oNear']->distanceSQL('centroid');
                         }
                         if (sizeof($this->aExcludePlaceIDs)) {
                             $aTerms[] = "place_id not in (".join(',', $this->aExcludePlaceIDs).")";
@@ -1382,7 +1364,9 @@ class Geocode
                         }
 
                         if ($bBoundingBoxSearch) $aTerms[] = "centroid && $this->sViewboxSmallSQL";
-                        if ($sNearPointSQL) $aOrder[] = "ST_Distance($sNearPointSQL, centroid) ASC";
+                        if ($oNearPoint) {
+                            $aOrder[] = $oNearPoint->distanceSQL('centroid');
+                        }
 
                         if ($aSearch['sHouseNumber']) {
                             $sImportanceSQL = '- abs(26 - address_rank) + 3';
@@ -1590,9 +1574,13 @@ class Geocode
                                         $fRange = 0.05;
 
                                         $sOrderBySQL = '';
-                                        if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.centroid)";
-                                        elseif ($sPlaceIDs) $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
-                                        elseif ($sPlaceGeom) $sOrderBysSQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
+                                        if ($oNearPoint) {
+                                            $sOrderBySQL = $oNearPoint->distanceSQL('l.centroid');
+                                        } elseif ($sPlaceIDs) {
+                                            $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
+                                        } elseif ($sPlaceGeom) {
+                                            $sOrderBysSQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
+                                        }
 
                                         $sSQL = "select distinct l.place_id".($sOrderBySQL?','.$sOrderBySQL:'')." from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
                                         if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
@@ -1614,11 +1602,16 @@ class Geocode
                                         if (CONST_Debug) var_dump($sSQL);
                                         $aClassPlaceIDs = array_merge($aClassPlaceIDs, chksql($this->oDB->getCol($sSQL)));
                                     } else {
-                                        if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
+                                        if ($aSearch['oNear']) {
+                                            $fRange = $aSearch['oNear']->radius();
+                                        }
 
                                         $sOrderBySQL = '';
-                                        if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.geometry)";
-                                        else $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
+                                        if ($oNearPoint) {
+                                            $sOrderBySQL = $oNearPoint->distanceSQL('l.geometry');
+                                        } else {
+                                            $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
+                                        }
 
                                         $sSQL = "SELECT distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'');
                                         $sSQL .= " FROM placex as l, placex as f ";
@@ -1708,8 +1701,8 @@ class Geocode
             $oReverse->setZoom(18);
 
             $aLookup = $oReverse->lookup(
-                (float)$this->aNearPoint[0],
-                (float)$this->aNearPoint[1],
+                $oNearPoint->lat(),
+                $oNearPoint->lon(),
                 false
             );
 
diff --git a/lib/NearPoint.php b/lib/NearPoint.php
new file mode 100644 (file)
index 0000000..e8d595c
--- /dev/null
@@ -0,0 +1,157 @@
+<?php
+
+namespace Nominatim;
+
+/**
+ * A geographic point with a search radius.
+ */
+class NearPoint
+{
+    private $fLat;
+    private $fLon;
+    private $fRadius;
+
+    private $sSQL;
+
+
+    public function __construct($lat, $lon, $radius = 0.1)
+    {
+        $this->fLat = (float)$lat;
+        $this->fLon = (float)$lon;
+        $this->fRadius = (float)$radius;
+        $this->sSQL = 'ST_SetSRID(ST_Point('.$this->fLon.','.$this->fLat.'),4326)';
+    }
+
+    public function lat()
+    {
+        return $this->fLat;
+    }
+
+    public function lon()
+    {
+        return $this->fLon;
+    }
+
+    public function radius()
+    {
+        return $this->fRadius;
+    }
+
+    public function distanceSQL($sObj)
+    {
+        return 'ST_Distance('.$this->sSQL.", $sObj)";
+    }
+
+    public function withinSQL($sObj)
+    {
+        return sprintf('ST_DWithin(%s, %s, %F)', $sObj, $this->sSQL, $this->fRadius);
+    }
+
+    /**
+     * Check that the coordinates are valid WSG84 coordinates.
+     *
+     * @return bool True if the coordinates are correctly bounded.
+     */
+    public function isValid()
+    {
+        return ($this->fLat <= 90.1
+                && $this->fLat >= -90.1
+                && $this->fLon <= 180.1
+                && $this->fLon >= -180.1);
+    }
+
+    /**
+     * Extract a coordinate point from a query string.
+     *
+     * If a coordinate is found an array of a new NearPoint and the
+     * remaining query is returned or false otherwise.
+     *
+     * @param string $sQuery Query to scan.
+     *
+     * @return array|false If a coordinate was found, an array with
+     *                     `pt` as the NearPoint coordinates and `query`
+     *                      with the remaining query string. False otherwiese.
+     */
+    public static function extractFromQuery($sQuery)
+    {
+        // Do we have anything that looks like a lat/lon pair?
+        // returns array(lat,lon,query_with_lat_lon_removed)
+        // or null
+        $sFound    = null;
+        $fQueryLat = null;
+        $fQueryLon = null;
+
+        if (preg_match('/\\b([NS])[ ]+([0-9]+[0-9.]*)[° ]+([0-9.]+)?[′\']*[, ]+([EW])[ ]+([0-9]+)[° ]+([0-9]+[0-9.]*)[′\']*?\\b/', $sQuery, $aData)) {
+            /*              1         2                   3                  4         5            6
+             * degrees decimal minutes
+             * N 40 26.767, W 79 58.933
+             * N 40°26.767′, W 79°58.933′
+             */
+            $sFound    = $aData[0];
+            $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
+            $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
+        } elseif (preg_match('/\\b([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\']*[ ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\' ]+([EW])\\b/', $sQuery, $aData)) {
+            /*                    1             2                      3          4            5                    6
+             * degrees decimal minutes
+             * 40 26.767 N, 79 58.933 W
+             * 40° 26.767′ N 79° 58.933′ W
+             */
+            $sFound    = $aData[0];
+            $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
+            $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
+        } elseif (preg_match('/\\b([NS])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*[, ]+([EW])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*\\b/', $sQuery, $aData)) {
+            /*                    1        2            3            4                5        6            7            8
+             * degrees decimal seconds
+             * N 40 26 46 W 79 58 56
+             * N 40° 26′ 46″, W 79° 58′ 56″
+             */
+            $sFound    = $aData[0];
+            $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
+            $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
+        } elseif (preg_match('/\\b([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″" ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″" ]+([EW])\\b/', $sQuery, $aData)) {
+            /*                    1            2            3            4          5            6            7            8
+             * degrees decimal seconds
+             * 40 26 46 N 79 58 56 W
+             * 40° 26′ 46″ N, 79° 58′ 56″ W
+             */
+            $sFound    = $aData[0];
+            $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
+            $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
+        } elseif (preg_match('/\\b([NS])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*[, ]+([EW])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*\\b/', $sQuery, $aData)) {
+            /*                    1        2                               3        4
+             * degrees decimal
+             * N 40.446° W 79.982°
+             */
+            $sFound    = $aData[0];
+            $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
+            $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
+        } elseif (preg_match('/\\b([0-9]+[0-9]*\\.[0-9]+)[° ]+([NS])[, ]+([0-9]+[0-9]*\\.[0-9]+)[° ]+([EW])\\b/', $sQuery, $aData)) {
+            /*                    1                           2          3                           4
+             * degrees decimal
+             * 40.446° N 79.982° W
+             */
+            $sFound    = $aData[0];
+            $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
+            $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
+        } elseif (preg_match('/(\\[|^|\\b)(-?[0-9]+[0-9]*\\.[0-9]+)[, ]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]|$|\\b)/', $sQuery, $aData)) {
+            /*                 1          2                             3                        4
+             * degrees decimal
+             * 12.34, 56.78
+             * [12.456,-78.90]
+             */
+            $sFound    = $aData[0];
+            $fQueryLat = $aData[2];
+            $fQueryLon = $aData[3];
+        } else {
+            return false;
+        }
+
+        $oPt = new NearPoint($fQueryLat, $fQueryLon);
+
+        if (!$oPt->isValid()) return false;
+
+        $sQuery = trim(str_replace($sFound, ' ', $sQuery));
+
+        return array('pt' => $oPt, 'query' => $sQuery);
+    }
+}
index de100fdcbc6a9314bb21b77e7d322aa4a4b33408..a535191879c3733329a2f528c3fd37f4931bd038 100644 (file)
@@ -599,89 +599,6 @@ function addQuotes($s)
     return "'".$s."'";
 }
 
-function validLatLon($fLat, $fLon)
-{
-    return ($fLat <= 90.1 && $fLat >= -90.1 && $fLon <= 180.1 && $fLon >= -180.1);
-}
-
-function looksLikeLatLonPair($sQuery)
-{
-    // Do we have anything that looks like a lat/lon pair?
-    // returns array(lat,lon,query_with_lat_lon_removed)
-    // or null
-    $sFound    = null;
-    $fQueryLat = null;
-    $fQueryLon = null;
-
-    if (preg_match('/\\b([NS])[ ]+([0-9]+[0-9.]*)[° ]+([0-9.]+)?[′\']*[, ]+([EW])[ ]+([0-9]+)[° ]+([0-9]+[0-9.]*)[′\']*?\\b/', $sQuery, $aData)) {
-        /*              1         2                   3                  4         5            6
-         * degrees decimal minutes
-         * N 40 26.767, W 79 58.933
-         * N 40°26.767′, W 79°58.933′
-         */
-        $sFound    = $aData[0];
-        $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
-        $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
-    } elseif (preg_match('/\\b([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\']*[ ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\' ]+([EW])\\b/', $sQuery, $aData)) {
-        /*                    1             2                      3          4            5                    6
-         * degrees decimal minutes
-         * 40 26.767 N, 79 58.933 W
-         * 40° 26.767′ N 79° 58.933′ W
-         */
-        $sFound    = $aData[0];
-        $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
-        $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
-    } elseif (preg_match('/\\b([NS])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*[, ]+([EW])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*\\b/', $sQuery, $aData)) {
-        /*                    1        2            3            4                5        6            7            8
-         * degrees decimal seconds
-         * N 40 26 46 W 79 58 56
-         * N 40° 26′ 46″, W 79° 58′ 56″
-         */
-        $sFound    = $aData[0];
-        $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
-        $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
-    } elseif (preg_match('/\\b([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″" ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″" ]+([EW])\\b/', $sQuery, $aData)) {
-        /*                    1            2            3            4          5            6            7            8
-         * degrees decimal seconds
-         * 40 26 46 N 79 58 56 W
-         * 40° 26′ 46″ N, 79° 58′ 56″ W
-         */
-        $sFound    = $aData[0];
-        $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
-        $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
-    } elseif (preg_match('/\\b([NS])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*[, ]+([EW])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*\\b/', $sQuery, $aData)) {
-        /*                    1        2                               3        4
-         * degrees decimal
-         * N 40.446° W 79.982°
-         */
-        $sFound    = $aData[0];
-        $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
-        $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
-    } elseif (preg_match('/\\b([0-9]+[0-9]*\\.[0-9]+)[° ]+([NS])[, ]+([0-9]+[0-9]*\\.[0-9]+)[° ]+([EW])\\b/', $sQuery, $aData)) {
-        /*                    1                           2          3                           4
-         * degrees decimal
-         * 40.446° N 79.982° W
-         */
-        $sFound    = $aData[0];
-        $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
-        $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
-    } elseif (preg_match('/(\\[|^|\\b)(-?[0-9]+[0-9]*\\.[0-9]+)[, ]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]|$|\\b)/', $sQuery, $aData)) {
-        /*                 1          2                             3                        4
-         * degrees decimal
-         * 12.34, 56.78
-         * [12.456,-78.90]
-         */
-        $sFound    = $aData[0];
-        $fQueryLat = $aData[2];
-        $fQueryLon = $aData[3];
-    }
-
-    if (!validLatLon($fQueryLat, $fQueryLon)) return;
-    $sQuery = trim(str_replace($sFound, ' ', $sQuery));
-
-    return array('lat' => $fQueryLat, 'lon' => $fQueryLon, 'query' => $sQuery);
-}
-
 
 function geometryText2Points($geometry_as_text, $fRadius)
 {
diff --git a/test/php/Nominatim/NearPointTest.php b/test/php/Nominatim/NearPointTest.php
new file mode 100644 (file)
index 0000000..6fa9b51
--- /dev/null
@@ -0,0 +1,90 @@
+<?php
+
+namespace Nominatim;
+
+require '../../lib/NearPoint.php';
+
+class NearPointTest extends \PHPUnit_Framework_TestCase
+{
+
+
+    protected function setUp()
+    {
+    }
+
+    public function testExtractFromQuery()
+    {
+        // no coordinates expected
+        $this->assertFalse(NearPoint::extractFromQuery(''));
+        $this->assertFalse(NearPoint::extractFromQuery('abc'));
+        $this->assertFalse(NearPoint::extractFromQuery('12 34'));
+        $this->assertFalse(NearPoint::extractFromQuery('200.1 89.9')); // because latitude > 180
+
+        // coordinates expected
+        $this->assertNotNull(NearPoint::extractFromQuery('0.0 -0.0'));
+
+        $aRes = NearPoint::extractFromQuery(' abc 12.456 -78.90 def ');
+        $this->assertEquals($aRes['pt']->lat(), 12.456);
+        $this->assertEquals($aRes['pt']->lon(), -78.90);
+        $this->assertEquals($aRes['pt']->radius(), 0.1);
+        $this->assertEquals($aRes['query'], 'abc   def');
+
+        $aRes = NearPoint::extractFromQuery(' [12.456,-78.90] ');
+        $this->assertEquals($aRes['pt']->lat(), 12.456);
+        $this->assertEquals($aRes['pt']->lon(), -78.90);
+        $this->assertEquals($aRes['pt']->radius(), 0.1);
+        $this->assertEquals($aRes['query'], '');
+
+        // http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
+        // these all represent the same location
+        $aQueries = array(
+                     '40 26.767 N 79 58.933 W',
+                     '40° 26.767′ N 79° 58.933′ W',
+                     "40° 26.767' N 79° 58.933' W",
+                     'N 40 26.767, W 79 58.933',
+                     'N 40°26.767′, W 79°58.933′',
+                     "N 40°26.767', W 79°58.933'",
+                     '40 26 46 N 79 58 56 W',
+                     '40° 26′ 46″ N 79° 58′ 56″ W',
+                     'N 40 26 46 W 79 58 56',
+                     'N 40° 26′ 46″, W 79° 58′ 56″',
+                     'N 40° 26\' 46", W 79° 58\' 56"',
+                     '40.446 -79.982',
+                     '40.446,-79.982',
+                     '40.446° N 79.982° W',
+                     'N 40.446° W 79.982°',
+                     '[40.446 -79.982]',
+                     '       40.446  ,   -79.982     ',
+                    );
+
+
+        foreach ($aQueries as $sQuery) {
+            $aRes = NearPoint::extractFromQuery($sQuery);
+            $this->assertEquals(40.446, $aRes['pt']->lat(), 'degrees decimal ' . $sQuery, 0.01);
+            $this->assertEquals(-79.982, $aRes['pt']->lon(), 'degrees decimal ' . $sQuery, 0.01);
+        }
+    }
+
+    public function testWithinSQL()
+    {
+        $np = new NearPoint(0.1, 23, 1);
+
+        $this->assertEquals(
+            'ST_DWithin(foo, ST_SetSRID(ST_Point(23,0.1),4326), 1.000000)',
+            $np->withinSQL('foo')
+        );
+    }
+
+    public function testDistanceSQL()
+    {
+        $np = new NearPoint(0.1, 23, 1);
+
+        $this->assertEquals(
+            'ST_Distance(ST_SetSRID(ST_Point(23,0.1),4326), foo)',
+            $np->distanceSQL('foo')
+        );
+    }
+}
index f8ba14c16b3f955c90a7483d00e091289af72771..a651fe40ca130102e893cc0382f3c8e9e5a16f46 100644 (file)
@@ -66,62 +66,6 @@ class NominatimTest extends \PHPUnit_Framework_TestCase
     }
 
 
-    public function testLooksLikeLatLonPair()
-    {
-        // no coordinates expected
-        $this->assertNull(looksLikeLatLonPair(''));
-        $this->assertNull(looksLikeLatLonPair('abc'));
-        $this->assertNull(looksLikeLatLonPair('12 34'));
-        $this->assertNull(looksLikeLatLonPair('200.1 89.9')); // because latitude > 180
-
-        // coordinates expected
-        $this->assertNotNull(looksLikeLatLonPair('0.0 -0.0'));
-
-        $this->assertEquals(
-            array( 'lat' => 12.456, 'lon' => -78.90, 'query' => 'abc   def'),
-            looksLikeLatLonPair(' abc 12.456 -78.90 def ')
-        );
-
-        $this->assertEquals(
-            array( 'lat' => 12.456, 'lon' => -78.90, 'query' => ''),
-            looksLikeLatLonPair(' [12.456,-78.90] ')
-        );
-
-        // http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
-        // these all represent the same location
-        $aQueries = array(
-                     '40 26.767 N 79 58.933 W',
-                     '40° 26.767′ N 79° 58.933′ W',
-                     "40° 26.767' N 79° 58.933' W",
-                     'N 40 26.767, W 79 58.933',
-                     'N 40°26.767′, W 79°58.933′',
-                     "N 40°26.767', W 79°58.933'",
-                     '40 26 46 N 79 58 56 W',
-                     '40° 26′ 46″ N 79° 58′ 56″ W',
-                     'N 40 26 46 W 79 58 56',
-                     'N 40° 26′ 46″, W 79° 58′ 56″',
-                     'N 40° 26\' 46", W 79° 58\' 56"',
-                     '40.446 -79.982',
-                     '40.446,-79.982',
-                     '40.446° N 79.982° W',
-                     'N 40.446° W 79.982°',
-                     '[40.446 -79.982]',
-                     '       40.446  ,   -79.982     ',
-                    );
-
-
-        foreach ($aQueries as $sQuery) {
-            $aRes = looksLikeLatLonPair($sQuery);
-            $this->assertEquals(40.446, $aRes['lat'], 'degrees decimal ' . $sQuery, 0.01);
-            $this->assertEquals(-79.982, $aRes['lon'], 'degrees decimal ' . $sQuery, 0.01);
-        }
-    }
-
-
-
     public function testGetWordSets()
     {
         // given an array of arrays like