]> git.openstreetmap.org Git - nominatim.git/blobdiff - website/search.php
lots of little bug fixes and missing files
[nominatim.git] / website / search.php
index a1052394410b5dc5e4dc40d1a3f900175c3ad029..cd567385e50628a36ed08af438ac820907ea8b5a 100755 (executable)
@@ -17,6 +17,7 @@
        $bDeDupe = isset($_GET['dedupe'])?(bool)$_GET['dedupe']:true;
        $bReverseInPlan = false;
        $iLimit = isset($_GET['limit'])?(int)$_GET['limit']:10;
+       $iOffset = isset($_GET['offset'])?(int)$_GET['offset']:0;
        $iMaxRank = 20;
        if ($iLimit > 100) $iLimit = 100;
 
@@ -34,7 +35,7 @@
 
        // Prefered language    
        $aLangPrefOrder = getPrefferedLangauges();
-       if (isset($aLangPrefOrder['name:de'])) $bReverseInPlan = true;
+//     if (isset($aLangPrefOrder['name:de'])) $bReverseInPlan = true;
        $sLanguagePrefArraySQL = "ARRAY[".join(',',array_map("getDBQuoted",$aLangPrefOrder))."]";
 
        if (isset($_GET['exclude_place_ids']) && $_GET['exclude_place_ids'])
@@ -62,6 +63,9 @@
        {
                $hLog = logStart($oDB, 'search', $sQuery, $aLangPrefOrder);
 
+               // Hack to make it handle "new york, ny" (and variants) correctly
+                $sQuery = str_ireplace(array('New York, ny','new york, new york', 'New York ny','new york new york'), 'new york city, new york', $sQuery);
+
                // If we have a view box create the SQL
                // Small is the actual view box, Large is double (on each axis) that 
                $sViewboxCentreSQL = $sViewboxSmallSQL = $sViewboxLargeSQL = false;
 
                        // Start with a blank search
                        $aSearches = array(
-                               array('iSearchRank' => 0, 'iNamePhrase' => 0, 'sCountryCode' => false, 'aName'=>array(), 'aAddress'=>array(), 
+                               array('iSearchRank' => 0, 'iNamePhrase' => -1, 'sCountryCode' => false, 'aName'=>array(), 'aAddress'=>array(), 
                                        'sOperator'=>'', 'aFeatureName' => array(), 'sClass'=>'', 'sType'=>'', 'sHouseNumber'=>'', 'fLat'=>'', 'fLon'=>'', 'fRadius'=>'')
                        );
 
                                $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
                                $sToken = $oDB->getOne("select make_standard_name('".$aSpecialTerm[1]."') as string");
                                $sSQL = 'select * from (select word_id,word_token, word, class, type, location, country_code, operator';
-                               $sSQL .= ' from word where word_token in (\' '.$sToken.'\')) as x where (class is not null and class != \'place\') or country_code is not null';
+                               $sSQL .= ' from word where word_token in (\' '.$sToken.'\')) as x where (class is not null and class not in (\'place\',\'highway\')) or country_code is not null';
                                $aSearchWords = $oDB->getAll($sSQL);
                                $aNewSearches = array();
                                foreach($aSearches as $aSearch)
                        // Check which tokens we have, get the ID numbers                       
                        $sSQL = 'select word_id,word_token, word, class, type, location, country_code, operator';
                        $sSQL .= ' from word where word_token in ('.join(',',array_map("getDBQuoted",$aTokens)).')';
-//                     $sSQL .= ' group by word_token, word, class, type, location,country_code';
+                       $sSQL .= ' and (class is null or class not in (\'highway\'))';
+//                     $sSQL .= ' group by word_token, word, class, type, location, country_code';
 
                        if (CONST_Debug) var_Dump($sSQL);
 
                                }
                        }
                        if ($bSuggestion) $sSuggestion = join(', ',$aSuggestion);
