]> git.openstreetmap.org Git - nominatim.git/blobdiff - lib/Geocode.php
replace NearPoint with a more generic context object
[nominatim.git] / lib / Geocode.php
index cce38de96b3cdcc248b94af0db281c4be8282048..a6baa96be830ef87560083a1eb4e595f80198215 100644 (file)
@@ -2,9 +2,10 @@
 
 namespace Nominatim;
 
 
 namespace Nominatim;
 
-require_once(CONST_BasePath.'/lib/NearPoint.php');
 require_once(CONST_BasePath.'/lib/PlaceLookup.php');
 require_once(CONST_BasePath.'/lib/ReverseGeocode.php');
 require_once(CONST_BasePath.'/lib/PlaceLookup.php');
 require_once(CONST_BasePath.'/lib/ReverseGeocode.php');
+require_once(CONST_BasePath.'/lib/SearchDescription.php');
+require_once(CONST_BasePath.'/lib/SearchContext.php');
 
 class Geocode
 {
 
 class Geocode
 {
@@ -367,7 +368,7 @@ class Geocode
         $this->aAddressRankList = array();
 
         $this->aStructuredQuery = array();
         $this->aAddressRankList = array();
 
         $this->aStructuredQuery = array();
-        $this->sAllowedTypesSQLList = False;
+        $this->sAllowedTypesSQLList = false;
 
         $this->loadStructuredAddressElement($sAmenity, 'amenity', 26, 30, false);
         $this->loadStructuredAddressElement($sStreet, 'street', 26, 30, false);
 
         $this->loadStructuredAddressElement($sAmenity, 'amenity', 26, 30, false);
         $this->loadStructuredAddressElement($sStreet, 'street', 26, 30, false);
@@ -412,8 +413,7 @@ class Geocode
         if (sizeof($aPlaceIDs) == 0) return array();
 
         $sLanguagePrefArraySQL = getArraySQL(
         if (sizeof($aPlaceIDs) == 0) return array();
 
         $sLanguagePrefArraySQL = getArraySQL(
-            array_map("getDBQuoted",
-            $this->aLangPrefOrder)
+            array_map("getDBQuoted", $this->aLangPrefOrder)
         );
 
         // Get the details for display (is this a redundant extra step?)
         );
 
         // Get the details for display (is this a redundant extra step?)
@@ -740,12 +740,15 @@ class Geocode
 
                         // If the token is valid
                         if (isset($aValidTokens[' '.$sToken])) {
 
                         // If the token is valid
                         if (isset($aValidTokens[' '.$sToken])) {
-                            // Recheck if the original word shows up in the query.
-                            $bWordInQuery = false;
-                            if (isset($aSearchTerm['word']) && $aSearchTerm['word']) {
-                                $bWordInQuery = $this->normTerm($aSearchTerm['word'])) !== false;
-                            }
                             foreach ($aValidTokens[' '.$sToken] as $aSearchTerm) {
                             foreach ($aValidTokens[' '.$sToken] as $aSearchTerm) {
+                                // Recheck if the original word shows up in the query.
+                                $bWordInQuery = false;
+                                if (isset($aSearchTerm['word']) && $aSearchTerm['word']) {
+                                    $bWordInQuery = strpos(
+                                        $sNormQuery,
+                                        $this->normTerm($aSearchTerm['word'])
+                                    ) !== false;
+                                }
                                 $aNewSearches = $oCurrentSearch->extendWithFullTerm(
                                     $aSearchTerm,
                                     $bWordInQuery,
                                 $aNewSearches = $oCurrentSearch->extendWithFullTerm(
                                     $aSearchTerm,
                                     $bWordInQuery,
@@ -785,18 +788,17 @@ class Geocode
                                         $aNewWordsetSearches[] = $oSearch;
                                     }
                                 }
                                         $aNewWordsetSearches[] = $oSearch;
                                     }
                                 }
-
                             }
                         }
                     }
                     // Sort and cut
                             }
                         }
                     }
                     // Sort and cut
-                    usort($aNewWordsetSearches, 'bySearchRank');
+                    usort($aNewWordsetSearches, array('Nominatim\SearchDescription', 'bySearchRank'));
                     $aWordsetSearches = array_slice($aNewWordsetSearches, 0, 50);
                 }
                 //var_Dump('<hr>',sizeof($aWordsetSearches)); exit;
 
                 $aNewPhraseSearches = array_merge($aNewPhraseSearches, $aNewWordsetSearches);
                     $aWordsetSearches = array_slice($aNewWordsetSearches, 0, 50);
                 }
                 //var_Dump('<hr>',sizeof($aWordsetSearches)); exit;
 
                 $aNewPhraseSearches = array_merge($aNewPhraseSearches, $aNewWordsetSearches);
