]> git.openstreetmap.org Git - nominatim.git/blobdiff - lib/lib.php
add logging of broken polygons, improve address export, better diff output in export...
[nominatim.git] / lib / lib.php
index 8a1fb902cbbdba574010f5f6a6d8bb069ec4ca2d..115a9495aa4cbfb03548b1e9db2c135021e3cfec 100644 (file)
                 $aLoadAverage = explode(' ',$sLoadAverage);
                return (int)$aLoadAverage[0];
        }
-       
+
+       function getProcessorCount()
+       {
+               $sCPU = file_get_contents('/proc/cpuinfo');
+               preg_match_all('#processor      : [0-9]+#', $sCPU, $aMatches);
+               return sizeof($aMatches[0]);
+       }
+
        function bySearchRank($a, $b)
        {
                if ($a['iSearchRank'] == $b['iSearchRank']) return 0;
@@ -37,6 +44,8 @@
                        return ($a['aPointPolygon']['numfeatures'] > $b['aPointPolygon']['numfeatures']?-1:1);
                if ($a['aPointPolygon']['area'] != $b['aPointPolygon']['area'])
                        return ($a['aPointPolygon']['area'] > $b['aPointPolygon']['area']?-1:1);
+               if ($a['levenshtein'] != $b['levenshtein'])
+                       return ($a['levenshtein'] < $b['levenshtein']?-1:1);
                if ($a['importance'] != $b['importance'])
                        return ($a['importance'] < $b['importance']?-1:1);
                return ($a['foundorder'] < $b['foundorder']?-1:1);
@@ -49,7 +58,7 @@
                {
                        $_SERVER["HTTP_ACCEPT_LANGUAGE"] = $_GET['accept-language'];
                }
-               
+
                $aLanguages = array();
                if (preg_match_all('/(([a-z]{1,8})(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $aLanguagesParse, PREG_SET_ORDER))
                {
        function gbPostcodeCalculate($sPostcode, $sPostcodeSector, $sPostcodeEnd, &$oDB)
        {
                // Try an exact match on the gb_postcode table
-               $sSQL = 'select \'AA\', ST_X(ST_Centroid(geometry)) as lon,ST_Y(ST_Centroid(geometry)) as lat from gb_postcode where upper(postcode) = \''.$sPostcode.'\'';
+               $sSQL = 'select \'AA\', ST_X(ST_Centroid(geometry)) as lon,ST_Y(ST_Centroid(geometry)) as lat from gb_postcode where postcode = \''.$sPostcode.'\'';
                $aNearPostcodes = $oDB->getAll($sSQL);
                if (PEAR::IsError($aNearPostcodes))
                {
                        var_dump($sSQL, $aNearPostcodes);
                        exit;
                }
+               
 
-               if (!sizeof($aNearPostcodes))
+               if (sizeof($aNearPostcodes))
                {
-                       $sSQL = 'select substring(upper(postcode) from \'^[A-Z][A-Z]?[0-9][0-9A-Z]? [0-9]([A-Z][A-Z])$\'),ST_X(ST_Centroid(geometry)) as lon,ST_Y(ST_Centroid(geometry)) as lat from placex where country_code::text = \'gb\'::text AND substring(upper(postcode) from \'^([A-Z][A-Z]?[0-9][0-9A-Z]? [0-9])[A-Z][A-Z]$\') = \''.$sPostcodeSector.'\' and class=\'place\' and type=\'postcode\' ';
-                       $sSQL .= ' union ';
-                       $sSQL .= 'select substring(upper(postcode) from \'^[A-Z][A-Z]?[0-9][0-9A-Z]? [0-9]([A-Z][A-Z])$\'),ST_X(ST_Centroid(geometry)) as lon,ST_Y(ST_Centroid(geometry)) as lat from gb_postcode where substring(upper(postcode) from \'^([A-Z][A-Z]?[0-9][0-9A-Z]? [0-9])[A-Z][A-Z]$\') = \''.$sPostcodeSector.'\'';
-                       $aNearPostcodes = $oDB->getAll($sSQL);
-                       if (PEAR::IsError($aNearPostcodes))
-                       {
-                               var_dump($sSQL, $aNearPostcodes);
-                               exit;
-                       }
+                       return array(array('lat' => $aNearPostcodes[0]['lat'], 'lon' => $aNearPostcodes[0]['lon'], 'radius' => 0.005));
+               }
+
+               $sSQL = 'select substring(upper(postcode) from \'^[A-Z][A-Z]?[0-9][0-9A-Z]? [0-9]([A-Z][A-Z])$\'),ST_X(ST_Centroid(geometry)) as lon,ST_Y(ST_Centroid(geometry)) as lat from placex where country_code::text = \'gb\'::text AND substring(postcode from \'^([A-Z][A-Z]?[0-9][0-9A-Z]? [0-9])[A-Z][A-Z]$\') = \''.$sPostcodeSector.'\' and class=\'place\' and type=\'postcode\' ';
+               $sSQL .= ' union ';
+               $sSQL .= 'select substring(upper(postcode) from \'^[A-Z][A-Z]?[0-9][0-9A-Z]? [0-9]([A-Z][A-Z])$\'),ST_X(ST_Centroid(geometry)) as lon,ST_Y(ST_Centroid(geometry)) as lat from gb_postcode where substring(postcode from \'^([A-Z][A-Z]?[0-9][0-9A-Z]? [0-9])[A-Z][A-Z]$\') = \''.$sPostcodeSector.'\'';
+               $aNearPostcodes = $oDB->getAll($sSQL);
+               if (PEAR::IsError($aNearPostcodes))
+               {
+                       var_dump($sSQL, $aNearPostcodes);
+                       exit;
                }
 
                if (!sizeof($aNearPostcodes))
        function getClassTypes()
        {
                return array(
- 'boundary:adminitrative:2' => array('label'=>'Country','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
- 'boundary:adminitrative:4' => array('label'=>'State','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
- 'boundary:adminitrative:5' => array('label'=>'State District','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
- 'boundary:adminitrative:6' => array('label'=>'County','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
- 'boundary:adminitrative:8' => array('label'=>'City','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
- 'boundary:adminitrative:9' => array('label'=>'City District','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
- 'boundary:adminitrative:10' => array('label'=>'Suburb','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
- 'boundary:adminitrative:11' => array('label'=>'Neighbourhood','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
+ 'boundary:administrative:2' => array('label'=>'Country','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
+ 'boundary:administrative:4' => array('label'=>'State','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
+ 'boundary:administrative:5' => array('label'=>'State District','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
+ 'boundary:administrative:6' => array('label'=>'County','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
+ 'boundary:administrative:8' => array('label'=>'City','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
+ 'boundary:administrative:9' => array('label'=>'City District','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
+ 'boundary:administrative:10' => array('label'=>'Suburb','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
+ 'boundary:administrative:11' => array('label'=>'Neighbourhood','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
  'place:city' => array('label'=>'City','frequency'=>66,'icon'=>'poi_place_city','defzoom'=>12, 'defdiameter' => 0.32,),
  'place:country' => array('label'=>'Country','frequency'=>0,'icon'=>'poi_boundary_administrative','defzoom'=>6, 'defdiameter' => 15,),
  'place:state' => array('label'=>'State','frequency'=>0,'icon'=>'poi_boundary_administrative','defzoom'=>8, 'defdiameter' => 5.12,),
  'place:region' => array('label'=>'State','frequency'=>0,'icon'=>'poi_boundary_administrative','defzoom'=>8, 'defdiameter' => 5.12,),
  'place:island' => array('label'=>'Island','frequency'=>288,'icon'=>'','defzoom'=>11, 'defdiameter' => 0.64,),
  'place:county' => array('label'=>'County','frequency'=>108,'icon'=>'poi_boundary_administrative','defzoom'=>10, 'defdiameter' => 1.28,),
- 'boundary:adminitrative' => array('label'=>'Administrative','frequency'=>413,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
+ 'boundary:administrative' => array('label'=>'Administrative','frequency'=>413,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
  'place:town' => array('label'=>'Town','frequency'=>1497,'icon'=>'poi_place_town','defzoom'=>14, 'defdiameter' => 0.08,),
  'place:village' => array('label'=>'Village','frequency'=>11230,'icon'=>'poi_place_village','defzoom'=>15, 'defdiameter' => 0.04,),
  'place:hamlet' => array('label'=>'Hamlet','frequency'=>7075,'icon'=>'poi_place_village','defzoom'=>15, 'defdiameter' => 0.04,),
  'amenity:shop' => array('label'=>'Shop','frequency'=>61,'icon'=>'',),
 
  'place:house' => array('label'=>'House','frequency'=>2086,'icon'=>'','defzoom'=>18,),
+ 'place:house_name' => array('label'=>'House','frequency'=>2086,'icon'=>'','defzoom'=>18,),
+ 'place:house_number' => array('label'=>'House Number','frequency'=>2086,'icon'=>'','defzoom'=>18,),
+ 'place:country_code' => array('label'=>'Country Code','frequency'=>2086,'icon'=>'','defzoom'=>18,),
 
 //
 
                 else
                 {
                         if (is_bool($xVal)) return $xVal?'true':'false';
-                       if (is_numeric($xVal)) return $xVal;
+//                     if (is_numeric($xVal)) return $xVal;
                         return '"'.str_replace('>','\\>',str_replace(array("\n","\r"),'\\n',str_replace(array("\n\r","\r\n"),'\\n',str_replace('"','\\"',$xVal)))).'"';
                 }
         }
        function _debugDumpGroupedSearches($aData, $aTokens)
        {
                $aWordsIDs = array();
-               foreach($aTokens as $sToken => $aWords)
+               if ($aTokens)
                {
-                       foreach($aWords as $aToken)
+                       foreach($aTokens as $sToken => $aWords)
                        {
-                               $aWordsIDs[$aToken['word_id']] = $sToken.'('.$aToken['word_id'].')';
+                               if ($aWords)
+                               {
+                                       foreach($aWords as $aToken)
+                                       {
+                                               $aWordsIDs[$aToken['word_id']] = $sToken.'('.$aToken['word_id'].')';
+                                       }
+                               }
                        }
                }
                echo "<table border=\"1\">";
                                $sSep = '';
                                foreach($aRow['aAddress'] as $iWordID)
                                {
-//                                     if (!isset($aRow['aName'][$iWordID]))
-                                       {
-                                               echo $sSep.'#'.$aWordsIDs[$iWordID].'#';
-                                               $sSep = ', ';
-                                       }
+                                       echo $sSep.'#'.$aWordsIDs[$iWordID].'#';
+                                       $sSep = ', ';
                                }
                                echo "</td>";
 
 
        function getAddressDetails(&$oDB, $sLanguagePrefArraySQL, $iPlaceID, $sCountryCode = false, $bRaw = false)
        {
-               $aHouseNumber = $oDB->getRow('select housenumber, get_name_by_language(name,ARRAY[\'addr:housename\']) as housename,rank_search from placex where place_id = '.$iPlaceID);
+               $sSQL = "select *,get_name_by_language(name,$sLanguagePrefArraySQL) as localname from get_addressdata($iPlaceID)";
+               IF (!$bRaw) $sSQL .= " WHERE isaddress OR type = 'country_code'";
+               $sSQL .= " order by rank_address desc,isaddress desc";
+               $aAddressLines = $oDB->getAll($sSQL);
+               if (PEAR::IsError($aAddressLines))
+               {
+                       var_dump($aAddressLines);
+                       exit;
+               }
+               if ($bRaw) return $aAddressLines;
+//echo "<pre>";
+//var_dump($aAddressLines);
+               $aAddress = array();
+               $aClassType = getClassTypes();
+               foreach($aAddressLines as $aLine)
+               {
+                       $aTypeLabel = false;
+                       if (isset($aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']];
+                       elseif (isset($aClassType[$aLine['class'].':'.$aLine['type']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type']];
+                       else $aTypeLabel = array('simplelabel'=>$aLine['class']);
+                       if ($aTypeLabel && ($aLine['localname'] || $aLine['housenumber']))
+                       {
+                               $sTypeLabel = strtolower(isset($aTypeLabel['simplelabel'])?$aTypeLabel['simplelabel']:$aTypeLabel['label']);
+                               $sTypeLabel = str_replace(' ','_',$sTypeLabel);
+                               if (!isset($aAddress[$sTypeLabel]) && $aLine['localname']) $aAddress[$sTypeLabel] = $aLine['localname']?$aLine['localname']:$aLine['housenumber'];
+                       }
+               }
+//var_dump($aAddress);
+//exit;
+               return $aAddress;
+
+               $aHouseNumber = $oDB->getRow('select housenumber, get_name_by_language(name,ARRAY[\'addr:housename\']) as housename,rank_search,postcode from placex where place_id = '.$iPlaceID);
                $sHouseNumber = $aHouseNumber['housenumber'];
                $sHouseName = $aHouseNumber['housename'];
+               $sPostcode = $aHouseNumber['postcode'];
                $iRank = $aHouseNumber['rank_search'];
 
                // Address
                $sSQL .= " length(name::text) as namelength ";
                $sSQL .= " from place_addressline join placex on (address_place_id = placex.place_id)";
                $sSQL .= " where place_addressline.place_id = $iPlaceID and (rank_address > 0 OR address_place_id = $iPlaceID)";
-// and isaddress";
-               if ($sCountryCode)
-               {
-                       $sSQL .= " and (placex.country_code IS NULL OR placex.country_code = '".$sCountryCode."' OR rank_address < 4)";
-               }
-               $sSQL .= " order by cached_rank_address desc,fromarea desc,distance asc,rank_search desc,namelength desc";
+               if (!$bRaw) $sSQL .= " and isaddress";
+               $sSQL .= " order by cached_rank_address desc,isaddress desc,fromarea desc,distance asc,rank_search desc,namelength desc";
 //var_dump($sSQL);
                $aAddressLines = $oDB->getAll($sSQL);
                if (PEAR::IsError($aAddressLines))
                                if ($aTypeLabel && ($aLine['localname'] || $aLine['housenumber']))
                                {
                                        $sTypeLabel = strtolower(isset($aTypeLabel['simplelabel'])?$aTypeLabel['simplelabel']:$aTypeLabel['label']);
+                                       $sTypeLabel = str_replace(' ','_',$sTypeLabel);
                                        if (!isset($aAddress[$sTypeLabel]) && $aLine['localname']) $aAddress[$sTypeLabel] = $aLine['localname']?$aLine['localname']:$aLine['housenumber'];
                                }
                                $iMinRank = $aLine['rank_address'];
                        }
                }
+
+               if ($sPostcode)
+               {
+                       $aAddress['postcode'] = $sPostcode;
+               }
+
                if ($iMinRank > 4 && $sCountryCode)
                {
                        $sSQL = "select get_name_by_language(country_name.name,$sLanguagePrefArraySQL) as name";