]> git.openstreetmap.org Git - nominatim.git/blobdiff - lib/PlaceLookup.php
consolidate warm and db-check into single admin command
[nominatim.git] / lib / PlaceLookup.php
index 46b17882a5ec90064454c86348c1cf5300c989f8..6d7b6be1af4ed6dd04d5e1a6e79fb9050746252e 100644 (file)
@@ -2,8 +2,8 @@
 
 namespace Nominatim;
 
-require_once(CONST_BasePath.'/lib/AddressDetails.php');
-require_once(CONST_BasePath.'/lib/Result.php');
+require_once(CONST_LibDir.'/AddressDetails.php');
+require_once(CONST_LibDir.'/Result.php');
 
 class PlaceLookup
 {
@@ -15,7 +15,6 @@ class PlaceLookup
     protected $bExtraTags = false;
     protected $bNameDetails = false;
 
-    protected $bIncludePolygonAsPoints = false;
     protected $bIncludePolygonAsText = false;
     protected $bIncludePolygonAsGeoJSON = false;
     protected $bIncludePolygonAsKML = false;
@@ -38,11 +37,6 @@ class PlaceLookup
         return $this->bDeDupe;
     }
 
-    public function setIncludePolygonAsPoints($b = true)
-    {
-        $this->bIncludePolygonAsPoints = $b;
-    }
-
     public function setIncludeAddressDetails($b)
     {
         $this->bAddressDetails = $b;
@@ -52,7 +46,7 @@ class PlaceLookup
     {
         $aLangs = $oParams->getPreferredLanguages();
         $this->aLangPrefOrderSql =
-            'ARRAY['.join(',', array_map('getDBQuoted', $aLangs)).']';
+            'ARRAY['.join(',', $this->oDB->getDBQuotedList($aLangs)).']';
 
         $this->bExtraTags = $oParams->getBool('extratags', false);
         $this->bNameDetails = $oParams->getBool('namedetails', false);
@@ -61,7 +55,6 @@ class PlaceLookup
 
         if ($sGeomType === null || $sGeomType == 'geojson') {
             $this->bIncludePolygonAsGeoJSON = $oParams->getBool('polygon_geojson');
-            $this->bIncludePolygonAsPoints = false;
         }
 
         if ($oParams->getString('format', '') !== 'geojson') {
@@ -100,7 +93,6 @@ class PlaceLookup
         if ($this->bExtraTags) $aParams['extratags'] = '1';
         if ($this->bNameDetails) $aParams['namedetails'] = '1';
 
-        if ($this->bIncludePolygonAsPoints) $aParams['polygon'] = '1';
         if ($this->bIncludePolygonAsText) $aParams['polygon_text'] = '1';
         if ($this->bIncludePolygonAsGeoJSON) $aParams['polygon_geojson'] = '1';
         if ($this->bIncludePolygonAsKML) $aParams['polygon_kml'] = '1';
@@ -132,8 +124,9 @@ class PlaceLookup
 
     public function setLanguagePreference($aLangPrefOrder)
     {
-        $this->aLangPrefOrderSql =
-            'ARRAY['.join(',', array_map('getDBQuoted', $aLangPrefOrder)).']';
+        $this->aLangPrefOrderSql = $this->oDB->getArraySQL(
+            $this->oDB->getDBQuotedList($aLangPrefOrder)
+        );
     }
 
     private function addressImportanceSql($sGeometry, $sPlaceId)
@@ -162,8 +155,8 @@ class PlaceLookup
 
     public function lookupOSMID($sType, $iID)
     {
-        $sSQL = "select place_id from placex where osm_type = '".$sType."' and osm_id = ".$iID;
-        $iPlaceID = chksql($this->oDB->getOne($sSQL));
+        $sSQL = 'select place_id from placex where osm_type = :type and osm_id = :id';
+        $iPlaceID = $this->oDB->getOne($sSQL, array(':type' => $sType, ':id' => $iID));
 
         if (!$iPlaceID) {
             return null;
@@ -214,7 +207,7 @@ class PlaceLookup
                 'ST_Collect(centroid)',
                 'min(CASE WHEN placex.rank_search < 28 THEN placex.place_id ELSE placex.parent_place_id END)'
             );
-            $sSQL .= "    (extratags->'place') AS extra_place ";
+            $sSQL .= "    COALESCE(extratags->'place', extratags->'linked_place') AS extra_place ";
             $sSQL .= ' FROM placex';
             $sSQL .= " WHERE place_id in ($sPlaceIDs) ";
             $sSQL .= '   AND (';
@@ -247,7 +240,7 @@ class PlaceLookup
             $sSQL .= '     ref, ';
             if ($this->bExtraTags) $sSQL .= 'extratags, ';
             if ($this->bNameDetails) $sSQL .= 'name, ';
-            $sSQL .= "     extratags->'place' ";
+            $sSQL .= '     extra_place ';
 
             $aSubSelects[] = $sSQL;
         }
@@ -424,9 +417,10 @@ class PlaceLookup
 
         $sSQL = join(' UNION ', $aSubSelects);
         Debug::printSQL($sSQL);
-        $aPlaces = chksql($this->oDB->getAll($sSQL), 'Could not lookup place');
+        $aPlaces = $this->oDB->getAll($sSQL, null, 'Could not lookup place');
 
         foreach ($aPlaces as &$aPlace) {
+            $aPlace['importance'] = (float) $aPlace['importance'];
             if ($this->bAddressDetails) {
                 // to get addressdetails for tiger data, the housenumber is needed
                 $aPlace['address'] = new AddressDetails(
@@ -454,16 +448,24 @@ class PlaceLookup
                 }
             }
 
-            $aPlace['addresstype'] = ClassTypes\getProperty(
+            $aPlace['addresstype'] = ClassTypes\getLabelTag(
                 $aPlace,
-                'simplelabel',
-                $aPlace['class']
+                $aPlace['country_code']
             );
+
+            $aResults[$aPlace['place_id']] = $aPlace;
         }
 
-        Debug::printVar('Places', $aPlaces);
+        $aResults = array_filter(
+            $aResults,
+            function ($v) {
+                return !($v instanceof Result);
+            }
+        );
+
+        Debug::printVar('Places', $aResults);
 
-        return $aPlaces;
+        return $aResults;
     }
 
     /* returns an array which will contain the keys
@@ -484,82 +486,73 @@ class PlaceLookup
         $aOutlineResult = array();
         if (!$iPlaceID) return $aOutlineResult;
 
-        if (CONST_Search_AreaPolygons) {
-            // Get the bounding box and outline polygon
-            $sSQL = 'select place_id,0 as numfeatures,st_area(geometry) as area,';
-            if ($fLonReverse != null && $fLatReverse != null) {
-                $sSQL .= ' ST_Y(closest_point) as centrelat,';
-                $sSQL .= ' ST_X(closest_point) as centrelon,';
-            } else {
-                $sSQL .= ' ST_Y(centroid) as centrelat, ST_X(centroid) as centrelon,';
-            }
-            $sSQL .= ' ST_YMin(geometry) as minlat,ST_YMax(geometry) as maxlat,';
-            $sSQL .= ' ST_XMin(geometry) as minlon,ST_XMax(geometry) as maxlon';
-            if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ',ST_AsGeoJSON(geometry) as asgeojson';
-            if ($this->bIncludePolygonAsKML) $sSQL .= ',ST_AsKML(geometry) as askml';
-            if ($this->bIncludePolygonAsSVG) $sSQL .= ',ST_AsSVG(geometry) as assvg';
-            if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ',ST_AsText(geometry) as astext';
-            if ($fLonReverse != null && $fLatReverse != null) {
-                $sFrom = ' from (SELECT * , CASE WHEN (class = \'highway\') AND (ST_GeometryType(geometry) = \'ST_LineString\') THEN ';
-                $sFrom .=' ST_ClosestPoint(geometry, ST_SetSRID(ST_Point('.$fLatReverse.','.$fLonReverse.'),4326))';
-                $sFrom .=' ELSE centroid END AS closest_point';
-                $sFrom .= ' from placex where place_id = '.$iPlaceID.') as plx';
-            } else {
-                $sFrom = ' from placex where place_id = '.$iPlaceID;
-            }
-            if ($this->fPolygonSimplificationThreshold > 0) {
-                $sSQL .= ' from (select place_id,centroid,ST_SimplifyPreserveTopology(geometry,'.$this->fPolygonSimplificationThreshold.') as geometry'.$sFrom.') as plx';
-            } else {
-                $sSQL .= $sFrom;
-            }
-
-            $aPointPolygon = chksql($this->oDB->getRow($sSQL), 'Could not get outline');
-
-            if ($aPointPolygon['place_id']) {
-                if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null) {
-                    $aOutlineResult['lat'] = $aPointPolygon['centrelat'];
-                    $aOutlineResult['lon'] = $aPointPolygon['centrelon'];
-                }
+        // Get the bounding box and outline polygon
+        $sSQL = 'select place_id,0 as numfeatures,st_area(geometry) as area,';
+        if ($fLonReverse != null && $fLatReverse != null) {
+            $sSQL .= ' ST_Y(closest_point) as centrelat,';
+            $sSQL .= ' ST_X(closest_point) as centrelon,';
+        } else {
+            $sSQL .= ' ST_Y(centroid) as centrelat, ST_X(centroid) as centrelon,';
+        }
+        $sSQL .= ' ST_YMin(geometry) as minlat,ST_YMax(geometry) as maxlat,';
+        $sSQL .= ' ST_XMin(geometry) as minlon,ST_XMax(geometry) as maxlon';
+        if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ',ST_AsGeoJSON(geometry) as asgeojson';
+        if ($this->bIncludePolygonAsKML) $sSQL .= ',ST_AsKML(geometry) as askml';
+        if ($this->bIncludePolygonAsSVG) $sSQL .= ',ST_AsSVG(geometry) as assvg';
+        if ($this->bIncludePolygonAsText) $sSQL .= ',ST_AsText(geometry) as astext';
+        if ($fLonReverse != null && $fLatReverse != null) {
+            $sFrom = ' from (SELECT * , CASE WHEN (class = \'highway\') AND (ST_GeometryType(geometry) = \'ST_LineString\') THEN ';
+            $sFrom .=' ST_ClosestPoint(geometry, ST_SetSRID(ST_Point('.$fLatReverse.','.$fLonReverse.'),4326))';
+            $sFrom .=' ELSE centroid END AS closest_point';
+            $sFrom .= ' from placex where place_id = '.$iPlaceID.') as plx';
+        } else {
+            $sFrom = ' from placex where place_id = '.$iPlaceID;
+        }
+        if ($this->fPolygonSimplificationThreshold > 0) {
+            $sSQL .= ' from (select place_id,centroid,ST_SimplifyPreserveTopology(geometry,'.$this->fPolygonSimplificationThreshold.') as geometry'.$sFrom.') as plx';
+        } else {
+            $sSQL .= $sFrom;
+        }
 
-                if ($this->bIncludePolygonAsGeoJSON) $aOutlineResult['asgeojson'] = $aPointPolygon['asgeojson'];
-                if ($this->bIncludePolygonAsKML) $aOutlineResult['askml'] = $aPointPolygon['askml'];
-                if ($this->bIncludePolygonAsSVG) $aOutlineResult['assvg'] = $aPointPolygon['assvg'];
-                if ($this->bIncludePolygonAsText) $aOutlineResult['astext'] = $aPointPolygon['astext'];
-                if ($this->bIncludePolygonAsPoints) $aOutlineResult['aPolyPoints'] = geometryText2Points($aPointPolygon['astext'], $fRadius);
+        $aPointPolygon = $this->oDB->getRow($sSQL, null, 'Could not get outline');
 
+        if ($aPointPolygon && $aPointPolygon['place_id']) {
+            if ($aPointPolygon['centrelon'] !== null && $aPointPolygon['centrelat'] !== null) {
+                $aOutlineResult['lat'] = $aPointPolygon['centrelat'];
+                $aOutlineResult['lon'] = $aPointPolygon['centrelon'];
+            }
 
-                if (abs($aPointPolygon['minlat'] - $aPointPolygon['maxlat']) < 0.0000001) {
-                    $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
-                    $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
-                }
+            if ($this->bIncludePolygonAsGeoJSON) $aOutlineResult['asgeojson'] = $aPointPolygon['asgeojson'];
+            if ($this->bIncludePolygonAsKML) $aOutlineResult['askml'] = $aPointPolygon['askml'];
+            if ($this->bIncludePolygonAsSVG) $aOutlineResult['assvg'] = $aPointPolygon['assvg'];
+            if ($this->bIncludePolygonAsText) $aOutlineResult['astext'] = $aPointPolygon['astext'];
 
-                if (abs($aPointPolygon['minlon'] - $aPointPolygon['maxlon']) < 0.0000001) {
-                    $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
-                    $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
-                }
+            if (abs($aPointPolygon['minlat'] - $aPointPolygon['maxlat']) < 0.0000001) {
+                $aPointPolygon['minlat'] = $aPointPolygon['minlat'] - $fRadius;
+                $aPointPolygon['maxlat'] = $aPointPolygon['maxlat'] + $fRadius;
+            }
 
-                $aOutlineResult['aBoundingBox'] = array(
-                                                   (string)$aPointPolygon['minlat'],
-                                                   (string)$aPointPolygon['maxlat'],
-                                                   (string)$aPointPolygon['minlon'],
-                                                   (string)$aPointPolygon['maxlon']
-                                                  );
+            if (abs($aPointPolygon['minlon'] - $aPointPolygon['maxlon']) < 0.0000001) {
+                $aPointPolygon['minlon'] = $aPointPolygon['minlon'] - $fRadius;
+                $aPointPolygon['maxlon'] = $aPointPolygon['maxlon'] + $fRadius;
             }
+
+            $aOutlineResult['aBoundingBox'] = array(
+                                               (string)$aPointPolygon['minlat'],
+                                               (string)$aPointPolygon['maxlat'],
+                                               (string)$aPointPolygon['minlon'],
+                                               (string)$aPointPolygon['maxlon']
+                                              );
         }
 
         // as a fallback we generate a bounding box without knowing the size of the geometry
         if ((!isset($aOutlineResult['aBoundingBox'])) && isset($fLon)) {
-            //
-            if ($this->bIncludePolygonAsPoints) {
-                $sGeometryText = 'POINT('.$fLon.','.$fLat.')';
-                $aOutlineResult['aPolyPoints'] = geometryText2Points($sGeometryText, $fRadius);
-            }
-
-            $aBounds = array();
-            $aBounds['minlat'] = $fLat - $fRadius;
-            $aBounds['maxlat'] = $fLat + $fRadius;
-            $aBounds['minlon'] = $fLon - $fRadius;
-            $aBounds['maxlon'] = $fLon + $fRadius;
+            $aBounds = array(
+                        'minlat' => $fLat - $fRadius,
+                        'maxlat' => $fLat + $fRadius,
+                        'minlon' => $fLon - $fRadius,
+                        'maxlon' => $fLon + $fRadius
+                       );
 
             $aOutlineResult['aBoundingBox'] = array(
                                                (string)$aBounds['minlat'],