]> git.openstreetmap.org Git - nominatim.git/blobdiff - lib/Geocode.php
fix coordinate order for route parameter
[nominatim.git] / lib / Geocode.php
index d3cc793ff5f48f91e4aca2dfc2583a50e2cac697..0b3bd51f1f72c87bb6b26be336b5d467d2c9d89a 100644 (file)
                                $sCountryCodesSQL = join(',', array_map('addQuotes', $this->aCountryCodes));
                        }
 
-                       // 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, ny', $this->sQuery);
+                       $sQuery = $this->sQuery;
 
                        // Conflicts between US state abreviations and various words for 'the' in different languages
                        if (isset($this->aLangPrefOrder['name:en']))
                                foreach($this->aRoutePoints as $aPoint)
                                {
                                        if (!$bFirst) $sViewboxCentreSQL .= ",";
-                                       $sViewboxCentreSQL .= $aPoint[1].' '.$aPoint[0];
+                                       $sViewboxCentreSQL .= $aPoint[0].' '.$aPoint[1];
                                        $bFirst = false;
                                }
                                $sViewboxCentreSQL .= ")'::geometry,4326)";
                                                                                                if ($aSearch['sHouseNumber'] === '')
                                                                                                {
                                                                                                        $aSearch['sHouseNumber'] = $sToken;
+                                                                                                       // 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']++;
                                                                                                        if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
                                                                                                        /*
                                                                                                        // Fall back to not searching for this item (better than nothing)
                                                                                                        $aSearch['sType'] = $aSearchTerm['type'];
                                                                                                        if (sizeof($aSearch['aName'])) $aSearch['sOperator'] = 'name';
                                                                                                        else $aSearch['sOperator'] = 'near'; // near = in for the moment
+                                                                                                       if (strlen($aSearchTerm['operator']) == 0) $aSearch['iSearchRank'] += 1;
 
                                                                                                        // Do we have a shortcut id?
                                                                                                        if ($aSearch['sOperator'] == 'name')
                                                                        if (CONST_Debug) var_dump($sSQL);
                                                                        $aPlaceIDs = $this->oDB->getCol($sSQL);
                                                                }
+                                                               else
+                                                               {
+                                                                       $aPlaceIDs = array();
+                                                               }
                                                        }
                                                        else
                                                        {
                                                                        // If excluded place IDs are given, it is fair to assume that
                                                                        // there have been results in the small box, so no further
                                                                        // expansion in that case.
-                                                                       if (!sizeof($aPlaceIDs) && !sizeof($this->aExcludePlaceIDs))
+                                                                       // Also don't expand if bounded results were requested.
+                                                                       if (!sizeof($aPlaceIDs) && !sizeof($this->aExcludePlaceIDs) && !$this->bBoundedSearch)
                                                                        {
                                                                                $sSQL = "select place_id from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
                                                                                if ($sCountryCodesSQL) $sSQL .= " join placex using (place_id)";
                                                                }
                                                        }
                                                        if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
-                                                       if ($aSearch['sHouseNumber']) $aTerms[] = "address_rank between 16 and 27";
+                                                       if ($aSearch['sHouseNumber'])
+                                                       {
+                                                               $aTerms[] = "address_rank between 16 and 27";
+                                                       }
+                                                       else
+                                                       {
+                                                               if ($this->iMinAddressRank > 0)
+                                                               {
+                                                                       $aTerms[] = "address_rank >= ".$this->iMinAddressRank;
+                                                               }
+                                                               if ($this->iMaxAddressRank < 30)
+                                                               {
+                                                                       $aTerms[] = "address_rank <= ".$this->iMaxAddressRank;
+                                                               }
+                                                       }
                                                        if ($aSearch['fLon'] && $aSearch['fLat'])
                                                        {
                                                                $aTerms[] = "ST_DWithin(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326), ".$aSearch['fRadius'].")";
                                                                $sPlaceIDs = join(',',$aPlaceIDs);
 
                                                                // 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."'";
+                                                               $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
+                                                               $sSQL = "select place_id from placex where parent_place_id in (".$sPlaceIDs.") and transliteration(housenumber) ~* E'".$sHouseNumberRegex."'";
                                                                if (sizeof($this->aExcludePlaceIDs))
                                                                {
                                                                        $sSQL .= " and place_id not in (".join(',',$this->aExcludePlaceIDs).")";
                                                                                        }
                                                                                        if ($sCountryCodesSQL) $sSQL .= " and lp.calculated_country_code in ($sCountryCodesSQL)";
                                                                                        if ($sOrderBySQL) $sSQL .= "order by ".$sOrderBySQL." asc";
-                                                                                       if ($iOffset) $sSQL .= " offset $iOffset";
+                                                                                       if ($this->iOffset) $sSQL .= " offset $this->iOffset";
                                                                                        $sSQL .= " limit $this->iLimit";
                                                                                        if (CONST_Debug) var_dump($sSQL);
                                                                                        $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
                                                                                        }
                                                                                        if ($sCountryCodesSQL) $sSQL .= " and l.calculated_country_code in ($sCountryCodesSQL)";
                                                                                        if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
-                                                                                       if ($iOffset) $sSQL .= " offset $iOffset";
+                                                                                       if ($this->iOffset) $sSQL .= " offset $this->iOffset";
                                                                                        $sSQL .= " limit $this->iLimit";
                                                                                        if (CONST_Debug) var_dump($sSQL);
                                                                                        $aClassPlaceIDs = array_merge($aClassPlaceIDs, $this->oDB->getCol($sSQL));
                        }
 
                        $aClassType = getClassTypesWithImportance();
-                       $aRecheckWords = preg_split('/\b/u',$sQuery);
+                       $aRecheckWords = preg_split('/\b[\s,\\-]*/u',$sQuery);
                        foreach($aRecheckWords as $i => $sWord)
                        {
                                if (!$sWord) unset($aRecheckWords[$i]);
                                $sAddress = $aResult['langaddress'];
                                foreach($aRecheckWords as $i => $sWord)
                                {
-                                       if (stripos($sAddress, $sWord)!==false) $iCountWords++;
+                                       if (stripos($sAddress, $sWord)!==false)
+                                       {
+                                               $iCountWords++;
+                                               if (preg_match("/(^|,)\s*$sWord\s*(,|$)/", $sAddress)) $iCountWords += 0.1;
+                                       }
                                }
 
                                $aResult['importance'] = $aResult['importance'] + ($iCountWords*0.1); // 0.1 is a completely arbitrary number but something in the range 0.1 to 0.5 would seem right
 
        } // end class
 
-
-/*
-               if (isset($_GET['route']) && $_GET['route'] && isset($_GET['routewidth']) && $_GET['routewidth'])
-               {
-                       $aPoints = explode(',',$_GET['route']);
-                       if (sizeof($aPoints) % 2 != 0)
-                       {
-                               userError("Uneven number of points");
-                               exit;
-                       }
-                       $sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
-                       $fPrevCoord = false;
-               }
-*/