-
+/*
                        // Try and calculate GB postcodes we might be missing
                        foreach($aTokens as $sToken)
                        {
                                        }
                                }
                        }
-
+*/
                        // Any words that have failed completely?
                        // TODO: suggestions
 
                                                // Add all words from this wordset
                                                foreach($aWordset as $sToken)
                                                {
+//echo "<br><b>$sToken</b>";
                                                        $aNewWordsetSearches = array();
                                                        
                                                        foreach($aWordsetSearches as $aCurrentSearch)
                                                        {
+//echo "<i>";
+//var_dump($aCurrentSearch);
+//echo "</i>";
+
                                                                // If the token is valid
                                                                if (isset($aValidTokens[' '.$sToken]))
                                                                {
                                                                                        {
                                                                                                $aSearch['sHouseNumber'] = $sToken;
                                                                                                if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
-
+/*
                                                                                                // Fall back to not searching for this item (better than nothing)
                                                                                                $aSearch = $aCurrentSearch;
-                                                                                               $aSearch['iSearchRank'] += 2;
+                                                                                               $aSearch['iSearchRank'] += 1;
                                                                                                if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
+*/
                                                                                        }
                                                                                }
                                                                                elseif ($aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null)
                                                                                {
                                                                                        if (sizeof($aSearch['aName']))
                                                                                        {
-                                                                                               $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
+                                                                                               if (!isset($aValidTokens[$sToken]) || strlen($sToken) < 4)
+                                                                                               {
+                                                                                                       $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
+                                                                                               }
+                                                                                               else
+                                                                                               {
+                                                                                                       $aSearch['iSearchRank'] += 1000; // skip;
+                                                                                               }
                                                                                        }
                                                                                        else
                                                                                        {
                                                                                                $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
-                                                                                               $aSearch['iNamePhrase'] = $iPhrase;
+//                                                                                             $aSearch['iNamePhrase'] = $iPhrase;
                                                                                        }
                                                                                        if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
                                                                                }
                                                                        }
                                                                }
-                                                               if (isset($aValidTokens[$sToken]))
+                                                               if (isset($aValidTokens[$sToken]) && strlen($sToken) >= 4)
                                                                {
                                                                        // Allow searching for a word - but at extra cost
                                                                        foreach($aValidTokens[$sToken] as $aSearchTerm)
                                                                        {
-                                                                               $aSearch = $aCurrentSearch;
-                                                                               $aSearch['iSearchRank']+=5;
-                                                                               $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
-                                                                               if (!sizeof($aSearch['aName']) || $aSearch['iNamePhrase'] == $iPhrase)
+//var_Dump('<hr>',$aSearch['aName']);
+
+                                                                               if (sizeof($aCurrentSearch['aName']))
                                                                                {
+                                                                               $aSearch = $aCurrentSearch;
+                                                                                       $aSearch['iSearchRank'] += 1;
+                                                                                       $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
+                                                                               if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
+                                                                               }
+
+                                                                               if (!sizeof($aCurrentSearch['aName']) || $aCurrentSearch['iNamePhrase'] == $iPhrase)
+                                                                               {
+                                                                               $aSearch = $aCurrentSearch;
+                                                                                       $aSearch['iSearchRank'] += 4;
                                                                                        $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
                                                                                        $aSearch['iNamePhrase'] = $iPhrase;
+                                                                               if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
                                                                                }
-                                                                               if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
                                                                        }
                                                                }
                                                                else
                                                $aSearches = array_merge($aSearches, $aNewSearches);
                                                if ($iSearchCount > 50) break;
                                        }
+
+//                                     if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
+
                                }
                        }
                        else
                                
                                if (CONST_Debug) var_Dump($aGroupedSearches);
 
