- $sSQL = 'select place_id,parent_place_id,rank_search,country_code,';
- $sSQL .=' ST_distance('.$sPointSQL.', geometry) as distance';
- $sSQL .= ' FROM placex';
- $sSQL .= ' WHERE osm_type = \'N\'';
- $sSQL .= ' AND rank_search >= '.$iRankSearch;
- $sSQL .= ' AND rank_search <= LEAST(25, '.$iMaxRank.')';
- $sSQL .= ' AND ST_CONTAINS(\''.$iGeometry.'\'::geometry, geometry )';
- $sSQL .= ' AND type != \'postcode\'';
- $sSQL .= ' AND name IS NOT NULL ';
- $sSQL .= ' ORDER BY distance ASC,';
- $sSQL .= ' rank_search DESC';
- $sSQL .= ' limit 1';
- if (CONST_Debug) var_dump($sSQL);
- $aPlacNode = chksql(
- $this->oDB->getRow($sSQL),
- 'Could not determine place node.'
- );
- if ($aPlacNode) {
- return $aPlacNode;
+ if ($iRankAddress != $iMaxRank) {
+ $sSQL = 'SELECT *';
+ $sSQL .= ' FROM (';
+ $sSQL .= ' SELECT place_id, rank_address,country_code, geometry,';
+ $sSQL .= ' ST_distance('.$sPointSQL.', geometry) as distance';
+ $sSQL .= ' FROM placex';
+ $sSQL .= ' WHERE osm_type = \'N\'';
+ $sSQL .= ' AND rank_address > '.$iRankAddress;
+ $sSQL .= ' AND rank_address <= '.Min(25,$iMaxRank);
+ $sSQL .= ' AND type != \'postcode\'';
+ $sSQL .= ' AND name IS NOT NULL ';
+ $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
+ $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
+ // preselection through bbox
+ $sSQL .= ' AND (SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.') && geometry';
+ $sSQL .= ' ORDER BY distance ASC,';
+ $sSQL .= ' rank_address DESC';
+ $sSQL .= ' limit 500) as a';
+ $sSQL .= ' WHERE ST_CONTAINS((SELECT geometry FROM placex WHERE place_id = '.$iPlaceID.'), geometry )';
+ $sSQL .= ' ORDER BY distance ASC, rank_address DESC';
+ $sSQL .= ' LIMIT 1';
+
+ if (CONST_Debug) var_dump($sSQL);
+ $aPlacNode = chksql(
+ $this->oDB->getRow($sSQL),
+ 'Could not determine place node.'
+ );
+ if ($aPlacNode) {
+ return $aPlacNode;
+ }