X-Git-Url: https://git.openstreetmap.org/nominatim.git/blobdiff_plain/55629a48913d6e091247105cf52d6492574691f7..c02bf4986f4c6a820325727fa4b56e54a63c40d6:/lib/SearchDescription.php diff --git a/lib/SearchDescription.php b/lib/SearchDescription.php index 39f7ffca..c287c898 100644 --- a/lib/SearchDescription.php +++ b/lib/SearchDescription.php @@ -59,11 +59,6 @@ class SearchDescription return $this->iSearchRank; } - public function getPostCode() - { - return $this->sPostcode; - } - public function setPoiSearch($iOperator, $sClass, $sType) { $this->iOperator = $iOperator; @@ -71,22 +66,6 @@ class SearchDescription $this->sType = $sType; } - public function isNamedSearch() - { - return sizeof($this->aName) > 0 || sizeof($this->aAddress) > 0; - } - - public function isCountrySearch() - { - return $this->sCountryCode && sizeof($this->aName) == 0 - && !$this->iOperator && !$this->oContext->hasNearPoint(); - } - - public function isPoiSearch() - { - return (bool) $this->sClass; - } - public function looksLikeFullAddress() { return sizeof($this->aName) @@ -94,33 +73,6 @@ class SearchDescription && preg_match('/[0-9]+/', $this->sHouseNumber); } - public function isOperator($iType) - { - return $this->iOperator == $iType; - } - - public function hasHouseNumber() - { - return (bool) $this->sHouseNumber; - } - - private function poiTable() - { - return 'place_classtype_'.$this->sClass.'_'.$this->sType; - } - - public function countryCodeSQL($sVar) - { - if ($this->sCountryCode) { - return $sVar.' = \''.$this->sCountryCode."'"; - } - if ($this->oContext->sqlCountryList) { - return $sVar.' in '.$this->oContext->sqlCountryList; - } - - return ''; - } - public function hasOperator() { return $this->iOperator != Operator::NONE; @@ -367,7 +319,91 @@ class SearchDescription /////////// Query functions - public function queryCountry(&$oDB) + public function query(&$oDB, &$aWordFrequencyScores, &$aExactMatchCache, $iMinRank, $iMaxRank, $iLimit) + { + $aPlaceIDs = array(); + $iHousenumber = -1; + + if ($this->sCountryCode + && !sizeof($this->aName) + && !$this->iOperator + && !$this->sClass + && !$this->oContext->hasNearPoint() + ) { + // Just looking for a country - look it up + if (4 >= $iMinRank && 4 <= $iMaxRank) { + $aPlaceIDs = $this->queryCountry($oDB); + } + } elseif (!sizeof($this->aName) && !sizeof($this->aAddress)) { + // Neither name nor address? Then we must be + // looking for a POI in a geographic area. + if ($this->oContext->isBoundedSearch()) { + $aPlaceIDs = $this->queryNearbyPoi($oDB, $iLimit); + } + } elseif ($this->iOperator == Operator::POSTCODE) { + // looking for postcode + $aPlaceIDs = $this->queryPostcode($oDB, $iLimit); + } else { + // Ordinary search: + // First search for places according to name and address. + $aNamedPlaceIDs = $this->queryNamedPlace( + $oDB, + $aWordFrequencyScores, + $iMinRank, + $iMaxRank, + $iLimit + ); + + if (sizeof($aNamedPlaceIDs)) { + foreach ($aNamedPlaceIDs as $aRow) { + $aPlaceIDs[] = $aRow['place_id']; + $aExactMatchCache[$aRow['place_id']] = $aRow['exactmatch']; + } + } + + //now search for housenumber, if housenumber provided + if ($this->sHouseNumber && sizeof($aPlaceIDs)) { + $aResult = $this->queryHouseNumber($oDB, $aPlaceIDs, $iLimit); + + if (sizeof($aResult)) { + $iHousenumber = $aResult['iHouseNumber']; + $aPlaceIDs = $aResult['aPlaceIDs']; + } elseif (!$this->looksLikeFullAddress()) { + $aPlaceIDs = array(); + } + } + + // finally get POIs if requested + if ($this->sClass && sizeof($aPlaceIDs)) { + $aPlaceIDs = $this->queryPoiByOperator($oDB, $aPlaceIDs, $iLimit); + } + } + + if (CONST_Debug) { + echo "
Place IDs: "; + var_Dump($aPlaceIDs); + } + + if (sizeof($aPlaceIDs) && $this->sPostcode) { + $sSQL = 'SELECT place_id FROM placex'; + $sSQL .= ' WHERE place_id in ('.join(',', $aPlaceIDs).')'; + $sSQL .= " AND postcode = '".$this->sPostcode."'"; + if (CONST_Debug) var_dump($sSQL); + $aFilteredPlaceIDs = chksql($oDB->getCol($sSQL)); + if ($aFilteredPlaceIDs) { + $aPlaceIDs = $aFilteredPlaceIDs; + if (CONST_Debug) { + echo "
Place IDs after postcode filtering: "; + var_Dump($aPlaceIDs); + } + } + } + + return array('IDs' => $aPlaceIDs, 'houseNumber' => $iHousenumber); + } + + + private function queryCountry(&$oDB) { $sSQL = 'SELECT place_id FROM placex '; $sSQL .= "WHERE country_code='".$this->sCountryCode."'"; @@ -382,7 +418,7 @@ class SearchDescription return chksql($oDB->getCol($sSQL)); } - public function queryNearbyPoi(&$oDB, $iLimit) + private function queryNearbyPoi(&$oDB, $iLimit) { if (!$this->sClass) { return array(); @@ -398,7 +434,7 @@ class SearchDescription } if ($this->oContext->hasNearPoint()) { $sSQL .= ' WHERE '.$this->oContext->withinSQL('ct.centroid'); - } else if ($this->oContext->bViewboxBounded) { + } elseif ($this->oContext->bViewboxBounded) { $sSQL .= ' WHERE ST_Contains('.$this->oContext->sqlViewboxSmall.', ct.centroid)'; } if ($this->oContext->sqlCountryList) { @@ -433,7 +469,7 @@ class SearchDescription return array(); } - public function queryPostcode(&$oDB, $iLimit) + private function queryPostcode(&$oDB, $iLimit) { $sSQL = 'SELECT p.place_id FROM location_postcode p '; @@ -456,7 +492,7 @@ class SearchDescription return chksql($oDB->getCol($sSQL)); } - public function queryNamedPlace(&$oDB, $aWordFrequencyScores, $iMinAddressRank, $iMaxAddressRank, $iLimit) + private function queryNamedPlace(&$oDB, $aWordFrequencyScores, $iMinAddressRank, $iMaxAddressRank, $iLimit) { $aTerms = array(); $aOrder = array(); @@ -586,7 +622,7 @@ class SearchDescription return array(); } - public function queryHouseNumber(&$oDB, $aRoadPlaceIDs, $iLimit) + private function queryHouseNumber(&$oDB, $aRoadPlaceIDs, $iLimit) { $sPlaceIDs = join(',', $aRoadPlaceIDs); @@ -680,7 +716,7 @@ class SearchDescription } - public function queryPoiByOperator(&$oDB, $aParentIDs, $iLimit) + private function queryPoiByOperator(&$oDB, $aParentIDs, $iLimit) { $sPlaceIDs = join(',', $aParentIDs); $aClassPlaceIDs = array(); @@ -817,6 +853,22 @@ class SearchDescription return $aClassPlaceIDs; } + private function poiTable() + { + return 'place_classtype_'.$this->sClass.'_'.$this->sType; + } + + private function countryCodeSQL($sVar) + { + if ($this->sCountryCode) { + return $sVar.' = \''.$this->sCountryCode."'"; + } + if ($this->oContext->sqlCountryList) { + return $sVar.' in '.$this->oContext->sqlCountryList; + } + + return ''; + } /////////// Sort functions