-                               if ($bReverseInPlan)
-                               {
-                                       foreach($aGroupedSearches as $iGroup => $aSearches)
-                                       {
-                                               foreach($aSearches as $iSearch => $aSearch)
-                                               {
-                                                       if (sizeof($aSearch['aAddress']))
-                                                       {
-                                                               $aReverseSearch = $aSearch;
-                                                               $iReverseItem = array_pop($aSearch['aAddress']);
-                                                               $aReverseSearch['aName'][$iReverseItem] = $iReverseItem;
-                                                               $aGroupedSearches[$iGroup][] = $aReverseSearch;
-                                                       }
-                                               }
-                                       }
-                               }
-
-//var_Dump($aGroupedSearches); exit;
-
-                               // Filter out duplicate searches
-                               $aSearchHash = array();
-                               foreach($aGroupedSearches as $iGroup => $aSearches)
-                               {
-                                       foreach($aSearches as $iSearch => $aSearch)
-                                       {
-                                               $sHash = serialize($aSearch);
-                                               if (isset($aSearchHash[$sHash]))
-                                               {
-                                                       unset($aGroupedSearches[$iGroup][$iSearch]);
-                                                       if (sizeof($aGroupedSearches[$iGroup]) == 0) unset($aGroupedSearches[$iGroup]);
-                                               }
-                                               else
-                                               {
-                                                       $aSearchHash[$sHash] = 1;
-                                               }
-                                       }
-                               }
-                               
-                               if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
-
-                               if ($bReverseInPlan)
+                               if ($bReverseInPlan && false)
                                {
                                        foreach($aGroupedSearches as $iGroup => $aSearches)
                                        {
                                        foreach($aSearches as $aSearch)
                                        {
                                                $iQueryLoop++;
+
                                                // Must have a location term
-                                               if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress'] && !$aSearch['fLon']))
+                                               if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon'])
                                                {
                                                        if (!$bBoundingBoxSearch && !$aSearch['fLon']) continue;
                                                        if (!$aSearch['sClass']) continue;
                                                }
                                                else
                                                {
-                                                       if ($aSearch['aName'] == array(282=>'282')) continue;
-
                                                        if (CONST_Debug) var_dump('<hr>',$aSearch);
                                                        if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);     
                                                        $aPlaceIDs = array();
 
                                                        if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs))
                                                        {
+                                                               $aRoadPlaceIDs = $aPlaceIDs;
                                                                $sPlaceIDs = join(',',$aPlaceIDs);
        
-                                                               $sHouseNumberRegex = '\\\\m'.str_replace(' ','[-, ]',$aSearch['sHouseNumber']).'\\\\M';
-
-                                                               // Make sure everything nearby is indexed (if we pre-indexed houses this wouldn't be needed!)
-                                                               $sSQL = "update placex set indexed = true from placex as f where placex.indexed = false";
-                                                               $sSQL .= " and f.place_id in (".$sPlaceIDs.") and ST_DWithin(placex.geometry, f.geometry, 0.004)";
-                                                               $sSQL .= " and placex.housenumber ~* E'".$sHouseNumberRegex."'";
-                                                               $sSQL .= " and placex.class='place' and placex.type='house'";
-                                                               if (CONST_Debug) var_dump($sSQL);
-                                                               $oDB->query($sSQL);
-                                                       
                                                                // Now they are indexed look for a house attached to a street we found
+                                                               $sHouseNumberRegex = '\\\\m'.str_replace(' ','[-, ]',$aSearch['sHouseNumber']).'\\\\M';                                         
                                                                $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and housenumber ~* E'".$sHouseNumberRegex."'";
                                                                if (sizeof($aExcludePlaceIDs))
                                                                {
                                                                $sSQL .= " limit $iLimit";
                                                                if (CONST_Debug) var_dump($sSQL);
                                                                $aPlaceIDs = $oDB->getCol($sSQL);
+
+                                                               // If not try the aux fallback table
+                                                               if (!sizeof($aPlaceIDs))
+                                                               {
+                                                                       $sSQL = "select place_id from location_property_aux where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
+                                                                       if (sizeof($aExcludePlaceIDs))
+                                                                       {
+                                                                               $sSQL .= " and place_id not in (".join(',',$aExcludePlaceIDs).")";
+                                                                       }
+//                                                                     $sSQL .= " limit $iLimit";
+                                                                       if (CONST_Debug) var_dump($sSQL);
+                                                                       $aPlaceIDs = $oDB->getCol($sSQL);
+                                                               }
+
+                                                               if (!sizeof($aPlaceIDs))
+                                                               {
+                                                                       $sSQL = "select place_id from location_property_tiger where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
+                                                                       if (sizeof($aExcludePlaceIDs))
+                                                                       {
+                                                                               $sSQL .= " and place_id not in (".join(',',$aExcludePlaceIDs).")";
+                                                                       }
+//                                                                     $sSQL .= " limit $iLimit";
+                                                                       if (CONST_Debug) var_dump($sSQL);
+                                                                       $aPlaceIDs = $oDB->getCol($sSQL);
+                                                               }
+
+                                                               // Fallback to the road
+                                                               if (!sizeof($aPlaceIDs))
+                                                               {
+                                                                       $aPlaceIDs = $aRoadPlaceIDs;
+                                                               }
+                                                               
                                                        }
                                                
                                                        if ($aSearch['sClass'] && sizeof($aPlaceIDs))
                                                                                {
                                                                                        $sSQL .= " and l.place_id not in (".join(',',$aExcludePlaceIDs).")";
                                                                                }
-                                                                               if ($sNearPointSQL) $sSQL .= " order by ST_Distance($sNearPointSQL, l.geometry) ASC";
+                                                                               if ($sNearPointSQL) $sSQL .= " order by ST_Distance($sNearPointSQL, l.centroid) ASC";
                                                                                else $sSQL .= " order by ST_Distance(l.centroid, f.geometry) asc";
                                                                                $sSQL .= " limit $iLimit";
                                                                                if (CONST_Debug) var_dump($sSQL);
                                        {
                                                $sOrderSQL .= 'when min(place_id) = '.$iPlaceID.' then '.$iOrder.' ';
                                        }
-                                       $sOrderSQL .= ' ELSE 10000000 END ASC';
+                                       $sOrderSQL .= ' ELSE 10000000 END';
                                        $sSQL = "select osm_type,osm_id,class,type,rank_search,rank_address,min(place_id) as place_id,country_code,";
                                        $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
                                        $sSQL .= "get_name_by_language(name, $sLanguagePrefArraySQL) as placename,";
                                        $sSQL .= "get_name_by_language(name, ARRAY['ref']) as ref,";
-                                       $sSQL .= "avg(ST_X(ST_Centroid(geometry))) as lon,avg(ST_Y(ST_Centroid(geometry))) as lat ";
+                                       $sSQL .= "avg(ST_X(ST_Centroid(geometry))) as lon,avg(ST_Y(ST_Centroid(geometry))) as lat, ";
+                                       $sSQL .= $sOrderSQL." as porder ";
                                        $sSQL .= "from placex where place_id in ($sPlaceIDs) ";
                                        $sSQL .= "group by osm_type,osm_id,class,type,rank_search,rank_address,country_code";
                                        if (!$bDeDupe) $sSQL .= ",place_id";
                                        $sSQL .= ",get_address_by_language(place_id, $sLanguagePrefArraySQL) ";
                                        $sSQL .= ",get_name_by_language(name, $sLanguagePrefArraySQL) ";
                                        $sSQL .= ",get_name_by_language(name, ARRAY['ref']) ";
-                                       $sSQL .= "order by rank_search,rank_address,".$sOrderSQL;
+                                       $sSQL .= " union ";
+                                       $sSQL .= "select 'T' as osm_type,place_id as osm_id,'place' as class,'house' as type,30 as rank_search,30 as rank_address,min(place_id) as place_id,'us' as country_code,";
+                                       $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
+                                       $sSQL .= "null as placename,";
+                                       $sSQL .= "null as ref,";
+                                       $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
+                                       $sSQL .= $sOrderSQL." as porder ";
+                                       $sSQL .= "from location_property_tiger where place_id in ($sPlaceIDs) ";
+                                       $sSQL .= "group by place_id";
+                                       if (!$bDeDupe) $sSQL .= ",place_id";
+                                       $sSQL .= " union ";
+                                       $sSQL .= "select 'T' as osm_type,place_id as osm_id,'place' as class,'house' as type,30 as rank_search,30 as rank_address,min(place_id) as place_id,'us' as country_code,";
+                                       $sSQL .= "get_address_by_language(place_id, $sLanguagePrefArraySQL) as langaddress,";
+                                       $sSQL .= "null as placename,";
+                                       $sSQL .= "null as ref,";
+                                       $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
+                                       $sSQL .= $sOrderSQL." as porder ";
+                                       $sSQL .= "from location_property_aux where place_id in ($sPlaceIDs) ";
+                                       $sSQL .= "group by place_id";
+                                       if (!$bDeDupe) $sSQL .= ",place_id";
+                                       $sSQL .= ",get_address_by_language(place_id, $sLanguagePrefArraySQL) ";
+                                       $sSQL .= "order by rank_search,rank_address,porder asc";
                                        if (CONST_Debug) var_dump('<hr>',$sSQL);
                                        $aSearchResults = $oDB->getAll($sSQL);
 //var_dump($sSQL,$aSearchResults);exit;
                        $sSQL .= "ST_Y(ST_PointN(ExteriorRing(ST_Box2D(outline)),4)) as minlat,ST_Y(ST_PointN(ExteriorRing(ST_Box2D(outline)),2)) as maxlat,";
                        $sSQL .= "ST_X(ST_PointN(ExteriorRing(ST_Box2D(outline)),1)) as minlon,ST_X(ST_PointN(ExteriorRing(ST_Box2D(outline)),3)) as maxlon,";
                        $sSQL .= "ST_AsText(outline) as outlinestring from get_place_boundingbox_quick(".$aResult['place_id'].")";
+
+                       $sSQL = "select place_id,0 as numfeatures,st_area(geometry) as area,";
+                       $sSQL .= "ST_Y(ST_PointN(ExteriorRing(ST_Box2D(geometry)),4)) as minlat,ST_Y(ST_PointN(ExteriorRing(ST_Box2D(geometry)),2)) as maxlat,";
+                       $sSQL .= "ST_X(ST_PointN(ExteriorRing(ST_Box2D(geometry)),1)) as minlon,ST_X(ST_PointN(ExteriorRing(ST_Box2D(geometry)),3)) as maxlon,";
+                       $sSQL .= "ST_AsText(geometry) as outlinestring from placex where place_id = ".$aResult['place_id'].' and st_geometrytype(ST_Box2D(geometry)) = \'ST_Polygon\'';
                        $aPointPolygon = $oDB->getRow($sSQL);
                        if (PEAR::IsError($aPointPolygon))
                        {
                if ($bShowAddressDetails)
                {
                        $aResult['address'] = getAddressDetails($oDB, $sLanguagePrefArraySQL, $aResult['place_id'], $aResult['country_code']);
+//var_dump($aResult['address']);
+//exit;
                }
 
                if (isset($aClassType[$aResult['class'].':'.$aResult['type']]['importance']) 
                $aResult['foundorder'] = $iResNum;
                $aSearchResults[$iResNum] = $aResult;
        }
+
+//var_dump($aSearchResults);exit;
        
        uasort($aSearchResults, 'byImportance');
        
 
        if (CONST_Debug) exit;
 
-       include('.htlib/output/search-'.$sOutputFormat.'.php');
+       include(CONST_BasePath.'/lib/template/search-'.$sOutputFormat.'.php');