X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/8ffa5be81fddc6e82df7466cd3f07cad873a440c..671c08848c1531f0c52f0dfff9448086a6fc09cb:/lib/Geocode.php diff --git a/lib/Geocode.php b/lib/Geocode.php index a01869fd..016fc101 100644 --- a/lib/Geocode.php +++ b/lib/Geocode.php @@ -20,18 +20,22 @@ protected $iLimit = 20; protected $iFinalLimit = 10; protected $iOffset = 0; + protected $bFallback = false; protected $aCountryCodes = false; protected $aNearPoint = false; protected $bBoundedSearch = false; protected $aViewBox = false; + protected $sViewboxSmallSQL = false; + protected $sViewboxLargeSQL = false; protected $aRoutePoints = false; protected $iMaxRank = 20; protected $iMinAddressRank = 0; protected $iMaxAddressRank = 30; protected $aAddressRankList = array(); + protected $exactMatchCache = array(); protected $sAllowedTypesSQLList = false; @@ -43,6 +47,11 @@ $this->oDB =& $oDB; } + function setReverseInPlan($bReverse) + { + $this->bReverseInPlan = $bReverse; + } + function setLanguagePreference($aLangPref) { $this->aLangPrefOrder = $aLangPref; @@ -112,6 +121,11 @@ $this->iOffset = $iOffset; } + function setFallback($bFallback = true) + { + $this->bFallback = (bool)$bFallback; + } + function setExcludedPlaceIDs($a) { // TODO: force to int @@ -190,20 +204,131 @@ return $this->sQuery; } + + function loadParamArray($aParams) + { + if (isset($aParams['addressdetails'])) $this->bIncludeAddressDetails = (bool)$aParams['addressdetails']; + if (isset($aParams['bounded'])) $this->bBoundedSearch = (bool)$aParams['bounded']; + if (isset($aParams['dedupe'])) $this->bDeDupe = (bool)$aParams['dedupe']; + + if (isset($aParams['limit'])) $this->setLimit((int)$aParams['limit']); + if (isset($aParams['offset'])) $this->iOffset = (int)$aParams['offset']; + + if (isset($aParams['fallback'])) $this->bFallback = (bool)$aParams['fallback']; + + // List of excluded Place IDs - used for more acurate pageing + if (isset($aParams['exclude_place_ids']) && $aParams['exclude_place_ids']) + { + foreach(explode(',',$aParams['exclude_place_ids']) as $iExcludedPlaceID) + { + $iExcludedPlaceID = (int)$iExcludedPlaceID; + if ($iExcludedPlaceID) $aExcludePlaceIDs[$iExcludedPlaceID] = $iExcludedPlaceID; + } + $this->aExcludePlaceIDs = $aExcludePlaceIDs; + } + + // Only certain ranks of feature + if (isset($aParams['featureType'])) $this->setFeatureType($aParams['featureType']); + if (isset($aParams['featuretype'])) $this->setFeatureType($aParams['featuretype']); + + // Country code list + if (isset($aParams['countrycodes'])) + { + $aCountryCodes = array(); + foreach(explode(',',$aParams['countrycodes']) as $sCountryCode) + { + if (preg_match('/^[a-zA-Z][a-zA-Z]$/', $sCountryCode)) + { + $aCountryCodes[] = strtolower($sCountryCode); + } + } + $this->aCountryCodes = $aCountryCodes; + } + + if (isset($aParams['viewboxlbrt']) && $aParams['viewboxlbrt']) + { + $aCoOrdinatesLBRT = explode(',',$aParams['viewboxlbrt']); + $this->setViewBox($aCoOrdinatesLBRT[0], $aCoOrdinatesLBRT[1], $aCoOrdinatesLBRT[2], $aCoOrdinatesLBRT[3]); + } + else if (isset($aParams['viewbox']) && $aParams['viewbox']) + { + $aCoOrdinatesLTRB = explode(',',$aParams['viewbox']); + $this->setViewBox($aCoOrdinatesLTRB[0], $aCoOrdinatesLTRB[3], $aCoOrdinatesLTRB[2], $aCoOrdinatesLTRB[1]); + } + + if (isset($aParams['route']) && $aParams['route'] && isset($aParams['routewidth']) && $aParams['routewidth']) + { + $aPoints = explode(',',$aParams['route']); + if (sizeof($aPoints) % 2 != 0) + { + userError("Uneven number of points"); + exit; + } + $fPrevCoord = false; + $aRoute = array(); + foreach($aPoints as $i => $fPoint) + { + if ($i%2) + { + $aRoute[] = array((float)$fPoint, $fPrevCoord); + } + else + { + $fPrevCoord = (float)$fPoint; + } + } + $this->aRoutePoints = $aRoute; + } + } + + function setQueryFromParams($aParams) + { + // Search query + $sQuery = (isset($aParams['q'])?trim($aParams['q']):''); + if (!$sQuery) + { + $this->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']); + $this->setReverseInPlan(false); + } + else + { + $this->setQuery($sQuery); + } + } + + function loadStructuredAddressElement($sValue, $sKey, $iNewMinAddressRank, $iNewMaxAddressRank, $aItemListValues) + { + $sValue = trim($sValue); + if (!$sValue) return false; + $this->aStructuredQuery[$sKey] = $sValue; + if ($this->iMinAddressRank == 0 && $this->iMaxAddressRank == 30) + { + $this->iMinAddressRank = $iNewMinAddressRank; + $this->iMaxAddressRank = $iNewMaxAddressRank; + } + if ($aItemListValues) $this->aAddressRankList = array_merge($this->aAddressRankList, $aItemListValues); + return true; + } + function setStructuredQuery($sAmentiy = false, $sStreet = false, $sCity = false, $sCounty = false, $sState = false, $sCountry = false, $sPostalCode = false) { $this->sQuery = false; + // Reset + $this->iMinAddressRank = 0; + $this->iMaxAddressRank = 30; + $this->aAddressRankList = array(); + $this->aStructuredQuery = array(); $this->sAllowedTypesSQLList = ''; - loadStructuredAddressElement($this->aStructuredQuery, $this->iMinAddressRank, $this->iMaxAddressRank, $this->aAddressRankList, $sAmentiy, 'amenity', 26, 30, false); - loadStructuredAddressElement($this->aStructuredQuery, $this->iMinAddressRank, $this->iMaxAddressRank, $this->aAddressRankList, $sStreet, 'street', 26, 30, false); - loadStructuredAddressElement($this->aStructuredQuery, $this->iMinAddressRank, $this->iMaxAddressRank, $this->aAddressRankList, $sCity, 'city', 14, 24, false); - loadStructuredAddressElement($this->aStructuredQuery, $this->iMinAddressRank, $this->iMaxAddressRank, $this->aAddressRankList, $sCounty, 'county', 9, 13, false); - loadStructuredAddressElement($this->aStructuredQuery, $this->iMinAddressRank, $this->iMaxAddressRank, $this->aAddressRankList, $sState, 'state', 8, 8, false); - loadStructuredAddressElement($this->aStructuredQuery, $this->iMinAddressRank, $this->iMaxAddressRank, $this->aAddressRankList, $sCountry, 'country', 4, 4, false); - loadStructuredAddressElement($this->aStructuredQuery, $this->iMinAddressRank, $this->iMaxAddressRank, $this->aAddressRankList, $sPostalCode, 'postalcode' , 5, 11, array(5, 11)); + $this->loadStructuredAddressElement($sAmentiy, 'amenity', 26, 30, false); + $this->loadStructuredAddressElement($sStreet, 'street', 26, 30, false); + $this->loadStructuredAddressElement($sCity, 'city', 14, 24, false); + $this->loadStructuredAddressElement($sCounty, 'county', 9, 13, false); + $this->loadStructuredAddressElement($sState, 'state', 8, 8, false); + $this->loadStructuredAddressElement($sPostalCode, 'postalcode' , 5, 11, array(5, 11)); + $this->loadStructuredAddressElement($sCountry, 'country', 4, 4, false); if (sizeof($this->aStructuredQuery) > 0) { @@ -213,10 +338,32 @@ $sAllowedTypesSQLList = '(\'place\',\'boundary\')'; } } + } + + function fallbackStructuredQuery() + { + if (!$this->aStructuredQuery) return false; + + $aParams = $this->aStructuredQuery; + if (sizeof($aParams) == 1) return false; + + $aOrderToFallback = array('postalcode', 'street', 'city', 'county', 'state'); + + foreach($aOrderToFallback as $sType) + { + if (isset($aParams[$sType])) + { + unset($aParams[$sType]); + $this->setStructuredQuery(@$aParams['amenity'], @$aParams['street'], @$aParams['city'], @$aParams['county'], @$aParams['state'], @$aParams['country'], @$aParams['postalcode']); + return true; + } + } + + return false; } - function getDetails($aPlaceIDs, $iMinAddressRank = 0, $iMaxAddressRank = 30, $aAddressRankList = false, $sAllowedTypesSQLList = false, $bDeDupe = false) + function getDetails($aPlaceIDs) { if (sizeof($aPlaceIDs) == 0) return array(); @@ -225,60 +372,66 @@ // Get the details for display (is this a redundant extra step?) $sPlaceIDs = join(',',$aPlaceIDs); - $sSQL = "select osm_type,osm_id,class,type,admin_level,rank_search,rank_address,min(place_id) as place_id,calculated_country_code as country_code,"; + $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 * "; + + $sSQL = "select osm_type,osm_id,class,type,admin_level,rank_search,rank_address,min(place_id) as place_id, min(parent_place_id) as parent_place_id, calculated_country_code as 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(centroid)) as lon,avg(ST_Y(centroid)) as lat, "; - $sSQL .= "coalesce(importance,0.75-(rank_search::float/40)) as importance, "; - $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(placex.place_id) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, "; + $sSQL .= $sImportanceSQL."coalesce(importance,0.75-(rank_search::float/40)) as importance, "; + $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(CASE WHEN placex.rank_search < 28 THEN placex.place_id ELSE placex.parent_place_id END) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, "; $sSQL .= "(extratags->'place') as extra_place "; $sSQL .= "from placex where place_id in ($sPlaceIDs) "; - $sSQL .= "and (placex.rank_address between $iMinAddressRank and $iMaxAddressRank "; - if (14 >= $iMinAddressRank && 14 <= $iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'"; - if ($aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$aAddressRankList).")"; + $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank "; + if (14 >= $this->iMinAddressRank && 14 <= $this->iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'"; + if ($this->aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$this->aAddressRankList).")"; $sSQL .= ") "; - if ($sAllowedTypesSQLList) $sSQL .= "and placex.class in $sAllowedTypesSQLList "; + if ($this->sAllowedTypesSQLList) $sSQL .= "and placex.class in $this->sAllowedTypesSQLList "; $sSQL .= "and linked_place_id is null "; $sSQL .= "group by osm_type,osm_id,class,type,admin_level,rank_search,rank_address,calculated_country_code,importance"; - if (!$bDeDupe) $sSQL .= ",place_id"; + if (!$this->bDeDupe) $sSQL .= ",place_id"; $sSQL .= ",langaddress "; $sSQL .= ",placename "; $sSQL .= ",ref "; $sSQL .= ",extratags->'place' "; - if (30 >= $iMinAddressRank && 30 <= $iMaxAddressRank) + if (30 >= $this->iMinAddressRank && 30 <= $this->iMaxAddressRank) { $sSQL .= " union "; - $sSQL .= "select 'T' as osm_type,place_id as osm_id,'place' as class,'house' as type,null as admin_level,30 as rank_search,30 as rank_address,min(place_id) as place_id,'us' as country_code,"; + $sSQL .= "select 'T' as osm_type,place_id as osm_id,'place' as class,'house' as type,null as admin_level,30 as rank_search,30 as rank_address,min(place_id) as place_id, min(parent_place_id) as parent_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 .= "-0.15 as importance, "; - $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(location_property_tiger.place_id) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, "; + $sSQL .= $sImportanceSQL."-1.15 as importance, "; + $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(location_property_tiger.parent_place_id) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, "; $sSQL .= "null as extra_place "; $sSQL .= "from location_property_tiger where place_id in ($sPlaceIDs) "; - $sSQL .= "and 30 between $iMinAddressRank and $iMaxAddressRank "; + $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank "; $sSQL .= "group by place_id"; - if (!$bDeDupe) $sSQL .= ",place_id"; + if (!$this->bDeDupe) $sSQL .= ",place_id "; + /* $sSQL .= " union "; - $sSQL .= "select 'L' as osm_type,place_id as osm_id,'place' as class,'house' as type,null as admin_level,30 as rank_search,30 as rank_address,min(place_id) as place_id,'us' as country_code,"; + $sSQL .= "select 'L' as osm_type,place_id as osm_id,'place' as class,'house' as type,null as admin_level,30 as rank_search,30 as rank_address,min(place_id) as place_id, min(parent_place_id) as parent_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 .= "-0.10 as importance, "; - $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(location_property_aux.place_id) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, "; + $sSQL .= $sImportanceSQL."-1.10 as importance, "; + $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(location_property_aux.parent_place_id) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, "; $sSQL .= "null as extra_place "; $sSQL .= "from location_property_aux where place_id in ($sPlaceIDs) "; - $sSQL .= "and 30 between $iMinAddressRank and $iMaxAddressRank "; + $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank "; $sSQL .= "group by place_id"; - if (!$bDeDupe) $sSQL .= ",place_id"; + if (!$this->bDeDupe) $sSQL .= ",place_id"; $sSQL .= ",get_address_by_language(place_id, $sLanguagePrefArraySQL) "; + */ } - $sSQL .= "order by importance desc"; + $sSQL .= " order by importance desc"; if (CONST_Debug) { echo "