]> git.openstreetmap.org Git - nominatim.git/blobdiff - lib/SearchDescription.php
move country list to SearchContext
[nominatim.git] / lib / SearchDescription.php
index 7073186b09a778f9c528fde56b90199d9f7084d0..39f7ffca90910c9b2b416d952156982ef90fd9ec 100644 (file)
@@ -109,13 +109,13 @@ class SearchDescription
         return 'place_classtype_'.$this->sClass.'_'.$this->sType;
     }
 
-    public function countryCodeSQL($sVar, $sCountryList)
+    public function countryCodeSQL($sVar)
     {
         if ($this->sCountryCode) {
             return $sVar.' = \''.$this->sCountryCode."'";
         }
-        if ($sCountryList) {
-            return $sVar.' in ('.$sCountryList.')';
+        if ($this->oContext->sqlCountryList) {
+            return $sVar.' in '.$this->oContext->sqlCountryList;
         }
 
         return '';
@@ -367,13 +367,13 @@ class SearchDescription
     /////////// Query functions
 
 
-    public function queryCountry(&$oDB, $sViewboxSQL)
+    public function queryCountry(&$oDB)
     {
         $sSQL = 'SELECT place_id FROM placex ';
         $sSQL .= "WHERE country_code='".$this->sCountryCode."'";
         $sSQL .= ' AND rank_search = 4';
-        if ($sViewboxSQL) {
-            $sSQL .= " AND ST_Intersects($sViewboxSQL, geometry)";
+        if ($this->oContext->bViewboxBounded) {
+            $sSQL .= ' AND ST_Intersects('.$this->oContext->sqlViewboxSmall.', geometry)';
         }
         $sSQL .= " ORDER BY st_area(geometry) DESC LIMIT 1";
 
@@ -382,7 +382,7 @@ class SearchDescription
         return chksql($oDB->getCol($sSQL));
     }
 
-    public function queryNearbyPoi(&$oDB, $sCountryList, $sViewboxSQL, $sViewboxCentreSQL, $sExcludeSQL, $iLimit)
+    public function queryNearbyPoi(&$oDB, $iLimit)
     {
         if (!$this->sClass) {
             return array();
@@ -393,22 +393,21 @@ class SearchDescription
         $sSQL = 'SELECT count(*) FROM pg_tables WHERE tablename = \''.$sPoiTable."'";
         if (chksql($oDB->getOne($sSQL))) {
             $sSQL = 'SELECT place_id FROM '.$sPoiTable.' ct';
-            if ($sCountryList) {
+            if ($this->oContext->sqlCountryList) {
                 $sSQL .= ' JOIN placex USING (place_id)';
             }
             if ($this->oContext->hasNearPoint()) {
                 $sSQL .= ' WHERE '.$this->oContext->withinSQL('ct.centroid');
-            } else {
-                $sSQL .= " WHERE ST_Contains($sViewboxSQL, ct.centroid)";
-            }
-            if ($sCountryList) {
-                $sSQL .= " AND country_code in ($sCountryList)";
+            } else if ($this->oContext->bViewboxBounded) {
+                $sSQL .= ' WHERE ST_Contains('.$this->oContext->sqlViewboxSmall.', ct.centroid)';
             }
-            if ($sExcludeSQL) {
-                $sSQL .= ' AND place_id not in ('.$sExcludeSQL.')';
+            if ($this->oContext->sqlCountryList) {
+                $sSQL .= ' AND country_code in '.$this->oContext->sqlCountryList;
             }
-            if ($sViewboxCentreSQL) {
-                $sSQL .= " ORDER BY ST_Distance($sViewboxCentreSQL, ct.centroid) ASC";
+            $sSQL .= $this->oContext->excludeSQL(' AND place_id');
+            if ($this->oContext->sqlViewboxCentre) {
+                $sSQL .= ' ORDER BY ST_Distance(';
+                $sSQL .= $this->oContext->sqlViewboxCentre.', ct.centroid) ASC';
             } elseif ($this->oContext->hasNearPoint()) {
                 $sSQL .= ' ORDER BY '.$this->oContext->distanceSQL('ct.centroid').' ASC';
             }
@@ -422,8 +421,8 @@ class SearchDescription
             $sSQL .= 'class=\''.$this->sClass."' and type='".$this->sType."'";
             $sSQL .= ' AND '.$this->oContext->withinSQL('geometry');
             $sSQL .= ' AND linked_place_id is null';
-            if ($sCountryList) {
-                $sSQL .= " AND country_code in ($sCountryList)";
+            if ($this->oContext->sqlCountryList) {
+                $sSQL .= ' AND country_code in '.$this->oContext->sqlCountryList;
             }
             $sSQL .= ' ORDER BY '.$this->oContext->distanceSQL('centroid')." ASC";
             $sSQL .= " LIMIT $iLimit";
@@ -434,7 +433,7 @@ class SearchDescription
         return array();
     }
 
-    public function queryPostcode(&$oDB, $sCountryList, $iLimit)
+    public function queryPostcode(&$oDB, $iLimit)
     {
         $sSQL = 'SELECT p.place_id FROM location_postcode p ';
 
@@ -448,10 +447,8 @@ class SearchDescription
         }
 
         $sSQL .= "p.postcode = '".reset($this->aName)."'";
-        $sCountryTerm = $this->countryCodeSQL('p.country_code', $sCountryList);
-        if ($sCountryTerm) {
-            $sSQL .= ' AND '.$sCountryTerm;
-        }
+        $sSQL .= $this->countryCodeSQL(' AND p.country_code');
+        $sSQL .= $this->oContext->excludeSQL(' AND p.place_id');
         $sSQL .= " LIMIT $iLimit";
 
         if (CONST_Debug) var_dump($sSQL);
@@ -459,7 +456,7 @@ class SearchDescription
         return chksql($oDB->getCol($sSQL));
     }
 
-    public function queryNamedPlace(&$oDB, $aWordFrequencyScores, $sCountryList, $iMinAddressRank, $iMaxAddressRank, $sExcludeSQL, $sViewboxSmall, $sViewboxLarge, $iLimit)
+    public function queryNamedPlace(&$oDB, $aWordFrequencyScores, $iMinAddressRank, $iMaxAddressRank, $iLimit)
     {
         $aTerms = array();
         $aOrder = array();
@@ -506,7 +503,7 @@ class SearchDescription
             }
         }
 
-        $sCountryTerm = $this->countryCodeSQL('country_code', $sCountryList);
+        $sCountryTerm = $this->countryCodeSQL('country_code');
         if ($sCountryTerm) {
             $aTerms[] = $sCountryTerm;
         }
@@ -533,12 +530,13 @@ class SearchDescription
             }
         }
 
+        $sExcludeSQL = $this->oContext->excludeSQL('place_id');
         if ($sExcludeSQL) {
-            $aTerms[] = 'place_id not in ('.$sExcludeSQL.')';
+            $aTerms[] = $sExcludeSQL;
         }
 
-        if ($sViewboxSmall) {
-            $aTerms[] = 'centroid && '.$sViewboxSmall;
+        if ($this->oContext->bViewboxBounded) {
+            $aTerms[] = 'centroid && '.$this->oContext->sqlViewboxSmall;
         }
 
         if ($this->oContext->hasNearPoint()) {
@@ -550,12 +548,7 @@ class SearchDescription
         } else {
             $sImportanceSQL = '(CASE WHEN importance = 0 OR importance IS NULL THEN 0.75-(search_rank::float/40) ELSE importance END)';
         }
-        if ($sViewboxSmall) {
-            $sImportanceSQL .= " * CASE WHEN ST_Contains($sViewboxSmall, centroid) THEN 1 ELSE 0.5 END";
-        }
-        if ($sViewboxLarge) {
-            $sImportanceSQL .= " * CASE WHEN ST_Contains($sViewboxLarge, centroid) THEN 1 ELSE 0.5 END";
-        }
+        $sImportanceSQL .= $this->oContext->viewboxImportanceSQL('centroid');
         $aOrder[] = "$sImportanceSQL DESC";
 
         if (sizeof($this->aFullNameAddress)) {
@@ -593,8 +586,7 @@ class SearchDescription
         return array();
     }
 
-
-    public function queryHouseNumber(&$oDB, $aRoadPlaceIDs, $sExcludeSQL, $iLimit)
+    public function queryHouseNumber(&$oDB, $aRoadPlaceIDs, $iLimit)
     {
         $sPlaceIDs = join(',', $aRoadPlaceIDs);
 
@@ -602,9 +594,7 @@ class SearchDescription
         $sSQL = 'SELECT place_id FROM placex ';
         $sSQL .= 'WHERE parent_place_id in ('.$sPlaceIDs.')';
         $sSQL .= "  AND transliteration(housenumber) ~* E'".$sHouseNumberRegex."'";
-        if ($sExcludeSQL) {
-            $sSQL .= ' AND place_id not in ('.$sExcludeSQL.')';
-        }
+        $sSQL .= $this->oContext->excludeSQL(' AND place_id');
         $sSQL .= " LIMIT $iLimit";
 
         if (CONST_Debug) var_dump($sSQL);
@@ -633,10 +623,7 @@ class SearchDescription
             $sSQL .= " or interpolationtype='all') and ";
             $sSQL .= $iHousenumber.">=startnumber and ";
             $sSQL .= $iHousenumber."<=endnumber";
-
-            if ($sExcludeSQL) {
-                $sSQL .= ' AND place_id not in ('.$sExcludeSQL.')';
-            }
+            $sSQL .= $this->oContext->excludeSQL(' AND place_id');
             $sSQL .= " limit $iLimit";
 
             if (CONST_Debug) var_dump($sSQL);
@@ -653,9 +640,7 @@ class SearchDescription
             $sSQL = 'SELECT place_id FROM location_property_aux';
             $sSQL .= ' WHERE parent_place_id in ('.$sPlaceIDs.')';
             $sSQL .= " AND housenumber = '".$this->sHouseNumber."'";
-            if ($sExcludeSQL) {
-                $sSQL .= " AND place_id not in ($sExcludeSQL)";
-            }
+            $sSQL .= $this->oContext->excludeSQL(' AND place_id');
             $sSQL .= " limit $iLimit";
 
             if (CONST_Debug) var_dump($sSQL);
@@ -679,10 +664,7 @@ class SearchDescription
             $sSQL .= " or interpolationtype='all') and ";
             $sSQL .= $iHousenumber.">=startnumber and ";
             $sSQL .= $iHousenumber."<=endnumber";
-
-            if ($sExcludeSQL) {
-                $sSQL .= ' AND place_id not in ('.$sExcludeSQL.')';
-            }
+            $sSQL .= $this->oContext->excludeSQL(' AND place_id');
             $sSQL .= " limit $iLimit";
 
             if (CONST_Debug) var_dump($sSQL);
@@ -698,7 +680,7 @@ class SearchDescription
     }
 
 
-    public function queryPoiByOperator(&$oDB, $aParentIDs, $sExcludeSQL, $iLimit)
+    public function queryPoiByOperator(&$oDB, $aParentIDs, $iLimit)
     {
         $sPlaceIDs = join(',', $aParentIDs);
         $aClassPlaceIDs = array();
@@ -711,6 +693,7 @@ class SearchDescription
             $sSQL .= "   AND class='".$this->sClass."' ";
             $sSQL .= "   AND type='".$this->sType."'";
             $sSQL .= "   AND linked_place_id is null";
+            $sSQL .= $this->oContext->excludeSQL(' AND place_id');
             $sSQL .= " ORDER BY rank_search ASC ";
             $sSQL .= " LIMIT $iLimit";
 
@@ -787,9 +770,7 @@ class SearchDescription
                         $sSQL .= " WHERE ST_Contains('$sPlaceGeom', l.centroid)";
                     }
 
-                    if ($sExcludeSQL) {
-                        $sSQL .= ' AND l.place_id not in ('.$sExcludeSQL.')';
-                    }
+                    $sSQL .= $this->oContext->excludeSQL(' AND l.place_id');
                     $sSQL .= 'limit 300) i ';
                     if ($sOrderBySQL) {
                         $sSQL .= 'order by order_term asc';
@@ -820,9 +801,7 @@ class SearchDescription
                     $sSQL .= "  AND ST_DWithin(l.geometry, f.centroid, $fRange)";
                     $sSQL .= "  AND l.class='".$this->sClass."'";
                     $sSQL .= "  AND l.type='".$this->sType."'";
-                    if ($sExcludeSQL) {
-                        $sSQL .= " AND l.place_id not in (".$sExcludeSQL.")";
-                    }
+                    $sSQL .= $this->oContext->excludeSQL(' AND l.place_id');
                     if ($sOrderBySQL) {
                         $sSQL .= "ORDER BY orderterm ASC";
                     }