]> git.openstreetmap.org Git - nominatim.git/blobdiff - lib/ReverseGeocode.php
test adjusting
[nominatim.git] / lib / ReverseGeocode.php
index 93da7caf168066f41c80329046b0eb4b4c34c6a6..b69e6446eee299e80e5925665d5d1913fe98568b 100644 (file)
@@ -68,7 +68,43 @@ class ReverseGeocode
             'Could not determine closest housenumber on an osm interpolation line.'
         );
     }
-
+    
+    protected function noPolygonFound($sPointSQL, $iMaxRank)
+    {   
+        // searches for polygon in table country_osm_grid which contains the searchpoint
+        $sSQL = 'SELECT * FROM country_osm_grid';
+        $sSQL .= ' WHERE ST_CONTAINS (geometry, '.$sPointSQL.' )';
+        
+        $aPoly = chksql(
+            $this->oDB->getRow($sSQL),
+            'Could not determine polygon containing the point.'
+        );
+        if ($aPoly) {
+            $sCountryCode = $aPoly['country_code'];
+            
+            $sSQL = 'SELECT *, ST_distance('.$sPointSQL.', geometry) as distance';
+            $sSQL .= ' FROM placex';
+            $sSQL .= ' WHERE osm_type = \'N\'';
+            $sSQL .= ' AND country_code = \''.$sCountryCode.'\'';
+            $sSQL .= ' AND rank_address > 0';
+            $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
+            $sSQL .= ' AND type != \'postcode\'';
+            $sSQL .= ' AND name IS NOT NULL ';
+            $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
+            $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;
+            }
+        }
+    }
+    
     protected function lookupPolygon($sPointSQL, $iMaxRank)
     {
     
@@ -76,7 +112,7 @@ class ReverseGeocode
         $aPlace = null;
         
         $sSQL = 'SELECT * FROM';
-        $sSQL .= '(select place_id,parent_place_id,rank_address,country_code, geometry';
+        $sSQL .= '(select place_id,parent_place_id,rank_address, rank_search, country_code, geometry';
         $sSQL .= ' FROM placex';
         $sSQL .= ' WHERE ST_GeometryType(geometry) in (\'ST_Polygon\', \'ST_MultiPolygon\')';
         $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
@@ -95,6 +131,7 @@ class ReverseGeocode
         if ($aPoly) {
             $iParentPlaceID = $aPoly['parent_place_id'];
             $iRankAddress = $aPoly['rank_address'];
+            $iRankSearch = $aPoly['rank_search'];
             $iPlaceID = $aPoly['place_id'];
             
             if ($iRankAddress != $iMaxRank) {
@@ -104,8 +141,15 @@ class ReverseGeocode
                 $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);
+                if ($iRankAddress = 16){
+                //  using rank_search because of a better differentiation for place nodes at rank_address 16
+                    $sSQL .= ' AND rank_search > '.$iRankSearch;
+                    $sSQL .= ' AND rank_search <= ' .Min(25, $iMaxRank);
+                    $sSQL .= ' AND class = \'place\'';
+                }else{
+                    $sSQL .= ' AND rank_address > '.$iRankAddress;
+                    $sSQL .= ' AND rank_address <= ' .Min(25, $iMaxRank);
+                }
                 $sSQL .= ' AND type != \'postcode\'';
                 $sSQL .= ' AND name IS NOT NULL ';
                 $sSQL .= ' and indexed_status = 0 and linked_place_id is null';
@@ -152,7 +196,7 @@ class ReverseGeocode
         $bIsTigerStreet = false;
         
         // try with interpolations before continuing
-        if ($bDoInterpolation && $iMaxRank >= 30 ) {
+        if ($bDoInterpolation && $iMaxRank >= 30) {
             $aHouse = $this->lookupInterpolation($sPointSQL, $fSearchDiam/3);
 
             if ($aHouse) {
@@ -231,6 +275,11 @@ class ReverseGeocode
                 $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
                 if ($aPlace) {
                     $oResult = new Result($aPlace['place_id']);
+                } elseif(!$aPlace && $iMaxRank > 4)  {
+                    $aPlace = $this->noPolygonFound($sPointSQL, $iMaxRank);
+                    if ($aPlace) {
+                        $oResult = new Result($aPlace['place_id']);
+                    }
                 }
             }
             // lower than street level ($iMaxRank < 26 )
@@ -238,6 +287,11 @@ class ReverseGeocode
             $aPlace = $this->lookupPolygon($sPointSQL, $iMaxRank);
             if ($aPlace) {
                 $oResult = new Result($aPlace['place_id']);
+            } elseif(!$aPlace && $iMaxRank > 4) {
+                $aPlace = $this->noPolygonFound($sPointSQL, $iMaxRank);
+                if ($aPlace) {
+                    $oResult = new Result($aPlace['place_id']);
+                }
             }
         }
         return $oResult;