]> git.openstreetmap.org Git - nominatim.git/blobdiff - website/search.php
over tokenising 'la'
[nominatim.git] / website / search.php
index 3a9295c25724b56b51289d733464da5ff4f6c0fe..0de922f990a2f549452bc213bcf49fa1d0912f9d 100755 (executable)
@@ -54,6 +54,8 @@
        }
 
   // Only certain ranks of feature
+       if (isset($_GET['featureType']) && !isset($_GET['featuretype'])) $_GET['featuretype'] = $_GET['featureType'];
+
        if (isset($_GET['featuretype']))
        {
                switch($_GET['featuretype'])
                        $sQuery = preg_replace('/\\bil\\b/','illinois', $sQuery);
                        $sQuery = preg_replace('/\\bal\\b/','alabama', $sQuery);
                        $sQuery = preg_replace('/\\bla\\b/','louisiana', $sQuery);
+                       $sQuery = preg_replace('/\\bde louisiana\\b/','de la', $sQuery);
                }
 
                // If we have a view box create the SQL
                                                                
                                                                if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') // & in
                                                                {
-                                                                       $sSQL = "select rank_search from placex where place_id in ($sPlaceIDs) order by rank_search asc limit 1";
+                                                                       $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
+                                                                       $bCacheTable = $oDB->getOne($sSQL);
+
+                                                                       $sSQL = "select min(rank_search) from placex where place_id in ($sPlaceIDs)";
 
                                                                        if (CONST_Debug) var_dump($sSQL);
-                                                                       $iMaxRank = ((int)$oDB->getOne($sSQL)) + 5;
+                                                                       $iMaxRank = ((int)$oDB->getOne($sSQL));
 
+                                                                       // For state / country level searches the normal radius search doesn't work very well
+                                                                       $sPlaceGeom = false;
+                                                                       if ($iMaxRank < 9 && $bCacheTable)
+                                                                       {
+                                                                               // Try and get a polygon to search in instead
+       $sSQL = "select geometry from placex where place_id in ($sPlaceIDs) and rank_search < $iMaxRank + 5 and st_geometrytype(geometry) in ('ST_Polygon','ST_MultiPolygon') order by rank_search asc limit 1";
+       if (CONST_Debug) var_dump($sSQL);
+       $sPlaceGeom = $oDB->getOne($sSQL);
+                                                                       }
+                                                                       
+                                                                       if ($sPlaceGeom)
+                                                                       {
+                                                                               $sPlaceIDs = false;
+                                                                       }
+                                                                       else
+                                                                       {
+                                                                               $iMaxRank += 5;
                                                                        $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and rank_search < $iMaxRank";
                                                                        if (CONST_Debug) var_dump($sSQL);
                                                                        $aPlaceIDs = $oDB->getCol($sSQL);
                                                                        $sPlaceIDs = join(',',$aPlaceIDs);
+                                                                       }
 
-                                                                       if ($sPlaceIDs)
+                                                                       if ($sPlaceIDs || $sPlaceGeom)
                                                                        {
 
                                                                        $fRange = 0.01;
-                                                                       $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
-                                                                       if ($oDB->getOne($sSQL))
+                                                                       if ($bCacheTable)
                                                                        {
                                                                                // More efficient - can make the range bigger
-                                                                       $fRange = 0.05;
-                                                                               $sSQL = "select l.place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
+                                                                               $fRange = 0.05;
+
+                                                                               $sOrderBySQL = '';
+                                                                               if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.centroid)";
+                                                                               else if ($sPlaceIDs) $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
+                                                                               else if ($sPlaceGeom) $sOrderBysSQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
+                                                                               
+                                                                               $sSQL = "select distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'')." from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
                                                                                if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
-                                                                               $sSQL .= ",placex as f where ";
-                                                                               $sSQL .= "f.place_id in ($sPlaceIDs) and ST_DWithin(l.centroid, st_centroid(f.geometry), $fRange) ";
+                                                                               if ($sPlaceIDs)
+                                                                               {
+                                                                                       $sSQL .= ",placex as f where ";
+                                                                                       $sSQL .= "f.place_id in ($sPlaceIDs) and ST_DWithin(l.centroid, st_centroid(f.geometry), $fRange) ";
+                                                                               }
+                                                                               if ($sPlaceGeom)
+                                                                               {
+                                                                                       $sSQL .= " where ";
+                                                                                       $sSQL .= "ST_Contains('".$sPlaceGeom."', l.centroid) ";
+                                                                               }
                                                                                if (sizeof($aExcludePlaceIDs))
                                                                                {
                                                                                        $sSQL .= " and l.place_id not in (".join(',',$aExcludePlaceIDs).")";
                                                                                }
                                                                                if ($sCountryCodesSQL) $sSQL .= " and lp.country_code in ($sCountryCodesSQL)";
-                                                                               if ($sNearPointSQL) $sSQL .= " order by ST_Distance($sNearPointSQL, l.centroid) ASC";
-                                                                               else $sSQL .= " order by ST_Distance(l.centroid, f.geometry) asc";
+                                                                               if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
+                                                                               if ($iOffset) $sSQL .= " offset $iOffset";
                                                                                $sSQL .= " limit $iLimit";
                                                                                if (CONST_Debug) var_dump($sSQL);
                                                                                $aPlaceIDs = $oDB->getCol($sSQL);
                                                                        else
                                                                        {
                                                                                if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
-                                                                               $sSQL = "select l.place_id from placex as l,placex as f where ";
+
+                                                                               $sOrderBySQL = '';
+                                                                               if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.geometry)";
+                                                                               else $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
+
+                                                                               $sSQL = "select distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'')." from placex as l,placex as f where ";
                                                                                $sSQL .= "f.place_id in ( $sPlaceIDs) and ST_DWithin(l.geometry, st_centroid(f.geometry), $fRange) ";
                                                                                $sSQL .= "and l.class='".$aSearch['sClass']."' and l.type='".$aSearch['sType']."' ";
                                                                                if (sizeof($aExcludePlaceIDs))
                                                                                        $sSQL .= " and l.place_id not in (".join(',',$aExcludePlaceIDs).")";
                                                                                }
                                                                                if ($sCountryCodesSQL) $sSQL .= " and l.country_code in ($sCountryCodesSQL)";                                                           
-                                                                               if ($sNearPointSQL) $sSQL .= " order by ST_Distance($sNearPointSQL, l.geometry) ASC";
-                                                                               else $sSQL .= " order by ST_Distance(l.geometry, f.geometry) asc, l.rank_search ASC";
+                                                                               if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
+                                                                               if ($iOffset) $sSQL .= " offset $iOffset";
                                                                                $sSQL .= " limit $iLimit";
                                                                                if (CONST_Debug) var_dump($sSQL);
                                                                                $aPlaceIDs = $oDB->getCol($sSQL);