+ $sSQL = 'select place_id,parent_place_id,rank_address,country_code, geometry';
+ $sSQL .= ' FROM placex';
+ $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\',\'ST_MultiPolygon\')';
+ $sSQL .= ' AND rank_address <= LEAST(25, '.$iMaxRank.')';
+ $sSQL .= ' AND ST_CONTAINS(geometry, '.$sPointSQL.' )';
+ $sSQL .= ' AND type != \'postcode\' ';
+ $sSQL .= ' and rank_address != 28';
+ $sSQL .= ' and (name is not null or housenumber is not null';
+ $sSQL .= ' or rank_address between 26 and 27)';
+ $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
+ $sSQL .= ' ORDER BY rank_address DESC LIMIT 1';
+
+ $aPoly = chksql(
+ $this->oDB->getRow($sSQL),
+ 'Could not determine polygon containing the point.'
+ );
+ if ($aPoly) {
+ $iParentPlaceID = $aPoly['parent_place_id'];
+ $iRankAddress = $aPoly['rank_address'];
+ $iPlaceID = $aPoly['place_id'];
+
+ $sSQL = 'SELECT *';
+ $sSQL .= ' FROM (';
+ $sSQL .= ' SELECT place_id, rank_address,country_code, linked_place_id, 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 <= LEAST(25, '.$iMaxRank.')';
+ $sSQL .= ' AND type != \'postcode\'';
+ $sSQL .= ' AND name IS NOT NULL ';
+ $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
+ $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;
+ }
+ }
+ return $aPoly;
+ }
+
+ public function lookup($fLat, $fLon, $bDoInterpolation = true)
+ {
+ return $this->lookupPoint(
+ 'ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326)',
+ $bDoInterpolation
+ );
+ }
+
+ public function lookupPoint($sPointSQL, $bDoInterpolation = true)
+ {
+