X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/09f16616388490578ddebdb1d8be5ae545cd1d64..8e0ffde3e0a403d289b91ad720444fd94f3ae210:/lib/Geocode.php
diff --git a/lib/Geocode.php b/lib/Geocode.php
index 971eb85d..58bc3bfc 100644
--- a/lib/Geocode.php
+++ b/lib/Geocode.php
@@ -5,6 +5,7 @@ namespace Nominatim;
require_once(CONST_BasePath.'/lib/NearPoint.php');
require_once(CONST_BasePath.'/lib/PlaceLookup.php');
require_once(CONST_BasePath.'/lib/ReverseGeocode.php');
+require_once(CONST_BasePath.'/lib/SearchDescription.php');
class Geocode
{
@@ -51,10 +52,22 @@ class Geocode
protected $sQuery = false;
protected $aStructuredQuery = false;
+ protected $oNormalizer = null;
+
public function __construct(&$oDB)
{
$this->oDB =& $oDB;
+ $this->oNormalizer = \Transliterator::createFromRules(CONST_Term_Normalization_Rules);
+ }
+
+ private function normTerm($sTerm)
+ {
+ if ($this->oNormalizer === null) {
+ return $sTerm;
+ }
+
+ return $this->oNormalizer->transliterate($sTerm);
}
public function setReverseInPlan($bReverse)
@@ -292,7 +305,7 @@ class Geocode
$aViewbox = $oParams->getStringList('viewboxlbrt');
if ($aViewbox) {
if (count($aViewbox) != 4) {
- userError("Bad parmater 'viewbox'. Expected 4 coordinates.");
+ userError("Bad parmater 'viewboxlbrt'. Expected 4 coordinates.");
}
$this->setViewbox($aViewbox);
} else {
@@ -301,12 +314,7 @@ class Geocode
if (count($aViewbox) != 4) {
userError("Bad parmater 'viewbox'. Expected 4 coordinates.");
}
- $this->setViewBox(array(
- $aViewbox[0],
- $aViewbox[3],
- $aViewbox[2],
- $aViewbox[1]
- ));
+ $this->setViewBox($aViewbox);
} else {
$aRoute = $oParams->getStringList('route');
$fRouteWidth = $oParams->getFloat('routewidth');
@@ -360,7 +368,7 @@ class Geocode
$this->aAddressRankList = array();
$this->aStructuredQuery = array();
- $this->sAllowedTypesSQLList = '';
+ $this->sAllowedTypesSQLList = false;
$this->loadStructuredAddressElement($sAmenity, 'amenity', 26, 30, false);
$this->loadStructuredAddressElement($sStreet, 'street', 26, 30, false);
@@ -373,7 +381,7 @@ class Geocode
if (sizeof($this->aStructuredQuery) > 0) {
$this->sQuery = join(', ', $this->aStructuredQuery);
if ($this->iMaxAddressRank < 30) {
- $sAllowedTypesSQLList = '(\'place\',\'boundary\')';
+ $this->sAllowedTypesSQLList = '(\'place\',\'boundary\')';
}
}
}
@@ -404,14 +412,23 @@ class Geocode
//$aPlaceIDs is an array with key: placeID and value: tiger-housenumber, if found, else -1
if (sizeof($aPlaceIDs) == 0) return array();
- $sLanguagePrefArraySQL = "ARRAY[".join(',', array_map("getDBQuoted", $this->aLangPrefOrder))."]";
+ $sLanguagePrefArraySQL = getArraySQL(
+ array_map("getDBQuoted", $this->aLangPrefOrder)
+ );
// Get the details for display (is this a redundant extra step?)
$sPlaceIDs = join(',', array_keys($aPlaceIDs));
$sImportanceSQL = '';
- if ($this->sViewboxSmallSQL) $sImportanceSQL .= " CASE WHEN ST_Contains($this->sViewboxSmallSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
- if ($this->sViewboxLargeSQL) $sImportanceSQL .= " CASE WHEN ST_Contains($this->sViewboxLargeSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
+ $sImportanceSQLGeom = '';
+ if ($this->sViewboxSmallSQL) {
+ $sImportanceSQL .= " CASE WHEN ST_Contains($this->sViewboxSmallSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
+ $sImportanceSQLGeom .= " CASE WHEN ST_Contains($this->sViewboxSmallSQL, geometry) THEN 1 ELSE 0.75 END * ";
+ }
+ if ($this->sViewboxLargeSQL) {
+ $sImportanceSQL .= " CASE WHEN ST_Contains($this->sViewboxLargeSQL, ST_Collect(centroid)) THEN 1 ELSE 0.75 END * ";
+ $sImportanceSQLGeom .= " CASE WHEN ST_Contains($this->sViewboxLargeSQL, geometry) THEN 1 ELSE 0.75 END * ";
+ }
$sSQL = "SELECT ";
$sSQL .= " osm_type,";
@@ -423,7 +440,7 @@ class Geocode
$sSQL .= " rank_address,";
$sSQL .= " min(place_id) AS place_id, ";
$sSQL .= " min(parent_place_id) AS parent_place_id, ";
- $sSQL .= " calculated_country_code AS country_code, ";
+ $sSQL .= " country_code, ";
$sSQL .= " get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) AS langaddress,";
$sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL) AS placename,";
$sSQL .= " get_name_by_language(name, ARRAY['ref']) AS ref,";
@@ -466,7 +483,7 @@ class Geocode
$sSQL .= " admin_level, ";
$sSQL .= " rank_search, ";
$sSQL .= " rank_address, ";
- $sSQL .= " calculated_country_code, ";
+ $sSQL .= " country_code, ";
$sSQL .= " importance, ";
if (!$this->bDeDupe) $sSQL .= "place_id,";
$sSQL .= " langaddress, ";
@@ -476,6 +493,35 @@ class Geocode
if ($this->bIncludeNameDetails) $sSQL .= "name, ";
$sSQL .= " extratags->'place' ";
+ // postcode table
+ $sSQL .= "UNION ";
+ $sSQL .= "SELECT";
+ $sSQL .= " 'P' as osm_type,";
+ $sSQL .= " (SELECT osm_id from placex p WHERE p.place_id = lp.parent_place_id) as osm_id,";
+ $sSQL .= " 'place' as class, 'postcode' as type,";
+ $sSQL .= " null as admin_level, rank_search, rank_address,";
+ $sSQL .= " place_id, parent_place_id, country_code,";
+ $sSQL .= " get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) AS langaddress,";
+ $sSQL .= " postcode as placename,";
+ $sSQL .= " postcode as ref,";
+ if ($this->bIncludeExtraTags) $sSQL .= "null AS extra,";
+ if ($this->bIncludeNameDetails) $sSQL .= "null AS names,";
+ $sSQL .= " ST_x(st_centroid(geometry)) AS lon, ST_y(st_centroid(geometry)) AS lat,";
+ $sSQL .= $sImportanceSQLGeom."(0.75-(rank_search::float/40)) AS importance, ";
+ $sSQL .= " (";
+ $sSQL .= " SELECT max(p.importance*(p.rank_address+2))";
+ $sSQL .= " FROM ";
+ $sSQL .= " place_addressline s, ";
+ $sSQL .= " placex p";
+ $sSQL .= " WHERE s.place_id = lp.parent_place_id";
+ $sSQL .= " AND p.place_id = s.address_place_id ";
+ $sSQL .= " AND s.isaddress";
+ $sSQL .= " AND p.importance is not null";
+ $sSQL .= " ) AS addressimportance, ";
+ $sSQL .= " null AS extra_place ";
+ $sSQL .= "FROM location_postcode lp";
+ $sSQL .= " WHERE place_id in ($sPlaceIDs) ";
+
if (30 >= $this->iMinAddressRank && 30 <= $this->iMaxAddressRank) {
// only Tiger housenumbers and interpolation lines need to be interpolated, because they are saved as lines
// with start- and endnumber, the common osm housenumbers are usually saved as points
@@ -551,7 +597,7 @@ class Geocode
$sSQL .= " 30 AS rank_address, ";
$sSQL .= " min(place_id) as place_id, ";
$sSQL .= " min(parent_place_id) AS parent_place_id, ";
- $sSQL .= " calculated_country_code AS country_code, ";
+ $sSQL .= " country_code, ";
$sSQL .= " get_address_by_language(place_id, housenumber_for_place, $sLanguagePrefArraySQL) AS langaddress, ";
$sSQL .= " null AS placename, ";
$sSQL .= " null AS ref, ";
@@ -576,7 +622,7 @@ class Geocode
$sSQL .= " SELECT ";
$sSQL .= " osm_id, ";
$sSQL .= " place_id, ";
- $sSQL .= " calculated_country_code, ";
+ $sSQL .= " country_code, ";
$sSQL .= " CASE "; // interpolate the housenumbers here
$sSQL .= " WHEN startnumber != endnumber ";
$sSQL .= " THEN ST_LineInterpolatePoint(linegeo, (housenumber_for_place-startnumber::float)/(endnumber-startnumber)::float) ";
@@ -595,7 +641,7 @@ class Geocode
$sSQL .= " osm_id, ";
$sSQL .= " place_id, ";
$sSQL .= " housenumber_for_place, ";
- $sSQL .= " calculated_country_code "; //is this group by really needed?, place_id + housenumber (in combination) are unique
+ $sSQL .= " country_code "; //is this group by really needed?, place_id + housenumber (in combination) are unique
if (!$this->bDeDupe) $sSQL .= ", place_id ";
if (CONST_Use_Aux_Location_data) {
@@ -653,7 +699,7 @@ class Geocode
return $aSearchResults;
}
- public function getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases)
+ public function getGroupedSearches($aSearches, $aPhraseTypes, $aPhrases, $aValidTokens, $aWordFrequencyScores, $bStructuredPhrases, $sNormQuery)
{
/*
Calculate all searches using aValidTokens i.e.
@@ -666,12 +712,17 @@ class Geocode
Score how good the search is so they can be ordered
*/
- foreach ($aPhrases as $iPhrase => $sPhrase) {
+ $iGlobalRank = 0;
+
+ foreach ($aPhrases as $iPhrase => $aPhrase) {
$aNewPhraseSearches = array();
- if ($bStructuredPhrases) $sPhraseType = $aPhraseTypes[$iPhrase];
- else $sPhraseType = '';
+ if ($bStructuredPhrases) {
+ $sPhraseType = $aPhraseTypes[$iPhrase];
+ } else {
+ $sPhraseType = '';
+ }
- foreach ($aPhrases[$iPhrase]['wordsets'] as $iWordSet => $aWordset) {
+ foreach ($aPhrase['wordsets'] as $iWordSet => $aWordset) {
// Too many permutations - too expensive
if ($iWordSet > 120) break;
@@ -682,167 +733,72 @@ class Geocode
//echo "
$sToken";
$aNewWordsetSearches = array();
- foreach ($aWordsetSearches as $aCurrentSearch) {
+ foreach ($aWordsetSearches as $oCurrentSearch) {
//echo "";
- //var_dump($aCurrentSearch);
+ //var_dump($oCurrentSearch);
//echo "";
// If the token is valid
if (isset($aValidTokens[' '.$sToken])) {
foreach ($aValidTokens[' '.$sToken] as $aSearchTerm) {
- $aSearch = $aCurrentSearch;
- $aSearch['iSearchRank']++;
- if (($sPhraseType == '' || $sPhraseType == 'country') && !empty($aSearchTerm['country_code']) && $aSearchTerm['country_code'] != '0') {
- if ($aSearch['sCountryCode'] === false) {
- $aSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
- // Country is almost always at the end of the string - increase score for finding it anywhere else (optimisation)
- if (($iToken+1 != sizeof($aWordset) || $iPhrase+1 != sizeof($aPhrases))) {
- $aSearch['iSearchRank'] += 5;
- }
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
- }
- } elseif (isset($aSearchTerm['lat']) && $aSearchTerm['lat'] !== '' && $aSearchTerm['lat'] !== null) {
- if ($aSearch['oNear'] === false) {
- $aSearch['oNear'] = new NearPoint(
- $aSearchTerm['lat'],
- $aSearchTerm['lon'],
- $aSearchTerm['radius']
- );
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
- }
- } elseif ($sPhraseType == 'postalcode') {
- // We need to try the case where the postal code is the primary element (i.e. no way to tell if it is (postalcode, city) OR (city, postalcode) so try both
- if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id']) {
- // If we already have a name try putting the postcode first
- if (sizeof($aSearch['aName'])) {
- $aNewSearch = $aSearch;
- $aNewSearch['aAddress'] = array_merge($aNewSearch['aAddress'], $aNewSearch['aName']);
- $aNewSearch['aName'] = array();
- $aNewSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aNewSearch;
- }
-
- if (sizeof($aSearch['aName'])) {
- if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false)) {
- $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
- } else {
- $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
- $aSearch['iSearchRank'] += 1000; // skip;
- }
- } else {
- $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
- //$aSearch['iNamePhrase'] = $iPhrase;
- }
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
- }
- } elseif (($sPhraseType == '' || $sPhraseType == 'street') && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house') {
- 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']++;
- // 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)
- $aSearch = $aCurrentSearch;
- $aSearch['iSearchRank'] += 1;
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
- */
- }
- } elseif ($sPhraseType == '' && $aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null) {
- if ($aSearch['sClass'] === '') {
- $aSearch['sOperator'] = $aSearchTerm['operator'];
- $aSearch['sClass'] = $aSearchTerm['class'];
- $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;
-
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
- }
- } elseif (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id']) {
- if (sizeof($aSearch['aName'])) {
- if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false)) {
- $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
- } else {
- $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
- $aSearch['iSearchRank'] += 1000; // skip;
- }
- } else {
- $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
- //$aSearch['iNamePhrase'] = $iPhrase;
+ // Recheck if the original word shows up in the query.
+ $bWordInQuery = false;
+ if (isset($aSearchTerm['word']) && $aSearchTerm['word']) {
+ $bWordInQuery = strpos(
+ $sNormQuery,
+ $this->normTerm($aSearchTerm['word'])
+ ) !== false;
+ }
+ $aNewSearches = $oCurrentSearch->extendWithFullTerm(
+ $aSearchTerm,
+ $bWordInQuery,
+ isset($aValidTokens[$sToken])
+ && strpos($sToken, ' ') === false,
+ $sPhraseType,
+ $iToken == 0 && $iPhrase == 0,
+ $iPhrase == 0,
+ $iToken + 1 == sizeof($aWordset)
+ && $iPhrase + 1 == sizeof($aPhrases),
+ $iGlobalRank
+ );
+
+ foreach ($aNewSearches as $oSearch) {
+ if ($oSearch->getRank() < $this->iMaxRank) {
+ $aNewWordsetSearches[] = $oSearch;
}
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
}
}
}
// Look for partial matches.
// Note that there is no point in adding country terms here
- // because country are omitted in the address.
+ // because country is omitted in the address.
if (isset($aValidTokens[$sToken]) && $sPhraseType != 'country') {
// Allow searching for a word - but at extra cost
foreach ($aValidTokens[$sToken] as $aSearchTerm) {
- if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id']) {
- if ((!$bStructuredPhrases || $iPhrase > 0) && sizeof($aCurrentSearch['aName']) && strpos($sToken, ' ') === false) {
- $aSearch = $aCurrentSearch;
- $aSearch['iSearchRank'] += 1;
- if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency) {
- $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
- } elseif (isset($aValidTokens[' '.$sToken])) { // revert to the token version?
- $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
- $aSearch['iSearchRank'] += 1;
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
- foreach ($aValidTokens[' '.$sToken] as $aSearchTermToken) {
- if (empty($aSearchTermToken['country_code'])
- && empty($aSearchTermToken['lat'])
- && empty($aSearchTermToken['class'])
- ) {
- $aSearch = $aCurrentSearch;
- $aSearch['iSearchRank'] += 1;
- $aSearch['aAddress'][$aSearchTermToken['word_id']] = $aSearchTermToken['word_id'];
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
- }
- }
- } else {
- $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
- if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
- }
- }
-
- if (!sizeof($aCurrentSearch['aName']) || $aCurrentSearch['iNamePhrase'] == $iPhrase) {
- $aSearch = $aCurrentSearch;
- $aSearch['iSearchRank'] += 1;
- if (!sizeof($aCurrentSearch['aName'])) $aSearch['iSearchRank'] += 1;
- if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
- if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency) {
- $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
- } else {
- $aSearch['aNameNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
- }
- $aSearch['iNamePhrase'] = $iPhrase;
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
+ $aNewSearches = $oCurrentSearch->extendWithPartialTerm(
+ $aSearchTerm,
+ $bStructuredPhrases,
+ $iPhrase,
+ $aWordFrequencyScores,
+ isset($aValidTokens[' '.$sToken]) ? $aValidTokens[' '.$sToken] : array()
+ );
+
+ foreach ($aNewSearches as $oSearch) {
+ if ($oSearch->getRank() < $this->iMaxRank) {
+ $aNewWordsetSearches[] = $oSearch;
}
}
}
- } else {
- // Allow skipping a word - but at EXTREAM cost
- //$aSearch = $aCurrentSearch;
- //$aSearch['iSearchRank']+=100;
- //$aNewWordsetSearches[] = $aSearch;
}
}
// Sort and cut
- usort($aNewWordsetSearches, 'bySearchRank');
+ usort($aNewWordsetSearches, array('Nominatim\SearchDescription', 'bySearchRank'));
$aWordsetSearches = array_slice($aNewWordsetSearches, 0, 50);
}
//var_Dump('