- $sSQL = 'select place_id,parent_place_id,rank_search from placex';
- $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
- $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
- $sSQL .= ' and (name is not null or housenumber is not null)';
- $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\')';
- $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
- $sSQL .= ' and indexed_status = 0 ';
- $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
- $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
- if (CONST_Debug) var_dump($sSQL);
- $aPlace = $oDB->getRow($sSQL);
- if (PEAR::IsError($aPlace))
- {
- failInternalError("Could not determine closest place.", $sSQL, $iPlaceID);
- }
- $iPlaceID = $aPlace['place_id'];
- $iParentPlaceID = $aPlace['parent_place_id'];
- }
-
- // The point we found might be too small - use the address to find what it is a child of
- if ($iPlaceID && $iMaxRank < 28)
- {
- if ($aPlace['rank_search'] > 28 && $iParentPlaceID)
- {
- $iPlaceID = $iParentPlaceID;
- }
- $sSQL = "select address_place_id from place_addressline where place_id = $iPlaceID order by abs(cached_rank_address - $iMaxRank) asc,cached_rank_address desc,isaddress desc,distance desc limit 1";
- $iPlaceID = $oDB->getOne($sSQL);
- if (PEAR::IsError($iPlaceID))
- {
- failInternalError("Could not get parent for place.", $sSQL, $iPlaceID);
- }
- if (!$iPlaceID)
- {
- $iPlaceID = $aPlace['place_id'];
- }
- }