- if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);
-
- if ($sCountryCodesSQL && $aSearch['sCountryCode'] && !in_array($aSearch['sCountryCode'], $this->aCountryCodes)) {
- continue;
- }
-
- // No location term?
- if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['oNear']) {
- if ($aSearch['sCountryCode'] && !$aSearch['sClass'] && !$aSearch['sHouseNumber']) {
- // Just looking for a country by code - look it up
- if (4 >= $this->iMinAddressRank && 4 <= $this->iMaxAddressRank) {
- $sSQL = "SELECT place_id FROM placex WHERE country_code='".$aSearch['sCountryCode']."' AND rank_search = 4";
- if ($bBoundingBoxSearch)
- $sSQL .= " AND _st_intersects($this->sViewboxSmallSQL, geometry)";
- $sSQL .= " ORDER BY st_area(geometry) DESC LIMIT 1";
- if (CONST_Debug) var_dump($sSQL);
- $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
- } else {
- $aPlaceIDs = array();
- }
- } else {
- if (!$bBoundingBoxSearch && !$aSearch['oNear']) continue;
- if (!$aSearch['sClass']) continue;
-
- $sSQL = "SELECT COUNT(*) FROM pg_tables WHERE tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
- if (chksql($this->oDB->getOne($sSQL))) {
- $sSQL = "SELECT place_id FROM place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." ct";
- if ($sCountryCodesSQL) $sSQL .= " JOIN placex USING (place_id)";
- $sSQL .= " WHERE st_contains($this->sViewboxSmallSQL, ct.centroid)";
- if ($sCountryCodesSQL) $sSQL .= " AND country_code in ($sCountryCodesSQL)";
- if (sizeof($this->aExcludePlaceIDs)) {
- $sSQL .= " AND place_id not in (".join(',', $this->aExcludePlaceIDs).")";
- }
- if ($this->sViewboxCentreSQL) $sSQL .= " ORDER BY ST_Distance($this->sViewboxCentreSQL, ct.centroid) ASC";
- $sSQL .= " limit $this->iLimit";
- if (CONST_Debug) var_dump($sSQL);
- $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
-
- // 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.
- // 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)";
- $sSQL .= " WHERE ST_Contains($this->sViewboxLargeSQL, ct.centroid)";
- if ($sCountryCodesSQL) $sSQL .= " AND country_code in ($sCountryCodesSQL)";
- if ($this->sViewboxCentreSQL) $sSQL .= " ORDER BY ST_Distance($this->sViewboxCentreSQL, ct.centroid) ASC";
- $sSQL .= " LIMIT $this->iLimit";
- if (CONST_Debug) var_dump($sSQL);
- $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
- }
- } else {
- $sSQL = "SELECT place_id ";
- $sSQL .= "FROM placex ";
- $sSQL .= "WHERE class='".$aSearch['sClass']."' ";
- $sSQL .= " AND type='".$aSearch['sType']."'";
- $sSQL .= " AND ST_Contains($this->sViewboxSmallSQL, geometry) ";
- $sSQL .= " AND linked_place_id is null";
- if ($sCountryCodesSQL) $sSQL .= " AND country_code in ($sCountryCodesSQL)";
- if ($this->sViewboxCentreSQL) $sSQL .= " ORDER BY ST_Distance($this->sViewboxCentreSQL, centroid) ASC";
- $sSQL .= " LIMIT $this->iLimit";
- if (CONST_Debug) var_dump($sSQL);
- $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
- }
- }
- } elseif ($aSearch['oNear'] && !sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['sClass']) {
- // If a coordinate is given, the search must either
- // be for a name or a special search. Ignore everythin else.
- $aPlaceIDs = array();
- } elseif ($aSearch['sOperator'] == 'postcode') {
- $sSQL = "SELECT p.place_id FROM location_postcode p ";
- if (sizeof($aSearch['aAddress'])) {
- $sSQL .= ", search_name s ";
- $sSQL .= "WHERE s.place_id = p.parent_place_id ";
- $sSQL .= "AND array_cat(s.nameaddress_vector, s.name_vector) @> ARRAY[".join($aSearch['aAddress'], ",")."] AND ";
- } else {
- $sSQL .= " WHERE ";
- }
- $sSQL .= "p.postcode = '".pg_escape_string(reset($aSearch['aName']))."'";
- if ($aSearch['sCountryCode']) {
- $sSQL .= " AND p.country_code = '".$aSearch['sCountryCode']."'";
- } elseif ($sCountryCodesSQL) {
- $sSQL .= " AND p.country_code in ($sCountryCodesSQL)";
- }
- $sSQL .= " LIMIT $this->iLimit";
- if (CONST_Debug) var_dump($sSQL);
- $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
- } else {
- $aPlaceIDs = array();
-
- // First we need a position, either aName or fLat or both
- $aTerms = array();
- $aOrder = array();
-
- if ($aSearch['sHouseNumber'] && sizeof($aSearch['aAddress'])) {
- $sHouseNumberRegex = '\\\\m'.$aSearch['sHouseNumber'].'\\\\M';
- $aOrder[] = "";
- $aOrder[0] = " (";
- $aOrder[0] .= " EXISTS(";
- $aOrder[0] .= " SELECT place_id ";
- $aOrder[0] .= " FROM placex ";
- $aOrder[0] .= " WHERE parent_place_id = search_name.place_id";
- $aOrder[0] .= " AND transliteration(housenumber) ~* E'".$sHouseNumberRegex."' ";
- $aOrder[0] .= " LIMIT 1";
- $aOrder[0] .= " ) ";
- // also housenumbers from interpolation lines table are needed
- $aOrder[0] .= " OR EXISTS(";
- $aOrder[0] .= " SELECT place_id ";
- $aOrder[0] .= " FROM location_property_osmline ";
- $aOrder[0] .= " WHERE parent_place_id = search_name.place_id";
- $aOrder[0] .= " AND startnumber is not NULL";
- $aOrder[0] .= " AND ".intval($aSearch['sHouseNumber']).">=startnumber ";
- $aOrder[0] .= " AND ".intval($aSearch['sHouseNumber'])."<=endnumber ";
- $aOrder[0] .= " LIMIT 1";
- $aOrder[0] .= " )";
- $aOrder[0] .= " )";
- $aOrder[0] .= " 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'], ",")."]";
- if (sizeof($aSearch['aNameNonSearch'])) $aTerms[] = "array_cat(name_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aNameNonSearch'], ",")."]";
- if (sizeof($aSearch['aAddress']) && $aSearch['aName'] != $aSearch['aAddress']) {
- // For infrequent name terms disable index usage for address
- if (CONST_Search_NameOnlySearchFrequencyThreshold
- && sizeof($aSearch['aName']) == 1
- && $aWordFrequencyScores[$aSearch['aName'][reset($aSearch['aName'])]] < CONST_Search_NameOnlySearchFrequencyThreshold
- ) {
- $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join(array_merge($aSearch['aAddress'], $aSearch['aAddressNonSearch']), ",")."]";
- } else {
- $aTerms[] = "nameaddress_vector @> ARRAY[".join($aSearch['aAddress'], ",")."]";
- if (sizeof($aSearch['aAddressNonSearch'])) {
- $aTerms[] = "array_cat(nameaddress_vector,ARRAY[]::integer[]) @> ARRAY[".join($aSearch['aAddressNonSearch'], ",")."]";
- }
- }
- }
- if ($aSearch['sCountryCode']) $aTerms[] = "country_code = '".pg_escape_string($aSearch['sCountryCode'])."'";
- 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['oNear']) {
- $aTerms[] = $aSearch['oNear']->withinSQL('centroid');
-
- $aOrder[] = $aSearch['oNear']->distanceSQL('centroid');
- } elseif ($aSearch['sPostcode']) {
- $aOrder[] = "(SELECT min(ST_Distance(search_name.centroid, p.geometry)) FROM location_postcode p WHERE p.postcode = '".$aSearch['sPostcode']."')";
- }
- if (sizeof($this->aExcludePlaceIDs)) {
- $aTerms[] = "place_id not in (".join(',', $this->aExcludePlaceIDs).")";
- }
- if ($sCountryCodesSQL) {
- $aTerms[] = "country_code in ($sCountryCodesSQL)";
- }
-
- if ($bBoundingBoxSearch) $aTerms[] = "centroid && $this->sViewboxSmallSQL";
- if ($oNearPoint) {
- $aOrder[] = $oNearPoint->distanceSQL('centroid');
- }
-
- if ($aSearch['sHouseNumber']) {
- $sImportanceSQL = '- abs(26 - address_rank) + 3';
- } else {
- $sImportanceSQL = '(CASE WHEN importance = 0 OR importance IS NULL THEN 0.75-(search_rank::float/40) ELSE importance END)';