-
- 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 calculated_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
- $sSQL = "SELECT count(*) FROM pg_tables ";
- $sSQL .= "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 ";
- $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 ($sNearPointSQL) $sOrderBySQL = "ST_Distance($sNearPointSQL, l.centroid)";
- elseif ($sPlaceIDs) $sOrderBySQL = "ST_Distance(l.centroid, f.geometry)";
- elseif ($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:'');
- $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.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;