-                usort($aNewPhraseSearches, 'bySearchRank');
+                usort($aNewPhraseSearches, array('Nominatim\SearchDescription', 'bySearchRank'));
 
                 $aSearchHash = array();
                 foreach ($aNewPhraseSearches as $iSearch => $aSearch) {
 
                 $aSearchHash = array();
                 foreach ($aNewPhraseSearches as $iSearch => $aSearch) {
@@ -835,12 +837,12 @@ class Geocode
         // Revisit searches, drop bad searches and give penalty to unlikely combinations.
         $aGroupedSearches = array();
         foreach ($aSearches as $oSearch) {
         // Revisit searches, drop bad searches and give penalty to unlikely combinations.
         $aGroupedSearches = array();
         foreach ($aSearches as $oSearch) {
-            if (!$oSearch->isValidSearch()) {
+            if (!$oSearch->isValidSearch($this->aCountryCodes)) {
                 continue;
             }
 
             $iRank = $oSearch->addToRank($iGlobalRank);
                 continue;
             }
 
             $iRank = $oSearch->addToRank($iGlobalRank);
-            if (!isset($aGroupedSearches[$iRank]) {
+            if (!isset($aGroupedSearches[$iRank])) {
                 $aGroupedSearches[$iRank] = array();
             }
             $aGroupedSearches[$iRank][] = $oSearch;
                 $aGroupedSearches[$iRank] = array();
             }
             $aGroupedSearches[$iRank][] = $oSearch;
@@ -886,10 +888,11 @@ class Geocode
     {
         if (!$this->sQuery && !$this->aStructuredQuery) return array();
 
     {
         if (!$this->sQuery && !$this->aStructuredQuery) return array();
 
+        $oCtx = new SearchContext();
+
         $sNormQuery = $this->normTerm($this->sQuery);
         $sLanguagePrefArraySQL = getArraySQL(
         $sNormQuery = $this->normTerm($this->sQuery);
         $sLanguagePrefArraySQL = getArraySQL(
-            array_map("getDBQuoted",
-            $this->aLangPrefOrder)
+            array_map("getDBQuoted", $this->aLangPrefOrder)
         );
         $sCountryCodesSQL = false;
         if ($this->aCountryCodes) {
         );
         $sCountryCodesSQL = false;
         if ($this->aCountryCodes) {
@@ -925,20 +928,12 @@ class Geocode
         }
 
         // Do we have anything that looks like a lat/lon pair?
         }
 
         // Do we have anything that looks like a lat/lon pair?
-        $oNearPoint = false;
-        if ($aLooksLike = NearPoint::extractFromQuery($sQuery)) {
-            $oNearPoint = $aLooksLike['pt'];
-            $sQuery = $aLooksLike['query'];
-        }
+        $sQuery = $oCtx->setNearPointFromQuery($sQuery);
 
         $aSearchResults = array();
         if ($sQuery || $this->aStructuredQuery) {
             // Start with a single blank search
 
         $aSearchResults = array();
         if ($sQuery || $this->aStructuredQuery) {
             // Start with a single blank search
-            $aSearches = array(new SearchDescription());
-
-            if ($oNearPoint) {
-                $aSearches[0]->setNear($oNearPoint);
-            }
+            $aSearches = array(new SearchDescription($oCtx));
 
             if ($sQuery) {
                 $sQuery = $aSearches[0]->extractKeyValuePairs($sQuery);
 
             if ($sQuery) {
                 $sQuery = $aSearches[0]->extractKeyValuePairs($sQuery);
@@ -983,7 +978,7 @@ class Geocode
                         $oNewSearch->setPoiSearch(
                             Operator::TYPE,
                             $aSearchTerm['class'],
                         $oNewSearch->setPoiSearch(
                             Operator::TYPE,
                             $aSearchTerm['class'],
-                            $aSearchTerm['type'],
+                            $aSearchTerm['type']
                         );
                         $aNewSearches[] = $oNewSearch;
                     }
                         );
                         $aNewSearches[] = $oNewSearch;
                     }
@@ -1077,8 +1072,8 @@ class Geocode
 
                 foreach ($aTokens as $sToken) {
                     // Unknown single word token with a number - assume it is a house number
 
                 foreach ($aTokens as $sToken) {
                     // Unknown single word token with a number - assume it is a house number
-                    if (!isset($aValidTokens[' '.$sToken]) && strpos($sToken, ' ') === false && preg_match('/[0-9]/', $sToken)) {
-                        $aValidTokens[' '.$sToken] = array(array('class' => 'place', 'type' => 'house'));
+                    if (!isset($aValidTokens[' '.$sToken]) && strpos($sToken, ' ') === false && preg_match('/^[0-9]+$/', $sToken)) {
+                        $aValidTokens[' '.$sToken] = array(array('class' => 'place', 'type' => 'house', 'word_token' => ' '.$sToken));
                     }
                 }
 
                     }
                 }
 
@@ -1165,7 +1160,7 @@ class Geocode
                         }
                     } elseif (!$oSearch->isNamedSearch()) {
                         // looking for a POI in a geographic area
                         }
                     } elseif (!$oSearch->isNamedSearch()) {
                         // looking for a POI in a geographic area
-                        if (!$bBoundingBoxSearch && !$oSearch->isNearSearch()) {
+                        if (!$bBoundingBoxSearch && !$oCtx->hasNearPoint()) {
                             continue;
                         }
 
                             continue;
                         }
 
@@ -1173,13 +1168,13 @@ class Geocode
                             $this->oDB,
                             $sCountryCodesSQL,
                             $bBoundingBoxSearch ? $this->sViewboxSmallSQL : '',
                             $this->oDB,
                             $sCountryCodesSQL,
                             $bBoundingBoxSearch ? $this->sViewboxSmallSQL : '',
-                            $sViewboxCentreSQL,
+                            $this->sViewboxCentreSQL,
                             $this->aExcludePlaceIDs ? join(',', $this->aExcludePlaceIDs) : '',
                             $this->iLimit
                         );
                     } elseif ($oSearch->isOperator(Operator::POSTCODE)) {
                         $aPlaceIDs = $oSearch->queryPostcode(
                             $this->aExcludePlaceIDs ? join(',', $this->aExcludePlaceIDs) : '',
                             $this->iLimit
                         );
                     } elseif ($oSearch->isOperator(Operator::POSTCODE)) {
                         $aPlaceIDs = $oSearch->queryPostcode(
-                            $oDB,
+                            $this->oDB,
                             $sCountryCodesSQL,
                             $this->iLimit
                         );
                             $sCountryCodesSQL,
                             $this->iLimit
                         );
@@ -1210,13 +1205,13 @@ class Geocode
                             $aResult = $oSearch->queryHouseNumber(
                                 $this->oDB,
                                 $aPlaceIDs,
                             $aResult = $oSearch->queryHouseNumber(
                                 $this->oDB,
                                 $aPlaceIDs,
-                                $this->aExcludePlaceIDs ? join(',', $this->aExcludePlaceIDs) : ''
+                                $this->aExcludePlaceIDs ? join(',', $this->aExcludePlaceIDs) : '',
                                 $this->iLimit
                             );
 
                             if (sizeof($aResult)) {
                                 $searchedHousenumber = $aResult['iHouseNumber'];
                                 $this->iLimit
                             );
 
                             if (sizeof($aResult)) {
                                 $searchedHousenumber = $aResult['iHouseNumber'];
-                                $aPlaceIDs = $aResults['aPlaceIDs'];
+                                $aPlaceIDs = $aResult['aPlaceIDs'];
                             } elseif (!$oSearch->looksLikeFullAddress()) {
                                 $aPlaceIDs = array();
                             }
                             } elseif (!$oSearch->looksLikeFullAddress()) {
                                 $aPlaceIDs = array();
                             }
@@ -1227,7 +1222,7 @@ class Geocode
                             $aPlaceIDs = $oSearch->queryPoiByOperator(
                                 $this->oDB,
                                 $aPlaceIDs,
                             $aPlaceIDs = $oSearch->queryPoiByOperator(
                                 $this->oDB,
                                 $aPlaceIDs,
-                                $this->aExcludePlaceIDs ? join(',', $this->aExcludePlaceIDs) : ''
+                                $this->aExcludePlaceIDs ? join(',', $this->aExcludePlaceIDs) : '',
                                 $this->iLimit
                             );
                         }
                                 $this->iLimit
                             );
                         }
@@ -1318,11 +1313,7 @@ class Geocode
             $oReverse = new ReverseGeocode($this->oDB);
             $oReverse->setZoom(18);
 
             $oReverse = new ReverseGeocode($this->oDB);
             $oReverse->setZoom(18);
 
-            $aLookup = $oReverse->lookup(
-                $oNearPoint->lat(),
-                $oNearPoint->lon(),
-                false
-            );
+            $aLookup = $oReverse->lookupPoint($oCtx->sqlNear, false);
 
             if (CONST_Debug) var_dump("Reverse search", $aLookup);
 
 
             if (CONST_Debug) var_dump("Reverse search", $aLookup);