- $searchedHousenumber = -1;
-
- if (CONST_Debug) { echo "<hr><b>Search Loop, group $iGroupLoop, loop $iQueryLoop</b>"; }
- if (CONST_Debug) _debugDumpGroupedSearches(array($iGroupedRank => array($aSearch)), $aValidTokens);
-
- // No location term?
- if (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['fLon'])
- {
- 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 calculated_country_code='".$aSearch['sCountryCode']."' and rank_search = 4";
- if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
- 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['fLon']) 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 calculated_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 calculated_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 from placex where class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
- $sSQL .= " and st_contains($this->sViewboxSmallSQL, geometry) and linked_place_id is null";
- if ($sCountryCodesSQL) $sSQL .= " and calculated_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));
- }
- }
- }
- // If a coordinate is given, the search must either
- // be for a name or a special search. Ignore everythin else.
- else if ($aSearch['fLon'] && !sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && !$aSearch['sClass'])
- {
- $aPlaceIDs = array();
- }
- 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] = " (exists(select place_id from placex where parent_place_id = search_name.place_id";
- $aOrder[0] .= " and transliteration(housenumber) ~* E'".$sHouseNumberRegex."' limit 1) ";
- // also housenumbers from interpolation lines table are needed
- $aOrder[0] .= " or exists(select place_id from location_property_osmline where parent_place_id = search_name.place_id";
- $aOrder[0] .= " and ".intval($aSearch['sHouseNumber']).">=startnumber and ".intval($aSearch['sHouseNumber'])."<=endnumber limit 1))";
- $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['fLon'] && $aSearch['fLat'])
- {
- $aTerms[] = "ST_DWithin(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326), ".$aSearch['fRadius'].")";
- $aOrder[] = "ST_Distance(centroid, ST_SetSRID(ST_Point(".$aSearch['fLon'].",".$aSearch['fLat']."),4326)) ASC";
- }
- 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 ($sNearPointSQL) $aOrder[] = "ST_Distance($sNearPointSQL, centroid) asc";
-
- 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)';
- }
- if ($this->sViewboxSmallSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxSmallSQL, centroid) THEN 1 ELSE 0.5 END";
- if ($this->sViewboxLargeSQL) $sImportanceSQL .= " * case when ST_Contains($this->sViewboxLargeSQL, centroid) THEN 1 ELSE 0.5 END";
-
- $aOrder[] = "$sImportanceSQL DESC";
- if (sizeof($aSearch['aFullNameAddress']))
- {
- $sExactMatchSQL = '(select count(*) from (select unnest(ARRAY['.join($aSearch['aFullNameAddress'],",").']) INTERSECT select unnest(nameaddress_vector))s) as exactmatch';
- $aOrder[] = 'exactmatch DESC';
- } else {
- $sExactMatchSQL = '0::int as exactmatch';
- }
-
- if (sizeof($aTerms))
- {
- $sSQL = "select place_id, ";
- $sSQL .= $sExactMatchSQL;
- $sSQL .= " from search_name";
- $sSQL .= " where ".join(' and ',$aTerms);
- $sSQL .= " order by ".join(', ',$aOrder);
- if ($aSearch['sHouseNumber'] || $aSearch['sClass'])
- $sSQL .= " limit 20";
- elseif (!sizeof($aSearch['aName']) && !sizeof($aSearch['aAddress']) && $aSearch['sClass'])
- $sSQL .= " limit 1";
- else
- $sSQL .= " limit ".$this->iLimit;
-
- if (CONST_Debug) { var_dump($sSQL); }
- $aViewBoxPlaceIDs = chksql($this->oDB->getAll($sSQL),
- "Could not get places for search terms.");
- //var_dump($aViewBoxPlaceIDs);
- // Did we have an viewbox matches?
- $aPlaceIDs = array();
- $bViewBoxMatch = false;
- foreach($aViewBoxPlaceIDs as $aViewBoxRow)
- {
- //if ($bViewBoxMatch == 1 && $aViewBoxRow['in_small'] == 'f') break;
- //if ($bViewBoxMatch == 2 && $aViewBoxRow['in_large'] == 'f') break;
- //if ($aViewBoxRow['in_small'] == 't') $bViewBoxMatch = 1;
- //else if ($aViewBoxRow['in_large'] == 't') $bViewBoxMatch = 2;
- $aPlaceIDs[] = $aViewBoxRow['place_id'];
- $this->exactMatchCache[$aViewBoxRow['place_id']] = $aViewBoxRow['exactmatch'];
- }
- }
- //var_Dump($aPlaceIDs);
- //exit;
-
- //now search for housenumber, if housenumber provided
- if ($aSearch['sHouseNumber'] && sizeof($aPlaceIDs))
- {
- $searchedHousenumber = intval($aSearch['sHouseNumber']);
- $aRoadPlaceIDs = $aPlaceIDs;
- $sPlaceIDs = join(',',$aPlaceIDs);
-
- // Now they are indexed, look for a house attached to a street we found
- $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).")";
- }
- $sSQL .= " limit $this->iLimit";
- if (CONST_Debug) var_dump($sSQL);
- $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
-
- // if nothing found, search in the interpolation line table
- if(!sizeof($aPlaceIDs))
- {
- // do we need to use transliteration and the regex for housenumbers???
- //new query for lines, not housenumbers anymore
- if($searchedHousenumber%2 == 0){
- //if housenumber is even, look for housenumber in streets with interpolationtype even or all
- $sSQL = "select distinct place_id from location_property_osmline where parent_place_id in (".$sPlaceIDs.") and (interpolationtype='even' or interpolationtype='all') and ".$searchedHousenumber.">=startnumber and ".$searchedHousenumber."<=endnumber";
- }else{
- //look for housenumber in streets with interpolationtype odd or all
- $sSQL = "select distinct place_id from location_property_osmline where parent_place_id in (".$sPlaceIDs.") and (interpolationtype='odd' or interpolationtype='all') and ".$searchedHousenumber.">=startnumber and ".$searchedHousenumber."<=endnumber";
- }
-
- if (sizeof($this->aExcludePlaceIDs))
- {
- $sSQL .= " and place_id not in (".join(',', $this->aExcludePlaceIDs).")";
- }
- //$sSQL .= " limit $this->iLimit";
- if (CONST_Debug) var_dump($sSQL);
- //get place IDs
- $aPlaceIDs = chksql($this->oDB->getCol($sSQL, 0));
- }
-
- // If nothing found try the aux fallback table
- if (CONST_Use_Aux_Location_data && !sizeof($aPlaceIDs))
- {
- $sSQL = "select place_id from location_property_aux where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
- if (sizeof($this->aExcludePlaceIDs))
- {
- $sSQL .= " and parent_place_id not in (".join(',',$this->aExcludePlaceIDs).")";
- }
- //$sSQL .= " limit $this->iLimit";
- if (CONST_Debug) var_dump($sSQL);
- $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
- }
-
- //if nothing was found in placex or location_property_aux, then search in Tiger data for this housenumber(location_property_tiger)
- if (CONST_Use_US_Tiger_Data && !sizeof($aPlaceIDs))
- {
- //new query for lines, not housenumbers anymore
- if($searchedHousenumber%2 == 0){
- //if housenumber is even, look for housenumber in streets with interpolationtype even or all
- $sSQL = "select distinct place_id from location_property_tiger where parent_place_id in (".$sPlaceIDs.") and (interpolationtype='even' or interpolationtype='all') and ".$searchedHousenumber.">=startnumber and ".$searchedHousenumber."<=endnumber";
- }else{
- //look for housenumber in streets with interpolationtype odd or all
- $sSQL = "select distinct place_id from location_property_tiger where parent_place_id in (".$sPlaceIDs.") and (interpolationtype='odd' or interpolationtype='all') and ".$searchedHousenumber.">=startnumber and ".$searchedHousenumber."<=endnumber";
- }
-
- if (sizeof($this->aExcludePlaceIDs))
- {
- $sSQL .= " and place_id not in (".join(',', $this->aExcludePlaceIDs).")";
- }
- //$sSQL .= " limit $this->iLimit";
- if (CONST_Debug) var_dump($sSQL);
- //get place IDs
- $aPlaceIDs = chksql($this->oDB->getCol($sSQL, 0));
- }
-
- // Fallback to the road (if no housenumber was found)
- if (!sizeof($aPlaceIDs) && preg_match('/[0-9]+/', $aSearch['sHouseNumber']))
- {
- $aPlaceIDs = $aRoadPlaceIDs;
- //set to -1, if no housenumbers were found
- $searchedHousenumber = -1;
- }
- //else: housenumber was found, remains saved in searchedHousenumber
- }
-
-
- if ($aSearch['sClass'] && sizeof($aPlaceIDs))
- {
- $sPlaceIDs = join(',', $aPlaceIDs);
- $aClassPlaceIDs = array();
-
- if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'name')
- {
- // If they were searching for a named class (i.e. 'Kings Head pub') then we might have an extra match
- $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and class='".$aSearch['sClass']."' and type='".$aSearch['sType']."'";
- $sSQL .= " and linked_place_id is null";
- if ($sCountryCodesSQL) $sSQL .= " and calculated_country_code in ($sCountryCodesSQL)";
- $sSQL .= " order by rank_search asc limit $this->iLimit";
- if (CONST_Debug) var_dump($sSQL);
- $aClassPlaceIDs = chksql($this->oDB->getCol($sSQL));
- }
-
- if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') // & in
- {
- $sSQL = "select count(*) from pg_tables where tablename = 'place_classtype_".$aSearch['sClass']."_".$aSearch['sType']."'";
- $bCacheTable = chksql($this->oDB->getOne($sSQL));
-
- $sSQL = "select min(rank_search) from placex where place_id in ($sPlaceIDs)";
-
- if (CONST_Debug) var_dump($sSQL);
- $this->iMaxRank = ((int)chksql($this->oDB->getOne($sSQL)));
-
- // For state / country level searches the normal radius search doesn't work very well
- $sPlaceGeom = false;
- if ($this->iMaxRank < 9 && $bCacheTable)
- {
- // Try and get a polygon to search in instead
- $sSQL = "select geometry from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank + 5 and st_geometrytype(geometry) in ('ST_Polygon','ST_MultiPolygon') order by rank_search asc limit 1";
- if (CONST_Debug) var_dump($sSQL);
- $sPlaceGeom = chksql($this->oDB->getOne($sSQL));
- }
-
- if ($sPlaceGeom)
- {
- $sPlaceIDs = false;
- }
- else
- {
- $this->iMaxRank += 5;
- $sSQL = "select place_id from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank";
- if (CONST_Debug) var_dump($sSQL);
- $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
- $sPlaceIDs = join(',',$aPlaceIDs);
- }
-
- if ($sPlaceIDs || $sPlaceGeom)
- {
-
- $fRange = 0.01;
- if ($bCacheTable)
- {
- // More efficient - can make the range bigger
- $fRange = 0.05;
-
- $sOrderBySQL = '';
- if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.centroid)";
- else if ($sPlaceIDs) $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
- else if ($sPlaceGeom) $sOrderBysSQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
-
- $sSQL = "select distinct l.place_id".($sOrderBySQL?','.$sOrderBySQL:'')." from place_classtype_".$aSearch['sClass']."_".$aSearch['sType']." as l";
- if ($sCountryCodesSQL) $sSQL .= " join placex as lp using (place_id)";
- if ($sPlaceIDs)
- {
- $sSQL .= ",placex as f where ";
- $sSQL .= "f.place_id in ($sPlaceIDs) and ST_DWithin(l.centroid, f.centroid, $fRange) ";
- }
- if ($sPlaceGeom)
- {
- $sSQL .= " where ";
- $sSQL .= "ST_Contains('".$sPlaceGeom."', l.centroid) ";
- }
- if (sizeof($this->aExcludePlaceIDs))
- {
- $sSQL .= " and l.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 ($this->iOffset) $sSQL .= " offset $this->iOffset";
- $sSQL .= " limit $this->iLimit";
- if (CONST_Debug) var_dump($sSQL);
- $aClassPlaceIDs = array_merge($aClassPlaceIDs, chksql($this->oDB->getCol($sSQL)));
- }
- else
- {
- if (isset($aSearch['fRadius']) && $aSearch['fRadius']) $fRange = $aSearch['fRadius'];
-
- $sOrderBySQL = '';
- if ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.geometry)";
- else $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
-
- $sSQL = "select distinct l.place_id".($sOrderBysSQL?','.$sOrderBysSQL:'')." from placex as l,placex as f where ";
- $sSQL .= "f.place_id in ( $sPlaceIDs) and ST_DWithin(l.geometry, f.centroid, $fRange) ";
- $sSQL .= "and l.class='".$aSearch['sClass']."' and l.type='".$aSearch['sType']."' ";
- if (sizeof($this->aExcludePlaceIDs))
- {
- $sSQL .= " and l.place_id not in (".join(',',$this->aExcludePlaceIDs).")";
- }
- if ($sCountryCodesSQL) $sSQL .= " and l.calculated_country_code in ($sCountryCodesSQL)";
- if ($sOrderBy) $sSQL .= "order by ".$OrderBysSQL." asc";
- if ($this->iOffset) $sSQL .= " offset $this->iOffset";
- $sSQL .= " limit $this->iLimit";
- if (CONST_Debug) var_dump($sSQL);
- $aClassPlaceIDs = array_merge($aClassPlaceIDs, chksql($this->oDB->getCol($sSQL)));
- }
- }
- }
-
- $aPlaceIDs = $aClassPlaceIDs;
-
- }