]> git.openstreetmap.org Git - nominatim.git/blobdiff - lib/Geocode.php
reduce maximum line length to 199 for all php files
[nominatim.git] / lib / Geocode.php
index 6bc2c1e6d37273e36b1f2b88d80ed9a71834517f..0f79eae5c8c00901ba331bb5b6ebfe11223f33d8 100644 (file)
@@ -162,7 +162,7 @@ class Geocode
 
         $this->sViewboxCentreSQL = "ST_SetSRID('LINESTRING(";
         $sSep = '';
-        foreach ($this->aRoutePoints as $aPoint) {
+        foreach ($aRoutePoints as $aPoint) {
             $fPoint = (float)$aPoint;
             $this->sViewboxCentreSQL .= $sSep.$fPoint;
             $sSep = ($sSep == ' ') ? ',' : ' ';
@@ -180,6 +180,16 @@ class Geocode
     {
         $this->aViewBox = array_map('floatval', $aViewbox);
 
+        if ($this->aViewBox[0] < -180
+            || $this->aViewBox[2] > 180
+            || $this->aViewBox[0] >= $this->aViewBox[2]
+            || $this->aViewBox[1] < -90
+            || $this->aViewBox[3] > 90
+            || $this->aViewBox[1] >= $this->aViewBox[3]
+        ) {
+            userError("Bad parameter 'viewbox'. Out of range.");
+        }
+
         $fHeight = $this->aViewBox[0] - $this->aViewBox[2];
         $fWidth = $this->aViewBox[1] - $this->aViewBox[3];
         $aBigViewBox[0] = $this->aViewBox[0] + $fHeight;
@@ -252,16 +262,22 @@ class Geocode
                     $aCountries[] = strtolower($sCountryCode);
                 }
             }
-            if (isset($aCountryCodes))
+            if (isset($aCountries))
                 $this->aCountryCodes = $aCountries;
         }
 
         $aViewbox = $oParams->getStringList('viewboxlbrt');
         if ($aViewbox) {
+            if (count($aViewbox) != 4) {
+                userError("Bad parmater 'viewbox'. Expected 4 coordinates.");
+            }
             $this->setViewbox($aViewbox);
         } else {
             $aViewbox = $oParams->getStringList('viewbox');
             if ($aViewbox) {
+                if (count($aViewbox) != 4) {
+                    userError("Bad parmater 'viewbox'. Expected 4 coordinates.");
+                }
                 $this->setViewBox(array(
                                    $aViewbox[0],
                                    $aViewbox[3],
@@ -382,7 +398,10 @@ class Geocode
         if ($this->bIncludeNameDetails) $sSQL .= "hstore_to_json(name)::text as names,";
         $sSQL .= "avg(ST_X(centroid)) as lon,avg(ST_Y(centroid)) as lat, ";
         $sSQL .= $sImportanceSQL."coalesce(importance,0.75-(rank_search::float/40)) as importance, ";
-        $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(CASE WHEN placex.rank_search < 28 THEN placex.place_id ELSE placex.parent_place_id END) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, ";
+        $sSQL .= "(select max(p.importance*(p.rank_address+2))";
+        $sSQL .= "   from place_addressline s, placex p";
+        $sSQL .= "   where s.place_id = min(CASE WHEN placex.rank_search < 28 THEN placex.place_id ELSE placex.parent_place_id END)";
+        $sSQL .= "   and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, ";
         $sSQL .= "(extratags->'place') as extra_place ";
         $sSQL .= "from placex where place_id in ($sPlaceIDs) ";
         $sSQL .= "and (placex.rank_address between $this->iMinAddressRank and $this->iMaxAddressRank ";
@@ -414,15 +433,21 @@ class Geocode
             if (CONST_Use_US_Tiger_Data) {
                 // Tiger search only if a housenumber was searched and if it was found (i.e. aPlaceIDs[placeID] = housenumber != -1) (realized through a join)
                 $sSQL .= " union";
-                $sSQL .= " select 'T' as osm_type, place_id as osm_id, 'place' as class, 'house' as type, null as admin_level, 30 as rank_search, 30 as rank_address, min(place_id) as place_id, min(parent_place_id) as parent_place_id, 'us' as country_code";
-                $sSQL .= ", get_address_by_language(place_id, housenumber_for_place, $sLanguagePrefArraySQL) as langaddress ";
-                $sSQL .= ", null as placename";
-                $sSQL .= ", null as ref";
+                $sSQL .= " select 'T' as osm_type, place_id as osm_id, 'place' as class,";
+                $sSQL .= " 'house' as type, null as admin_level, 30 as rank_search,";
+                $sSQL .= " 30 as rank_address, min(place_id) as place_id,";
+                $sSQL .= " min(parent_place_id) as parent_place_id, 'us' as country_code,";
+                $sSQL .= " get_address_by_language(place_id, housenumber_for_place, $sLanguagePrefArraySQL) as langaddress,";
+                $sSQL .= " null as placename, null as ref";
                 if ($this->bIncludeExtraTags) $sSQL .= ", null as extra";
                 if ($this->bIncludeNameDetails) $sSQL .= ", null as names";
                 $sSQL .= ", avg(st_x(centroid)) as lon, avg(st_y(centroid)) as lat,";
                 $sSQL .= $sImportanceSQL."-1.15 as importance ";
-                $sSQL .= ", (select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(blub.parent_place_id) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance ";
+                $sSQL .= ", (select max(p.importance*(p.rank_address+2))";
+                $sSQL .= "   from place_addressline s, placex p";
+                $sSQL .= "   where s.place_id = min(blub.parent_place_id)";
+                $sSQL .= "   and p.place_id = s.address_place_id and s.isaddress";
+                $sSQL .= "   and p.importance is not null) as addressimportance ";
                 $sSQL .= ", null as extra_place ";
                 $sSQL .= " from (select place_id";
                 // interpolate the Tiger housenumbers here
@@ -436,7 +461,10 @@ class Geocode
             // osmline
             // interpolation line search only if a housenumber was searched and if it was found (i.e. aPlaceIDs[placeID] = housenumber != -1) (realized through a join)
             $sSQL .= " union ";
-            $sSQL .= "select 'W' as osm_type, place_id as osm_id, 'place' as class, 'house' as type, null as admin_level, 30 as rank_search, 30 as rank_address, min(place_id) as place_id, min(parent_place_id) as parent_place_id, calculated_country_code as country_code, ";
+            $sSQL .= "select 'W' as osm_type, place_id as osm_id, 'place' as class,";
+            $sSQL .= " 'house' as type, null as admin_level, 30 as rank_search,";
+            $sSQL .= " 30 as rank_address, min(place_id) as place_id,";
+            $sSQL .= " min(parent_place_id) as parent_place_id, calculated_country_code as country_code, ";
             $sSQL .= "get_address_by_language(place_id, housenumber_for_place, $sLanguagePrefArraySQL) as langaddress, ";
             $sSQL .= "null as placename, ";
             $sSQL .= "null as ref, ";
@@ -460,7 +488,10 @@ class Geocode
 
             if (CONST_Use_Aux_Location_data) {
                 $sSQL .= " union ";
-                $sSQL .= "select 'L' as osm_type, place_id as osm_id, 'place' as class, 'house' as type, null as admin_level, 0 as rank_search, 0 as rank_address, min(place_id) as place_id, min(parent_place_id) as parent_place_id, 'us' as country_code, ";
+                $sSQL .= "select 'L' as osm_type, place_id as osm_id, 'place' as class,";
+                $sSQL .= " 'house' as type, null as admin_level, 0 as rank_search,";
+                $sSQL .= " 0 as rank_address, min(place_id) as place_id,";
+                $sSQL .= " min(parent_place_id) as parent_place_id, 'us' as country_code, ";
                 $sSQL .= "get_address_by_language(place_id, -1, $sLanguagePrefArraySQL) as langaddress, ";
                 $sSQL .= "null as placename, ";
                 $sSQL .= "null as ref, ";
@@ -468,7 +499,11 @@ class Geocode
                 if ($this->bIncludeNameDetails) $sSQL .= "null as names, ";
                 $sSQL .= "avg(ST_X(centroid)) as lon, avg(ST_Y(centroid)) as lat, ";
                 $sSQL .= $sImportanceSQL."-1.10 as importance, ";
-                $sSQL .= "(select max(p.importance*(p.rank_address+2)) from place_addressline s, placex p where s.place_id = min(location_property_aux.parent_place_id) and p.place_id = s.address_place_id and s.isaddress and p.importance is not null) as addressimportance, ";
+                $sSQL .= "(select max(p.importance*(p.rank_address+2))";
+                $sSQL .= " from place_addressline s, placex p";
+                $sSQL .= " where s.place_id = min(location_property_aux.parent_place_id)";
+                $sSQL .= " and p.place_id = s.address_place_id and s.isaddress";
+                $sSQL .= " and p.importance is not null) as addressimportance, ";
                 $sSQL .= "null as extra_place ";
                 $sSQL .= "from location_property_aux where place_id in ($sPlaceIDs) ";
                 $sSQL .= "and 30 between $this->iMinAddressRank and $this->iMaxAddressRank ";
@@ -880,7 +915,7 @@ class Geocode
             foreach ($aPhrases as $iPhrase => $sPhrase) {
                 $aPhrase = chksql(
                     $this->oDB->getRow("select make_standard_name('".pg_escape_string($sPhrase)."') as string"),
-                    "Cannot nomralize query string (is it an UTF-8 string?)"
+                    "Cannot normalize query string (is it a UTF-8 string?)"
                 );
                 if (trim($aPhrase['string'])) {
                     $aPhrases[$iPhrase] = $aPhrase;
@@ -1258,18 +1293,23 @@ class Geocode
                             $sSQL .= " limit $this->iLimit";
                             if (CONST_Debug) var_dump($sSQL);
                             $aPlaceIDs = chksql($this->oDB->getCol($sSQL));
-                            
+
                             // if nothing found, search in the interpolation line table
                             if (!sizeof($aPlaceIDs)) {
                                 // do we need to use transliteration and the regex for housenumbers???
                                 //new query for lines, not housenumbers anymore
+                                $sSQL = "select distinct place_id from location_property_osmline";
+                                $sSQL .= " where parent_place_id in (".$sPlaceIDs.") and (";
                                 if ($searchedHousenumber%2 == 0) {
                                     //if housenumber is even, look for housenumber in streets with interpolationtype even or all
-                                    $sSQL = "select distinct place_id from location_property_osmline where parent_place_id in (".$sPlaceIDs.") and (interpolationtype='even' or interpolationtype='all') and ".$searchedHousenumber.">=startnumber and ".$searchedHousenumber."<=endnumber";
+                                    $sSQL .= "interpolationtype='even'";
                                 } else {
                                     //look for housenumber in streets with interpolationtype odd or all
-                                    $sSQL = "select distinct place_id from location_property_osmline where parent_place_id in (".$sPlaceIDs.") and (interpolationtype='odd' or interpolationtype='all') and ".$searchedHousenumber.">=startnumber and ".$searchedHousenumber."<=endnumber";
+                                    $sSQL .= "interpolationtype='odd'";
                                 }
+                                $sSQL .= " or interpolationtype='all') and ";
+                                $sSQL .= $searchedHousenumber.">=startnumber and ";
+                                $sSQL .= $searchedHousenumber."<=endnumber";
 
                                 if (sizeof($this->aExcludePlaceIDs)) {
                                     $sSQL .= " and place_id not in (".join(',', $this->aExcludePlaceIDs).")";
@@ -1279,7 +1319,7 @@ class Geocode
                                 //get place IDs
                                 $aPlaceIDs = chksql($this->oDB->getCol($sSQL, 0));
                             }
-                                
+
                             // If nothing found try the aux fallback table
                             if (CONST_Use_Aux_Location_data && !sizeof($aPlaceIDs)) {
                                 $sSQL = "select place_id from location_property_aux where parent_place_id in (".$sPlaceIDs.") and housenumber = '".pg_escape_string($aSearch['sHouseNumber'])."'";
@@ -1293,14 +1333,16 @@ class Geocode
 
                             //if nothing was found in placex or location_property_aux, then search in Tiger data for this housenumber(location_property_tiger)
                             if (CONST_Use_US_Tiger_Data && !sizeof($aPlaceIDs)) {
-                                //new query for lines, not housenumbers anymore
+                                $sSQL = "select distinct place_id from location_property_tiger";
+                                $sSQL .= " where parent_place_id in (".$sPlaceIDs.") and (";
                                 if ($searchedHousenumber%2 == 0) {
-                                    //if housenumber is even, look for housenumber in streets with interpolationtype even or all
-                                    $sSQL = "select distinct place_id from location_property_tiger where parent_place_id in (".$sPlaceIDs.") and (interpolationtype='even' or interpolationtype='all') and ".$searchedHousenumber.">=startnumber and ".$searchedHousenumber."<=endnumber";
+                                    $sSQL .= "interpolationtype='even'";
                                 } else {
-                                    //look for housenumber in streets with interpolationtype odd or all
-                                    $sSQL = "select distinct place_id from location_property_tiger where parent_place_id in (".$sPlaceIDs.") and (interpolationtype='odd' or interpolationtype='all') and ".$searchedHousenumber.">=startnumber and ".$searchedHousenumber."<=endnumber";
+                                    $sSQL .= "interpolationtype='odd'";
                                 }
+                                $sSQL .= " or interpolationtype='all') and ";
+                                $sSQL .= $searchedHousenumber.">=startnumber and ";
+                                $sSQL .= $searchedHousenumber."<=endnumber";
 
                                 if (sizeof($this->aExcludePlaceIDs)) {
                                     $sSQL .= " and place_id not in (".join(',', $this->aExcludePlaceIDs).")";
@@ -1348,7 +1390,11 @@ class Geocode
                                 $sPlaceGeom = false;
                                 if ($this->iMaxRank < 9 && $bCacheTable) {
                                     // Try and get a polygon to search in instead
-                                    $sSQL = "select geometry from placex where place_id in ($sPlaceIDs) and rank_search < $this->iMaxRank + 5 and st_geometrytype(geometry) in ('ST_Polygon','ST_MultiPolygon') order by rank_search asc limit 1";
+                                    $sSQL = "select geometry from placex";
+                                    $sSQL .= " where place_id in ($sPlaceIDs)";
+                                    $sSQL .= " and rank_search < $this->iMaxRank + 5";
+                                    $sSQL .= " and st_geometrytype(geometry) in ('ST_Polygon','ST_MultiPolygon')";
+                                    $sSQL .= " order by rank_search asc limit 1";
                                     if (CONST_Debug) var_dump($sSQL);
                                     $sPlaceGeom = chksql($this->oDB->getOne($sSQL));
                                 }
@@ -1467,7 +1513,7 @@ class Geocode
             }
         } else {
             // Just interpret as a reverse geocode
-            $oReverse = new Nominatim\ReverseGeocode($this->oDB);
+            $oReverse = new ReverseGeocode($this->oDB);
             $oReverse->setZoom(18);
 
             $aLookup = $oReverse->lookup(
@@ -1480,6 +1526,7 @@ class Geocode
 
             if ($aLookup['place_id']) {
                 $aSearchResults = $this->getDetails(array($aLookup['place_id'] => -1));
+                $aResultPlaceIDs[$aLookup['place_id']] = -1;
             } else {
                 $aSearchResults = array();
             }
@@ -1551,7 +1598,7 @@ class Geocode
                 // getAddressDetails() is defined in lib.php and uses the SQL function get_addressdata in functions.sql
                 $aResult['address'] = getAddressDetails($this->oDB, $sLanguagePrefArraySQL, $aResult['place_id'], $aResult['country_code'], $aResultPlaceIDs[$aResult['place_id']]);
                 if ($aResult['extra_place'] == 'city' && !isset($aResult['address']['city'])) {
-                    $aResult['address'] = array_merge(array('city' => array_shift(array_values($aResult['address']))), $aResult['address']);
+                    $aResult['address'] = array_merge(array('city' => array_values($aResult['address'])[0]), $aResult['address']);
                 }
             }