]> git.openstreetmap.org Git - nominatim.git/blobdiff - lib/PlaceLookup.php
search/reverse/lookup with geojson,geocodejson output
[nominatim.git] / lib / PlaceLookup.php
index 4fce7e3683a74a32d1b6a95faca75e731d831446..0bf8c2fe73f85785ff85280fb6b1fe050b1d3e86 100644 (file)
@@ -11,6 +11,7 @@ class PlaceLookup
     protected $aLangPrefOrderSql = "''";
 
     protected $bAddressDetails = false;
     protected $aLangPrefOrderSql = "''";
 
     protected $bAddressDetails = false;
+    protected $bAddressAdminLevels = false;
     protected $bExtraTags = false;
     protected $bNameDetails = false;
 
     protected $bExtraTags = false;
     protected $bNameDetails = false;
 
@@ -42,6 +43,16 @@ class PlaceLookup
         $this->bIncludePolygonAsPoints = $b;
     }
 
         $this->bIncludePolygonAsPoints = $b;
     }
 
+    public function setAddressDetails($b = true)
+    {
+        $this->bAddressDetails = $b;
+    }
+
+    public function setAddressAdminLevels($b = true)
+    {
+        $this->bAddressAdminLevels = $b;
+    }
+
     public function loadParamArray($oParams, $sGeomType = null)
     {
         $aLangs = $oParams->getPreferredLanguages();
     public function loadParamArray($oParams, $sGeomType = null)
     {
         $aLangs = $oParams->getPreferredLanguages();
@@ -54,17 +65,21 @@ class PlaceLookup
 
         $this->bDeDupe = $oParams->getBool('dedupe', $this->bDeDupe);
 
 
         $this->bDeDupe = $oParams->getBool('dedupe', $this->bDeDupe);
 
-        if ($sGeomType === null || $sGeomType == 'text') {
-            $this->bIncludePolygonAsText = $oParams->getBool('polygon_text');
-        }
         if ($sGeomType === null || $sGeomType == 'geojson') {
             $this->bIncludePolygonAsGeoJSON = $oParams->getBool('polygon_geojson');
         if ($sGeomType === null || $sGeomType == 'geojson') {
             $this->bIncludePolygonAsGeoJSON = $oParams->getBool('polygon_geojson');
+            $this->bIncludePolygonAsPoints = false;
         }
         }
-        if ($sGeomType === null || $sGeomType == 'kml') {
-            $this->bIncludePolygonAsKML = $oParams->getBool('polygon_kml');
-        }
-        if ($sGeomType === null || $sGeomType == 'svg') {
-            $this->bIncludePolygonAsSVG = $oParams->getBool('polygon_svg');
+
+        if ($oParams->getString('format', '') !== 'geojson') {
+            if ($sGeomType === null || $sGeomType == 'text') {
+                $this->bIncludePolygonAsText = $oParams->getBool('polygon_text');
+            }
+            if ($sGeomType === null || $sGeomType == 'kml') {
+                $this->bIncludePolygonAsKML = $oParams->getBool('polygon_kml');
+            }
+            if ($sGeomType === null || $sGeomType == 'svg') {
+                $this->bIncludePolygonAsSVG = $oParams->getBool('polygon_svg');
+            }
         }
         $this->fPolygonSimplificationThreshold
             = $oParams->getFloat('polygon_threshold', 0.0);
         }
         $this->fPolygonSimplificationThreshold
             = $oParams->getFloat('polygon_threshold', 0.0);
@@ -164,19 +179,21 @@ class PlaceLookup
 
         $aResults = $this->lookup(array($iPlaceID => new Result($iPlaceID)));
 
 
         $aResults = $this->lookup(array($iPlaceID => new Result($iPlaceID)));
 
-        return sizeof($aResults) ? reset($aResults) : null;
+        return empty($aResults) ? null : reset($aResults);
     }
 
     public function lookup($aResults, $iMinRank = 0, $iMaxRank = 30)
     {
     }
 
     public function lookup($aResults, $iMinRank = 0, $iMaxRank = 30)
     {
-        if (!sizeof($aResults)) {
+        Debug::newFunction('Place lookup');
+
+        if (empty($aResults)) {
             return array();
         }
         $aSubSelects = array();
 
         $sPlaceIDs = Result::joinIdsByTable($aResults, Result::TABLE_PLACEX);
             return array();
         }
         $aSubSelects = array();
 
         $sPlaceIDs = Result::joinIdsByTable($aResults, Result::TABLE_PLACEX);
-        if (CONST_Debug) var_dump('PLACEX', $sPlaceIDs);
         if ($sPlaceIDs) {
         if ($sPlaceIDs) {
+            Debug::printVar('Ids from placex', $sPlaceIDs);
             $sSQL  = 'SELECT ';
             $sSQL .= '    osm_type,';
             $sSQL .= '    osm_id,';
             $sSQL  = 'SELECT ';
             $sSQL .= '    osm_type,';
             $sSQL .= '    osm_id,';
@@ -246,6 +263,7 @@ class PlaceLookup
         // postcode table
         $sPlaceIDs = Result::joinIdsByTable($aResults, Result::TABLE_POSTCODE);
         if ($sPlaceIDs) {
         // postcode table
         $sPlaceIDs = Result::joinIdsByTable($aResults, Result::TABLE_POSTCODE);
         if ($sPlaceIDs) {
+            Debug::printVar('Ids from location_postcode', $sPlaceIDs);
             $sSQL = 'SELECT';
             $sSQL .= "  'P' as osm_type,";
             $sSQL .= '  (SELECT osm_id from placex p WHERE p.place_id = lp.parent_place_id) as osm_id,';
             $sSQL = 'SELECT';
             $sSQL .= "  'P' as osm_type,";
             $sSQL .= '  (SELECT osm_id from placex p WHERE p.place_id = lp.parent_place_id) as osm_id,';
@@ -276,6 +294,7 @@ class PlaceLookup
             if (CONST_Use_US_Tiger_Data) {
                 $sPlaceIDs = Result::joinIdsByTable($aResults, Result::TABLE_TIGER);
                 if ($sPlaceIDs) {
             if (CONST_Use_US_Tiger_Data) {
                 $sPlaceIDs = Result::joinIdsByTable($aResults, Result::TABLE_TIGER);
                 if ($sPlaceIDs) {
+                    Debug::printVar('Ids from Tiger table', $sPlaceIDs);
                     $sHousenumbers = Result::sqlHouseNumberTable($aResults, Result::TABLE_TIGER);
                     // Tiger search only if a housenumber was searched and if it was found
                     // (realized through a join)
                     $sHousenumbers = Result::sqlHouseNumberTable($aResults, Result::TABLE_TIGER);
                     // Tiger search only if a housenumber was searched and if it was found
                     // (realized through a join)
@@ -321,6 +340,7 @@ class PlaceLookup
             // osmline - interpolated housenumbers
             $sPlaceIDs = Result::joinIdsByTable($aResults, Result::TABLE_OSMLINE);
             if ($sPlaceIDs) {
             // osmline - interpolated housenumbers
             $sPlaceIDs = Result::joinIdsByTable($aResults, Result::TABLE_OSMLINE);
             if ($sPlaceIDs) {
+                Debug::printVar('Ids from interpolation', $sPlaceIDs);
                 $sHousenumbers = Result::sqlHouseNumberTable($aResults, Result::TABLE_OSMLINE);
                 // interpolation line search only if a housenumber was searched
                 // (realized through a join)
                 $sHousenumbers = Result::sqlHouseNumberTable($aResults, Result::TABLE_OSMLINE);
                 // interpolation line search only if a housenumber was searched
                 // (realized through a join)
@@ -406,16 +426,13 @@ class PlaceLookup
             }
         }
 
             }
         }
 
-        if (CONST_Debug) var_dump($aSubSelects);
-
-        if (!sizeof($aSubSelects)) {
+        if (empty($aSubSelects)) {
             return array();
         }
 
             return array();
         }
 
-        $aPlaces = chksql(
-            $this->oDB->getAll(join(' UNION ', $aSubSelects)),
-            'Could not lookup place'
-        );
+        $sSQL = join(' UNION ', $aSubSelects);
+        Debug::printSQL($sSQL);
+        $aPlaces = chksql($this->oDB->getAll($sSQL), 'Could not lookup place');
 
         $aClassType = getClassTypes();
         foreach ($aPlaces as &$aPlace) {
 
         $aClassType = getClassTypes();
         foreach ($aPlaces as &$aPlace) {
@@ -427,6 +444,13 @@ class PlaceLookup
                 );
             }
 
                 );
             }
 
+            if ($this->bAddressAdminLevels) {
+                $aPlace['aAddressAdminLevels'] = $this->getAddressAdminLevels(
+                    $aPlace['place_id'],
+                    $aPlace['housenumber']
+                );
+            }
+
             if ($this->bExtraTags) {
                 if ($aPlace['extra']) {
                     $aPlace['sExtraTags'] = json_decode($aPlace['extra']);
             if ($this->bExtraTags) {
                 if ($aPlace['extra']) {
                     $aPlace['sExtraTags'] = json_decode($aPlace['extra']);
@@ -457,12 +481,12 @@ class PlaceLookup
             $aPlace['addresstype'] = $sAddressType;
         }
 
             $aPlace['addresstype'] = $sAddressType;
         }
 
-        if (CONST_Debug) var_dump($aPlaces);
+        Debug::printVar('Places', $aPlaces);
 
         return $aPlaces;
     }
 
 
         return $aPlaces;
     }
 
-    private function getAddressDetails($iPlaceID, $bAll, $sHousenumber)
+    public function getAddressDetails($iPlaceID, $bAll = false, $sHousenumber = -1)
     {
         $sSQL = 'SELECT *,';
         $sSQL .= '  get_name_by_language(name,'.$this->aLangPrefOrderSql.') as localname';
     {
         $sSQL = 'SELECT *,';
         $sSQL .= '  get_name_by_language(name,'.$this->aLangPrefOrderSql.') as localname';
@@ -512,6 +536,33 @@ class PlaceLookup
         return $aAddress;
     }
 
         return $aAddress;
     }
 
+    /* "Downing Street, London"
+     * [
+     *   "level15" => "Covent Garden",
+     *   "level8" => "Westminster",
+     *   "level6" => "London",
+     *   "level5" => "Greater London",
+     *   "level4" => "England",
+     *   "level2" => "United Kingdom"
+     * ]
+     */
+
+    public function getAddressAdminLevels($iPlaceID, $sHousenumber = null)
+    {
+        $aAddressLines = $this->getAddressDetails(
+            $iPlaceID,
+            false,
+            $sHousenumber === null ? -1 : $sHousenumber
+        );
+
+        $aAddress = array();
+        foreach ($aAddressLines as $aLine) {
+            if (isset($aLine['admin_level'])) {
+                $aAddress['level'.$aLine['admin_level']] = $aLine['localname'];
+            }
+        }
+        return $aAddress;
+    }
 
 
     /* returns an array which will contain the keys
 
 
     /* returns an array which will contain the keys