-
- 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 ";
- $sSQL .= " FROM placex ";
- $sSQL .= " WHERE place_id in ($sPlaceIDs) ";
- $sSQL .= " AND class='".$aSearch['sClass']."' ";
- $sSQL .= " AND type='".$aSearch['sType']."'";
- $sSQL .= " AND linked_place_id is null";
- if ($sCountryCodesSQL) $sSQL .= " AND country_code in ($sCountryCodesSQL)";
- $sSQL .= " ORDER BY rank_search ASC ";
- $sSQL .= " LIMIT $this->iLimit";
- if (CONST_Debug) var_dump($sSQL);
- $aClassPlaceIDs = chksql($this->oDB->getCol($sSQL));
- }
-
- if (!$aSearch['sOperator'] || $aSearch['sOperator'] == 'near') { // & in
- $sClassTable = 'place_classtype_'.$aSearch['sClass'].'_'.$aSearch['sType'];
- $sSQL = "SELECT count(*) FROM pg_tables ";
- $sSQL .= "WHERE tablename = '$sClassTable'";
- $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 ";
- $sSQL .= " FROM placex";
- $sSQL .= " WHERE place_id in ($sPlaceIDs)";
- $sSQL .= " AND rank_search < $this->iMaxRank + 5";
- $sSQL .= " AND ST_Geometrytype(geometry) in ('ST_Polygon','ST_MultiPolygon')";
- $sSQL .= " ORDER BY rank_search ASC ";
- $sSQL .= " 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 ($oNearPoint) {
- $sOrderBySQL = $oNearPoint->distanceSQL('l.centroid');
- } elseif ($sPlaceIDs) {
- $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
- } elseif ($sPlaceGeom) {
- $sOrderBySQL = "ST_Distance(st_centroid('".$sPlaceGeom."'), l.centroid)";
- }
-
- $sSQL = "select distinct i.place_id".($sOrderBySQL?', i.order_term':'')." from (";
- $sSQL .= "select l.place_id".($sOrderBySQL?','.$sOrderBySQL.' as order_term':'')." from ".$sClassTable." 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.country_code in ($sCountryCodesSQL)";
- $sSQL .= 'limit 300) i ';
- if ($sOrderBySQL) $sSQL .= "order by order_term 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 ($aSearch['oNear']) {
- $fRange = $aSearch['oNear']->radius();
- }
-
- $sOrderBySQL = '';
- if ($oNearPoint) {
- $sOrderBySQL = $oNearPoint->distanceSQL('l.geometry');
- } else {
- $sOrderBySQL = "ST_Distance(l.geometry, f.geometry)";
- }
-
- $sSQL = "SELECT distinct l.place_id".($sOrderBySQL?','.$sOrderBySQL:'');
- $sSQL .= " FROM placex as l, placex as f ";
- $sSQL .= " WHERE f.place_id in ($sPlaceIDs) ";
- $sSQL .= " AND ST_DWithin(l.geometry, f.centroid, $fRange) ";
- $sSQL .= " AND l.class='".$aSearch['sClass']."' ";
- $sSQL .= " AND l.type='".$aSearch['sType']."' ";
- if (sizeof($this->aExcludePlaceIDs)) {
- $sSQL .= " AND l.place_id not in (".join(',', $this->aExcludePlaceIDs).")";
- }
- if ($sCountryCodesSQL) $sSQL .= " AND l.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)));
- }
- }
- }
- $aPlaceIDs = $aClassPlaceIDs;