]> git.openstreetmap.org Git - nominatim.git/blobdiff - lib/Geocode.php
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / lib / Geocode.php
index 85aa31f08202d99ac1e26ba80ee3444ee4c785e4..48b3b1ad4c843d2c2a3bf7a448485608f240fd3e 100644 (file)
                        return $aSearchResults;
                }
 
-               function getGroupedSearches($aSearches, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases)
+               function getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases)
                {
                        /*
                           Calculate all searches using aValidTokens i.e.
 
                                                                                        if (sizeof($aSearch['aName']))
                                                                                        {
-                                                                                               if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strlen($sToken) < 4 || strpos($sToken, ' ') !== false))
+                                                                                               if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false))
                                                                                                {
                                                                                                        $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
                                                                                                }
                                                                                        // sanity check: if the housenumber is not mainly made
                                                                                        // up of numbers, add a penalty
                                                                                        if (preg_match_all("/[^0-9]/", $sToken, $aMatches) > 2) $aSearch['iSearchRank']++;
+                                                                                       // also housenumbers should appear in the first or second phrase
+                                                                                       if ($iPhrase > 1) $aSearch['iSearchRank'] += 1;
                                                                                        if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
                                                                                        /*
                                                                                        // Fall back to not searching for this item (better than nothing)
                                                                        {
                                                                                if (sizeof($aSearch['aName']))
                                                                                {
-                                                                                       if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strlen($sToken) < 4 || strpos($sToken, ' ') !== false))
+                                                                                       if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false))
                                                                                        {
                                                                                                $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
                                                                                        }
                                                                        }
                                                                }
                                                        }
-                                                       if (isset($aValidTokens[$sToken]))
+                                                       // Look for partial matches.
+                                                       // Note that there is no point in adding country terms here
+                                                       // because country are omitted in the address.
+                                                       if (isset($aValidTokens[$sToken]) && $sPhraseType != 'country')
                                                        {
                                                                // Allow searching for a word - but at extra cost
                                                                foreach($aValidTokens[$sToken] as $aSearchTerm)
                                                                                                $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
                                                                                                if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
                                                                                        }
-                                                                                       elseif (isset($aValidTokens[' '.$sToken]) && strlen($sToken) >= 4) // revert to the token version?
+                                                                                       elseif (isset($aValidTokens[' '.$sToken])) // revert to the token version?
                                                                                        {
                                                                                                $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
                                                                                                $aSearch['iSearchRank'] += 1;
                                        // Start the search process
                                        $aResultPlaceIDs = array();
 
-                                       $aGroupedSearches = $this->getGroupedSearches($aSearches, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases);
+                                       $aGroupedSearches = $this->getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases);
 
                                        if ($this->bReverseInPlan)
                                        {
                                                if (sizeof($aPhrases) > 1)
                                                {
                                                        $aFinalPhrase = end($aPhrases);
-                                                       $aFinalPhrase['wordsets'] = getInverseWordSets($aFinalPhrase['words'], 0);
+                                                       $aPhrases[sizeof($aPhrases)-1]['wordsets'] = getInverseWordSets($aFinalPhrase['words'], 0);
                                                }
-                                               $aReverseGroupedSearches = $this->getGroupedSearches($aSearches, $aPhrases, $aValidTokens, $aWordFrequencyScores, false);
+                                               $aReverseGroupedSearches = $this->getGroupedSearches($aSearches, null, $aPhrases, $aValidTokens, $aWordFrequencyScores, false);
 
                                                foreach($aGroupedSearches as $aSearches)
                                                {
                                                                        $sSQL = "select place_id from placex where calculated_country_code='".$aSearch['sCountryCode']."' and rank_search = 4";
                                                                        if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
                                     if ($bBoundingBoxSearch)
-                                        $sSQL .= " and st_overlaps($this->sViewboxSmallSQL, geometry)";
+                                        $sSQL .= " and _st_intersects($this->sViewboxSmallSQL, geometry)";
                                                                        $sSQL .= " order by st_area(geometry) desc limit 1";
                                                                        if (CONST_Debug) var_dump($sSQL);
                                                                        $aPlaceIDs = $this->oDB->getCol($sSQL);
                                                        $aTerms = array();
                                                        $aOrder = array();
 
+                                                       if ($aSearch['sHouseNumber'] && sizeof($aSearch['aAddress']))
+                                                       {
+                                                               $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
+                                                               $aOrder[] = "exists(select place_id from placex where parent_place_id = search_name.place_id and transliteration(housenumber) ~* E'".$sHouseNumberRegex."' limit 1) desc";
+                                                       }
+
                                                        // TODO: filter out the pointless search terms (2 letter name tokens and less)
                                                        // they might be right - but they are just too darned expensive to run
                                                        if (sizeof($aSearch['aName'])) $aTerms[] = "name_vector @> ARRAY[".join($aSearch['aName'],",")."]";
                                                                $sSQL .= " where ".join(' and ',$aTerms);
                                                                $sSQL .= " order by ".join(', ',$aOrder);
                                                                if ($aSearch['sHouseNumber'] || $aSearch['sClass'])
-                                                                       $sSQL .= " limit 50";
+                                                                       $sSQL .= " limit 20";
                                                                elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass'])
                                                                        $sSQL .= " limit 1";
                                                                else
                                        // Get the bounding box and outline polygon
                                        $sSQL = "select place_id,0 as numfeatures,st_area(geometry) as area,";
                                        $sSQL .= "ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,";
-                                       $sSQL .= "ST_Y(ST_PointN(ST_ExteriorRing(Box2D(geometry)),4)) as minlat,ST_Y(ST_PointN(ST_ExteriorRing(Box2D(geometry)),2)) as maxlat,";
-                                       $sSQL .= "ST_X(ST_PointN(ST_ExteriorRing(Box2D(geometry)),1)) as minlon,ST_X(ST_PointN(ST_ExteriorRing(Box2D(geometry)),3)) as maxlon";
+                                       $sSQL .= "ST_YMin(geometry) as minlat,ST_YMax(geometry) as maxlat,";
+                                       $sSQL .= "ST_XMin(geometry) as minlon,ST_XMax(geometry) as maxlon";
                                        if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ",ST_AsGeoJSON(geometry) as asgeojson";
                                        if ($this->bIncludePolygonAsKML) $sSQL .= ",ST_AsKML(geometry) as askml";
                                        if ($this->bIncludePolygonAsSVG) $sSQL .= ",ST_AsSVG(geometry) as assvg";
                                        if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ",ST_AsText(geometry) as astext";
-                                       $sSQL .= " from placex where place_id = ".$aResult['place_id'].' and st_geometrytype(Box2D(geometry)) = \'ST_Polygon\'';
+                                       $sSQL .= " from placex where place_id = ".$aResult['place_id'];
                                        $aPointPolygon = $this->oDB->getRow($sSQL);
                                        if (PEAR::IsError($aPointPolygon))
                                        {
 
                                                if ($this->bIncludePolygonAsPoints)
                                                {
-                                                       // Translate geometary string to point array
+                                                       // Translate geometry string to point array
                                                        if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#',$aPointPolygon['astext'],$aMatch))
                                                        {
                                                                preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/',$aMatch[1],$aPolyPoints,PREG_SET_ORDER);
                        $bFirst = true;
                        foreach($aToFilter as $iResNum => $aResult)
                        {
-                               if ($aResult['type'] == 'adminitrative') $aResult['type'] = 'administrative';
                                $this->aExcludePlaceIDs[$aResult['place_id']] = $aResult['place_id'];
                                if ($bFirst)
                                {